{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":29193,"databundleVersionId":2318453,"sourceType":"competition"}],"dockerImageVersionId":30097,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sys.platform","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:02:48.718847Z","iopub.execute_input":"2024-06-27T14:02:48.719112Z","iopub.status.idle":"2024-06-27T14:02:48.726209Z","shell.execute_reply.started":"2024-06-27T14:02:48.719086Z","shell.execute_reply":"2024-06-27T14:02:48.725345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.get_device_name(0)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:02:48.728003Z","iopub.execute_input":"2024-06-27T14:02:48.728350Z","iopub.status.idle":"2024-06-27T14:02:48.810927Z","shell.execute_reply.started":"2024-06-27T14:02:48.728316Z","shell.execute_reply":"2024-06-27T14:02:48.809929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.get_device_capability(device=0)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:02:48.812817Z","iopub.execute_input":"2024-06-27T14:02:48.813092Z","iopub.status.idle":"2024-06-27T14:02:48.834075Z","shell.execute_reply.started":"2024-06-27T14:02:48.813065Z","shell.execute_reply":"2024-06-27T14:02:48.833087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.get_arch_list()","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:02:48.835288Z","iopub.execute_input":"2024-06-27T14:02:48.835585Z","iopub.status.idle":"2024-06-27T14:02:48.844119Z","shell.execute_reply.started":"2024-06-27T14:02:48.835557Z","shell.execute_reply":"2024-06-27T14:02:48.843271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.get_device_properties(device=0)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:02:48.845187Z","iopub.execute_input":"2024-06-27T14:02:48.845463Z","iopub.status.idle":"2024-06-27T14:02:48.854435Z","shell.execute_reply.started":"2024-06-27T14:02:48.845425Z","shell.execute_reply":"2024-06-27T14:02:48.853697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INPUT_DIR = '../input/classify-leaves/images/'","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:02:48.855659Z","iopub.execute_input":"2024-06-27T14:02:48.855933Z","iopub.status.idle":"2024-06-27T14:02:48.861793Z","shell.execute_reply.started":"2024-06-27T14:02:48.855907Z","shell.execute_reply":"2024-06-27T14:02:48.861092Z"},"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":"2024-06-27T14:02:48.864570Z","iopub.execute_input":"2024-06-27T14:02:48.864878Z","iopub.status.idle":"2024-06-27T14:02:48.870422Z","shell.execute_reply.started":"2024-06-27T14:02:48.864848Z","shell.execute_reply":"2024-06-27T14:02:48.869678Z"},"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":"2024-06-27T14:02:48.872078Z","iopub.execute_input":"2024-06-27T14:02:48.872356Z","iopub.status.idle":"2024-06-27T14:02:48.938157Z","shell.execute_reply.started":"2024-06-27T14:02:48.872330Z","shell.execute_reply":"2024-06-27T14:02:48.936933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(img_PIL)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:02:48.939827Z","iopub.execute_input":"2024-06-27T14:02:48.940204Z","iopub.status.idle":"2024-06-27T14:02:48.946857Z","shell.execute_reply.started":"2024-06-27T14:02:48.940165Z","shell.execute_reply":"2024-06-27T14:02:48.945724Z"},"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":"2024-06-27T14:02:48.948332Z","iopub.execute_input":"2024-06-27T14:02:48.948720Z","iopub.status.idle":"2024-06-27T14:02:48.962490Z","shell.execute_reply.started":"2024-06-27T14:02:48.948678Z","shell.execute_reply":"2024-06-27T14:02:48.961691Z"},"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 = [os.path.join(INPUT_DIR, f) for f in os.listdir(INPUT_DIR)]\nprint('total image files: {}'.format(len(image_files)))\n\nimage_files_1000 = image_files[:1000]","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:02:51.499383Z","iopub.execute_input":"2024-06-27T14:02:51.499713Z","iopub.status.idle":"2024-06-27T14:02:51.503623Z","shell.execute_reply.started":"2024-06-27T14:02:51.499681Z","shell.execute_reply":"2024-06-27T14:02:51.502626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## opencv","metadata":{}},{"cell_type":"code","source":"%%timeit -r 5 -p 5\n\nfor path in image_files_1000:\n    img = cv2.imread(path , cv2.IMREAD_UNCHANGED)\n    img = torch.from_numpy(img).permute(2,0,1)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:12:48.886671Z","iopub.execute_input":"2024-06-27T14:12:48.887009Z","iopub.status.idle":"2024-06-27T14:12:58.035686Z","shell.execute_reply.started":"2024-06-27T14:12:48.886980Z","shell.execute_reply":"2024-06-27T14:12:58.034562Z"},"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    img = np.array(Image.open(path))\n    img = torch.from_numpy(img).permute(2,0,1)\n    ","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:10:15.890102Z","iopub.execute_input":"2024-06-27T14:10:15.890429Z","iopub.status.idle":"2024-06-27T14:10:24.481194Z","shell.execute_reply.started":"2024-06-27T14:10:15.890392Z","shell.execute_reply":"2024-06-27T14:10:24.480271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## torchvision","metadata":{}},{"cell_type":"code","source":"%%timeit -r 5 -p 5\n\nfor path in image_files_1000:\n    a = torchvision.io.read_image(path)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:11:58.545854Z","iopub.execute_input":"2024-06-27T14:11:58.546179Z","iopub.status.idle":"2024-06-27T14:12:05.410506Z","shell.execute_reply.started":"2024-06-27T14:11:58.546151Z","shell.execute_reply":"2024-06-27T14:12:05.409496Z"},"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    img = skimage.io.imread(path)\n    img = torch.from_numpy(img).permute(2,0,1)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:12:27.414373Z","iopub.execute_input":"2024-06-27T14:12:27.414735Z","iopub.status.idle":"2024-06-27T14:12:36.545984Z","shell.execute_reply.started":"2024-06-27T14:12:27.414703Z","shell.execute_reply":"2024-06-27T14:12:36.545004Z"},"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    img = plt.imread(path)\n    img = torch.from_numpy(img).permute(2,0,1)","metadata":{"execution":{"iopub.status.busy":"2024-06-27T14:14:07.426936Z","iopub.execute_input":"2024-06-27T14:14:07.427221Z","iopub.status.idle":"2024-06-27T14:14:16.174357Z","shell.execute_reply.started":"2024-06-27T14:14:07.427192Z","shell.execute_reply":"2024-06-27T14:14:16.173367Z"},"trusted":true},"execution_count":null,"outputs":[]}]}