{"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 matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np\nfrom tqdm import tqdm\nimport os\n\nfrom PIL import Image\n\nimport albumentations as A\nimport cv2\n\nfrom skimage.transform import resize\nimport tifffile as tiff \nimport tensorflow as tf\n\n\nfrom glob import glob\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport tensorflow_datasets as tfds\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport tifffile as tiff \nfrom tensorflow.keras.applications import EfficientNetB4\nfrom skimage.transform import resize\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nimport os\nimport glob\nfrom PIL import Image, ImageOps\n\nfrom tqdm.auto import tqdm\nfrom joblib import Parallel, delayed","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-27T13:09:05.998035Z","iopub.execute_input":"2022-08-27T13:09:05.998525Z","iopub.status.idle":"2022-08-27T13:09:16.521315Z","shell.execute_reply.started":"2022-08-27T13:09:05.998415Z","shell.execute_reply":"2022-08-27T13:09:16.519926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainCsv = pd.read_csv('../input/hubmap-organ-segmentation/train.csv')\ntestCsv = pd.read_csv('../input/hubmap-organ-segmentation/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-27T13:10:16.382834Z","iopub.execute_input":"2022-08-27T13:10:16.383732Z","iopub.status.idle":"2022-08-27T13:10:16.744437Z","shell.execute_reply.started":"2022-08-27T13:10:16.383699Z","shell.execute_reply":"2022-08-27T13:10:16.743135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Helper Function**","metadata":{}},{"cell_type":"code","source":"def rle2mask(rle, width, target_size=None):\n    if target_size == None:\n        target_size = width\n\n    rle = np.array(list(map(int, rle.split())))\n    label = np.zeros((width*width))\n    \n    for start, end in zip(rle[::2], rle[1::2]):\n        label[start:start+end] = 1\n        \n    #Convert label to image\n    label = Image.fromarray(label.reshape(width, width))\n    #Resize label\n    label = label.resize((target_size, target_size))\n    label = np.array(label).astype(float)\n    #rescale label\n    label = np.round((label - label.min())/(label.max() - label.min()))\n    \n    return label.T\n\ndef mask2rle(mask, orig_dim=160):\n    #Rescale image to original size\n    size = int(len(mask.flatten())**.5)\n    n = Image.fromarray(mask.reshape((size, size))*255.0)\n    n = n.resize((orig_dim, orig_dim))\n    n = np.array(n).astype(np.float32)\n    #Get pixels to flatten\n    pixels = n.T.flatten()\n    #Round the pixels using the half of the range of pixel value\n    pixels = (pixels-min(pixels) > ((max(pixels)-min(pixels))/2)).astype(int)\n    pixels = np.nan_to_num(pixels) #incase of zero-div-error\n    \n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0]\n    runs[1::2] -= runs[::2]\n    \n    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2022-08-27T13:10:16.973760Z","iopub.execute_input":"2022-08-27T13:10:16.974959Z","iopub.status.idle":"2022-08-27T13:10:16.989157Z","shell.execute_reply.started":"2022-08-27T13:10:16.974907Z","shell.execute_reply":"2022-08-27T13:10:16.987422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#We will be training a model for each organ\norgans = trainCsv.organ.unique()\nprint(\"Organs:\", organs)","metadata":{"execution":{"iopub.status.busy":"2022-08-27T13:10:17.528168Z","iopub.execute_input":"2022-08-27T13:10:17.528974Z","iopub.status.idle":"2022-08-27T13:10:17.556259Z","shell.execute_reply.started":"2022-08-27T13:10:17.528859Z","shell.execute_reply":"2022-08-27T13:10:17.553363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"largeintestine = tf.keras.models.load_model(\"../input/hubmap-hpa-organ-segmentor/largeintestine_DLV3BestModel.h5\", compile=False)\nlung = tf.keras.models.load_model(\"../input/hubmap-hpa-organ-segmentor/lung_DLV3BestModel.h5\", compile=False)\nspleen = tf.keras.models.load_model(\"../input/hubmap-hpa-organ-segmentor/spleen_DLV3BestModel.h5\", compile=False)\nprostate = tf.keras.models.load_model(\"../input/hubmap-hpa-organ-segmentor/prostate_DLV3BestModel.h5\", compile=False)\nkidney = tf.keras.models.load_model(\"../input/hubmap-hpa-organ-segmentor/kidney_DLV3BestModel.h5\", compile=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-27T13:10:17.814566Z","iopub.execute_input":"2022-08-27T13:10:17.815005Z","iopub.status.idle":"2022-08-27T13:10:34.925300Z","shell.execute_reply.started":"2022-08-27T13:10:17.814971Z","shell.execute_reply":"2022-08-27T13:10:34.923941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainCsv = pd.read_csv('../input/hubmap-organ-segmentation/train.csv')\ntestCsv = pd.read_csv('../input/hubmap-organ-segmentation/test.csv')\n\nsub = {'id':[], 'rle':[]}\n\nfor idx,row in tqdm(testCsv.iterrows(),total=len(testCsv)):\n    idx = str(row['id'])\n    \n    image = Image.open(f\"../input/hubmap-organ-segmentation/test_images/{idx}.tiff\")        \n    image = image.resize((768, 768))\n    image = np.array(image) / 255.\n    image = image.reshape(-1,768,768,3)\n    \n    if(row['organ'] == 'spleen'): preds = spleen.predict(image).round()\n    elif(row['organ'] == 'largeintestine'): preds = largeintestine.predict(image).round()\n    elif(row['organ'] == 'lung'): preds = lung.predict(image).round()\n    elif(row['organ'] == 'prostate'): preds = prostate.predict(image).round()\n    elif(row['organ'] == 'kidney'): preds = kidney.predict(image).round()\n    \n    rle = [mask2rle(preds[0], row['img_width'])]\n\n    sub['id'] += [idx]\n    sub['rle'] += rle\n    \n\ndf = pd.DataFrame(sub)\ndf.to_csv('submission.csv',index=False)\ndf","metadata":{"execution":{"iopub.status.busy":"2022-08-27T13:10:34.929078Z","iopub.execute_input":"2022-08-27T13:10:34.929387Z","iopub.status.idle":"2022-08-27T13:10:45.147642Z","shell.execute_reply.started":"2022-08-27T13:10:34.929356Z","shell.execute_reply":"2022-08-27T13:10:45.145949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 20))\nfor i in range(1):\n    plt.figure(figsize=(20, 20))\n    plt.subplot(1, 2, 1)\n    plt.title('Orginal Image')\n    plt.imshow(image[0])\n    plt.subplot(1, 2, 2)\n    plt.title('Predicted')\n    plt.imshow(image[0])\n    plt.imshow(preds[i], cmap='hot', alpha=0.5)","metadata":{"execution":{"iopub.status.busy":"2022-08-27T13:10:45.149787Z","iopub.execute_input":"2022-08-27T13:10:45.150557Z","iopub.status.idle":"2022-08-27T13:10:46.153320Z","shell.execute_reply.started":"2022-08-27T13:10:45.150510Z","shell.execute_reply":"2022-08-27T13:10:46.147787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"---","metadata":{}}]}