{"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":"!conda install -y pyvips","metadata":{"execution":{"iopub.status.busy":"2022-07-30T14:42:16.587329Z","iopub.execute_input":"2022-07-30T14:42:16.587817Z","iopub.status.idle":"2022-07-30T14:44:32.474952Z","shell.execute_reply.started":"2022-07-30T14:42:16.587726Z","shell.execute_reply":"2022-07-30T14:44:32.473424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2 \nimport time\nimport pyvips\nimport numpy as np\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-30T15:13:18.354068Z","iopub.execute_input":"2022-07-30T15:13:18.354543Z","iopub.status.idle":"2022-07-30T15:13:18.360072Z","shell.execute_reply.started":"2022-07-30T15:13:18.354506Z","shell.execute_reply":"2022-07-30T15:13:18.359032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir(\"/kaggle/working/\")","metadata":{"execution":{"iopub.status.busy":"2022-07-30T15:13:18.911081Z","iopub.execute_input":"2022-07-30T15:13:18.911744Z","iopub.status.idle":"2022-07-30T15:13:18.917098Z","shell.execute_reply.started":"2022-07-30T15:13:18.911695Z","shell.execute_reply":"2022-07-30T15:13:18.916166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2022-07-30T15:13:19.03856Z","iopub.execute_input":"2022-07-30T15:13:19.039322Z","iopub.status.idle":"2022-07-30T15:13:19.854568Z","shell.execute_reply.started":"2022-07-30T15:13:19.039282Z","shell.execute_reply":"2022-07-30T15:13:19.853342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_dir = \"../input/mayo-train-images-size1024-n16\"\noutput_dir = \"./mayo-train-images-size64-n16\"","metadata":{"execution":{"iopub.status.busy":"2022-07-30T15:13:19.856626Z","iopub.execute_input":"2022-07-30T15:13:19.85773Z","iopub.status.idle":"2022-07-30T15:13:19.863081Z","shell.execute_reply.started":"2022-07-30T15:13:19.85769Z","shell.execute_reply":"2022-07-30T15:13:19.861391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir mayo-train-images-size64-n16","metadata":{"execution":{"iopub.status.busy":"2022-07-30T15:13:19.864356Z","iopub.execute_input":"2022-07-30T15:13:19.865157Z","iopub.status.idle":"2022-07-30T15:13:20.693106Z","shell.execute_reply.started":"2022-07-30T15:13:19.865119Z","shell.execute_reply":"2022-07-30T15:13:20.691793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ","metadata":{"execution":{"iopub.status.busy":"2022-07-30T15:13:20.695184Z","iopub.execute_input":"2022-07-30T15:13:20.696267Z","iopub.status.idle":"2022-07-30T15:13:21.518176Z","shell.execute_reply.started":"2022-07-30T15:13:20.696228Z","shell.execute_reply":"2022-07-30T15:13:21.516679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image_dir in os.listdir(root_dir):\n    image_dir_path = os.path.join(root_dir, image_dir)\n    dest_dir_path = os.path.join(output_dir, image_dir)\n    if os.path.exists(dest_dir_path):\n        pass\n    else :\n        print(\"creating directory:\", dest_dir_path) \n        os.makedirs(dest_dir_path)\n\n    for image_name in tqdm(os.listdir(image_dir_path)):\n        image_path = os.path.join(image_dir_path, image_name)\n        dest_path = os.path.join(dest_dir_path, image_name)\n        \n        image = cv2.imread(image_path)\n        image = cv2.resize(image, (64,64))\n        \n        cv2.imwrite(dest_path, image)\n\n    \n    time.sleep(120)","metadata":{"execution":{"iopub.status.busy":"2022-07-30T15:13:35.582593Z","iopub.execute_input":"2022-07-30T15:13:35.583072Z","iopub.status.idle":"2022-07-30T15:15:35.649874Z","shell.execute_reply.started":"2022-07-30T15:13:35.583031Z","shell.execute_reply":"2022-07-30T15:15:35.648754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ./mayo-train-images-size64-n16/","metadata":{"execution":{"iopub.status.busy":"2022-07-30T14:51:54.276543Z","iopub.execute_input":"2022-07-30T14:51:54.277579Z","iopub.status.idle":"2022-07-30T14:51:55.101271Z","shell.execute_reply.started":"2022-07-30T14:51:54.277526Z","shell.execute_reply":"2022-07-30T14:51:55.10015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test","metadata":{}},{"cell_type":"code","source":"!mkdir ./mayo-train-images-size64-n16-test/","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:48:04.167054Z","iopub.execute_input":"2022-07-30T11:48:04.167547Z","iopub.status.idle":"2022-07-30T11:48:04.981477Z","shell.execute_reply.started":"2022-07-30T11:48:04.167512Z","shell.execute_reply":"2022-07-30T11:48:04.97967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2022-07-30T14:52:06.562908Z","iopub.execute_input":"2022-07-30T14:52:06.563526Z","iopub.status.idle":"2022-07-30T14:52:07.371992Z","shell.execute_reply.started":"2022-07-30T14:52:06.563461Z","shell.execute_reply":"2022-07-30T14:52:07.370543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = \"../input/mayo-clinic-strip-ai/test\"\ndest_test_dir = \"./mayo-train-images-size64-n16-test/\"","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:48:40.633406Z","iopub.execute_input":"2022-07-30T11:48:40.633837Z","iopub.status.idle":"2022-07-30T11:48:40.640348Z","shell.execute_reply.started":"2022-07-30T11:48:40.633802Z","shell.execute_reply":"2022-07-30T11:48:40.638923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls mayo-train-images-size64-n16-test/","metadata":{"execution":{"iopub.status.busy":"2022-07-30T11:48:20.292971Z","iopub.execute_input":"2022-07-30T11:48:20.293421Z","iopub.status.idle":"2022-07-30T11:48:21.056558Z","shell.execute_reply.started":"2022-07-30T11:48:20.293386Z","shell.execute_reply":"2022-07-30T11:48:21.055316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def vips2numpy(vi):\n    return np.ndarray(\n        buffer=vi.write_to_memory(),\n        dtype=format_to_dtype[vi.format],\n        shape=[vi.height, vi.width, vi.bands])","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:23:55.021826Z","iopub.execute_input":"2022-07-29T13:23:55.022449Z","iopub.status.idle":"2022-07-29T13:23:55.033015Z","shell.execute_reply.started":"2022-07-29T13:23:55.022411Z","shell.execute_reply":"2022-07-29T13:23:55.031784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"format_to_dtype = {\n   'uchar': np.uint8,\n   'char': np.int8,\n   'ushort': np.uint16,\n   'short': np.int16,\n   'uint': np.uint32,\n   'int': np.int32,\n   'float': np.float32,\n   'double': np.float64,\n   'complex': np.complex64,\n   'dpcomplex': np.complex128,\n}","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:23:55.035511Z","iopub.execute_input":"2022-07-29T13:23:55.03589Z","iopub.status.idle":"2022-07-29T13:23:55.044668Z","shell.execute_reply.started":"2022-07-29T13:23:55.035847Z","shell.execute_reply":"2022-07-29T13:23:55.04382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def tile(img, sz=128, N=16):\n    shape = img.shape\n    pad0,pad1 = (sz - shape[0]%sz)%sz, (sz - shape[1]%sz)%sz\n    img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],constant_values=255)\n    img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3)\n    img = img.transpose(0,2,1,3,4).reshape(-1,sz,sz,3)\n    if len(img) < N:\n        img = np.pad(img,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=255)\n    idxs = np.argsort(img.reshape(img.shape[0],-1).sum(-1))[:N]\n    img = img[idxs]\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:23:55.045771Z","iopub.execute_input":"2022-07-29T13:23:55.046488Z","iopub.status.idle":"2022-07-29T13:23:55.082118Z","shell.execute_reply.started":"2022-07-29T13:23:55.046457Z","shell.execute_reply":"2022-07-29T13:23:55.081186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"crop_size = 64\nmax_size = 2000\nn_images = 16","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:23:55.104843Z","iopub.execute_input":"2022-07-29T13:23:55.106022Z","iopub.status.idle":"2022-07-29T13:23:55.114702Z","shell.execute_reply.started":"2022-07-29T13:23:55.105972Z","shell.execute_reply":"2022-07-29T13:23:55.11345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img_name in tqdm(os.listdir(test_dir)):\n    file_path = os.path.join(test_dir, img_name)\n    fname = img_name.split('.')[0]\n    image = pyvips.Image.thumbnail(file_path, max_size)\n    image = vips2numpy(image)\n    images = []\n    for img in tile(image, sz=crop_size, N=n_images):\n        image = pyvips.Image.thumbnail(file_path, max_size)\n        image = vips2numpy(image)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        images.append(image)\n        \n    images = np.stack(np.array(images), axis=0)\n    np.save(f\"{fname}.npy\", images)","metadata":{"execution":{"iopub.status.busy":"2022-07-29T13:28:17.424724Z","iopub.execute_input":"2022-07-29T13:28:17.428155Z","iopub.status.idle":"2022-07-29T14:01:50.846977Z","shell.execute_reply.started":"2022-07-29T13:28:17.42808Z","shell.execute_reply":"2022-07-29T14:01:50.845477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ","metadata":{"execution":{"iopub.status.busy":"2022-07-29T15:33:12.80989Z","iopub.execute_input":"2022-07-29T15:33:12.810861Z","iopub.status.idle":"2022-07-29T15:33:13.567257Z","shell.execute_reply.started":"2022-07-29T15:33:12.81081Z","shell.execute_reply":"2022-07-29T15:33:13.565933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}