{"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":"markdown","source":"# Importing Required Libs","metadata":{}},{"cell_type":"code","source":"import os\nimport sys\nimport random\nimport warnings\n\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n\nfrom tqdm import tqdm\nfrom itertools import chain\nfrom skimage.io import imread, imshow, imread_collection, concatenate_images\nfrom skimage.transform import resize\nfrom skimage.morphology import label\n\nfrom keras.models import Model, load_model\nfrom keras.layers import Input\nfrom keras.layers.core import Dropout, Lambda\nfrom keras.layers.convolutional import Conv2D, Conv2DTranspose\nfrom keras.layers.pooling import MaxPooling2D\nfrom keras.layers.merge import concatenate\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom keras import backend as K\n\nimport tensorflow as tf\nwarnings.filterwarnings('ignore', category=UserWarning, module='skimage')\nseed = 42","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:08:21.946776Z","iopub.execute_input":"2021-12-16T09:08:21.947321Z","iopub.status.idle":"2021-12-16T09:08:29.263876Z","shell.execute_reply.started":"2021-12-16T09:08:21.947223Z","shell.execute_reply":"2021-12-16T09:08:29.262677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.image as immg\nimport gc\nimport numpy as np\nimport random\nfrom PIL import Image\nimport cv2\nimport imageio","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:08:29.265694Z","iopub.execute_input":"2021-12-16T09:08:29.265995Z","iopub.status.idle":"2021-12-16T09:08:29.615366Z","shell.execute_reply.started":"2021-12-16T09:08:29.265953Z","shell.execute_reply":"2021-12-16T09:08:29.614382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1) Train Data Read","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/sartorius-cell-instance-segmentation/train.csv')\ntrain\n#train csv imageid-celltype-pixeldetails in which cell is present","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:08:35.452517Z","iopub.execute_input":"2021-12-16T09:08:35.452825Z","iopub.status.idle":"2021-12-16T09:08:36.016700Z","shell.execute_reply.started":"2021-12-16T09:08:35.452779Z","shell.execute_reply":"2021-12-16T09:08:36.015545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['file_path'] =train['id'].apply(lambda x: '../input/sartorius-cell-instance-segmentation/train/{}.png'.format(x))\ntrain.head()\n#in train csv , we have mapped the image id with image location and stored it in the df col file path","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:08:39.420992Z","iopub.execute_input":"2021-12-16T09:08:39.421509Z","iopub.status.idle":"2021-12-16T09:08:39.489310Z","shell.execute_reply.started":"2021-12-16T09:08:39.421474Z","shell.execute_reply":"2021-12-16T09:08:39.488384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#unique Images\nuid = train.id.unique()\nprint(\"no.of unique images\")\nlen(uid)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:08:42.485584Z","iopub.execute_input":"2021-12-16T09:08:42.485971Z","iopub.status.idle":"2021-12-16T09:08:42.504221Z","shell.execute_reply.started":"2021-12-16T09:08:42.485941Z","shell.execute_reply":"2021-12-16T09:08:42.502885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Functions for generating Masks using pixel information provided in train.csv","metadata":{}},{"cell_type":"code","source":"#Masking functions\ndef get_image(path):\n    image = np.array(Image.open(path))\n    #image=cv2.imread(Image.open(path))\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    return image\n\ndef get_annot(img_id):\n    #train[train[\"id\"] == id_][\"annotation\"].tolist()\n    return train[train.id == img_id][\"annotation\"].tolist()\n\ndef get_mask(img_annotations,colours=True): \n    if colours:\n        mask = np.zeros((520, 704, 3))\n        for annot in img_annotations:\n            mask += rle_decode(annot, shape=(520, 704, 3), color=np.random.rand(3))\n    else:\n        mask = np.zeros((520, 704, 1))\n        for annot in img_annotations:\n            mask += rle_decode(annot, shape=(520, 704, 1),color = 1)\n    mask = mask.clip(0, 1)\n    return mask\n\n","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:08:45.205409Z","iopub.execute_input":"2021-12-16T09:08:45.206008Z","iopub.status.idle":"2021-12-16T09:08:45.216938Z","shell.execute_reply.started":"2021-12-16T09:08:45.205973Z","shell.execute_reply":"2021-12-16T09:08:45.215548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_decode(mask_rle, shape, color=1):\n\n    s = mask_rle.split()\n    \n    starts = list(map(lambda x: int(x) - 1, s[0::2]))\n    lengths = list(map(int, s[1::2]))\n    ends = [x + y for x, y in zip(starts, lengths)]\n    \n    img = np.zeros((shape[0] * shape[1], shape[2]), dtype=np.float32)\n            \n    for start, end in zip(starts, ends):\n        img[start : end] = color\n    \n    return img.reshape(shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:08:46.747240Z","iopub.execute_input":"2021-12-16T09:08:46.747606Z","iopub.status.idle":"2021-12-16T09:08:46.756449Z","shell.execute_reply.started":"2021-12-16T09:08:46.747544Z","shell.execute_reply":"2021-12-16T09:08:46.754884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Understanding masking functions and visualizations","metadata":{}},{"cell_type":"code","source":"#OriginalImage\nim=np.array(Image.open('../input/sartorius-cell-instance-segmentation/train/0030fd0e6378.png'))\nplt.title(\"Original Image\")\nplt.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:08:50.166947Z","iopub.execute_input":"2021-12-16T09:08:50.167607Z","iopub.status.idle":"2021-12-16T09:08:50.561332Z","shell.execute_reply.started":"2021-12-16T09:08:50.167549Z","shell.execute_reply":"2021-12-16T09:08:50.560262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Image after BGR2RGB conversion\nim2=get_image('../input/sartorius-cell-instance-segmentation/train/0030fd0e6378.png')\nplt.title(\"Image after BGR2RGB conversion\")\nplt.imshow(im2)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:08:57.866878Z","iopub.execute_input":"2021-12-16T09:08:57.867178Z","iopub.status.idle":"2021-12-16T09:08:58.219750Z","shell.execute_reply.started":"2021-12-16T09:08:57.867147Z","shell.execute_reply":"2021-12-16T09:08:58.218756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(uid[0])","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:09:01.591801Z","iopub.execute_input":"2021-12-16T09:09:01.592155Z","iopub.status.idle":"2021-12-16T09:09:01.599647Z","shell.execute_reply.started":"2021-12-16T09:09:01.592098Z","shell.execute_reply":"2021-12-16T09:09:01.598563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann=get_annot(uid[0])# will get the pixel(annotation) values where the cell is precent of all cells present in a image w.r.t imgID\nprint(len(ann))\nim_mask=get_mask(ann,colours=False)#will generate a mask with values from annotations\nplt.imshow(im_mask)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:09:02.654714Z","iopub.execute_input":"2021-12-16T09:09:02.655397Z","iopub.status.idle":"2021-12-16T09:09:03.179843Z","shell.execute_reply.started":"2021-12-16T09:09:02.655320Z","shell.execute_reply":"2021-12-16T09:09:03.178729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Visualizing images and masks\n\nplt.figure(figsize=(20, 20))\n\nplt.subplot(1, 3, 1)\nplt.imshow(im)\nplt.title('Original image')\n\nplt.subplot( 1, 3, 2)\nplt.imshow(im_mask)\nplt.title('Mask')\n\nplt.subplot( 1, 3, 3)\nplt.imshow(im)\nplt.imshow(im_mask,alpha=0.2)\nplt.title('Both')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:09:05.508027Z","iopub.execute_input":"2021-12-16T09:09:05.508305Z","iopub.status.idle":"2021-12-16T09:09:06.413787Z","shell.execute_reply.started":"2021-12-16T09:09:05.508275Z","shell.execute_reply":"2021-12-16T09:09:06.412876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 2. Preparing the data","metadata":{}},{"cell_type":"code","source":"# Set some parameters\nIMG_HEIGHT = 256\nIMG_WIDTH = 256\nIMG_CHANNELS = 1\nX_TRAIN_PATH = '../input/sartorius-cell-instance-segmentation/train/'\nX_TEST_PATH = '../input/sartorius-cell-instance-segmentation/test/'","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:09:15.136766Z","iopub.execute_input":"2021-12-16T09:09:15.137116Z","iopub.status.idle":"2021-12-16T09:09:15.142545Z","shell.execute_reply.started":"2021-12-16T09:09:15.137069Z","shell.execute_reply":"2021-12-16T09:09:15.141326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get trainIDs\ntrain_ids = train['id'].unique().tolist()\nprint(len(train_ids))\nprint(train_ids[0])","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:09:17.703787Z","iopub.execute_input":"2021-12-16T09:09:17.704490Z","iopub.status.idle":"2021-12-16T09:09:17.718630Z","shell.execute_reply.started":"2021-12-16T09:09:17.704455Z","shell.execute_reply":"2021-12-16T09:09:17.717384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get and resize train images and masks\nX_train = np.zeros((train['id'].nunique(), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\nY_train = np.zeros((train['id'].nunique(), IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:09:19.228452Z","iopub.execute_input":"2021-12-16T09:09:19.228981Z","iopub.status.idle":"2021-12-16T09:09:19.249002Z","shell.execute_reply.started":"2021-12-16T09:09:19.228940Z","shell.execute_reply":"2021-12-16T09:09:19.248051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Getting and resizing train images')\nsys.stdout.flush()\nfor n, id_ in tqdm(enumerate(train_ids), total=len(train_ids)):\n    path = X_TRAIN_PATH + id_\n    img = imread(path + '.png')[:,:]\n    img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n    img = np.expand_dims(img, axis = 2)\n    X_train[n] = img\n    mask = np.zeros((520, 704, 1))\n    annots = get_annot(id_)\n    mask=get_mask(annots,colours=False)\n    mask = mask[:,:,0]\n    mask = np.expand_dims(resize(mask, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True), axis=-1)\n    \n    Y_train[n] = mask\n    \nprint(\"done\")","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:09:21.713433Z","iopub.execute_input":"2021-12-16T09:09:21.714063Z","iopub.status.idle":"2021-12-16T09:11:02.381823Z","shell.execute_reply.started":"2021-12-16T09:09:21.714018Z","shell.execute_reply":"2021-12-16T09:11:02.374830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(X_train))\nprint(X_train.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:11:28.844955Z","iopub.execute_input":"2021-12-16T09:11:28.845246Z","iopub.status.idle":"2021-12-16T09:11:28.851689Z","shell.execute_reply.started":"2021-12-16T09:11:28.845216Z","shell.execute_reply":"2021-12-16T09:11:28.850639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(Y_train))\nprint(Y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:11:30.145159Z","iopub.execute_input":"2021-12-16T09:11:30.145509Z","iopub.status.idle":"2021-12-16T09:11:30.152287Z","shell.execute_reply.started":"2021-12-16T09:11:30.145464Z","shell.execute_reply":"2021-12-16T09:11:30.150896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Getting Test Ids\ntest_ids = next(os.walk(X_TEST_PATH))[2]\nprint(test_ids)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:11:31.421188Z","iopub.execute_input":"2021-12-16T09:11:31.423794Z","iopub.status.idle":"2021-12-16T09:11:31.437360Z","shell.execute_reply.started":"2021-12-16T09:11:31.423759Z","shell.execute_reply":"2021-12-16T09:11:31.436244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get and resize test images\n\nX_test = np.zeros((len(test_ids), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\nprint('Getting and resizing test images ... ')\nsys.stdout.flush()\n\nfor n, id_ in tqdm(enumerate(test_ids), total=len(test_ids)):\n    print(n, id_)\n    path = '../input/sartorius-cell-instance-segmentation/test/'+id_\n    img = imread(path)[:,:]\n    img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)\n    img = np.expand_dims(img, axis = 2)\n    X_test[n] = img\n\nprint('Done!')","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:11:33.445541Z","iopub.execute_input":"2021-12-16T09:11:33.446299Z","iopub.status.idle":"2021-12-16T09:11:33.573195Z","shell.execute_reply.started":"2021-12-16T09:11:33.446260Z","shell.execute_reply":"2021-12-16T09:11:33.572149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_id_num = 8\nplt.imshow(X_train[sample_id_num][:,:,0])\nplt.show()\nplt.imshow(Y_train[sample_id_num][:,:,0])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:11:35.628473Z","iopub.execute_input":"2021-12-16T09:11:35.628902Z","iopub.status.idle":"2021-12-16T09:11:36.072376Z","shell.execute_reply.started":"2021-12-16T09:11:35.628811Z","shell.execute_reply":"2021-12-16T09:11:36.071314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 3)Metric Definitions","metadata":{}},{"cell_type":"code","source":"tf.to_int32=lambda x: tf.cast(x, tf.int32)\n\n# Define IoU metric\ndef mean_iou(y_true, y_pred, smooth=1):\n    intersection = K.sum(K.abs(y_true * y_pred), axis=[1,2,3])\n    union = K.sum(y_true,[1,2,3])+K.sum(y_pred,[1,2,3])-intersection\n    iou = K.mean((intersection + smooth) / (union + smooth), axis=0)\n    return tf.convert_to_tensor(iou)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:11:40.276578Z","iopub.execute_input":"2021-12-16T09:11:40.276910Z","iopub.status.idle":"2021-12-16T09:11:40.285134Z","shell.execute_reply.started":"2021-12-16T09:11:40.276869Z","shell.execute_reply":"2021-12-16T09:11:40.283667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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-16T09:11:41.626510Z","iopub.execute_input":"2021-12-16T09:11:41.629300Z","iopub.status.idle":"2021-12-16T09:11:41.637618Z","shell.execute_reply.started":"2021-12-16T09:11:41.629253Z","shell.execute_reply":"2021-12-16T09:11:41.636714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 4) Unet","metadata":{}},{"cell_type":"code","source":"# Building a U-Net model\n#functional\ninputs = Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))\nla = Lambda(lambda x: x / 255) (inputs)\n\nec1 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(la)#(None, 256, 256, 16)\nec1 = Dropout(0.1) (ec1)#layer Dropout#shape=(None, 256, 256, 16)\nec_f1 = Conv2D(16,(3,3),activation='relu', kernel_initializer='he_normal', padding='same') (ec1)\nec_f1_red = MaxPooling2D((2, 2)) (ec_f1)#reduced shape=(None, 128, 128, 16)\n\nec2 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(ec_f1_red)#shape=(None, 128, 128, 32)\nec2 = Dropout(0.1) (ec2)#layer Dropout#shape=(None, 128, 128, 132)\nec_f2 = Conv2D(32,(3,3),activation='relu', kernel_initializer='he_normal', padding='same') (ec2)#shape=(None, 128, 128, 32)\nec_f2_red = MaxPooling2D((2, 2)) (ec_f2)#reduced shape=(None, 64, 64, 32)\n\nec3 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (ec_f2_red)#shape=(None, 64, 64, 64)\nec3 = Dropout(0.2) (ec3)#layer Dropout#shape=(None, 64, 64, 64)\nec_f3 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (ec3)#shape=(None, 64, 64, 64)\nec_f3_red = MaxPooling2D((2, 2)) (ec_f3)#reduced shape=(None, 32, 32, 64)\n\nec4 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (ec_f3_red)#shape=(None, 32, 32, 128)\nec4 = Dropout(0.2) (ec4)#shape=(None, 32, 32, 128)\nec_f4 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (ec4)#shape=(None, 32, 32, 128)\nec_f4_red = MaxPooling2D(pool_size=(2, 2)) (ec_f4)#shape=(None, 16, 16, 128)\n\nec5 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (ec_f4_red)#shape=(None, 16, 16, 256)\nec5 = Dropout(0.3) (ec5)#shape=(None, 16, 16, 256)\nec_f5 = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (ec5)#shape=(None, 16, 16, 256)\n\nduc1 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same') (ec_f5)\ndconc1 = concatenate([duc1, ec_f4])\ndc1 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (dconc1)\ndc1 = Dropout(0.2) (dc1)\ndc_f1 = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (dc1)\n\nduc2 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same') (dc_f1)\ndconc2 = concatenate([duc2 , ec_f3])\ndc2 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (dconc2)\ndc2 = Dropout(0.2) (dc2)\ndc_f2 = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (dc2)\n\nduc3 = Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same') (dc_f2)\ndconc3 = concatenate([duc3, ec_f2])\ndc3 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (dconc3)\ndc3 = Dropout(0.1) (dc3)\ndc_f3 = Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (dc3)\n\nduc4 = Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same') (dc_f3)\ndconc4 = concatenate([duc4, ec_f1], axis=3)\ndc4 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (dconc4)\ndc4 = Dropout(0.1) (dc4)\ndc_f4 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (dc4)\n\noutputs = Conv2D(1, (1, 1), activation='sigmoid') (dc_f4)\nmodel = Model(inputs=[inputs], outputs=[outputs])\nmodel.compile(optimizer='adam', loss='binary_crossentropy', metrics=[dice_coefficient,mean_iou])\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:11:46.955706Z","iopub.execute_input":"2021-12-16T09:11:46.956046Z","iopub.status.idle":"2021-12-16T09:11:50.155520Z","shell.execute_reply.started":"2021-12-16T09:11:46.956014Z","shell.execute_reply":"2021-12-16T09:11:50.154490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit model\nearlystopper = EarlyStopping(patience=16, verbose=1)\ncheckpointer = ModelCheckpoint('best_model.h5', verbose=1, save_best_only=True)\nresults = model.fit(X_train, Y_train, validation_split=0.15, batch_size=5, epochs=100,callbacks=[earlystopper, checkpointer])","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:11:50.158436Z","iopub.execute_input":"2021-12-16T09:11:50.158978Z","iopub.status.idle":"2021-12-16T09:15:28.182895Z","shell.execute_reply.started":"2021-12-16T09:11:50.158917Z","shell.execute_reply":"2021-12-16T09:15:28.181886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(results.history['dice_coefficient'])\nplt.plot(results.history['val_dice_coefficient'])\nplt.title('dice_coefficient')\nplt.ylabel('dice_coefficient')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:17:27.476325Z","iopub.execute_input":"2021-12-16T09:17:27.476672Z","iopub.status.idle":"2021-12-16T09:17:27.721844Z","shell.execute_reply.started":"2021-12-16T09:17:27.476642Z","shell.execute_reply":"2021-12-16T09:17:27.720772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(results.history['mean_iou'])\nplt.plot(results.history['val_mean_iou'])\nplt.title('mean_iou')\nplt.ylabel('mean_iou')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper right')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:17:28.650432Z","iopub.execute_input":"2021-12-16T09:17:28.651243Z","iopub.status.idle":"2021-12-16T09:17:28.888690Z","shell.execute_reply.started":"2021-12-16T09:17:28.651189Z","shell.execute_reply":"2021-12-16T09:17:28.887680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 5)Predictions","metadata":{}},{"cell_type":"code","source":"# Predictions\n#model = load_model('best_model.h5', custom_objects={'dice_coefficient': dice_coefficient})\npreds_train = model.predict(X_train[:int(X_train.shape[0]*0.9)], verbose=1)\npreds_val = model.predict(X_train[int(X_train.shape[0]*0.9):], verbose=1)\npreds_test = model.predict(X_test, verbose=1)\n\n# Applying Threshols\npreds_train_t = (preds_train > 0.5).astype(np.uint8)\npreds_val_t = (preds_val > 0.5).astype(np.uint8)\npreds_test_t = (preds_test > 0.5).astype(np.uint8)\n'''\n# Threshold predictions\npreds_train_t = (preds_train > 0.6).astype(np.uint8)\npreds_val_t = (preds_val > 0.6).astype(np.uint8)\npreds_test_t = (preds_test > 0.6).astype(np.uint8)\n# Threshold predictions\npreds_train_t = (preds_train > 0.7).astype(np.uint8)\npreds_val_t = (preds_val > 0.7).astype(np.uint8)\npreds_test_t = (preds_test > 0.7).astype(np.uint8)'''\n\n# list of upsampled test masks\npreds_test_upsampled = []\nfor i in range(len(preds_test)):\n    preds_test_upsampled.append(resize(np.squeeze(preds_test[i]), (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True))","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:17:32.684751Z","iopub.execute_input":"2021-12-16T09:17:32.685085Z","iopub.status.idle":"2021-12-16T09:17:36.962369Z","shell.execute_reply.started":"2021-12-16T09:17:32.685039Z","shell.execute_reply":"2021-12-16T09:17:36.961149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sanity check on a random training sample\nix = 25\nimshow(X_train[ix])\nplt.show()\nimshow(np.squeeze(Y_train[ix]))\nplt.show()\nimshow(np.squeeze(preds_train_t[ix]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:17:36.964405Z","iopub.execute_input":"2021-12-16T09:17:36.964910Z","iopub.status.idle":"2021-12-16T09:17:37.847867Z","shell.execute_reply.started":"2021-12-16T09:17:36.964864Z","shell.execute_reply":"2021-12-16T09:17:37.846969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Performing a sanity check on a random validation sample\nix = 6\nimshow(X_train[int(X_train.shape[0]*0.9):][ix])\nplt.show()\nimshow(np.squeeze(Y_train[int(Y_train.shape[0]*0.9):][ix]))\nplt.show()\nimshow(np.squeeze(preds_val_t[ix]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:17:43.977871Z","iopub.execute_input":"2021-12-16T09:17:43.978220Z","iopub.status.idle":"2021-12-16T09:17:44.834013Z","shell.execute_reply.started":"2021-12-16T09:17:43.978189Z","shell.execute_reply":"2021-12-16T09:17:44.832761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test samples\nfor ix in range(len(X_test)): \n    print(ix)\n    plt.title('Image')\n    imshow(X_test[ix])\n    plt.show()\n    plt.title('Predicted Mask')\n    imshow(np.squeeze(preds_test_t[ix]))\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:17:46.130669Z","iopub.execute_input":"2021-12-16T09:17:46.131375Z","iopub.status.idle":"2021-12-16T09:17:48.217763Z","shell.execute_reply.started":"2021-12-16T09:17:46.131320Z","shell.execute_reply":"2021-12-16T09:17:48.216745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Visualizing images and masks\nfor ix in range(len(X_test)):\n    \n    print('for image',ix+1)\n    im=X_test[ix]\n    im_mask=np.squeeze(preds_test_t[ix])\n\n    plt.figure(figsize=(20, 20))\n\n    plt.subplot(1, 3, 1)\n    plt.imshow(im[:,:,0])\n    plt.title('Test image')\n\n    plt.subplot( 1, 3, 2)\n    plt.imshow(im_mask)\n    plt.title('Predicted Mask')\n\n    plt.subplot( 1, 3, 3)\n    plt.imshow(im)\n    plt.imshow(im_mask,alpha=0.2)\n    plt.title('Both')\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:17:49.224283Z","iopub.execute_input":"2021-12-16T09:17:49.224585Z","iopub.status.idle":"2021-12-16T09:17:51.894494Z","shell.execute_reply.started":"2021-12-16T09:17:49.224555Z","shell.execute_reply":"2021-12-16T09:17:51.893255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 6) Resizing","metadata":{}},{"cell_type":"code","source":"sub=pd.read_csv('../input/sartorius-cell-instance-segmentation/sample_submission.csv')\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:17:57.851771Z","iopub.execute_input":"2021-12-16T09:17:57.852085Z","iopub.status.idle":"2021-12-16T09:17:57.874837Z","shell.execute_reply.started":"2021-12-16T09:17:57.852053Z","shell.execute_reply":"2021-12-16T09:17:57.873861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred =preds_test_t[0]\nplt.imshow(pred)\nplt.title('pred before resize')\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:17:59.124012Z","iopub.execute_input":"2021-12-16T09:17:59.124606Z","iopub.status.idle":"2021-12-16T09:17:59.258652Z","shell.execute_reply.started":"2021-12-16T09:17:59.124572Z","shell.execute_reply":"2021-12-16T09:17:59.257449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_mask: after reshape \ntest_masks=[]\nfor pred in preds_test_t:\n    print(pred.shape)\n    t_mask=cv2.resize(pred,dsize=(704,520),interpolation=cv2.INTER_CUBIC).reshape(520,704,1)\n    test_masks.append(t_mask)\n    print(t_mask.shape)  \n    plt.imshow(t_mask)\n    plt.title('pred after resize')\n    plt.axis(\"off\")\n    plt.show()\nlen(test_masks)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:18:00.557985Z","iopub.execute_input":"2021-12-16T09:18:00.558588Z","iopub.status.idle":"2021-12-16T09:18:01.067390Z","shell.execute_reply.started":"2021-12-16T09:18:00.558554Z","shell.execute_reply":"2021-12-16T09:18:01.065399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 7)Fixoverlap","metadata":{}},{"cell_type":"markdown","source":"Reference: https://www.kaggle.com/c/sartorius-cell-instance-segmentation/discussion/279995","metadata":{}},{"cell_type":"code","source":"#rle_encoding\ndef rle_encoding(x):\n    dots = np.where(x.flatten() == 1)[0]\n    run_lengths = []\n    prev = -2\n    for b in dots:\n        if (b>prev+1): run_lengths.extend((b + 1, 0))\n        run_lengths[-1] += 1\n        prev = b\n    return ' '.join(map(str, run_lengths))","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:18:07.353480Z","iopub.execute_input":"2021-12-16T09:18:07.353769Z","iopub.status.idle":"2021-12-16T09:18:07.360329Z","shell.execute_reply.started":"2021-12-16T09:18:07.353739Z","shell.execute_reply":"2021-12-16T09:18:07.359004Z"},"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-16T09:18:08.878939Z","iopub.execute_input":"2021-12-16T09:18:08.879763Z","iopub.status.idle":"2021-12-16T09:18:08.884832Z","shell.execute_reply.started":"2021-12-16T09:18:08.879725Z","shell.execute_reply":"2021-12-16T09:18:08.883801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fix_overlap(msk):\n    \"\"\"\n    Args:\n        mask: multi-channel mask, each channel is an instance of cell, shape:(520,704,None)\n    Returns:\n        multi-channel mask with non-overlapping values, shape:(520,704,None)\n    \"\"\"\n    msk = np.array(msk)\n    msk = np.pad(msk, [[0,0],[0,0],[1,0]])\n    ins_len = msk.shape[-1]\n    msk = np.argmax(msk,axis=-1)\n    msk = tf.keras.utils.to_categorical(msk, num_classes=ins_len)\n    msk = msk[...,1:]\n    msk = msk[...,np.any(msk, axis=(0,1))]\n    return msk","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:18:11.453782Z","iopub.execute_input":"2021-12-16T09:18:11.454755Z","iopub.status.idle":"2021-12-16T09:18:11.463390Z","shell.execute_reply.started":"2021-12-16T09:18:11.454692Z","shell.execute_reply":"2021-12-16T09:18:11.462202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for test_mask in test_masks:\n    overlap_test_masks=check_overlap(test_mask)\n    print(overlap_test_masks)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:18:12.785058Z","iopub.execute_input":"2021-12-16T09:18:12.785948Z","iopub.status.idle":"2021-12-16T09:18:12.796310Z","shell.execute_reply.started":"2021-12-16T09:18:12.785913Z","shell.execute_reply":"2021-12-16T09:18:12.795187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_mask2: after reshape after fix_overlapping\ntest_masks2=[]\nfor test_mask in test_masks:\n    test_mask2 = fix_overlap(test_mask).reshape(520,704,1)\n    print(test_mask2.shape)\n    test_masks2+=[test_mask2]","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:18:13.980340Z","iopub.execute_input":"2021-12-16T09:18:13.980620Z","iopub.status.idle":"2021-12-16T09:18:14.010905Z","shell.execute_reply.started":"2021-12-16T09:18:13.980589Z","shell.execute_reply":"2021-12-16T09:18:14.009805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for test_mask2 in test_masks2:\n    overlap_test_masks2=check_overlap(test_mask2)\n    print(overlap_test_masks2)","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:18:16.130704Z","iopub.execute_input":"2021-12-16T09:18:16.131579Z","iopub.status.idle":"2021-12-16T09:18:16.144105Z","shell.execute_reply.started":"2021-12-16T09:18:16.131542Z","shell.execute_reply":"2021-12-16T09:18:16.142789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted2 = [rle_encoding(test_mask2) for test_mask2 in test_masks2]\n#print(predicted2[0])","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:18:17.076180Z","iopub.execute_input":"2021-12-16T09:18:17.077002Z","iopub.status.idle":"2021-12-16T09:18:17.413074Z","shell.execute_reply.started":"2021-12-16T09:18:17.076966Z","shell.execute_reply":"2021-12-16T09:18:17.411987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 8)Writng to csv","metadata":{}},{"cell_type":"code","source":"submit = sub.copy()\nsubmit['predicted'] = predicted2\nsubmit.to_csv('submission.csv', index=False)\nsubmit","metadata":{"execution":{"iopub.status.busy":"2021-12-16T09:18:20.707101Z","iopub.execute_input":"2021-12-16T09:18:20.707407Z","iopub.status.idle":"2021-12-16T09:18:20.728215Z","shell.execute_reply.started":"2021-12-16T09:18:20.707374Z","shell.execute_reply":"2021-12-16T09:18:20.727204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"ReferencesUsed:\n* https://www.kaggle.com/c/sartorius-cell-instance-segmentation/discussion/293159\n* https://www.kaggle.com/karan23258/cell-instance-segmentation-unetfromscratch","metadata":{}}]}