{"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 https://github.com/CellProfiling/HPA-Cell-Segmentation/archive/master.zip","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-05-20T01:09:31.677111Z","iopub.execute_input":"2021-05-20T01:09:31.677519Z","iopub.status.idle":"2021-05-20T01:09:44.27243Z","shell.execute_reply.started":"2021-05-20T01:09:31.677435Z","shell.execute_reply":"2021-05-20T01:09:44.271166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ipyplot -q","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:09:44.275237Z","iopub.execute_input":"2021-05-20T01:09:44.275554Z","iopub.status.idle":"2021-05-20T01:09:51.335243Z","shell.execute_reply.started":"2021-05-20T01:09:44.27552Z","shell.execute_reply":"2021-05-20T01:09:51.33395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import hpacellseg.cellsegmentator as cellsegmentator\nfrom hpacellseg.utils import label_cell, label_nuclei\nimport glob\nimport os\n\nimport os\nimport numpy as np\nimport pandas as pd\n\nimport hpacellseg.cellsegmentator as cellsegmentator\nfrom hpacellseg.utils import label_cell, label_nuclei\n\nfrom sklearn.preprocessing import MultiLabelBinarizer\n\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport plotly.graph_objects as go\nimport plotly.express as px\n\nfrom fastai.vision.all import *\nimport ipyplot\nimport imageio\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:09:51.337227Z","iopub.execute_input":"2021-05-20T01:09:51.337531Z","iopub.status.idle":"2021-05-20T01:09:56.638554Z","shell.execute_reply.started":"2021-05-20T01:09:51.337497Z","shell.execute_reply":"2021-05-20T01:09:56.637694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATA_DIR = \"/kaggle/input/hpa-single-cell-image-classification\"\ntrain = pd.read_csv(os.path.join(DATA_DIR,'train.csv'))\n\ncolours = ['_red.png', '_blue.png', '_yellow.png', '_green.png']\nTRAIN = '../input/hpa-single-cell-image-classification/train'\npaths = [[os.path.join(TRAIN, train.iloc[idx,0])+ colour for colour in colours] for idx in range(len(train))]","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:09:56.640462Z","iopub.execute_input":"2021-05-20T01:09:56.641213Z","iopub.status.idle":"2021-05-20T01:09:59.814701Z","shell.execute_reply.started":"2021-05-20T01:09:56.641163Z","shell.execute_reply":"2021-05-20T01:09:59.813559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LABELS= {\n    0: \"Nucleoplasm\",\n    1: \"Nuclear membrane\",\n    2: \"Nucleoli\",\n    3: \"Nucleoli fibrillar center\",\n    4: \"Nuclear speckles\",\n    5: \"Nuclear bodies\",\n    6: \"Endoplasmic reticulum\",\n    7: \"Golgi apparatus\",\n    8: \"Intermediate filaments\",\n    9: \"Actin filaments\",\n    10: \"Microtubules\",\n    11: \"Mitotic spindle\",\n    12: \"Centrosome\",\n    13: \"Plasma membrane\",\n    14: \"Mitochondria\",\n    15: \"Aggresome\",\n    16: \"Cytosol\",\n    17: \"Vesicles and punctate cytosolic patterns\",\n    18: \"Negative\"\n}","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:09:59.816438Z","iopub.execute_input":"2021-05-20T01:09:59.816892Z","iopub.status.idle":"2021-05-20T01:09:59.823791Z","shell.execute_reply.started":"2021-05-20T01:09:59.816846Z","shell.execute_reply":"2021-05-20T01:09:59.822298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = train.copy()\ntrain_csv['Label'] = train_csv['Label'].apply(lambda x: list(map(int,x.split(\"|\"))))\nmlb = MultiLabelBinarizer()\ntrain_csv[list(range(19))] = mlb.fit_transform(train_csv['Label'])\ntrain_csv.columns = [\"ID\", \"Label\"] + list(LABELS.values())","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:09:59.825687Z","iopub.execute_input":"2021-05-20T01:09:59.826461Z","iopub.status.idle":"2021-05-20T01:10:00.148351Z","shell.execute_reply.started":"2021-05-20T01:09:59.826377Z","shell.execute_reply":"2021-05-20T01:10:00.147175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:10:00.149892Z","iopub.execute_input":"2021-05-20T01:10:00.150316Z","iopub.status.idle":"2021-05-20T01:10:00.171649Z","shell.execute_reply.started":"2021-05-20T01:10:00.150271Z","shell.execute_reply":"2021-05-20T01:10:00.170574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = train.copy()\ntrain_csv['Label'] = train_csv['Label'].apply(lambda x: list(map(int,x.split(\"|\"))))\nmlb = MultiLabelBinarizer()\ntrain_csv[list(range(19))] = mlb.fit_transform(train_csv['Label'])\ntrain_csv.columns = [\"ID\", \"Label\"] + list(LABELS.values())\ntrain_csv.head(5)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:10:00.17436Z","iopub.execute_input":"2021-05-20T01:10:00.174694Z","iopub.status.idle":"2021-05-20T01:10:00.498751Z","shell.execute_reply.started":"2021-05-20T01:10:00.174665Z","shell.execute_reply":"2021-05-20T01:10:00.497588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_count = train_csv.iloc[:, 2:].sum()\npx.bar(label_count)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:10:00.50087Z","iopub.execute_input":"2021-05-20T01:10:00.501307Z","iopub.status.idle":"2021-05-20T01:10:01.859193Z","shell.execute_reply.started":"2021-05-20T01:10:00.501262Z","shell.execute_reply":"2021-05-20T01:10:01.858244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['num_classes'] = train['Label'].apply(lambda r: len(r.split('|')))\ntrain['num_classes'].value_counts().plot.bar(title='Examples with multiple labels', xlabel='number of labels per example', ylabel='# train examples')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:10:01.860531Z","iopub.execute_input":"2021-05-20T01:10:01.860826Z","iopub.status.idle":"2021-05-20T01:10:02.079739Z","shell.execute_reply.started":"2021-05-20T01:10:01.860798Z","shell.execute_reply":"2021-05-20T01:10:02.078731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nlabels = [str(i) for i in range(19)]\nclass_images = []\nfor label in labels:\n    r_img = train[train.Label == label].reset_index(drop=True).ID.loc[0] + '_red.png'\n    y_img = train[train.Label == label].reset_index(drop=True).ID.loc[0] + '_yellow.png'\n    b_img = train[train.Label == label].reset_index(drop=True).ID.loc[0] + '_blue.png'\n    r = imageio.imread(TRAIN + '/' + r_img)\n    y = imageio.imread(TRAIN + '/' + y_img)\n    b = imageio.imread(TRAIN + '/' + b_img)\n    rgb = np.dstack((r,y,b))\n    class_images.append(PILImage.create(rgb))\n\nipyplot.plot_images(images=class_images, labels=list(LABELS.values()), max_images=19, img_width=300)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:10:02.081231Z","iopub.execute_input":"2021-05-20T01:10:02.081672Z","iopub.status.idle":"2021-05-20T01:10:10.34117Z","shell.execute_reply.started":"2021-05-20T01:10:02.081627Z","shell.execute_reply":"2021-05-20T01:10:10.340369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nfor i in range(3):\n    img_visual = TRAIN + '/' + train_csv['ID'].iloc[i]\n    r = plt.imread(img_visual + '_red' + '.png')\n    g = plt.imread(img_visual + '_green' + '.png')    \n    b = plt.imread(img_visual + '_blue' + '.png')\n    y = plt.imread(img_visual + '_yellow' + '.png')\n    fig, ax = plt.subplots(1,4, figsize=(10,20))\n    img = np.dstack((r, g, b, y))\n    ax[0].set_title('r_g_b_y')\n    ax[0].imshow(img)\n    ax[0].axis('off')\n\n    img = np.dstack((r, g, b))\n    ax[1].set_title('r_g_b')\n    ax[1].imshow(img)\n    ax[1].axis('off')\n\n    img = np.dstack((r, y, b))\n    ax[2].set_title('r_y_b')\n    ax[2].imshow(img)\n    ax[2].axis('off')\n\n    img = np.dstack((b, y, g))\n    ax[3].set_title(\"b_y_g\")\n    ax[3].imshow(img)\n    ax[3].axis('off')\n\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:10:10.342551Z","iopub.execute_input":"2021-05-20T01:10:10.343131Z","iopub.status.idle":"2021-05-20T01:10:23.326202Z","shell.execute_reply.started":"2021-05-20T01:10:10.343094Z","shell.execute_reply":"2021-05-20T01:10:23.325158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Input: list of image filters as png\n# Output: list of image filters as np.arrays\ndef image_to_arrays(path):\n    \n    image_arrays = list()\n    for image in path:\n        array = np.asarray(Image.open(image))\n        image_arrays.append(array)\n        \n    return image_arrays","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:10:23.327699Z","iopub.execute_input":"2021-05-20T01:10:23.328302Z","iopub.status.idle":"2021-05-20T01:10:23.334222Z","shell.execute_reply.started":"2021-05-20T01:10:23.328254Z","shell.execute_reply":"2021-05-20T01:10:23.33313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Introduce list of image filters\n# Returns a processed image ready for the CNN and an encoded label as tensor\ndef image_prep(paths, label):\n\n    img = image_to_arrays(paths)\n    size = np.shape(img[0])[0]\n    img = tf.image.convert_image_dtype(img, dtype=tf.float32)\n    img = tf.reshape(img, (1, size, size, 3))\n    img = tf.image.resize(img, IMG_SIZE)\n\n    label = tf.strings.split(label, sep='|')\n    label = tf.strings.to_number(label, out_type=tf.int32)\n    label = tf.reduce_sum(tf.one_hot(indices=label, depth=19), axis=0)\n    label = tf.reshape(label, (1, 19))\n    \n    return img, label","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:10:23.335765Z","iopub.execute_input":"2021-05-20T01:10:23.336267Z","iopub.status.idle":"2021-05-20T01:10:23.357947Z","shell.execute_reply.started":"2021-05-20T01:10:23.336221Z","shell.execute_reply":"2021-05-20T01:10:23.357141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ndef plot_hist(hist):\n    plt.plot(hist.history[\"accuracy\"])\n    plt.plot(hist.history[\"val_accuracy\"])\n    plt.title(\"model accuracy\")\n    plt.ylabel(\"accuracy\")\n    plt.xlabel(\"epoch\")\n    plt.legend([\"train\", \"validation\"], loc=\"upper left\")\n    plt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:10:23.359297Z","iopub.execute_input":"2021-05-20T01:10:23.359923Z","iopub.status.idle":"2021-05-20T01:10:23.372428Z","shell.execute_reply.started":"2021-05-20T01:10:23.359877Z","shell.execute_reply":"2021-05-20T01:10:23.371575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\ntitles = ['microtubules', 'nuclei', 'endoplasmic reticulum', 'protein of interest']\nfig, axs = plt.subplots(3, 4, figsize =(16,8))\nfor entry in range(3):\n    for channel in range(4):\n        img = plt.imread(paths[entry][channel])\n        axs[entry, channel].imshow(img)        \n        if entry == 0:\n            axs[0, channel].set_title(titles[channel])\n\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:10:23.373783Z","iopub.execute_input":"2021-05-20T01:10:23.374408Z","iopub.status.idle":"2021-05-20T01:10:29.442177Z","shell.execute_reply.started":"2021-05-20T01:10:23.374361Z","shell.execute_reply":"2021-05-20T01:10:29.441362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUC_MODEL = \"./nuclei-model.pth\"\nCELL_MODEL = \"./cell-model.pth\"\nsegmentator = cellsegmentator.CellSegmentator(\n    NUC_MODEL,\n    CELL_MODEL,\n    scale_factor=0.25,\n    padding=False,\n    multi_channel_model=True,\n)\n\nimage = paths[4]\narrays = image_to_arrays(image)\nnuclei = arrays[1]\ncell = arrays[:-1]\n\nnuc_segmentations = segmentator.pred_nuclei([nuclei])\n\ninter_step = [[i] for i in image[:-1]]\ncell_segmentations = segmentator.pred_cells(inter_step)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:10:29.443467Z","iopub.execute_input":"2021-05-20T01:10:29.443975Z","iopub.status.idle":"2021-05-20T01:12:05.263451Z","shell.execute_reply.started":"2021-05-20T01:10:29.44391Z","shell.execute_reply":"2021-05-20T01:12:05.262338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n# Nuclei mask\nnuclei_mask = label_nuclei(nuc_segmentations[0])\n# Cell masks\ncell_nuclei_mask, cell_mask = label_cell(nuc_segmentations[0], cell_segmentations[0])\n# Plotting\n\nr = plt.imread(image[0])\nb = plt.imread(image[1])\ny = plt.imread(image[2])\n\nf, ax = plt.subplots(1, 2, figsize=(16,16))\nax[0].imshow(np.dstack((r,b,y)))\nax[0].set_title('image', size=20)\nax[1].imshow(cell_mask)\nax[1].set_title('Cell Mask', size=20)\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T01:12:05.27166Z","iopub.execute_input":"2021-05-20T01:12:05.272099Z","iopub.status.idle":"2021-05-20T01:12:15.93088Z","shell.execute_reply.started":"2021-05-20T01:12:05.272069Z","shell.execute_reply":"2021-05-20T01:12:15.929802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}