{"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":"import numpy as np\nimport pandas as pd\nimport imageio\nimport matplotlib.pyplot as plt\nimport cv2\nfrom skimage.io import imread, imshow, imread_collection, concatenate_images\nfrom skimage.transform import resize\nfrom tqdm import tqdm\nfrom keras.layers import Input\nfrom keras.models import Model, load_model\nimport tensorflow as tf\nfrom skimage.morphology import label as labeling\nimport random","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-19T14:07:47.365647Z","iopub.execute_input":"2021-12-19T14:07:47.366074Z","iopub.status.idle":"2021-12-19T14:07:47.372715Z","shell.execute_reply.started":"2021-12-19T14:07:47.365981Z","shell.execute_reply":"2021-12-19T14:07:47.371820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv('../input/sartorius-cell-instance-segmentation/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-19T14:07:47.374864Z","iopub.execute_input":"2021-12-19T14:07:47.375684Z","iopub.status.idle":"2021-12-19T14:07:47.387294Z","shell.execute_reply.started":"2021-12-19T14:07:47.375655Z","shell.execute_reply":"2021-12-19T14:07:47.386488Z"},"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-19T14:07:47.388830Z","iopub.execute_input":"2021-12-19T14:07:47.389730Z","iopub.status.idle":"2021-12-19T14:07:47.395277Z","shell.execute_reply.started":"2021-12-19T14:07:47.389628Z","shell.execute_reply":"2021-12-19T14:07:47.394488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_ids = sample_submission['id'].unique().tolist()\nIMG_HEIGHT = 256\nIMG_WIDTH = 256\nIMG_CHANNELS = 1\nTEST_PATH = '../input/sartorius-cell-instance-segmentation/test/'\nX_test = np.zeros((sample_submission['id'].nunique(), IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS), dtype=np.uint8)\nprint('Getting and resizing test images ... ')\n\nfor n, id_ in tqdm(enumerate(test_ids), total=len(test_ids)):\n    path = TEST_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_test[n] = img\n\nprint('Done!')","metadata":{"execution":{"iopub.status.busy":"2021-12-19T14:07:47.397946Z","iopub.execute_input":"2021-12-19T14:07:47.398300Z","iopub.status.idle":"2021-12-19T14:07:47.488881Z","shell.execute_reply.started":"2021-12-19T14:07:47.398252Z","shell.execute_reply":"2021-12-19T14:07:47.488025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Step_level = 0.74\n# Predict on test\nmodel = load_model('../input/den-unet/best_model(2).h5', custom_objects={'dice_coefficient': dice_coefficient})\npreds_test = model.predict(X_test, verbose=1)\n\n# Threshold predictions\npreds_test_t = (preds_test > Step_level).astype(np.uint8)\n\n# Create list of upsampled test masks\npreds_test_upsampled = []\nfor i in range(len(preds_test)):\n    preds_test_upsampled.append(resize(np.transpose(np.squeeze(preds_test[i])), \n                                       (704, 520), \n                                       mode='constant', preserve_range=True))","metadata":{"execution":{"iopub.status.busy":"2021-12-19T14:07:47.490669Z","iopub.execute_input":"2021-12-19T14:07:47.491241Z","iopub.status.idle":"2021-12-19T14:07:48.871726Z","shell.execute_reply.started":"2021-12-19T14:07:47.491194Z","shell.execute_reply":"2021-12-19T14:07:48.870532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Test samples\n#i = 2\n#path = TEST_PATH + test_ids[i]\n# img = cv2.imread(path + '.png')\n# imshow(img)\n# plt.show()\n# imshow(resize(resize(img,(256,256)),(520,704)))\n# plt.show()\n# imshow(resize(np.squeeze(preds_test_t[i]),(520,704)))\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-19T14:07:48.877725Z","iopub.execute_input":"2021-12-19T14:07:48.878287Z","iopub.status.idle":"2021-12-19T14:07:48.888265Z","shell.execute_reply.started":"2021-12-19T14:07:48.878234Z","shell.execute_reply":"2021-12-19T14:07:48.886767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Run-length encoding stolen from https://www.kaggle.com/rakhlin/fast-run-length-encoding-python\ndef rle_encoding(x):\n    dots = np.where(x.T.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 run_lengths\n\ndef prob_to_rles(x, cutoff=0.5):\n    lab_img = labeling(x > cutoff)\n    for i in range(1, lab_img.max() + 1):\n        yield rle_encoding(lab_img == i)","metadata":{"execution":{"iopub.status.busy":"2021-12-19T14:07:48.890973Z","iopub.execute_input":"2021-12-19T14:07:48.891763Z","iopub.status.idle":"2021-12-19T14:07:48.902891Z","shell.execute_reply.started":"2021-12-19T14:07:48.891713Z","shell.execute_reply":"2021-12-19T14:07:48.901638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_decode(mask_rle, shape=(520, 704)):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height,width) of array to return \n    Returns numpy array, 1 - mask, 0 - background\n\n    '''\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","metadata":{"execution":{"iopub.status.busy":"2021-12-19T14:07:48.905525Z","iopub.execute_input":"2021-12-19T14:07:48.906242Z","iopub.status.idle":"2021-12-19T14:07:48.918319Z","shell.execute_reply.started":"2021-12-19T14:07:48.906192Z","shell.execute_reply":"2021-12-19T14:07:48.917140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_df = pd.DataFrame(data = None, columns = sample_submission.columns)\ncount = 0\nminsize = 25\nfor n, id_ in enumerate(test_ids):\n    rle = list(prob_to_rles(preds_test_upsampled[n],cutoff=Step_level))\n    for i in range(0,len(rle)):\n        if len(rle[i]) <= minsize: \n            continue\n        else:\n            cell_annotations = ' '.join([str(x) for x in rle[i]])\n            output_df.loc[count] = id_,cell_annotations\n            count +=1","metadata":{"execution":{"iopub.status.busy":"2021-12-19T14:07:48.920715Z","iopub.execute_input":"2021-12-19T14:07:48.921542Z","iopub.status.idle":"2021-12-19T14:07:49.448240Z","shell.execute_reply.started":"2021-12-19T14:07:48.921497Z","shell.execute_reply":"2021-12-19T14:07:49.447489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output_df.to_csv('submission.csv', index = False)\npd.read_csv('submission.csv').head()","metadata":{"execution":{"iopub.status.busy":"2021-12-19T14:07:49.450417Z","iopub.execute_input":"2021-12-19T14:07:49.450636Z","iopub.status.idle":"2021-12-19T14:07:49.469025Z","shell.execute_reply.started":"2021-12-19T14:07:49.450609Z","shell.execute_reply":"2021-12-19T14:07:49.468299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transfile = pd.read_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-19T14:07:49.470363Z","iopub.execute_input":"2021-12-19T14:07:49.470643Z","iopub.status.idle":"2021-12-19T14:07:49.477855Z","shell.execute_reply.started":"2021-12-19T14:07:49.470606Z","shell.execute_reply":"2021-12-19T14:07:49.477159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 2\npath = TEST_PATH + test_ids[i]\nimg = cv2.imread(path + '.png')\nimr = test_ids[i]\n_,axs = plt.subplots(1,2,figsize=(40,15))\naxs[0].imshow(img)\nfor ids,row in transfile.iterrows():\n    if row.id ==imr:\n        rle = transfile.loc[row.name, 'predicted']\n        dec = rle_decode(rle,shape=(520, 704))\n        axs[1].imshow(np.ma.masked_where(dec==0, dec))","metadata":{"execution":{"iopub.status.busy":"2021-12-19T14:08:23.674820Z","iopub.execute_input":"2021-12-19T14:08:23.675942Z","iopub.status.idle":"2021-12-19T14:08:25.634005Z","shell.execute_reply.started":"2021-12-19T14:08:23.675902Z","shell.execute_reply":"2021-12-19T14:08:25.633350Z"},"trusted":true},"execution_count":null,"outputs":[]}]}