{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "train_nucleus_segmentation_model.ipynb", "provenance": [], "collapsed_sections": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" }, "accelerator": "GPU", "widgets": { "application/vnd.jupyter.widget-state+json": { "2d34bafe083f4349961c2a43be326296": { "model_module": "@jupyter-widgets/controls", "model_name": "HBoxModel", "state": { "_view_name": "HBoxView", "_dom_classes": [], "_model_name": "HBoxModel", "_view_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_view_count": null, "_view_module_version": "1.5.0", "box_style": "", "layout": "IPY_MODEL_cd36b2adfb964ee2a687a6b276852d7a", "_model_module": "@jupyter-widgets/controls", "children": [ "IPY_MODEL_57d86447e87a414b9f1232da8516105a", "IPY_MODEL_8663d310050a44d89620f49910cad18b" ] } }, "cd36b2adfb964ee2a687a6b276852d7a": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "state": { "_view_name": "LayoutView", "grid_template_rows": null, "right": null, "justify_content": null, "_view_module": "@jupyter-widgets/base", "overflow": null, "_model_module_version": "1.2.0", "_view_count": null, "flex_flow": null, "width": null, "min_width": null, "border": null, "align_items": null, "bottom": null, "_model_module": "@jupyter-widgets/base", "top": null, "grid_column": null, "overflow_y": null, "overflow_x": null, "grid_auto_flow": null, "grid_area": null, "grid_template_columns": null, "flex": null, "_model_name": "LayoutModel", "justify_items": null, "grid_row": null, "max_height": null, "align_content": null, "visibility": null, "align_self": null, "height": null, "min_height": null, "padding": null, "grid_auto_rows": null, "grid_gap": null, "max_width": null, "order": null, "_view_module_version": "1.2.0", "grid_template_areas": null, "object_position": null, "object_fit": null, "grid_auto_columns": null, "margin": null, "display": null, "left": null } }, "57d86447e87a414b9f1232da8516105a": { "model_module": "@jupyter-widgets/controls", "model_name": "FloatProgressModel", "state": { "_view_name": "ProgressView", "style": "IPY_MODEL_196fbf6aad424595ab98b39e24b1e8c4", "_dom_classes": [], "description": "100%", "_model_name": "FloatProgressModel", "bar_style": "success", "max": 6790, "_view_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "value": 6790, "_view_count": null, "_view_module_version": "1.5.0", "orientation": "horizontal", "min": 0, "description_tooltip": null, "_model_module": "@jupyter-widgets/controls", "layout": "IPY_MODEL_b15336e61a1241d49343afee93933512" } }, "8663d310050a44d89620f49910cad18b": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "state": { "_view_name": "HTMLView", "style": "IPY_MODEL_f8426089e67847a88d120bf3770a0319", "_dom_classes": [], "description": "", "_model_name": "HTMLModel", "placeholder": "", "_view_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "value": " 6790/6790 [02:08<00:00, 52.89it/s]", "_view_count": null, "_view_module_version": "1.5.0", "description_tooltip": null, "_model_module": "@jupyter-widgets/controls", "layout": "IPY_MODEL_bc419a9c1f78401597dc074416a0abe3" } }, "196fbf6aad424595ab98b39e24b1e8c4": { "model_module": "@jupyter-widgets/controls", "model_name": "ProgressStyleModel", "state": { "_view_name": "StyleView", "_model_name": "ProgressStyleModel", "description_width": "initial", "_view_module": "@jupyter-widgets/base", "_model_module_version": "1.5.0", "_view_count": null, "_view_module_version": "1.2.0", "bar_color": null, "_model_module": "@jupyter-widgets/controls" } }, "b15336e61a1241d49343afee93933512": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "state": { "_view_name": "LayoutView", "grid_template_rows": null, "right": null, "justify_content": null, "_view_module": "@jupyter-widgets/base", "overflow": null, "_model_module_version": "1.2.0", "_view_count": null, "flex_flow": null, "width": null, "min_width": null, "border": null, "align_items": null, "bottom": null, "_model_module": "@jupyter-widgets/base", "top": null, "grid_column": null, "overflow_y": null, "overflow_x": null, "grid_auto_flow": null, "grid_area": null, "grid_template_columns": null, "flex": null, "_model_name": "LayoutModel", "justify_items": null, "grid_row": null, "max_height": null, "align_content": null, "visibility": null, "align_self": null, "height": null, "min_height": null, "padding": null, "grid_auto_rows": null, "grid_gap": null, "max_width": null, "order": null, "_view_module_version": "1.2.0", "grid_template_areas": null, "object_position": null, "object_fit": null, "grid_auto_columns": null, "margin": null, "display": null, "left": null } }, "f8426089e67847a88d120bf3770a0319": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "state": { "_view_name": "StyleView", "_model_name": "DescriptionStyleModel", "description_width": "", "_view_module": "@jupyter-widgets/base", "_model_module_version": "1.5.0", "_view_count": null, "_view_module_version": "1.2.0", "_model_module": "@jupyter-widgets/controls" } }, "bc419a9c1f78401597dc074416a0abe3": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "state": { "_view_name": "LayoutView", "grid_template_rows": null, "right": null, "justify_content": null, "_view_module": "@jupyter-widgets/base", "overflow": null, "_model_module_version": "1.2.0", "_view_count": null, "flex_flow": null, "width": null, "min_width": null, "border": null, "align_items": null, "bottom": null, "_model_module": "@jupyter-widgets/base", "top": null, "grid_column": null, "overflow_y": null, "overflow_x": null, "grid_auto_flow": null, "grid_area": null, "grid_template_columns": null, "flex": null, "_model_name": "LayoutModel", "justify_items": null, "grid_row": null, "max_height": null, "align_content": null, "visibility": null, "align_self": null, "height": null, "min_height": null, "padding": null, "grid_auto_rows": null, "grid_gap": null, "max_width": null, "order": null, "_view_module_version": "1.2.0", "grid_template_areas": null, "object_position": null, "object_fit": null, "grid_auto_columns": null, "margin": null, "display": null, "left": null } }, "2dd1f1519c464af4b8d22015894215e3": { "model_module": "@jupyter-widgets/controls", "model_name": "HBoxModel", "state": { "_view_name": "HBoxView", "_dom_classes": [], "_model_name": "HBoxModel", "_view_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_view_count": null, "_view_module_version": "1.5.0", "box_style": "", "layout": "IPY_MODEL_32900f1a90c74002b622b46efbbe74af", "_model_module": "@jupyter-widgets/controls", "children": [ "IPY_MODEL_8eda6e50d05144abb1ca845b42f749e6", "IPY_MODEL_62ac4162278642298b89e894b25bdd70" ] } }, "32900f1a90c74002b622b46efbbe74af": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "state": { "_view_name": "LayoutView", "grid_template_rows": null, "right": null, "justify_content": null, "_view_module": "@jupyter-widgets/base", "overflow": null, "_model_module_version": "1.2.0", "_view_count": null, "flex_flow": null, "width": null, "min_width": null, "border": null, "align_items": null, "bottom": null, "_model_module": "@jupyter-widgets/base", "top": null, "grid_column": null, "overflow_y": null, "overflow_x": null, "grid_auto_flow": null, "grid_area": null, "grid_template_columns": null, "flex": null, "_model_name": "LayoutModel", "justify_items": null, "grid_row": null, "max_height": null, "align_content": null, "visibility": null, "align_self": null, "height": null, "min_height": null, "padding": null, "grid_auto_rows": null, "grid_gap": null, "max_width": null, "order": null, "_view_module_version": "1.2.0", "grid_template_areas": null, "object_position": null, "object_fit": null, "grid_auto_columns": null, "margin": null, "display": null, "left": null } }, "8eda6e50d05144abb1ca845b42f749e6": { "model_module": "@jupyter-widgets/controls", "model_name": "FloatProgressModel", "state": { "_view_name": "ProgressView", "style": "IPY_MODEL_c9fa2624e8cc42e09a0edad928649180", "_dom_classes": [], "description": "100%", "_model_name": "FloatProgressModel", "bar_style": "success", "max": 6790, "_view_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "value": 6790, "_view_count": null, "_view_module_version": "1.5.0", "orientation": "horizontal", "min": 0, "description_tooltip": null, "_model_module": "@jupyter-widgets/controls", "layout": "IPY_MODEL_abe2af4d50044e5cbbd67091f0d6d441" } }, "62ac4162278642298b89e894b25bdd70": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "state": { "_view_name": "HTMLView", "style": "IPY_MODEL_a98abae7b7b44093918dad706bcdc84c", "_dom_classes": [], "description": "", "_model_name": "HTMLModel", "placeholder": "", "_view_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "value": " 6790/6790 [01:18<00:00, 86.54it/s]", "_view_count": null, "_view_module_version": "1.5.0", "description_tooltip": null, "_model_module": "@jupyter-widgets/controls", "layout": "IPY_MODEL_ae552c62c49a4bba899b5b0573ff7f16" } }, "c9fa2624e8cc42e09a0edad928649180": { "model_module": "@jupyter-widgets/controls", "model_name": "ProgressStyleModel", "state": { "_view_name": "StyleView", "_model_name": "ProgressStyleModel", "description_width": "initial", "_view_module": "@jupyter-widgets/base", "_model_module_version": "1.5.0", "_view_count": null, "_view_module_version": "1.2.0", "bar_color": null, "_model_module": "@jupyter-widgets/controls" } }, "abe2af4d50044e5cbbd67091f0d6d441": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "state": { "_view_name": "LayoutView", "grid_template_rows": null, "right": null, "justify_content": null, "_view_module": "@jupyter-widgets/base", "overflow": null, "_model_module_version": "1.2.0", "_view_count": null, "flex_flow": null, "width": null, "min_width": null, "border": null, "align_items": null, "bottom": null, "_model_module": "@jupyter-widgets/base", "top": null, "grid_column": null, "overflow_y": null, "overflow_x": null, "grid_auto_flow": null, "grid_area": null, "grid_template_columns": null, "flex": null, "_model_name": "LayoutModel", "justify_items": null, "grid_row": null, "max_height": null, "align_content": null, "visibility": null, "align_self": null, "height": null, "min_height": null, "padding": null, "grid_auto_rows": null, "grid_gap": null, "max_width": null, "order": null, "_view_module_version": "1.2.0", "grid_template_areas": null, "object_position": null, "object_fit": null, "grid_auto_columns": null, "margin": null, "display": null, "left": null } }, "a98abae7b7b44093918dad706bcdc84c": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "state": { "_view_name": "StyleView", "_model_name": "DescriptionStyleModel", "description_width": "", "_view_module": "@jupyter-widgets/base", "_model_module_version": "1.5.0", "_view_count": null, "_view_module_version": "1.2.0", "_model_module": "@jupyter-widgets/controls" } }, "ae552c62c49a4bba899b5b0573ff7f16": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "state": { "_view_name": "LayoutView", "grid_template_rows": null, "right": null, "justify_content": null, "_view_module": "@jupyter-widgets/base", "overflow": null, "_model_module_version": "1.2.0", "_view_count": null, "flex_flow": null, "width": null, "min_width": null, "border": null, "align_items": null, "bottom": null, "_model_module": "@jupyter-widgets/base", "top": null, "grid_column": null, "overflow_y": null, "overflow_x": null, "grid_auto_flow": null, "grid_area": null, "grid_template_columns": null, "flex": null, "_model_name": "LayoutModel", "justify_items": null, "grid_row": null, "max_height": null, "align_content": null, "visibility": null, "align_self": null, "height": null, "min_height": null, "padding": null, "grid_auto_rows": null, "grid_gap": null, "max_width": null, "order": null, "_view_module_version": "1.2.0", "grid_template_areas": null, "object_position": null, "object_fit": null, "grid_auto_columns": null, "margin": null, "display": null, "left": null } } } } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "IHIESswwkqzr" }, "source": [ "### Goal: Train a model to segment nucleii from C. elegans Fluorescence Microscopy data" ] }, { "cell_type": "code", "metadata": { "id": "XLzR2Z5hF3sI" }, "source": [ "!pip install albumentations \n", "!pip install segmentation-models-pytorch\n", "!pip install optuna" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "rgqpz7uIF8DA" }, "source": [ "import os, sys\n", "import numpy as np\n", "import cv2\n", "from PIL import Image\n", "import pandas as pd \n", "import matplotlib.pyplot as plt\n", "from tqdm import tqdm_notebook as tqdm\n", "from IPython.display import clear_output\n", "\n", "import torch\n", "import torch.nn as nn\n", "from torch.nn import functional as F\n", "import torch.optim as optim\n", "from torch.utils.data import DataLoader, Dataset\n", "from torchvision import transforms, utils\n", "import torchvision\n", "import torchvision.transforms as transforms\n", "from torchvision.transforms import ToTensor\n", "from torchvision.transforms import ToPILImage\n", "from torch.utils.data.dataset import random_split\n", "from torch.optim.lr_scheduler import ReduceLROnPlateau\n", "\n", "import segmentation_models_pytorch as smp\n", "from albumentations import *\n", "import time\n", "import optuna\n", "\n", "from os import listdir\n", "from os.path import isfile, join\n", "import csv\n", "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n" ], "execution_count": 3, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "S_5wiWc3lDTn" }, "source": [ "### Copy Preprocessed data from GDrive:" ] }, { "cell_type": "code", "metadata": { "id": "fMWPae6RL9lt", "outputId": "be00221d-88e4-4751-9f48-8427848ba094", "colab": { "base_uri": "https://localhost:8080/" } }, "source": [ "from google.colab import drive\n", "drive.mount('/content/drive')" ], "execution_count": 4, "outputs": [ { "output_type": "stream", "text": [ "Mounted at /content/drive\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "f3EPQ36QF_Tl" }, "source": [ "!cp /content/drive/MyDrive/mydata/celltrackingchallenge/nucleus_seg_data.zip /content/\n", "!unzip /content/nucleus_seg_data.zip" ], "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "1R3bByKglQbO" }, "source": [ "### Resize Images to 256x256" ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 200, "referenced_widgets": [ "2d34bafe083f4349961c2a43be326296", "cd36b2adfb964ee2a687a6b276852d7a", "57d86447e87a414b9f1232da8516105a", "8663d310050a44d89620f49910cad18b", "196fbf6aad424595ab98b39e24b1e8c4", "b15336e61a1241d49343afee93933512", "f8426089e67847a88d120bf3770a0319", "bc419a9c1f78401597dc074416a0abe3", "2dd1f1519c464af4b8d22015894215e3", "32900f1a90c74002b622b46efbbe74af", "8eda6e50d05144abb1ca845b42f749e6", "62ac4162278642298b89e894b25bdd70", "c9fa2624e8cc42e09a0edad928649180", "abe2af4d50044e5cbbd67091f0d6d441", "a98abae7b7b44093918dad706bcdc84c", "ae552c62c49a4bba899b5b0573ff7f16" ] }, "id": "p45QBxQxGus9", "outputId": "96c6a2a1-afbb-4477-eb3d-cf0e16e959e3" }, "source": [ "def resize(path):\n", " dirs = os.listdir( path )\n", " for item in tqdm(dirs):\n", " if os.path.isfile(path+item):\n", " im = Image.open(path+item)\n", " f, e = os.path.splitext(path+item)\n", " imResize = im.resize((256,256), Image.NEAREST)\n", " imResize.save(f+e, 'PNG', quality=100)\n", " \n", "features_path, label_path = \"/content/features/\", \"/content/segmentation_maps/\"\n", "resize(label_path)\n", "resize(features_path)" ], "execution_count": 6, "outputs": [ { "output_type": "stream", "text": [ "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:3: TqdmDeprecationWarning:\n", "\n", "This function will be removed in tqdm==5.0.0\n", "Please use `tqdm.notebook.tqdm` instead of `tqdm.tqdm_notebook`\n", "\n" ], "name": "stderr" }, { "output_type": "display_data", "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "2d34bafe083f4349961c2a43be326296", "version_minor": 0, "version_major": 2 }, "text/plain": [ "HBox(children=(FloatProgress(value=0.0, max=6790.0), HTML(value='')))" ] }, "metadata": { "tags": [] } }, { "output_type": "stream", "text": [ "\n" ], "name": "stdout" }, { "output_type": "display_data", "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "2dd1f1519c464af4b8d22015894215e3", "version_minor": 0, "version_major": 2 }, "text/plain": [ "HBox(children=(FloatProgress(value=0.0, max=6790.0), HTML(value='')))" ] }, "metadata": { "tags": [] } }, { "output_type": "stream", "text": [ "\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "j_r8DCzKlbQR" }, "source": [ "### Create a csv containing image id's" ] }, { "cell_type": "code", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 221 }, "id": "afXxCOF-F9UR", "outputId": "88f006cd-fb8d-464f-f998-6e1a300ef3c9" }, "source": [ "ids=[]\n", "label_filenames = [f for f in listdir(label_path) if isfile(join(label_path, f))]\n", "feature_filenames = [f for f in listdir(features_path) if isfile(join(features_path, f))]\n", "for i in range(len(label_filenames)):\n", " ids.append(label_filenames[i][1:])\n", "print(len(ids))\n", "\n", "df = pd.DataFrame(ids ,columns=[\"file_ids\"])\n", "df.to_csv('file_ids.csv', index=False)\n", "\n", "#sanity check\n", "df = pd.read_csv('file_ids.csv')\n", "df.head()" ], "execution_count": 7, "outputs": [ { "output_type": "stream", "text": [ "6790\n" ], "name": "stdout" }, { "output_type": "execute_result", "data": { "text/html": [ "
\n", " | file_ids | \n", "
---|---|
0 | \n", "190_18.png | \n", "
1 | \n", "47_19.png | \n", "
2 | \n", "112_28.png | \n", "
3 | \n", "113_17.png | \n", "
4 | \n", "66_24.png | \n", "