{"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":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-17T10:24:24.777228Z","iopub.execute_input":"2021-10-17T10:24:24.778173Z","iopub.status.idle":"2021-10-17T10:24:25.044722Z","shell.execute_reply.started":"2021-10-17T10:24:24.77801Z","shell.execute_reply":"2021-10-17T10:24:25.043762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Refrences : https://www.kaggle.com/ihelon/cell-segmentation-run-length-decoding","metadata":{}},{"cell_type":"code","source":"import os\nimport seaborn as sns\nimport numpy as np\nimport pandas as pd\nimport cv2\nfrom tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport PIL\nimport gc\nimport zipfile\nimport matplotlib.image as immg\nfrom joblib import Parallel, delayed","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:12.428433Z","iopub.execute_input":"2021-10-17T14:59:12.428905Z","iopub.status.idle":"2021-10-17T14:59:13.663016Z","shell.execute_reply.started":"2021-10-17T14:59:12.428773Z","shell.execute_reply":"2021-10-17T14:59:13.662092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_trn = pd.read_csv(\"../input/sartorius-cell-instance-segmentation/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:13.664815Z","iopub.execute_input":"2021-10-17T14:59:13.66518Z","iopub.status.idle":"2021-10-17T14:59:14.248661Z","shell.execute_reply.started":"2021-10-17T14:59:13.665136Z","shell.execute_reply":"2021-10-17T14:59:14.247884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(df_trn.sample(10))","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:15.1485Z","iopub.execute_input":"2021-10-17T14:59:15.148808Z","iopub.status.idle":"2021-10-17T14:59:15.180111Z","shell.execute_reply.started":"2021-10-17T14:59:15.148775Z","shell.execute_reply":"2021-10-17T14:59:15.179193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_trn.describe()","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:15.328228Z","iopub.execute_input":"2021-10-17T14:59:15.328552Z","iopub.status.idle":"2021-10-17T14:59:15.363603Z","shell.execute_reply.started":"2021-10-17T14:59:15.328517Z","shell.execute_reply":"2021-10-17T14:59:15.362791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.random.rand(3)","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:15.488199Z","iopub.execute_input":"2021-10-17T14:59:15.48848Z","iopub.status.idle":"2021-10-17T14:59:15.495953Z","shell.execute_reply.started":"2021-10-17T14:59:15.488449Z","shell.execute_reply":"2021-10-17T14:59:15.49488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ref: https://www.kaggle.com/inversion/run-length-decoding-quick-start\ndef rle_decode(mask_rle, shape, color=1):\n    '''\n    mask_rle: run-length as string formated (start length)\n    shape: (height, width, channels) of array to return \n    color: color for the mask\n    Returns numpy array (mask)\n\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-10-17T14:59:15.628345Z","iopub.execute_input":"2021-10-17T14:59:15.628779Z","iopub.status.idle":"2021-10-17T14:59:15.638439Z","shell.execute_reply.started":"2021-10-17T14:59:15.628732Z","shell.execute_reply":"2021-10-17T14:59:15.637241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# www.kaggle.com/ihelon/cell-segmentation-run-length-decoding\ndef plot_masks(image_id, colors=True):\n    labels = df_trn[df_trn[\"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=(20, 8))\n    plt.subplot(131)\n    plt.imshow(image)\n    plt.title('raw image')\n    plt.axis(\"off\")\n    plt.subplot(132)\n    plt.imshow(image)\n    plt.imshow(mask, alpha=0.6)\n    plt.title('image + mask')\n    plt.axis(\"off\")\n    plt.subplot(133)\n    plt.imshow(mask)\n    plt.title('mask only')\n    plt.axis(\"off\")\n    plt.tight_layout()\n    plt.show();","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:15.788903Z","iopub.execute_input":"2021-10-17T14:59:15.789229Z","iopub.status.idle":"2021-10-17T14:59:15.801451Z","shell.execute_reply.started":"2021-10-17T14:59:15.789195Z","shell.execute_reply":"2021-10-17T14:59:15.800543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_masks(\"ffdb3cc02eef\", colors=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:15.948485Z","iopub.execute_input":"2021-10-17T14:59:15.948764Z","iopub.status.idle":"2021-10-17T14:59:17.0332Z","shell.execute_reply.started":"2021-10-17T14:59:15.948735Z","shell.execute_reply":"2021-10-17T14:59:17.03251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_masks(\"73df2962444f\", colors=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:17.034477Z","iopub.execute_input":"2021-10-17T14:59:17.03519Z","iopub.status.idle":"2021-10-17T14:59:18.51738Z","shell.execute_reply.started":"2021-10-17T14:59:17.035156Z","shell.execute_reply":"2021-10-17T14:59:18.51661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_masks(\"13325f865bb0\", colors=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:18.51879Z","iopub.execute_input":"2021-10-17T14:59:18.519229Z","iopub.status.idle":"2021-10-17T14:59:19.403352Z","shell.execute_reply.started":"2021-10-17T14:59:18.519191Z","shell.execute_reply":"2021-10-17T14:59:19.402797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=df_trn.cell_type);","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:19.404875Z","iopub.execute_input":"2021-10-17T14:59:19.405536Z","iopub.status.idle":"2021-10-17T14:59:19.677185Z","shell.execute_reply.started":"2021-10-17T14:59:19.405502Z","shell.execute_reply":"2021-10-17T14:59:19.676352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cell_type = df_trn['cell_type'].unique();cell_type","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:19.678321Z","iopub.execute_input":"2021-10-17T14:59:19.679056Z","iopub.status.idle":"2021-10-17T14:59:19.691778Z","shell.execute_reply.started":"2021-10-17T14:59:19.67902Z","shell.execute_reply":"2021-10-17T14:59:19.690859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_trn.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:19.693057Z","iopub.execute_input":"2021-10-17T14:59:19.693297Z","iopub.status.idle":"2021-10-17T14:59:19.710889Z","shell.execute_reply.started":"2021-10-17T14:59:19.693268Z","shell.execute_reply":"2021-10-17T14:59:19.710002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_trn['cell_type'].replace({'shsy5y':1,'astro':2,'cort':3},inplace=True)\ndf_trn['cell_type'] = pd.to_numeric(df_trn['cell_type'])","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:19.712366Z","iopub.execute_input":"2021-10-17T14:59:19.7126Z","iopub.status.idle":"2021-10-17T14:59:19.774095Z","shell.execute_reply.started":"2021-10-17T14:59:19.712573Z","shell.execute_reply":"2021-10-17T14:59:19.77327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_grp = df_trn.groupby('id')","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:19.775513Z","iopub.execute_input":"2021-10-17T14:59:19.775788Z","iopub.status.idle":"2021-10-17T14:59:19.780872Z","shell.execute_reply.started":"2021-10-17T14:59:19.775747Z","shell.execute_reply":"2021-10-17T14:59:19.78004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_mask(img_id,color=1):\n    temp = df_grp.get_group(img_id)\n    temp_annot = temp.loc[:,'annotation'].tolist()\n    mask = np.zeros((520, 704, 1))\n    for label in temp_annot:\n        mask += rle_decode(label, shape=(520, 704, 1))\n    mask = mask.clip(0, 1)\n    mask[mask==1] = color\n    return mask","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:19.782433Z","iopub.execute_input":"2021-10-17T14:59:19.782927Z","iopub.status.idle":"2021-10-17T14:59:19.79304Z","shell.execute_reply.started":"2021-10-17T14:59:19.782882Z","shell.execute_reply":"2021-10-17T14:59:19.792362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from scipy import stats","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:19.795193Z","iopub.execute_input":"2021-10-17T14:59:19.795709Z","iopub.status.idle":"2021-10-17T14:59:19.806064Z","shell.execute_reply.started":"2021-10-17T14:59:19.795673Z","shell.execute_reply":"2021-10-17T14:59:19.805195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ctype_df = df_trn[['id','cell_type']].groupby('id').agg(lambda x:stats.mode(np.array(x))[0]).reset_index()","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:19.807306Z","iopub.execute_input":"2021-10-17T14:59:19.807615Z","iopub.status.idle":"2021-10-17T14:59:19.913148Z","shell.execute_reply.started":"2021-10-17T14:59:19.807585Z","shell.execute_reply":"2021-10-17T14:59:19.912503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = np.array(list(zip(ctype_df['id'],ctype_df['cell_type'])))","metadata":{"execution":{"iopub.status.busy":"2021-10-17T14:59:19.914109Z","iopub.execute_input":"2021-10-17T14:59:19.91448Z","iopub.status.idle":"2021-10-17T14:59:19.921307Z","shell.execute_reply.started":"2021-10-17T14:59:19.914445Z","shell.execute_reply":"2021-10-17T14:59:19.920219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"OUT_TRAIN = 'TrainMask2x2.zip'","metadata":{"execution":{"iopub.status.busy":"2021-10-17T15:07:19.262989Z","iopub.execute_input":"2021-10-17T15:07:19.263332Z","iopub.status.idle":"2021-10-17T15:07:19.267611Z","shell.execute_reply.started":"2021-10-17T15:07:19.263285Z","shell.execute_reply":"2021-10-17T15:07:19.266531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(OUT_TRAIN, 'w') as img_out:\n    for idx in tqdm(range(0,len(files))):\n        temp_mask = build_mask(files[idx][0],color=int(files[idx][1]))\n        M = temp_mask.shape[0]//2\n        N = temp_mask.shape[1]//2\n        tiles = [temp_mask[x:x+M,y:y+N] for x in range(0,temp_mask.shape[0],M) for y in range(0,temp_mask.shape[1],N)]\n        for j in range(4):\n            mask1 = tiles[j]\n            mask1 = cv2.imencode('.png',mask1)[1]\n            img_out.writestr(files[idx][0] + f'_{j}_mask.png', mask1)","metadata":{"execution":{"iopub.status.busy":"2021-10-17T15:07:19.618589Z","iopub.execute_input":"2021-10-17T15:07:19.61892Z","iopub.status.idle":"2021-10-17T15:07:54.399Z","shell.execute_reply.started":"2021-10-17T15:07:19.618887Z","shell.execute_reply":"2021-10-17T15:07:54.398009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"OUT_TRAIN = 'TrainImage2x2.zip'","metadata":{"execution":{"iopub.status.busy":"2021-10-17T15:08:48.984345Z","iopub.execute_input":"2021-10-17T15:08:48.984621Z","iopub.status.idle":"2021-10-17T15:08:48.989627Z","shell.execute_reply.started":"2021-10-17T15:08:48.984592Z","shell.execute_reply":"2021-10-17T15:08:48.988509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(OUT_TRAIN, 'w') as img_out:\n    for idx in tqdm(range(0,len(files))):\n        image = cv2.imread(f\"../input/sartorius-cell-instance-segmentation/train/{files[idx][0]}.png\")\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        temp_mask = image\n        M = temp_mask.shape[0]//2\n        N = temp_mask.shape[1]//2\n        tiles = [temp_mask[x:x+M,y:y+N] for x in range(0,temp_mask.shape[0],M) for y in range(0,temp_mask.shape[1],N)]\n        for j in range(4):\n            mask1 = tiles[j]\n            mask1 = cv2.imencode('.png',mask1)[1]\n            img_out.writestr(files[idx][0] + f'_{j}.png', mask1)","metadata":{"execution":{"iopub.status.busy":"2021-10-17T15:09:21.973512Z","iopub.execute_input":"2021-10-17T15:09:21.973818Z","iopub.status.idle":"2021-10-17T15:09:44.527551Z","shell.execute_reply.started":"2021-10-17T15:09:21.973785Z","shell.execute_reply":"2021-10-17T15:09:44.526531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"600*4","metadata":{"execution":{"iopub.status.busy":"2021-10-17T15:12:38.863514Z","iopub.execute_input":"2021-10-17T15:12:38.863836Z","iopub.status.idle":"2021-10-17T15:12:38.869858Z","shell.execute_reply.started":"2021-10-17T15:12:38.863797Z","shell.execute_reply":"2021-10-17T15:12:38.869178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}