{"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":"!pip install \"../input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl\"\n!pip install \"../input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl\"\n!pip install \"../input/hpacellsegmentatormaster/HPA-Cell-Segmentation-master\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install \"../input/hpapytorchzoozip/pytorch_zoo-master\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import hpacellseg.cellsegmentator as cellsegmentator\nfrom hpacellseg.utils import label_cell, label_nuclei\n\nNUC_MODEL = '../input/hpacellsegmentatormodelweights/dpn_unet_nuclei_v1.pth'\nCELL_MODEL = '../input/hpacellsegmentatormodelweights/dpn_unet_cell_3ch_v1.pth'\n\nsegmentator = cellsegmentator.CellSegmentator(\n    NUC_MODEL,\n    CELL_MODEL,\n    scale_factor=0.25,\n    device='cuda',\n    padding=False,\n    multi_channel_model=True\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"channel_maps = {\"red\":\"microtubule \", \n                \"blue\":\"nuclei\", \n                \"yellow\":\"Endoplasmic Reticulum\",\n                \"green\":\"protein of interest\"}\n\ndef build_image_names(image_id: str) -> list:\n    # mt is the mitchondria\n    mt = f'/kaggle/input/hpa-single-cell-image-classification/test/{image_id}_red.png'\n    \n    # er is the endoplasmic reticulum\n    er = f'/kaggle/input/hpa-single-cell-image-classification/test/{image_id}_yellow.png'\n    \n    # nu is the nuclei\n    nu = f'/kaggle/input/hpa-single-cell-image-classification/test/{image_id}_blue.png'\n    \n    return [mt], [er], [nu], [[mt], [er], [nu]]\n\nmt, er, nu, images = build_image_names(image_id='0040581b-f1f2-4fbe-b043-b6bfea5404bb')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# For nuclei\nnuc_segmentations = segmentator.pred_nuclei(images[2])\n\n# For full cells\ncell_segmentations = segmentator.pred_cells(images)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfig, ax = plt.subplots(1,3,figsize=(40,100))\nraw = plt.imread(mt[0])\nax[0].imshow(raw)\nax[1].imshow(cell_segmentations[0])\nax[2].imshow(nuc_segmentations[0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# post-processing\ni = 0\nnuclei_mask, cell_mask = label_cell(nuc_segmentations[i], cell_segmentations[i])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\ni = 0\n\nmicrotubule = plt.imread(mt[i])    \nendoplasmicrec = plt.imread(er[i])    \nnuclei = plt.imread(nu[i])\nmask = cell_mask\nimg = np.dstack((microtubule, endoplasmicrec, nuclei))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20, 10))\nplt.imshow(img)\nplt.imshow(mask, alpha=0.5)\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.savez(\"0040581b-f1f2-4fbe-b043-b6bfea5404bb.npz\",mask)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm ./0040581b-f1f2-4fbe-b043-b6bfea5404bb.npz","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}