{"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 random\nimport os\nimport cv2\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt","metadata":{"papermill":{"duration":6.813456,"end_time":"2021-10-31T11:53:13.517415","exception":false,"start_time":"2021-10-31T11:53:06.703959","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-31T13:28:54.825572Z","iopub.execute_input":"2021-10-31T13:28:54.827558Z","iopub.status.idle":"2021-10-31T13:28:54.834704Z","shell.execute_reply.started":"2021-10-31T13:28:54.827452Z","shell.execute_reply":"2021-10-31T13:28:54.833825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '../input/sartorius-cell-instance-segmentation/train'","metadata":{"papermill":{"duration":0.059527,"end_time":"2021-10-31T11:53:13.707568","exception":false,"start_time":"2021-10-31T11:53:13.648041","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-31T13:28:54.836491Z","iopub.execute_input":"2021-10-31T13:28:54.837614Z","iopub.status.idle":"2021-10-31T13:28:54.848130Z","shell.execute_reply.started":"2021-10-31T13:28:54.837570Z","shell.execute_reply":"2021-10-31T13:28:54.847220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(\"../input/sartorius-cell-instance-segmentation/train.csv\")\ndf_train","metadata":{"papermill":{"duration":0.718257,"end_time":"2021-10-31T11:53:14.469041","exception":false,"start_time":"2021-10-31T11:53:13.750784","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-31T13:28:54.849696Z","iopub.execute_input":"2021-10-31T13:28:54.850024Z","iopub.status.idle":"2021-10-31T13:28:55.291133Z","shell.execute_reply.started":"2021-10-31T13:28:54.849991Z","shell.execute_reply":"2021-10-31T13:28:55.290237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize Annotaion\nreferred to https://www.kaggle.com/ihelon/cell-segmentation-run-length-decoding","metadata":{}},{"cell_type":"code","source":"def rle_decode(mask_rle, shape, color=1):\n    s = mask_rle.split()\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    img = np.zeros((shape[0] * shape[1], shape[2]), dtype=np.float32)            \n    for start, end in zip(starts, ends):\n        img[start : end] = color    \n    return img.reshape(shape)","metadata":{"papermill":{"duration":0.058477,"end_time":"2021-10-31T11:53:14.571083","exception":false,"start_time":"2021-10-31T11:53:14.512606","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-31T13:28:55.292471Z","iopub.execute_input":"2021-10-31T13:28:55.293180Z","iopub.status.idle":"2021-10-31T13:28:55.300351Z","shell.execute_reply.started":"2021-10-31T13:28:55.293143Z","shell.execute_reply":"2021-10-31T13:28:55.299728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_masks(image_id, colors=True):\n    labels = df_train[df_train[\"id\"] == image_id][\"annotation\"].tolist()    \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            \n    mask = mask.clip(0, 1)\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=(8, 16))\n    plt.subplot(3, 1, 1)\n    plt.imshow(image)\n    plt.axis(\"off\")\n    \n    plt.subplot(3, 1, 2)\n    plt.imshow(image)\n    plt.imshow(mask, alpha=0.5)\n    plt.axis(\"off\")\n    \n    plt.subplot(3, 1, 3)\n    plt.imshow(mask)\n    plt.axis(\"off\")\n    \n    plt.show();","metadata":{"papermill":{"duration":0.046386,"end_time":"2021-10-31T11:53:14.659340","exception":false,"start_time":"2021-10-31T11:53:14.612954","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-31T13:28:55.302476Z","iopub.execute_input":"2021-10-31T13:28:55.302712Z","iopub.status.idle":"2021-10-31T13:28:55.314508Z","shell.execute_reply.started":"2021-10-31T13:28:55.302684Z","shell.execute_reply":"2021-10-31T13:28:55.313656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_masks(\"ffdb3cc02eef\", colors=False)","metadata":{"papermill":{"duration":0.838821,"end_time":"2021-10-31T11:53:15.523543","exception":false,"start_time":"2021-10-31T11:53:14.684722","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-31T13:28:55.315865Z","iopub.execute_input":"2021-10-31T13:28:55.316076Z","iopub.status.idle":"2021-10-31T13:28:55.902340Z","shell.execute_reply.started":"2021-10-31T13:28:55.316051Z","shell.execute_reply":"2021-10-31T13:28:55.901577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_masks2(image_id, colors=True):\n    labels = df_train[df_train[\"id\"]==image_id][\"annotation\"].tolist()    \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            \n    mask = mask.clip(0,1)\n    return mask","metadata":{"papermill":{"duration":0.046213,"end_time":"2021-10-31T11:53:15.603685","exception":false,"start_time":"2021-10-31T11:53:15.557472","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-31T13:28:55.903767Z","iopub.execute_input":"2021-10-31T13:28:55.904235Z","iopub.status.idle":"2021-10-31T13:28:55.913167Z","shell.execute_reply.started":"2021-10-31T13:28:55.904194Z","shell.execute_reply":"2021-10-31T13:28:55.912228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Name0=df_train['cell_type'].unique().tolist()\nName=sorted(Name0)","metadata":{"papermill":{"duration":0.05003,"end_time":"2021-10-31T11:53:15.687958","exception":false,"start_time":"2021-10-31T11:53:15.637928","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-31T13:28:55.914745Z","iopub.execute_input":"2021-10-31T13:28:55.915215Z","iopub.status.idle":"2021-10-31T13:28:55.933233Z","shell.execute_reply.started":"2021-10-31T13:28:55.915180Z","shell.execute_reply":"2021-10-31T13:28:55.932412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N=list(range(len(Name)))\nnormal_mapping=dict(zip(Name,N)) \nreverse_mapping=dict(zip(N,Name)) ","metadata":{"papermill":{"duration":0.055095,"end_time":"2021-10-31T11:53:15.777360","exception":false,"start_time":"2021-10-31T11:53:15.722265","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-31T13:28:55.934641Z","iopub.execute_input":"2021-10-31T13:28:55.935112Z","iopub.status.idle":"2021-10-31T13:28:55.939207Z","shell.execute_reply.started":"2021-10-31T13:28:55.935074Z","shell.execute_reply":"2021-10-31T13:28:55.938503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainset=[]\ntrainname=[]\nfor im in tqdm(os.listdir(train_dir)):\n    mask=plot_masks2(im[0:-4])\n    celltype=df_train[df_train[\"id\"]==im[0:-4]]['cell_type'].tolist()[0]\n    mask2=cv2.resize(mask,dsize=(70,52),interpolation=cv2.INTER_CUBIC)\n    trainset+=[mask2]\n    trainname+=[celltype]","metadata":{"papermill":{"duration":115.174874,"end_time":"2021-10-31T11:55:10.994925","exception":false,"start_time":"2021-10-31T11:53:15.820051","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-31T13:28:55.940307Z","iopub.execute_input":"2021-10-31T13:28:55.941101Z","iopub.status.idle":"2021-10-31T13:30:57.936195Z","shell.execute_reply.started":"2021-10-31T13:28:55.941051Z","shell.execute_reply":"2021-10-31T13:30:57.934906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"m=len(trainset)\nM=list(range(m))\nrandom.seed(2021)\nrandom.shuffle(M)","metadata":{"papermill":{"duration":0.160653,"end_time":"2021-10-31T11:55:11.744748","exception":false,"start_time":"2021-10-31T11:55:11.584095","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-31T13:30:57.937612Z","iopub.execute_input":"2021-10-31T13:30:57.937826Z","iopub.status.idle":"2021-10-31T13:30:57.942369Z","shell.execute_reply.started":"2021-10-31T13:30:57.937796Z","shell.execute_reply":"2021-10-31T13:30:57.941808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axs = plt.subplots(3,3,figsize=(15,15))\nfor i in range(9):\n    r=i//3\n    c=i%3\n    img1=trainset[M[i]]\n    name=trainname[M[i]]\n    ax=axs[r][c].axis(\"off\")\n    ax=axs[r][c].set_title(name)\n    ax=axs[r][c].imshow(img1)\nplt.show()","metadata":{"papermill":{"duration":0.792434,"end_time":"2021-10-31T11:55:12.690768","exception":false,"start_time":"2021-10-31T11:55:11.898334","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2021-10-31T13:30:57.943688Z","iopub.execute_input":"2021-10-31T13:30:57.943906Z","iopub.status.idle":"2021-10-31T13:30:58.540195Z","shell.execute_reply.started":"2021-10-31T13:30:57.943879Z","shell.execute_reply":"2021-10-31T13:30:58.539312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.44667,"end_time":"2021-10-31T11:57:19.928185","exception":false,"start_time":"2021-10-31T11:57:19.481515","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.498786,"end_time":"2021-10-31T11:57:20.867352","exception":false,"start_time":"2021-10-31T11:57:20.368566","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":0.438214,"end_time":"2021-10-31T11:57:22.020257","exception":false,"start_time":"2021-10-31T11:57:21.582043","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}