{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Difference Image Loader performance\n\n| Image Reader | Augmentations  | Loader |   |   |\n|--------------|----------------|--------|---|---|\n| opencv       | Albumentations |        |   |   |\n| PIL          | Torchvision    |        |   |   |\n| skimage      |                |        |   |   |\n| torchvision. |                |        |   |   |\n","metadata":{"execution":{"iopub.status.busy":"2021-06-28T07:15:40.483818Z","iopub.execute_input":"2021-06-28T07:15:40.48429Z","iopub.status.idle":"2021-06-28T07:15:40.497694Z","shell.execute_reply.started":"2021-06-28T07:15:40.484167Z","shell.execute_reply":"2021-06-28T07:15:40.496318Z"}}},{"cell_type":"code","source":"import sys\nimport os\nimport numpy as np\n\nimport cv2\nfrom PIL import Image\nimport skimage.io\n\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\n\n\nimport torch\nfrom torch.utils.data import Dataset, DataLoader\nimport torchvision\nfrom torchvision import transforms\n\nimport matplotlib.pyplot as plt\n\n#torchvision.io.read_image","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:47:10.520249Z","iopub.execute_input":"2022-06-21T00:47:10.520656Z","iopub.status.idle":"2022-06-21T00:47:13.717762Z","shell.execute_reply.started":"2022-06-21T00:47:10.520564Z","shell.execute_reply":"2022-06-21T00:47:13.716872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sys.platform","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:47:18.026567Z","iopub.execute_input":"2022-06-21T00:47:18.026898Z","iopub.status.idle":"2022-06-21T00:47:18.033989Z","shell.execute_reply.started":"2022-06-21T00:47:18.026867Z","shell.execute_reply":"2022-06-21T00:47:18.032805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.get_device_name(0)","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:47:19.631614Z","iopub.execute_input":"2022-06-21T00:47:19.631945Z","iopub.status.idle":"2022-06-21T00:47:19.711386Z","shell.execute_reply.started":"2022-06-21T00:47:19.631913Z","shell.execute_reply":"2022-06-21T00:47:19.710322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.get_device_capability(device=0)","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:47:21.099584Z","iopub.execute_input":"2022-06-21T00:47:21.100010Z","iopub.status.idle":"2022-06-21T00:47:21.106187Z","shell.execute_reply.started":"2022-06-21T00:47:21.099976Z","shell.execute_reply":"2022-06-21T00:47:21.105176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.get_arch_list()","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:47:23.380612Z","iopub.execute_input":"2022-06-21T00:47:23.380952Z","iopub.status.idle":"2022-06-21T00:47:23.387014Z","shell.execute_reply.started":"2022-06-21T00:47:23.380921Z","shell.execute_reply":"2022-06-21T00:47:23.386093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.get_device_properties(device=0)","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:47:47.520331Z","iopub.execute_input":"2022-06-21T00:47:47.520768Z","iopub.status.idle":"2022-06-21T00:47:47.528238Z","shell.execute_reply.started":"2022-06-21T00:47:47.520738Z","shell.execute_reply":"2022-06-21T00:47:47.527391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INPUT_DIR = '../input/classify-leaves/images/'","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:47:50.806887Z","iopub.execute_input":"2022-06-21T00:47:50.807218Z","iopub.status.idle":"2022-06-21T00:47:50.811467Z","shell.execute_reply.started":"2022-06-21T00:47:50.807186Z","shell.execute_reply":"2022-06-21T00:47:50.810239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"pillow https://pillow.readthedocs.io/en/stable/reference/Image.html#functions\n\nopencv https://docs.opencv.org/master/d4/da8/group__imgcodecs.html#:~:text=%E2%97%86%C2%A0-,imread(),-Mat%20cv%3A%3Aimread\n\ntorchvision https://pytorch.org/vision/stable/io.html#torchvision.io.read_image\n\nskimage https://scikit-image.org/docs/dev/api/skimage.io.html#:~:text=functions%20as%20values.-,imread,-%C2%B6\n\nmatplotlib https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.imread.html\n","metadata":{}},{"cell_type":"markdown","source":"# Display image","metadata":{}},{"cell_type":"code","source":"imagePath = INPUT_DIR + '233.jpg'","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:48:01.062720Z","iopub.execute_input":"2022-06-21T00:48:01.063045Z","iopub.status.idle":"2022-06-21T00:48:01.067211Z","shell.execute_reply.started":"2022-06-21T00:48:01.063014Z","shell.execute_reply":"2022-06-21T00:48:01.065742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_PIL = Image.open(imagePath)\nimg_skimage = skimage.io.imread(imagePath)\nimg_opencv = cv2.imread(imagePath)\nimg_matplt = plt.imread(imagePath)\nimg_torch = torchvision.io.read_image(imagePath)","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:48:03.193329Z","iopub.execute_input":"2022-06-21T00:48:03.193711Z","iopub.status.idle":"2022-06-21T00:48:03.255487Z","shell.execute_reply.started":"2022-06-21T00:48:03.193680Z","shell.execute_reply":"2022-06-21T00:48:03.254603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(img_PIL)","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:48:05.838027Z","iopub.execute_input":"2022-06-21T00:48:05.838412Z","iopub.status.idle":"2022-06-21T00:48:05.844052Z","shell.execute_reply.started":"2022-06-21T00:48:05.838359Z","shell.execute_reply":"2022-06-21T00:48:05.843170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('The shape of: \\n img_skimage is {} \\n img_opencv is {}\\n img_matplt is {}\\n img_torch is {}\\n' .format(img_skimage.shape, img_opencv.shape, img_matplt.shape, img_torch.shape))\nprint('The type of: \\n img_skimage is {}\\n img_opencv is {}\\n img_matplt is {}\\n img_torch is {}\\n'.format(type(img_skimage), type(img_opencv), type(img_matplt), type(img_torch)))","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:48:07.752021Z","iopub.execute_input":"2022-06-21T00:48:07.752365Z","iopub.status.idle":"2022-06-21T00:48:07.758392Z","shell.execute_reply.started":"2022-06-21T00:48:07.752333Z","shell.execute_reply":"2022-06-21T00:48:07.757380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## skimage","metadata":{}},{"cell_type":"code","source":"# skimage.io.imread()\n# RGB\nimg_sk = skimage.io.imread(imagePath)\nplt.subplot(224)\nplt.imshow(img_sk)","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:48:15.575090Z","iopub.execute_input":"2022-06-21T00:48:15.575549Z","iopub.status.idle":"2022-06-21T00:48:15.799618Z","shell.execute_reply.started":"2022-06-21T00:48:15.575509Z","shell.execute_reply":"2022-06-21T00:48:15.798491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## opencv","metadata":{}},{"cell_type":"code","source":"# cv2.imread()\n# BGR\nimgcv = cv2.imread(imagePath)\nplt.subplot(224)\nplt.imshow(imgcv)","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:48:18.282544Z","iopub.execute_input":"2022-06-21T00:48:18.282902Z","iopub.status.idle":"2022-06-21T00:48:18.399151Z","shell.execute_reply.started":"2022-06-21T00:48:18.282870Z","shell.execute_reply":"2022-06-21T00:48:18.398269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cv2.imread()\n# BGR to RGB\nimg_cv_bgr = cv2.imread(imagePath)\nimg_cv_rgb = cv2.cvtColor(img_cv_bgr, cv2.COLOR_RGB2BGR)\n#img_rgb = img_cv_bgr[:, :, ::-1]\nplt.subplot(224)\nplt.imshow(imgcv)","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:48:54.581408Z","iopub.execute_input":"2022-06-21T00:48:54.581774Z","iopub.status.idle":"2022-06-21T00:48:54.700136Z","shell.execute_reply.started":"2022-06-21T00:48:54.581743Z","shell.execute_reply":"2022-06-21T00:48:54.699306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## torchvision","metadata":{}},{"cell_type":"code","source":"#RGB\nimg_torch = torchvision.io.read_image(imagePath)\nplt.subplot(224)\nplt.imshow(img_torch.permute(1,2,0))\n\n# need reshape image(), since it is a tensor formate with [image_channels, image_height, image_width]","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:49:03.932516Z","iopub.execute_input":"2022-06-21T00:49:03.932880Z","iopub.status.idle":"2022-06-21T00:49:04.052890Z","shell.execute_reply.started":"2022-06-21T00:49:03.932846Z","shell.execute_reply":"2022-06-21T00:49:04.052047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## pillow","metadata":{}},{"cell_type":"code","source":"# Image.open()\n# RGB\nimg_pil = Image.open(imagePath)\nplt.subplot(224)\nplt.imshow(img_pil)","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:49:06.424504Z","iopub.execute_input":"2022-06-21T00:49:06.424841Z","iopub.status.idle":"2022-06-21T00:49:06.549126Z","shell.execute_reply.started":"2022-06-21T00:49:06.424803Z","shell.execute_reply":"2022-06-21T00:49:06.547955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## matplotlib","metadata":{}},{"cell_type":"code","source":"#RGB\nimg_matplotlib = plt.imread(imagePath)\nplt.subplot(224)\nplt.imshow(img_matplotlib)\n\n# need reshape image(), since it is a tensor formate with [image_channels, image_height, image_width]","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:49:09.000878Z","iopub.execute_input":"2022-06-21T00:49:09.001336Z","iopub.status.idle":"2022-06-21T00:49:09.118485Z","shell.execute_reply.started":"2022-06-21T00:49:09.001296Z","shell.execute_reply":"2022-06-21T00:49:09.117474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_files = [os.path.join(INPUT_DIR, f) for f in os.listdir(INPUT_DIR)]\nprint('total image files: {}'.format(len(image_files)))","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:49:11.599401Z","iopub.execute_input":"2022-06-21T00:49:11.599748Z","iopub.status.idle":"2022-06-21T00:49:13.163966Z","shell.execute_reply.started":"2022-06-21T00:49:11.599718Z","shell.execute_reply":"2022-06-21T00:49:13.162899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Speed\n\n - 1. pillow\n - 2. opencv\n - 3. torchvision.io.read_image\n - 4. skimage.io.imread\n - 5. matplotlib","metadata":{}},{"cell_type":"code","source":"# choose 1000 picture for loading speed test\nimage_files_1000 = image_files[:1000]","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:49:48.647602Z","iopub.execute_input":"2022-06-21T00:49:48.647954Z","iopub.status.idle":"2022-06-21T00:49:48.652419Z","shell.execute_reply.started":"2022-06-21T00:49:48.647921Z","shell.execute_reply":"2022-06-21T00:49:48.651044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cv2_imread(path):\n    img = cv2.imread(path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:49:50.891869Z","iopub.execute_input":"2022-06-21T00:49:50.892526Z","iopub.status.idle":"2022-06-21T00:49:50.897694Z","shell.execute_reply.started":"2022-06-21T00:49:50.892476Z","shell.execute_reply":"2022-06-21T00:49:50.896589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nfor path in image_files_1000:\n    cv2_imread(path)\n    \n# CPU times: user 815 ms, sys: 376 ms, total: 1.19 s\n# Wall time: 2.01 s","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:49:53.801978Z","iopub.execute_input":"2022-06-21T00:49:53.802334Z","iopub.status.idle":"2022-06-21T00:50:06.708046Z","shell.execute_reply.started":"2022-06-21T00:49:53.802293Z","shell.execute_reply":"2022-06-21T00:50:06.707009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## pillow","metadata":{}},{"cell_type":"code","source":"%%timeit -r 5 -p 5\n# pillow\n\nfor path in image_files_1000:\n    Image.open(path)","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:50:06.709569Z","iopub.execute_input":"2022-06-21T00:50:06.709880Z","iopub.status.idle":"2022-06-21T00:50:09.593998Z","shell.execute_reply.started":"2022-06-21T00:50:06.709850Z","shell.execute_reply":"2022-06-21T00:50:09.593022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## opencv","metadata":{}},{"cell_type":"code","source":"%%timeit -r 5 -p 5\n# opencv\n\nfor path in image_files_1000:\n    cv2_imread(path)\n    \n# CPU times: user 815 ms, sys: 376 ms, total: 1.19 s\n# Wall time: 2.01 s","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:50:13.085925Z","iopub.execute_input":"2022-06-21T00:50:13.086268Z","iopub.status.idle":"2022-06-21T00:50:23.116037Z","shell.execute_reply.started":"2022-06-21T00:50:13.086235Z","shell.execute_reply":"2022-06-21T00:50:23.114526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## torchvision","metadata":{}},{"cell_type":"code","source":"%%timeit -r 5 -p 5\n# torchvision\n\nfor path in image_files_1000:\n    torchvision.io.read_image(path)","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:50:42.633503Z","iopub.execute_input":"2022-06-21T00:50:42.633858Z","iopub.status.idle":"2022-06-21T00:50:49.697180Z","shell.execute_reply.started":"2022-06-21T00:50:42.633816Z","shell.execute_reply":"2022-06-21T00:50:49.696027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## skimage","metadata":{}},{"cell_type":"code","source":"%%timeit -r 5 -p 5\n# torchvision\n\nfor path in image_files_1000:\n    skimage.io.imread(path)","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:50:49.698751Z","iopub.execute_input":"2022-06-21T00:50:49.699125Z","iopub.status.idle":"2022-06-21T00:50:59.053604Z","shell.execute_reply.started":"2022-06-21T00:50:49.699086Z","shell.execute_reply":"2022-06-21T00:50:59.052711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## matplotlib","metadata":{}},{"cell_type":"code","source":"%%timeit -r 5 -p 5\n# torchvision\n\nfor path in image_files_1000:\n    plt.imread(path)","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:51:04.437141Z","iopub.execute_input":"2022-06-21T00:51:04.437548Z","iopub.status.idle":"2022-06-21T00:51:12.768076Z","shell.execute_reply.started":"2022-06-21T00:51:04.437516Z","shell.execute_reply":"2022-06-21T00:51:12.767046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# single transform","metadata":{}},{"cell_type":"code","source":"A_transform = A.Compose([A.CenterCrop(64, 64)])\n\nT0_transform = transforms.Compose([transforms.CenterCrop([64, 64])])\n\n\n#skimage, cv2, matplotlib\nT1_transform = transforms.Compose([\n    transforms.ToPILImage(),\n    transforms.CenterCrop([64, 64]),\n])","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:51:20.192898Z","iopub.execute_input":"2022-06-21T00:51:20.193226Z","iopub.status.idle":"2022-06-21T00:51:20.198169Z","shell.execute_reply.started":"2022-06-21T00:51:20.193194Z","shell.execute_reply":"2022-06-21T00:51:20.197099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Albumentations","metadata":{}},{"cell_type":"code","source":"%%timeit -r 5 -p 5\n\nfor path in image_files_1000:\n    \n    A_transform(image = cv2_imread(path))","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:51:22.280838Z","iopub.execute_input":"2022-06-21T00:51:22.281183Z","iopub.status.idle":"2022-06-21T00:51:33.050810Z","shell.execute_reply.started":"2022-06-21T00:51:22.281152Z","shell.execute_reply":"2022-06-21T00:51:33.049314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%timeit -r 5 -p 5\n\nfor path in image_files_1000:\n    \n    A_transform(image = skimage.io.imread(path))","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:51:33.052542Z","iopub.execute_input":"2022-06-21T00:51:33.052947Z","iopub.status.idle":"2022-06-21T00:51:42.449266Z","shell.execute_reply.started":"2022-06-21T00:51:33.052908Z","shell.execute_reply":"2022-06-21T00:51:42.448244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%timeit -r 5 -p 5\n\nfor path in image_files_1000:\n    \n    A_transform(image = plt.imread(path))","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:51:42.451460Z","iopub.execute_input":"2022-06-21T00:51:42.451968Z","iopub.status.idle":"2022-06-21T00:51:50.826975Z","shell.execute_reply.started":"2022-06-21T00:51:42.451919Z","shell.execute_reply":"2022-06-21T00:51:50.825991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Torchvision","metadata":{}},{"cell_type":"code","source":"%%timeit -r 5 -p 5 \n\nimg_df = []\nfor path in image_files_1000:\n    T0_transform(torchvision.io.read_image(path))","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:52:45.246185Z","iopub.execute_input":"2022-06-21T00:52:45.246565Z","iopub.status.idle":"2022-06-21T00:52:52.637062Z","shell.execute_reply.started":"2022-06-21T00:52:45.246532Z","shell.execute_reply":"2022-06-21T00:52:52.636011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%timeit -r 5 -p 5 \n\nimg_df = []\nfor path in image_files_1000:\n    T0_transform(Image.open(path))","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:59:22.631544Z","iopub.execute_input":"2022-06-21T00:59:22.631880Z","iopub.status.idle":"2022-06-21T00:59:30.137966Z","shell.execute_reply.started":"2022-06-21T00:59:22.631848Z","shell.execute_reply":"2022-06-21T00:59:30.137010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%timeit -r 5 -p 5 \n\nimg_df = []\nfor path in image_files_1000:\n    T1_transform(skimage.io.imread(path))","metadata":{"execution":{"iopub.status.busy":"2022-06-21T00:58:39.390109Z","iopub.execute_input":"2022-06-21T00:58:39.390577Z","iopub.status.idle":"2022-06-21T00:58:49.491328Z","shell.execute_reply.started":"2022-06-21T00:58:39.390535Z","shell.execute_reply":"2022-06-21T00:58:49.489997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%timeit -r 5 -p 5 \n\nimg_df = []\nfor path in image_files_1000:\n    T1_transform(cv2_imread(path))","metadata":{"execution":{"iopub.status.busy":"2022-06-21T01:00:25.549184Z","iopub.execute_input":"2022-06-21T01:00:25.549535Z","iopub.status.idle":"2022-06-21T01:00:36.861665Z","shell.execute_reply.started":"2022-06-21T01:00:25.549501Z","shell.execute_reply":"2022-06-21T01:00:36.860751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%timeit -r 5 -p 5 \n\nimg_df = []\nfor path in image_files_1000:\n    T1_transform(plt.imread(path))","metadata":{"execution":{"iopub.status.busy":"2022-06-21T01:03:16.604893Z","iopub.execute_input":"2022-06-21T01:03:16.605254Z","iopub.status.idle":"2022-06-21T01:03:25.690937Z","shell.execute_reply.started":"2022-06-21T01:03:16.605222Z","shell.execute_reply":"2022-06-21T01:03:25.690111Z"},"trusted":true},"execution_count":null,"outputs":[]}]}