{"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 pandas as pd\nimport matplotlib.pyplot as plt\nimport os \nimport torch\nimport cv2\nfrom skimage import io","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-28T14:51:41.380462Z","iopub.execute_input":"2022-06-28T14:51:41.381101Z","iopub.status.idle":"2022-06-28T14:51:44.489287Z","shell.execute_reply.started":"2022-06-28T14:51:41.380986Z","shell.execute_reply":"2022-06-28T14:51:44.487612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_grid(idx,crop):\n    '''\n    idx = 11\n    img_t = np.transpose(output[idx].to(torch.uint8).numpy(),(1,2,0))\n    print(img_t.dtype)\n    plt.imshow(img_t)\n    \n    return a tensor of shape = (no_of_image,3,grid_w,grid_h) and dtype torch.float32\n    '''\n\n    img = io.imread(f\"../input/hubmap-organ-segmentation/train_images/{idx}.tiff\")\n    if crop:\n        img = img[250:-250,250:-250,:]\n    img_h,img_w = img.shape[0],img.shape[1]\n    img = torch.tensor(np.transpose(img,(2,0,1))).unsqueeze(0).to(torch.float32)\n\n    grid_h,grid_w = 250,250\n    unfold = torch.nn.Unfold(kernel_size = (grid_h,grid_w),stride = (grid_h,grid_w))\n    number_of_images = (img_h/grid_h)*(img_w/grid_w)\n    output = torch.transpose(unfold(img),2,1).reshape([1,int(number_of_images),3,250,250]).squeeze()\n    \n    return output\n","metadata":{"execution":{"iopub.status.busy":"2022-06-28T14:51:44.492019Z","iopub.execute_input":"2022-06-28T14:51:44.492676Z","iopub.status.idle":"2022-06-28T14:51:44.503394Z","shell.execute_reply.started":"2022-06-28T14:51:44.492617Z","shell.execute_reply":"2022-06-28T14:51:44.501724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%timeit\nop = make_grid(10274,True)","metadata":{"execution":{"iopub.status.busy":"2022-06-28T14:52:31.976196Z","iopub.execute_input":"2022-06-28T14:52:31.976698Z","iopub.status.idle":"2022-06-28T14:52:46.675554Z","shell.execute_reply.started":"2022-06-28T14:52:31.976641Z","shell.execute_reply":"2022-06-28T14:52:46.674322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"make_grid(10274,True).shape","metadata":{"execution":{"iopub.status.busy":"2022-06-28T14:53:11.790538Z","iopub.execute_input":"2022-06-28T14:53:11.791576Z","iopub.status.idle":"2022-06-28T14:53:12.00856Z","shell.execute_reply.started":"2022-06-28T14:53:11.791514Z","shell.execute_reply":"2022-06-28T14:53:12.006916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output[idx].to(torch.int32).numpy().shape","metadata":{"execution":{"iopub.status.busy":"2022-06-28T14:52:12.459805Z","iopub.execute_input":"2022-06-28T14:52:12.460268Z","iopub.status.idle":"2022-06-28T14:52:12.486043Z","shell.execute_reply.started":"2022-06-28T14:52:12.460235Z","shell.execute_reply":"2022-06-28T14:52:12.484251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}