{"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":"from pathlib import Path\nimport cv2\nimport matplotlib.pyplot as plt\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\nimport tensorflow as tf\nfrom statistics import mean\nfrom matplotlib.colors import ListedColormap","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-20T21:42:31.043332Z","iopub.execute_input":"2021-12-20T21:42:31.043695Z","iopub.status.idle":"2021-12-20T21:42:37.062199Z","shell.execute_reply.started":"2021-12-20T21:42:31.043597Z","shell.execute_reply":"2021-12-20T21:42:37.061285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# read data first. The csv is the core.\ntrain_data = pd.read_csv('../input/sartorius-cell-instance-segmentation/train.csv')\n# submision sample\nsample_submission=pd.read_csv('../input/sartorius-cell-instance-segmentation/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:42:37.063721Z","iopub.execute_input":"2021-12-20T21:42:37.064012Z","iopub.status.idle":"2021-12-20T21:42:37.694745Z","shell.execute_reply.started":"2021-12-20T21:42:37.063979Z","shell.execute_reply":"2021-12-20T21:42:37.694027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# https://www.kaggle.com/c/sartorius-cell-instance-segmentation/discussion/291627\ndef rle_decode(mask_rle, shape=(520, 704, 1)):\n    s = mask_rle.split()\n    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]\n    starts -= 1\n    ends = starts + lengths\n    img = np.zeros(shape[0]*shape[1], dtype=np.uint8)\n    for lo, hi in zip(starts, ends):\n        img[lo:hi] = 1\n    return img.reshape(shape)  # Needed to align to RLE direction\n\ndef rle_encode(img):\n    pixels = img.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:42:37.695752Z","iopub.execute_input":"2021-12-20T21:42:37.69633Z","iopub.status.idle":"2021-12-20T21:42:37.706338Z","shell.execute_reply.started":"2021-12-20T21:42:37.696295Z","shell.execute_reply":"2021-12-20T21:42:37.705278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# each mask annotation has one area\nmask = train_data[train_data[\"id\"] == \"0030fd0e6378\"][\"annotation\"].tolist()[0]\nimg = rle_decode(mask)\nplt.imshow(img, cmap=\"gray\");","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:42:37.708388Z","iopub.execute_input":"2021-12-20T21:42:37.708642Z","iopub.status.idle":"2021-12-20T21:42:38.194469Z","shell.execute_reply.started":"2021-12-20T21:42:37.70861Z","shell.execute_reply":"2021-12-20T21:42:38.193579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_masks(image_id, colors=False):\n    labels = train_data[train_data[\"id\"] == image_id][\"annotation\"].tolist()\n\n    if colors:\n        mask = np.zeros((520, 704, 3))\n        for label in labels:\n            mask += rle_decode(label, shape=(520, 704, 3), color=np.random.rand(3))\n    else:\n        mask = np.zeros((520, 704, 1))\n        for label in labels:\n            mask += rle_decode(label, shape=(520, 704, 1))\n    mask = mask.clip(0, 1)\n\n    image = cv2.imread(f\"../input/sartorius-cell-instance-segmentation/train/{image_id}.png\")\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n    plt.figure(figsize=(18,6))\n    plt.subplot(1, 3, 1)\n    plt.imshow(image)\n    plt.title('Input image')\n    plt.axis(\"off\")\n    \n    plt.subplot(1, 3, 2)\n    plt.imshow(image)\n    plt.imshow(mask, alpha=0.1)\n    plt.title('Input image with mask')\n    plt.axis(\"off\")\n    \n    plt.subplot(1, 3, 3)\n    plt.imshow(mask)\n    plt.title('Only mask')\n    plt.axis(\"off\")\n    \n    plt.show();","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:42:38.195999Z","iopub.execute_input":"2021-12-20T21:42:38.196314Z","iopub.status.idle":"2021-12-20T21:42:38.208364Z","shell.execute_reply.started":"2021-12-20T21:42:38.196272Z","shell.execute_reply":"2021-12-20T21:42:38.207416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_ids = ['0030fd0e6378','0140b3c8f445','01ae5a43a2ab']\n\nfor sample_id in sample_ids:\n    celltype=train_data[train_data['id']==sample_id]['cell_type'].tolist()[0]\n    file_path = '../input/sartorius-cell-instance-segmentation/train/' + sample_id + '.png'\n    image_df = cv2.imread(file_path)\n    print('ID:', sample_id, ', CellType:',celltype)\n    plot_masks(sample_id, colors=False)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:42:38.210073Z","iopub.execute_input":"2021-12-20T21:42:38.210615Z","iopub.status.idle":"2021-12-20T21:42:39.998678Z","shell.execute_reply.started":"2021-12-20T21:42:38.210564Z","shell.execute_reply":"2021-12-20T21:42:39.997663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reference: https://www.kaggle.com/keegil/keras-u-net-starter-lb-0-277\nIMG_HEIGHT = 520\nIMG_WIDTH = 704\nIMG_CHANNELS = 1\nTRAIN_PATH = '../input/sartorius-cell-instance-segmentation/train/'\n\ntrain_ids = train_data['id'].unique().tolist()\ntest_ids = sample_submission['id'].unique().tolist()\n\n# Get and resize train images and masks\nX_train = np.zeros((train_data['id'].nunique(), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\nY_train = np.zeros((train_data['id'].nunique(), IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:42:40.000169Z","iopub.execute_input":"2021-12-20T21:42:40.000489Z","iopub.status.idle":"2021-12-20T21:42:40.035461Z","shell.execute_reply.started":"2021-12-20T21:42:40.000446Z","shell.execute_reply":"2021-12-20T21:42:40.03439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\nfor n, id_ in tqdm(enumerate(train_ids), total=len(train_ids)):\n    path = TRAIN_PATH + id_\n    img = cv2.imread(path + '.png')[:,:]\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY).astype(np.float32) -125\n    img = np.expand_dims(img, axis = 2)\n    X_train[n] = img\n    \n    labels = train_data[train_data[\"id\"]\n                        == id_][\"annotation\"].tolist()\n    mask = np.zeros((520, 704, 1))\n    for label in labels:\n        mask += rle_decode(label, shape=(520, 704, 1))\n    mask = mask.clip(0, 1)\n\n    Y_train[n] = mask\nprint(\"Done\")","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:42:40.03692Z","iopub.execute_input":"2021-12-20T21:42:40.037554Z","iopub.status.idle":"2021-12-20T21:43:32.37549Z","shell.execute_reply.started":"2021-12-20T21:42:40.037513Z","shell.execute_reply":"2021-12-20T21:43:32.374729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get and resize test images\ntest_images_id = []\nX_test = np.zeros((sample_submission['id'].nunique(), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\nfor n, id_ in tqdm(enumerate(test_ids), total=len(test_ids)):\n    path = TRAIN_PATH.replace('train', 'test') + id_\n    img = cv2.imread(path + '.png')[:,:]\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY).astype(np.float32) -125\n    img = np.expand_dims(img, axis = 2)\n    X_test[n] = img\n    test_images_id.append(id_)\nprint(\"Done\")","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:49:07.914625Z","iopub.execute_input":"2021-12-20T22:49:07.915217Z","iopub.status.idle":"2021-12-20T22:49:07.963859Z","shell.execute_reply.started":"2021-12-20T22:49:07.915164Z","shell.execute_reply":"2021-12-20T22:49:07.962863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape,Y_train.shape,X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:43:32.44741Z","iopub.execute_input":"2021-12-20T21:43:32.448011Z","iopub.status.idle":"2021-12-20T21:43:32.453286Z","shell.execute_reply.started":"2021-12-20T21:43:32.447975Z","shell.execute_reply":"2021-12-20T21:43:32.452409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_id_num = 40\nplt.imshow(X_train[sample_id_num][:,:,0], cmap = 'gray')\nplt.show()\nplt.imshow(Y_train[sample_id_num][:,:,0])\nplt.show()\n\nprint('Input image:','Min:', X_train[sample_id_num][:,:,0].min(), '; Max:', X_train[sample_id_num][:,:,0].max(), '; Mean:', X_train[sample_id_num][:,:,0].mean())\nprint('Mask:','Min:', Y_train[sample_id_num][:,:,0].min(), '; Max:', Y_train[sample_id_num][:,:,0].max(), '; Mean:', Y_train[sample_id_num][:,:,0].mean())","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:43:32.454958Z","iopub.execute_input":"2021-12-20T21:43:32.455276Z","iopub.status.idle":"2021-12-20T21:43:32.857142Z","shell.execute_reply.started":"2021-12-20T21:43:32.455231Z","shell.execute_reply":"2021-12-20T21:43:32.856214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dice_coefficient\ndef dice_coefficient(y_true, y_pred):\n    numerator = 2 * tf.reduce_sum(y_true * y_pred)\n    denominator = tf.reduce_sum(y_true + y_pred)\n    return numerator / (denominator + tf.keras.backend.epsilon())","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:43:32.858641Z","iopub.execute_input":"2021-12-20T21:43:32.858949Z","iopub.status.idle":"2021-12-20T21:43:32.863989Z","shell.execute_reply.started":"2021-12-20T21:43:32.858914Z","shell.execute_reply":"2021-12-20T21:43:32.863047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"from torch import nn\nNN = nn.Sequential(\n    nn.Conv2d(1, 20, kernel_size=5, padding=\"same\"),\n    nn.BatchNorm2d(20),\n    nn.ReLU(),\n    nn.Conv2d(20, 10, kernel_size=1),\n\n    nn.Conv2d(10, 10, kernel_size=5, padding=\"same\"),\n    nn.BatchNorm2d(10),\n    nn.ReLU(),\n    nn.Conv2d(10, 1, kernel_size=1),\n)\nfrom torchsummary import summary\nsummary(model_to_transfer, input_size=[IMG_WIDTH,IMG_HEIGHT,IMG_CHANNELS])\n\"\"\"\n#input_var.shape[-3:]","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:43:32.865186Z","iopub.execute_input":"2021-12-20T21:43:32.865398Z","iopub.status.idle":"2021-12-20T21:43:32.879564Z","shell.execute_reply.started":"2021-12-20T21:43:32.865371Z","shell.execute_reply":"2021-12-20T21:43:32.878912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nfrom keras.models import Model, load_model\nfrom keras import layers\n\nmodel = keras.Sequential([\n    # Convolutional layer 1\n    keras.layers.Conv2D(filters=20, kernel_size=5, strides=1,\n                  padding='same',input_shape=[IMG_WIDTH,IMG_HEIGHT,IMG_CHANNELS],\n                  activation='relu'),\n    keras.layers.BatchNormalization(),\n    \n    # Convolutional layer 2\n    keras.layers.Conv2D(filters=10, kernel_size=1),\n\n    # Convolutional layer 3\n    keras.layers.Conv2D(filters=10, kernel_size=5, strides=1,\n                  padding='same', activation='relu'),\n    keras.layers.BatchNormalization(),\n\n    # Convolutional layer 4\n    keras.layers.Conv2D(filters=1, kernel_size=1),\n])","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:43:32.880402Z","iopub.execute_input":"2021-12-20T21:43:32.880618Z","iopub.status.idle":"2021-12-20T21:43:33.759005Z","shell.execute_reply.started":"2021-12-20T21:43:32.880592Z","shell.execute_reply":"2021-12-20T21:43:33.757959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.losses import BinaryCrossentropy\nloss = BinaryCrossentropy(from_logits=True)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:43:33.760635Z","iopub.execute_input":"2021-12-20T21:43:33.760985Z","iopub.status.idle":"2021-12-20T21:43:34.157284Z","shell.execute_reply.started":"2021-12-20T21:43:33.760953Z","shell.execute_reply":"2021-12-20T21:43:34.156607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss=loss)\n#model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:43:34.158212Z","iopub.execute_input":"2021-12-20T21:43:34.158543Z","iopub.status.idle":"2021-12-20T21:43:34.176758Z","shell.execute_reply.started":"2021-12-20T21:43:34.158515Z","shell.execute_reply":"2021-12-20T21:43:34.176144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit model\nn_epochs = 50\nbatch_size = 32\nfrom keras.callbacks import EarlyStopping\nearlystopper = EarlyStopping(patience=20, verbose=1)\n\nresults = model.fit(X_train, Y_train, validation_split=0.15, batch_size=batch_size, epochs=n_epochs, \n                    callbacks=[earlystopper])\nprint(\"Done!\")","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:43:34.177805Z","iopub.execute_input":"2021-12-20T21:43:34.178445Z","iopub.status.idle":"2021-12-20T21:55:06.797186Z","shell.execute_reply.started":"2021-12-20T21:43:34.178406Z","shell.execute_reply":"2021-12-20T21:55:06.795892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(14,4))\nplt.plot(results.history['loss'])\nplt.plot(results.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('Loss')\nplt.xlabel('epoch')\nplt.legend(['loss', 'val_loss'], loc='upper right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:55:06.799038Z","iopub.execute_input":"2021-12-20T21:55:06.800105Z","iopub.status.idle":"2021-12-20T21:55:07.007774Z","shell.execute_reply.started":"2021-12-20T21:55:06.800066Z","shell.execute_reply":"2021-12-20T21:55:07.006714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(Y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:55:07.009374Z","iopub.execute_input":"2021-12-20T21:55:07.009639Z","iopub.status.idle":"2021-12-20T21:55:07.014879Z","shell.execute_reply.started":"2021-12-20T21:55:07.009607Z","shell.execute_reply":"2021-12-20T21:55:07.014203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_train = model.predict(X_train, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:55:07.016228Z","iopub.execute_input":"2021-12-20T21:55:07.016463Z","iopub.status.idle":"2021-12-20T21:55:48.203052Z","shell.execute_reply.started":"2021-12-20T21:55:07.016436Z","shell.execute_reply":"2021-12-20T21:55:48.201914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:55:48.204984Z","iopub.execute_input":"2021-12-20T21:55:48.205332Z","iopub.status.idle":"2021-12-20T21:55:48.213089Z","shell.execute_reply.started":"2021-12-20T21:55:48.205284Z","shell.execute_reply":"2021-12-20T21:55:48.212158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Threshold predictions\npreds_train_t = (preds_train > 0.5).astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:55:48.214881Z","iopub.execute_input":"2021-12-20T21:55:48.215408Z","iopub.status.idle":"2021-12-20T21:55:48.477632Z","shell.execute_reply.started":"2021-12-20T21:55:48.215359Z","shell.execute_reply":"2021-12-20T21:55:48.476931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(preds_train_t[0], cmap=\"gray\");","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:55:48.478739Z","iopub.execute_input":"2021-12-20T21:55:48.479012Z","iopub.status.idle":"2021-12-20T21:55:48.71724Z","shell.execute_reply.started":"2021-12-20T21:55:48.478979Z","shell.execute_reply":"2021-12-20T21:55:48.716393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# unoptimized and slow; any way to speed up?\n\ndef get_threshold(Y, pred):\n    scores = list(pred.ravel())\n    mask = list(Y.ravel())\n    \n    idxs=np.argsort(scores)[::-1]\n    mask_sorted=np.array(mask)[idxs]\n    sum_mask_one=np.cumsum(mask_sorted)\n    IoU=sum_mask_one/(np.arange(1,len(mask_sorted)+1)+np.sum(mask_sorted)-sum_mask_one)\n    best_IoU_idx=IoU.argmax()\n    best_threshold=scores[idxs[best_IoU_idx]]\n    best_IoU=IoU[best_IoU_idx]\n\n    return best_threshold, best_IoU","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:55:48.718726Z","iopub.execute_input":"2021-12-20T21:55:48.71941Z","iopub.status.idle":"2021-12-20T21:55:48.726663Z","shell.execute_reply.started":"2021-12-20T21:55:48.719357Z","shell.execute_reply":"2021-12-20T21:55:48.72561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(preds_train.shape)\nprint(Y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:55:48.728195Z","iopub.execute_input":"2021-12-20T21:55:48.728807Z","iopub.status.idle":"2021-12-20T21:55:48.742055Z","shell.execute_reply.started":"2021-12-20T21:55:48.72875Z","shell.execute_reply":"2021-12-20T21:55:48.741159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"get_threshold(Y_train[0], preds_train[0])","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:55:48.743274Z","iopub.execute_input":"2021-12-20T21:55:48.743722Z","iopub.status.idle":"2021-12-20T21:55:49.059678Z","shell.execute_reply.started":"2021-12-20T21:55:48.743685Z","shell.execute_reply":"2021-12-20T21:55:49.058927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_thresholds = []         # one for each image\nimg_IoUs = []\nfor Y, P in tqdm(zip(Y_train, preds_train), total=Y_train.shape[0]):\n\n    best_img_threshold, best_img_IoU = get_threshold(Y, P)\n    img_thresholds.append(best_img_threshold)\n    img_IoUs.append(best_img_IoU)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:55:49.060977Z","iopub.execute_input":"2021-12-20T21:55:49.061185Z","iopub.status.idle":"2021-12-20T21:58:34.340082Z","shell.execute_reply.started":"2021-12-20T21:55:49.061159Z","shell.execute_reply":"2021-12-20T21:58:34.339162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_threshold = np.mean(img_thresholds)\nbest_threshold_spread = np.std(img_thresholds)\navg_IoU = mean(img_IoUs)\n\nprint(f\"Best threshold: {best_threshold:.3g} (+-{best_threshold_spread:.3g}), Avg. Train IoU: {avg_IoU:.3f}\")","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:58:34.34405Z","iopub.execute_input":"2021-12-20T21:58:34.344294Z","iopub.status.idle":"2021-12-20T21:58:34.35374Z","shell.execute_reply.started":"2021-12-20T21:58:34.344266Z","shell.execute_reply":"2021-12-20T21:58:34.352805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dice_coefficient(Y_train, preds_train)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:58:34.35518Z","iopub.execute_input":"2021-12-20T21:58:34.355414Z","iopub.status.idle":"2021-12-20T21:58:35.947367Z","shell.execute_reply.started":"2021-12-20T21:58:34.355382Z","shell.execute_reply":"2021-12-20T21:58:35.946757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_Y = (preds_train >= best_threshold)\n    \ndef plot(img_Y, img_pred):\n    output = np.zeros_like(img_Y)\n    output = np.where((img_Y == 0) & (img_pred == 1), 1, output)\n    output = np.where((img_Y == 1) & (img_pred == 0), 2, output)\n    output = np.where((img_Y == 1) & (img_pred == 1), 3, output)\n\n    plt.figure(figsize=(10,10))\n    plt.imshow(output, cmap=ListedColormap(['black', 'gray', 'orange', 'green']))\n    plt.xticks([])\n    plt.yticks([]);","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:58:35.948818Z","iopub.execute_input":"2021-12-20T21:58:35.949268Z","iopub.status.idle":"2021-12-20T21:58:36.093048Z","shell.execute_reply.started":"2021-12-20T21:58:35.949233Z","shell.execute_reply":"2021-12-20T21:58:36.092063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = 5\nfor i in range(N):\n    img_Y = Y_train[i]\n    img_pred = pred_Y[i]\n    \n    plot(img_Y, img_pred)\n    plt.show()\n\n# green: correct prediction\n# gray: false positive (too much)\n# orange: false negative (missed)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:58:36.094292Z","iopub.execute_input":"2021-12-20T21:58:36.094561Z","iopub.status.idle":"2021-12-20T21:58:37.026764Z","shell.execute_reply.started":"2021-12-20T21:58:36.094531Z","shell.execute_reply":"2021-12-20T21:58:37.025849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_test = model.predict(X_test, verbose=1)\npreds_test_t = (preds_test >= best_threshold).astype(np.uint8)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T21:58:37.028023Z","iopub.execute_input":"2021-12-20T21:58:37.028235Z","iopub.status.idle":"2021-12-20T21:58:37.2645Z","shell.execute_reply.started":"2021-12-20T21:58:37.028208Z","shell.execute_reply":"2021-12-20T21:58:37.263805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_test_t[1].shape","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:01:24.429423Z","iopub.execute_input":"2021-12-20T22:01:24.430313Z","iopub.status.idle":"2021-12-20T22:01:24.435966Z","shell.execute_reply.started":"2021-12-20T22:01:24.430255Z","shell.execute_reply":"2021-12-20T22:01:24.43527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test samples\nfrom random import randint\nix = randint(0, len(preds_test_t)-1)\nprint(ix)\nplt.imshow(X_test[ix])\nplt.show()\nplt.imshow(np.squeeze(preds_test_t[ix]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:01:18.926157Z","iopub.execute_input":"2021-12-20T22:01:18.927037Z","iopub.status.idle":"2021-12-20T22:01:19.410553Z","shell.execute_reply.started":"2021-12-20T22:01:18.926997Z","shell.execute_reply":"2021-12-20T22:01:19.409966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(preds_test_t[0].shape)\nprint(preds_test_t[1].shape)\nprint(preds_test_t[2].shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:01:42.705738Z","iopub.execute_input":"2021-12-20T22:01:42.706299Z","iopub.status.idle":"2021-12-20T22:01:42.712551Z","shell.execute_reply.started":"2021-12-20T22:01:42.706242Z","shell.execute_reply":"2021-12-20T22:01:42.711633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def check_overlap(msk):\n    msk = msk.astype(np.bool).astype(np.uint8)\n    return np.any(np.sum(msk, axis=-1)>1)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:01:46.288647Z","iopub.execute_input":"2021-12-20T22:01:46.288959Z","iopub.status.idle":"2021-12-20T22:01:46.293947Z","shell.execute_reply.started":"2021-12-20T22:01:46.288928Z","shell.execute_reply":"2021-12-20T22:01:46.292984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for test_mask in preds_test_t:\n    print(check_overlap(test_mask))","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:01:46.499238Z","iopub.execute_input":"2021-12-20T22:01:46.499559Z","iopub.status.idle":"2021-12-20T22:01:46.509269Z","shell.execute_reply.started":"2021-12-20T22:01:46.499526Z","shell.execute_reply":"2021-12-20T22:01:46.508377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split the mask into each cluster nucleus for the submision\n# seen on https://www.kaggle.com/c/sartorius-cell-instance-segmentation/discussion/288376\ndef post_process(mask, min_size=80):\n    num_component, component = cv2.connectedComponents(mask.astype(np.uint8))\n    predictions = []\n    for c in range(1, num_component):\n        p = (component == c)\n        if p.sum() > min_size:\n            a_prediction = np.zeros((520, 704), np.float32)\n            a_prediction[p] = 1\n            predictions.append(a_prediction)\n    return predictions","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:33:45.009395Z","iopub.execute_input":"2021-12-20T22:33:45.009909Z","iopub.status.idle":"2021-12-20T22:33:45.017683Z","shell.execute_reply.started":"2021-12-20T22:33:45.009857Z","shell.execute_reply":"2021-12-20T22:33:45.016738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test the nucleus thing\nplt.imshow(Y_train[4], cmap=\"gray\");","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:01:58.281387Z","iopub.execute_input":"2021-12-20T22:01:58.28169Z","iopub.status.idle":"2021-12-20T22:01:58.577667Z","shell.execute_reply.started":"2021-12-20T22:01:58.281644Z","shell.execute_reply":"2021-12-20T22:01:58.576563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_component, component = cv2.connectedComponents(Y_train[4].astype(np.uint8))\nnum_component","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:02:52.939991Z","iopub.execute_input":"2021-12-20T22:02:52.940292Z","iopub.status.idle":"2021-12-20T22:02:52.95201Z","shell.execute_reply.started":"2021-12-20T22:02:52.940263Z","shell.execute_reply":"2021-12-20T22:02:52.950936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(component, cmap=\"gray\");","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:05:33.692223Z","iopub.execute_input":"2021-12-20T22:05:33.692543Z","iopub.status.idle":"2021-12-20T22:05:33.927749Z","shell.execute_reply.started":"2021-12-20T22:05:33.69251Z","shell.execute_reply":"2021-12-20T22:05:33.92696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"compenent_1 = (component == 1)\nplt.imshow(compenent_1, cmap=\"gray\");","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:07:31.906418Z","iopub.execute_input":"2021-12-20T22:07:31.906717Z","iopub.status.idle":"2021-12-20T22:07:32.133563Z","shell.execute_reply.started":"2021-12-20T22:07:31.906686Z","shell.execute_reply":"2021-12-20T22:07:32.132706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final = post_process(Y_train[4])\nfinal[0].shape","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:20:36.900107Z","iopub.execute_input":"2021-12-20T22:20:36.90061Z","iopub.status.idle":"2021-12-20T22:20:36.90742Z","shell.execute_reply.started":"2021-12-20T22:20:36.900557Z","shell.execute_reply":"2021-12-20T22:20:36.906245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(final[0], cmap=\"gray\");","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:21:04.113765Z","iopub.execute_input":"2021-12-20T22:21:04.114201Z","iopub.status.idle":"2021-12-20T22:21:04.384821Z","shell.execute_reply.started":"2021-12-20T22:21:04.11417Z","shell.execute_reply":"2021-12-20T22:21:04.38376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# old submision\npredicted2 = [rle_encode(test_mask2) for test_mask2 in preds_test_t]\nlen(predicted2[0])","metadata":{"execution":{"iopub.status.busy":"2021-12-16T22:58:02.277949Z","iopub.execute_input":"2021-12-16T22:58:02.278589Z","iopub.status.idle":"2021-12-16T22:58:02.303601Z","shell.execute_reply.started":"2021-12-16T22:58:02.278543Z","shell.execute_reply":"2021-12-16T22:58:02.302742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def remove_isolated_points_from_rle(strin):\n    t2 = strin.split(\" \")\n    a = []\n    for i in range(0, len(t2), 2):\n        if t2[i+1]!=\"1\":\n            a.append(t2[i])\n            a.append(t2[i+1])\n    return ' '.join(a)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:36:29.652075Z","iopub.execute_input":"2021-12-20T22:36:29.652372Z","iopub.status.idle":"2021-12-20T22:36:29.658742Z","shell.execute_reply.started":"2021-12-20T22:36:29.652342Z","shell.execute_reply":"2021-12-20T22:36:29.65787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_filt = [remove_isolated_points_from_rle(s) for s in predicted2]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# new version with the mask nucleus split\npredicted_nucleus = []\ntest_nucleus_image_id = []\n\nfor index, s in enumerate(preds_test_t):\n    nucleus = post_process(s)\n    for nucl in nucleus:\n        predicted_nucleus.append(nucl)\n        test_nucleus_image_id.append(test_images_id[index])","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:51:18.001209Z","iopub.execute_input":"2021-12-20T22:51:18.001853Z","iopub.status.idle":"2021-12-20T22:51:18.632557Z","shell.execute_reply.started":"2021-12-20T22:51:18.00177Z","shell.execute_reply":"2021-12-20T22:51:18.631729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(predicted_nucleus[0], cmap=\"gray\");","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:35:17.241601Z","iopub.execute_input":"2021-12-20T22:35:17.241905Z","iopub.status.idle":"2021-12-20T22:35:17.514751Z","shell.execute_reply.started":"2021-12-20T22:35:17.241871Z","shell.execute_reply":"2021-12-20T22:35:17.513675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted2 = [rle_encode(test_mask2) for test_mask2 in predicted_nucleus]\nprint(predicted2[0])\npredicted_filt = [remove_isolated_points_from_rle(s) for s in predicted2]\nprint(predicted_filt[0])","metadata":{"execution":{"iopub.status.busy":"2021-12-20T22:37:09.347265Z","iopub.execute_input":"2021-12-20T22:37:09.347536Z","iopub.status.idle":"2021-12-20T22:37:09.50238Z","shell.execute_reply.started":"2021-12-20T22:37:09.347508Z","shell.execute_reply":"2021-12-20T22:37:09.501331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = sample_submission.copy()\n#submit['predicted'] = predicted2\nsubmit = pd.DataFrame({'id':test_nucleus_image_id, 'predicted':predicted_filt})","metadata":{"execution":{"iopub.status.busy":"2021-12-20T23:00:42.889047Z","iopub.execute_input":"2021-12-20T23:00:42.889387Z","iopub.status.idle":"2021-12-20T23:00:42.896774Z","shell.execute_reply.started":"2021-12-20T23:00:42.889351Z","shell.execute_reply":"2021-12-20T23:00:42.895832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(submit.shape)\nsubmit.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-20T23:01:08.87993Z","iopub.execute_input":"2021-12-20T23:01:08.880432Z","iopub.status.idle":"2021-12-20T23:01:08.900854Z","shell.execute_reply.started":"2021-12-20T23:01:08.88037Z","shell.execute_reply":"2021-12-20T23:01:08.899901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-12-20T23:01:20.241102Z","iopub.execute_input":"2021-12-20T23:01:20.241376Z","iopub.status.idle":"2021-12-20T23:01:20.256396Z","shell.execute_reply.started":"2021-12-20T23:01:20.241348Z","shell.execute_reply":"2021-12-20T23:01:20.255607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}