Last active
July 12, 2018 04:47
-
-
Save spnova12/00251a4a947eaf33bfba6ec8b19a1d6c to your computer and use it in GitHub Desktop.
Pytorch 이미지 전처리와 속도
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import torch\n", | |
"import numpy as np\n", | |
"from PIL import Image\n", | |
"import matplotlib.pyplot as plt\n", | |
"import torchvision.transforms as transforms\n", | |
"\n", | |
"import time\n", | |
"import tqdm\n", | |
"import io\n", | |
"\n", | |
"howlong = 1000\n", | |
"pil2tensor = transforms.ToTensor()\n", | |
"rc = transforms.RandomCrop([224, 224])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"PIL 이미지에서 pytorch의 torch로 변환할 때 걸리는 속도를 비교해보았다.\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"100%|████████████████████████████████████████████████████████████████████████████| 1000/1000 [00:00<00:00, 6961.66it/s]\n", | |
"100%|█████████████████████████████████████████████████████████████████████████████| 1000/1000 [00:01<00:00, 732.96it/s]\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"9.383577793278253\n" | |
] | |
} | |
], | |
"source": [ | |
"start_time = time.time()\n", | |
"for _ in tqdm.tqdm(range(0, howlong)):\n", | |
" pil_image = Image.open('cat.jpg')\n", | |
"end_time = time.time()\n", | |
"time_without_convert = end_time - start_time\n", | |
"start_time = time.time()\n", | |
"\n", | |
"for _ in tqdm.tqdm(range(0, howlong)):\n", | |
" pil_image = Image.open('cat.jpg')\n", | |
" rgb_image = pil2tensor(pil_image)\n", | |
"end_time = time.time()\n", | |
"time_with_convert = end_time - start_time\n", | |
"\n", | |
"print(time_with_convert/time_without_convert)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"변환 할 때 9 배 더 느리다는 것을 알 수 있었다.\n", | |
"\n", | |
"이번엔 이미지를 crop하는데 걸리는 시간을 알아보았다." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"100%|████████████████████████████████████████████████████████████████████████████| 1000/1000 [00:00<00:00, 6729.41it/s]\n", | |
"100%|████████████████████████████████████████████████████████████████████████████| 1000/1000 [00:00<00:00, 1021.06it/s]\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"6.516499487062578\n" | |
] | |
} | |
], | |
"source": [ | |
"start_time = time.time()\n", | |
"for _ in tqdm.tqdm(range(0, howlong)):\n", | |
" pil_image = Image.open('cat.jpg')\n", | |
"end_time = time.time()\n", | |
"time_without_crop = end_time - start_time\n", | |
"start_time = time.time()\n", | |
"\n", | |
"for _ in tqdm.tqdm(range(0, howlong)):\n", | |
" pil_image = Image.open('cat.jpg')\n", | |
" rc_image = rc(pil_image)\n", | |
"end_time = time.time()\n", | |
"time_with_crop = end_time - start_time\n", | |
"\n", | |
"print(time_with_crop/time_without_crop)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"crop 할 때 안할때 보다 7배 정도 더 느림을 알 수 있었다.\n", | |
"이미지 데이터를 읽고 크롭하고 pytorh의 tensor로 저장하는 과정에서 어쩌면 10배 정도의 시간이 괜히 낭비되지 않나 싶다.\n", | |
"이미지 데이터를 전처리 할 때 시간을 줄이기 위해 미리 전처리를 해서 저장한다면 어떨까." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"rgb_image *= 255 \n", | |
"# 데이터 용량을 줄이기 위해 byte 로 저장하고 이때 byte 로 저장하기 위해 255를 곱해준다. \n", | |
"# 안그러면 rgb_image 가 위 과정에서 0~1 사이기 때문에 byte로 저장하면서 소수점이 날라간다.\n", | |
"torch.save(rgb_image.byte(), 'image1.pt')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"100%|█████████████████████████████████████████████████████████████████████████████| 1000/1000 [00:01<00:00, 731.88it/s]\n", | |
"100%|████████████████████████████████████████████████████████████████████████████| 1000/1000 [00:00<00:00, 4915.03it/s]\n" | |
] | |
}, | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"6.655282177206729\n" | |
] | |
} | |
], | |
"source": [ | |
"start_time = time.time()\n", | |
"for _ in tqdm.tqdm(range(0, howlong)):\n", | |
" pil_image = Image.open('cat.jpg')\n", | |
" rgb_image = pil2tensor(rc(pil_image))\n", | |
"end_time = time.time()\n", | |
"time_pil = end_time - start_time\n", | |
"start_time = time.time()\n", | |
"\n", | |
"for _ in tqdm.tqdm(range(0, howlong)):\n", | |
" rgb_image = torch.load('image1.pt')\n", | |
"end_time = time.time()\n", | |
"time_tensor = end_time - start_time\n", | |
"print(time_pil/time_tensor)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"미리 전처리를 해서 불러오는게 7 배 정도 더 빠름을 확인하였다." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment