{"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":"# 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.91714Z"},"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.44824Z","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.67482Z","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.63335Z"},"trusted":true},"execution_count":null,"outputs":[]}]}