{"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":"raw","source":"!pip install tifffile\nfrom tifffile import imread as timread","metadata":{}},{"cell_type":"markdown","source":"# Analysis of time taken by different packages to read a .tiff file\n\nthis include reading the file, changing the channel order if necessary and then finally converting it into a pytorch tensor","metadata":{}},{"cell_type":"markdown","source":"# Results\n- skimage :17.06 ms\n- tifffile : 17.03 ms\n- PIL : 63.5ms\n- Rasterio: 234 ms\n- Opencv: 111ms","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport cv2\nimport torch \nimport matplotlib.pyplot as plt\nfrom skimage import io,img_as_float\nfrom torchvision.transforms import ToTensor\nimport rasterio\nfrom rasterio.plot import show\nfrom PIL import Image\n\n\nPATH = \"/kaggle/input/hubmap-organ-segmentation/train_images/28189.tiff\"\n","metadata":{"execution":{"iopub.status.busy":"2022-06-29T11:30:51.743707Z","iopub.execute_input":"2022-06-29T11:30:51.74421Z","iopub.status.idle":"2022-06-29T11:30:51.749652Z","shell.execute_reply.started":"2022-06-29T11:30:51.744173Z","shell.execute_reply":"2022-06-29T11:30:51.748351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## SKIMAGE -> torch Tensor\n17.6 ms","metadata":{}},{"cell_type":"code","source":"%%timeit\n\nimg = io.imread(PATH)\nimg = torch.tensor(img)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-29T11:31:43.369777Z","iopub.execute_input":"2022-06-29T11:31:43.370264Z","iopub.status.idle":"2022-06-29T11:31:44.08169Z","shell.execute_reply.started":"2022-06-29T11:31:43.370224Z","shell.execute_reply":"2022-06-29T11:31:44.080542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Rasterio -> tensor","metadata":{}},{"cell_type":"code","source":"%%timeit\nwith rasterio.open(PATH) as image:\n    image_array = image.read()\n    \ntorch_image = ToTensor()(image_array)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-29T11:33:26.063524Z","iopub.execute_input":"2022-06-29T11:33:26.063987Z","iopub.status.idle":"2022-06-29T11:33:28.163631Z","shell.execute_reply.started":"2022-06-29T11:33:26.06395Z","shell.execute_reply":"2022-06-29T11:33:28.162463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Rasterio\n2.04ms\n31.03ms\n38.07ms","metadata":{}},{"cell_type":"code","source":"%%timeit\ns = rasterio.open(PATH)\nimg = s.read()\nimg = torch.tensor(img)","metadata":{"execution":{"iopub.status.busy":"2022-06-29T11:37:45.457968Z","iopub.execute_input":"2022-06-29T11:37:45.458354Z","iopub.status.idle":"2022-06-29T11:37:48.704516Z","shell.execute_reply.started":"2022-06-29T11:37:45.458321Z","shell.execute_reply":"2022-06-29T11:37:48.702744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## PIL Image\n- 1.22ms only reading using image.open\n- 63.5ms to convert it to first numpy array and then to torch tensor\n","metadata":{}},{"cell_type":"code","source":"%%timeit\npil_image = Image.open(PATH)\nimg = torch.tensor(np.array(pil_image))","metadata":{"execution":{"iopub.status.busy":"2022-06-29T11:43:21.48988Z","iopub.execute_input":"2022-06-29T11:43:21.490334Z","iopub.status.idle":"2022-06-29T11:43:26.602566Z","shell.execute_reply.started":"2022-06-29T11:43:21.490293Z","shell.execute_reply":"2022-06-29T11:43:26.601169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Opencv","metadata":{}},{"cell_type":"code","source":"img = cv2.imread(PATH)\nimg.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-29T11:45:42.030308Z","iopub.execute_input":"2022-06-29T11:45:42.030723Z","iopub.status.idle":"2022-06-29T11:45:42.13922Z","shell.execute_reply.started":"2022-06-29T11:45:42.03069Z","shell.execute_reply":"2022-06-29T11:45:42.137997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%timeit\nimg = cv2.imread(PATH)\nimg = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\nimg = torch.tensor(img)","metadata":{"execution":{"iopub.status.busy":"2022-06-29T11:51:38.04989Z","iopub.execute_input":"2022-06-29T11:51:38.050407Z","iopub.status.idle":"2022-06-29T11:51:47.093131Z","shell.execute_reply.started":"2022-06-29T11:51:38.050365Z","shell.execute_reply":"2022-06-29T11:51:47.091619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nplt.imshow(img)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-29T11:51:24.636044Z","iopub.execute_input":"2022-06-29T11:51:24.636559Z","iopub.status.idle":"2022-06-29T11:51:25.794917Z","shell.execute_reply.started":"2022-06-29T11:51:24.636519Z","shell.execute_reply":"2022-06-29T11:51:25.793506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Tiffile","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"%%timeit\nimg = torch.tensor(timread(PATH))","metadata":{"execution":{"iopub.status.busy":"2022-06-29T11:58:06.084884Z","iopub.execute_input":"2022-06-29T11:58:06.086181Z","iopub.status.idle":"2022-06-29T11:58:20.286877Z","shell.execute_reply.started":"2022-06-29T11:58:06.086123Z","shell.execute_reply":"2022-06-29T11:58:20.285712Z"},"trusted":true},"execution_count":null,"outputs":[]}]}