{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport os\nimport cv2\nimport random\nimport skimage.io\nfrom tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"BASE_PATH = \"/kaggle/input/prostate-cancer-grade-assessment\"\nTRAIN_IMG_PATH = os.path.join(BASE_PATH, \"train_images\")\nMASKS_PATH = os.path.join(BASE_PATH, \"train_label_masks\")\n\ntrain = pd.read_csv(os.path.join(BASE_PATH, \"train.csv\"))\n\n# 0 is highest quality, 1 is x4 smaller, 2 is x16 smaller\nIMAGES_LEVEL = 1\nTILE_SIZE = 256\nTILE_NUM = 32","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\"\"\"\nTaken from https://www.kaggle.com/iafoss/panda-16x128x128-tiles\n\"\"\"\ndef tile(img, mask):\n    sz = TILE_SIZE\n    N = TILE_NUM\n    \n    result = []\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]],\n                constant_values=255)\n    mask = np.pad(mask,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],\n                constant_values=0)\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    mask = mask.reshape(mask.shape[0]//sz,sz,mask.shape[1]//sz,sz,3)\n    mask = mask.transpose(0,2,1,3,4).reshape(-1,sz,sz,3)\n    if len(img) < N:\n        mask = np.pad(mask,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=0)\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    mask = mask[idxs]\n    for i in range(len(img)):\n        result.append({'img':img[i], 'mask':mask[i], 'idx':i})\n    return result","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def read_img(image_id):\n    image = skimage.io.MultiImage(f\"{TRAIN_IMG_PATH}/{image_id}.tiff\")\n    return image[IMAGES_LEVEL]\n\ndef read_mask(image_id):\n    image = skimage.io.MultiImage(f\"{MASKS_PATH}/{image_id}_mask.tiff\")\n    if len(image) <= 1:\n        shape = read_img(image_id).shape\n        return np.zeros(shape=shape, dtype=np.uint8)\n    return image[IMAGES_LEVEL]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Let's visualize a chosen image and it's corresponding tiles side to side:","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# chosen_im_i = random.randint(0, len(train)-1)\nchosen_im_i = 100\nchosen_img = read_img(train.at[chosen_im_i, \"image_id\"])\nchosen_img_mask = read_mask(train.at[chosen_im_i, \"image_id\"])\ntiles_example = tile(chosen_img, chosen_img_mask)\n\nfig, axis = plt.subplots(8,8, figsize=(25,20))\nfor i in range(4):\n    for j in range(8):\n        t = tiles_example[j + 8*i]\n        axis[2*i, j].imshow(t['img'])\n        axis[2*i+1, j].imshow(t['mask']*100)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, axis = plt.subplots(1,2, figsize=(25,10))\naxis[0].imshow(chosen_img)\naxis[1].imshow(chosen_img_mask * 100)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Unfortunately, colab notebooks offer a limit of 5GB disk space.  \nThis should be done in preprocessing or saved elsewhere","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n\nos.makedirs(\"images\", exist_ok=True)\nos.makedirs(\"masks\", exist_ok=True)\n\nLIMIT = 3\nfor image_id in train.image_id[:LIMIT]:\n    img = read_img(image_id)\n    mask = read_mask(image_id)\n    tiles = tile(img, mask)\n\n    for i in range(TILE_NUM):\n        plt.imsave(fname=f\"images/{image_id}_{i}.png\", arr=tiles[i]['img'], format=\"png\")\n        plt.imsave(fname=f\"masks/{image_id}_{i}.png\", arr=tiles[i]['mask'], format=\"png\")","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}