{"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":"## Image loading speed test\n### Brett Olsen, March 2023\n\nWe're going to look at a couple of different options for loading in the raw tif files from an image stack.  \nWe'll use one of the larger image files to test with, from the second fragment.","metadata":{}},{"cell_type":"code","source":"import numpy as np\n\nimport rasterio\nfrom PIL import Image\nfrom tifffile import tifffile\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2023-10-19T16:32:40.803397Z","iopub.execute_input":"2023-10-19T16:32:40.804593Z","iopub.status.idle":"2023-10-19T16:32:41.633074Z","shell.execute_reply.started":"2023-10-19T16:32:40.804548Z","shell.execute_reply":"2023-10-19T16:32:41.631833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_FILE = \"/kaggle/input/vesuvius-challenge-ink-detection/train/2/surface_volume/08.tif\"","metadata":{"execution":{"iopub.status.busy":"2023-10-19T16:33:11.839660Z","iopub.execute_input":"2023-10-19T16:33:11.840113Z","iopub.status.idle":"2023-10-19T16:33:11.845532Z","shell.execute_reply.started":"2023-10-19T16:33:11.840078Z","shell.execute_reply":"2023-10-19T16:33:11.844268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%timeit rasterio.open(IMAGE_FILE).read(1)\ndataset1 = rasterio.open(IMAGE_FILE).read(1)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T16:33:12.738543Z","iopub.execute_input":"2023-10-19T16:33:12.738930Z","iopub.status.idle":"2023-10-19T16:33:20.818519Z","shell.execute_reply.started":"2023-10-19T16:33:12.738898Z","shell.execute_reply":"2023-10-19T16:33:20.817540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%timeit np.array(Image.open(IMAGE_FILE))\ndataset2 = np.array(Image.open(IMAGE_FILE))","metadata":{"execution":{"iopub.status.busy":"2023-10-19T16:33:55.052266Z","iopub.execute_input":"2023-10-19T16:33:55.052719Z","iopub.status.idle":"2023-10-19T16:34:00.891357Z","shell.execute_reply.started":"2023-10-19T16:33:55.052683Z","shell.execute_reply":"2023-10-19T16:34:00.890384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%timeit tifffile.imread(IMAGE_FILE)\ndataset3 = tifffile.imread(IMAGE_FILE)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T16:34:00.893082Z","iopub.execute_input":"2023-10-19T16:34:00.893514Z","iopub.status.idle":"2023-10-19T16:34:08.436871Z","shell.execute_reply.started":"2023-10-19T16:34:00.893482Z","shell.execute_reply":"2023-10-19T16:34:08.435663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%timeit cv2.imread(IMAGE_FILE, cv2.IMREAD_UNCHANGED)\ndataset4 = cv2.imread(IMAGE_FILE, cv2.IMREAD_UNCHANGED)","metadata":{"execution":{"iopub.status.busy":"2023-10-19T16:34:08.438175Z","iopub.execute_input":"2023-10-19T16:34:08.438542Z","iopub.status.idle":"2023-10-19T16:34:14.496370Z","shell.execute_reply.started":"2023-10-19T16:34:08.438510Z","shell.execute_reply":"2023-10-19T16:34:14.495518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for data in [dataset1, dataset2, dataset3, dataset4]:\n    print(data.dtype, data.shape)\n    if not np.all(data == dataset1):\n        print(\"Mismatched data\")","metadata":{"execution":{"iopub.status.busy":"2023-10-19T16:34:14.498242Z","iopub.execute_input":"2023-10-19T16:34:14.499488Z","iopub.status.idle":"2023-10-19T16:34:14.863886Z","shell.execute_reply.started":"2023-10-19T16:34:14.499422Z","shell.execute_reply":"2023-10-19T16:34:14.862598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Cool.  Same data across the board, but looks like I'm getting significantly faster load times using tiffile.  Looks good!","metadata":{}}]}