{"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":"code","source":"import numpy as np\nimport torch \n\nimport cv2\nimport PIL\nfrom PIL import Image\nimport kornia as K","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-03T13:37:37.829394Z","iopub.execute_input":"2023-05-03T13:37:37.829823Z","iopub.status.idle":"2023-05-03T13:37:37.834985Z","shell.execute_reply.started":"2023-05-03T13:37:37.829789Z","shell.execute_reply":"2023-05-03T13:37:37.833691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"OpenCV: \", cv2.__version__)\nprint(\"PIL: \", PIL.__version__)\nprint(\"Kornia:\", K.__version__)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T09:20:37.269849Z","iopub.execute_input":"2023-05-03T09:20:37.270260Z","iopub.status.idle":"2023-05-03T09:20:37.275859Z","shell.execute_reply.started":"2023-05-03T09:20:37.270232Z","shell.execute_reply":"2023-05-03T09:20:37.274797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imageFileNames = [\n    '/kaggle/input/image-matching-challenge-2023/train/heritage/wall/images/DSC_4928_acr.jpg',\n    '/kaggle/input/image-matching-challenge-2023/train/heritage/wall/images/DSC_4991_acr.jpg',\n    '/kaggle/input/image-matching-challenge-2023/train/heritage/wall/images/DSC_4931_acr.jpg',\n    '/kaggle/input/image-matching-challenge-2023/train/heritage/wall/images/DSC_4934_acr.jpg'\n]","metadata":{"execution":{"iopub.status.busy":"2023-05-03T13:38:06.603631Z","iopub.execute_input":"2023-05-03T13:38:06.604070Z","iopub.status.idle":"2023-05-03T13:38:06.609121Z","shell.execute_reply.started":"2023-05-03T13:38:06.604037Z","shell.execute_reply":"2023-05-03T13:38:06.607758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image_batch( fileNames\n                    , resizeWxH = (640, 480)\n                    , device = torch.device('cpu')\n                    ):\n    batch = []\n    for fileName in fileNames:\n        img = K.image_to_tensor(cv2.imread(fileName), False).float() / 255.\n        img = K.color.bgr_to_rgb(img)\n        img = K.geometry.resize(img, (resizeWxH[1], resizeWxH[0]), antialias=True)\n        batch.append(img)\n    return torch.cat(batch).to(device)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T11:40:05.981661Z","iopub.execute_input":"2023-05-03T11:40:05.982110Z","iopub.status.idle":"2023-05-03T11:40:05.990152Z","shell.execute_reply.started":"2023-05-03T11:40:05.982079Z","shell.execute_reply":"2023-05-03T11:40:05.988704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%timeit load_image_batch(imageFileNames)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T11:40:14.428556Z","iopub.execute_input":"2023-05-03T11:40:14.429434Z","iopub.status.idle":"2023-05-03T11:42:20.550875Z","shell.execute_reply.started":"2023-05-03T11:40:14.429389Z","shell.execute_reply":"2023-05-03T11:42:20.549743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image_batch2( fileNames\n                     , resizeWxH = (640, 480)\n                     , device = torch.device('cpu')\n                     ):\n    batch = []\n    for fileName in fileNames:\n        img = K.image_to_tensor(cv2.resize(cv2.imread(fileName), resizeWxH, cv2.INTER_LANCZOS4), False).float() / 255.\n        img = K.color.bgr_to_rgb(img)\n        batch.append(img)\n    return torch.cat(batch).to(device)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T11:43:20.556094Z","iopub.execute_input":"2023-05-03T11:43:20.556470Z","iopub.status.idle":"2023-05-03T11:43:20.562607Z","shell.execute_reply.started":"2023-05-03T11:43:20.556442Z","shell.execute_reply":"2023-05-03T11:43:20.561670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%timeit load_image_batch2(imageFileNames)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T11:43:23.304979Z","iopub.execute_input":"2023-05-03T11:43:23.305347Z","iopub.status.idle":"2023-05-03T11:43:41.919435Z","shell.execute_reply.started":"2023-05-03T11:43:23.305320Z","shell.execute_reply":"2023-05-03T11:43:41.918335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image_batch3( fileNames\n                     , resizeWxH = (640, 480)\n                     , device = torch.device('cpu')\n                     ):\n    batch = []\n    for fileName in fileNames:\n        cvImg = cv2.cvtColor(cv2.resize(cv2.imread(fileName), resizeWxH, cv2.INTER_LANCZOS4), cv2.COLOR_BGR2RGB)\n        img = K.image_to_tensor(cvImg, False).float() / 255.\n        batch.append(img)\n    return torch.cat(batch).to(device)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T13:41:32.215011Z","iopub.execute_input":"2023-05-03T13:41:32.215433Z","iopub.status.idle":"2023-05-03T13:41:32.222046Z","shell.execute_reply.started":"2023-05-03T13:41:32.215404Z","shell.execute_reply":"2023-05-03T13:41:32.220628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%timeit load_image_batch3(imageFileNames)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T13:41:36.183067Z","iopub.execute_input":"2023-05-03T13:41:36.183465Z","iopub.status.idle":"2023-05-03T13:41:54.427401Z","shell.execute_reply.started":"2023-05-03T13:41:36.183430Z","shell.execute_reply":"2023-05-03T13:41:54.426508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image_batch_PIL( fileNames\n                        , resizeWxH = (640, 480)\n                        , device = torch.device('cpu')\n                        ):\n    batch = []\n    for fileName in fileNames:\n        img = K.utils.image_to_tensor(np.asarray(Image.open(fileName).convert('RGB')), False).float() / 255.\n        img = K.geometry.resize(img, (resizeWxH[1], resizeWxH[0]), antialias=True)\n        batch.append(img)\n    return torch.cat(batch).to(device)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T10:29:06.160948Z","iopub.execute_input":"2023-05-03T10:29:06.161411Z","iopub.status.idle":"2023-05-03T10:29:06.167901Z","shell.execute_reply.started":"2023-05-03T10:29:06.161379Z","shell.execute_reply":"2023-05-03T10:29:06.166795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%timeit load_image_batch_PIL(imageFileNames)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T10:29:42.805881Z","iopub.execute_input":"2023-05-03T10:29:42.806501Z","iopub.status.idle":"2023-05-03T10:31:37.787155Z","shell.execute_reply.started":"2023-05-03T10:29:42.806466Z","shell.execute_reply":"2023-05-03T10:31:37.785892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image_batch_PIL2( fileNames\n                         , resizeWxH = (640, 480)\n                         , device = torch.device('cpu')\n                         ):\n    batch = []\n    for fileName in fileNames:\n        img = Image.open(fileName).convert('RGB').resize(resizeWxH, Image.Resampling.LANCZOS)\n        tensor = K.image_to_tensor(np.asarray(img), False).float() / 255.\n        batch.append(tensor)\n    return torch.cat(batch).to(device)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T10:39:41.072686Z","iopub.execute_input":"2023-05-03T10:39:41.073265Z","iopub.status.idle":"2023-05-03T10:39:41.079250Z","shell.execute_reply.started":"2023-05-03T10:39:41.073234Z","shell.execute_reply":"2023-05-03T10:39:41.078146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%timeit load_image_batch_PIL2(imageFileNames)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T10:40:31.793542Z","iopub.execute_input":"2023-05-03T10:40:31.793963Z","iopub.status.idle":"2023-05-03T10:40:56.791206Z","shell.execute_reply.started":"2023-05-03T10:40:31.793932Z","shell.execute_reply":"2023-05-03T10:40:56.789978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip uninstall -y pillow\n!pip install pillow-simd","metadata":{"execution":{"iopub.status.busy":"2023-05-03T10:49:26.343455Z","iopub.execute_input":"2023-05-03T10:49:26.343876Z","iopub.status.idle":"2023-05-03T10:50:02.813378Z","shell.execute_reply.started":"2023-05-03T10:49:26.343838Z","shell.execute_reply":"2023-05-03T10:50:02.812033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n%timeit load_image_batch_PIL2(imageFileNames)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T10:50:39.634166Z","iopub.execute_input":"2023-05-03T10:50:39.635064Z","iopub.status.idle":"2023-05-03T10:51:04.950326Z","shell.execute_reply.started":"2023-05-03T10:50:39.635003Z","shell.execute_reply":"2023-05-03T10:51:04.949436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /root/.cache/pip/wheels/5b/40/80/c3580fdb22f9cc7362b26cac344b501994785f96329a3f6b94\n!pip uninstall -y pillow-simd\n!CC=\"cc -mavx2\" pip install pillow-simd","metadata":{"execution":{"iopub.status.busy":"2023-05-03T11:15:43.460168Z","iopub.execute_input":"2023-05-03T11:15:43.460624Z","iopub.status.idle":"2023-05-03T11:16:38.830764Z","shell.execute_reply.started":"2023-05-03T11:15:43.460585Z","shell.execute_reply":"2023-05-03T11:16:38.828982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n%timeit load_image_batch_PIL2(imageFileNames)","metadata":{"execution":{"iopub.status.busy":"2023-05-03T11:17:08.539615Z","iopub.execute_input":"2023-05-03T11:17:08.540046Z","iopub.status.idle":"2023-05-03T11:17:33.800411Z","shell.execute_reply.started":"2023-05-03T11:17:08.540012Z","shell.execute_reply":"2023-05-03T11:17:33.797612Z"},"trusted":true},"execution_count":null,"outputs":[]}]}