{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nimport imageio\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames[:10]:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install \"../input/pycocotools/pycocotools-2.0-cp37-cp37m-linux_x86_64.whl\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install \"../input/hpapytorchzoozip/pytorch_zoo-master\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install \"../input/hpacellsegmentatormaster/HPA-Cell-Segmentation-master\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def 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]]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mt, er, nu, images = build_image_names(image_id='277b3f6d-099b-4b6d-8592-a06ad6f52beb')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"images","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"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)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# For nuclei\nnuc_segmentations = segmentator.pred_nuclei(images[2])\n\n# For full cells\ncell_segmentations = segmentator.pred_cells(images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# post-processing\ni = 0\nnuclei_mask, cell_mask = label_cell(nuc_segmentations[i], cell_segmentations[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ni = 0\nplt.figure(figsize=(20, 10))\nmicrotubule = plt.imread(mt[i])    \nendoplasmicrec = plt.imread(er[i])    \nnuclei = plt.imread(nu[i])\nmask = cell_mask\nimg = np.dstack((microtubule, endoplasmicrec, nuclei))\nplt.imshow(img)\nplt.imshow(mask, alpha=0.6)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot of just the mask\nplt.imshow(mask, alpha=0.6)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}