{"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":"# **Install MMDetection and MMDetection-Compatible Torch**","metadata":{}},{"cell_type":"code","source":"!pip install '/kaggle/input/pytorch-170-cuda-toolkit-110221/torch-1.7.0+cu110-cp37-cp37m-linux_x86_64.whl' --no-deps\n!pip install '/kaggle/input/pytorch-170-cuda-toolkit-110221/torchvision-0.8.1+cu110-cp37-cp37m-linux_x86_64.whl' --no-deps\n!pip install '/kaggle/input/pytorch-170-cuda-toolkit-110221/torchaudio-0.7.0-cp37-cp37m-linux_x86_64.whl' --no-deps","metadata":{"execution":{"iopub.status.busy":"2021-12-15T13:34:51.238832Z","iopub.execute_input":"2021-12-15T13:34:51.239253Z","iopub.status.idle":"2021-12-15T13:35:57.765741Z","shell.execute_reply.started":"2021-12-15T13:34:51.239135Z","shell.execute_reply":"2021-12-15T13:35:57.764964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install '/kaggle/input/mmdetectionv2140/addict-2.4.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/yapf-0.31.0-py2.py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/terminal-0.4.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/terminaltables-3.1.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/mmcv_full-1_3_8-cu110-torch1_7_0/mmcv_full-1.3.8-cp37-cp37m-manylinux1_x86_64.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/pycocotools-2.0.2/pycocotools-2.0.2' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/mmpycocotools-12.0.3/mmpycocotools-12.0.3' --no-deps\n\n!rm -rf mmdetection\n\n!cp -r /kaggle/input/mmdetectionv2140/mmdetection-2.14.0 /kaggle/working/\n!mv /kaggle/working/mmdetection-2.14.0 /kaggle/working/mmdetection\n%cd /kaggle/working/mmdetection\n!pip install -e .","metadata":{"execution":{"iopub.status.busy":"2021-12-15T13:35:57.767994Z","iopub.execute_input":"2021-12-15T13:35:57.768259Z","iopub.status.idle":"2021-12-15T13:39:27.800653Z","shell.execute_reply.started":"2021-12-15T13:35:57.768226Z","shell.execute_reply":"2021-12-15T13:39:27.799453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Import Libraries** ","metadata":{}},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torchvision.transforms as transforms\nimport torch.nn.functional as F\nimport sklearn\nimport torchvision\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nimport numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport PIL\nimport json\nfrom PIL import Image, ImageEnhance\nimport albumentations as A\nimport mmdet\nimport mmcv\nfrom albumentations.pytorch import ToTensorV2\nimport seaborn as sns\nimport glob\nfrom pathlib import Path\nimport pycocotools\nfrom pycocotools import mask\nimport numpy.random\nimport random\nimport cv2\nimport re\nfrom mmdet.datasets import build_dataset\nfrom mmdet.models import build_detector\nfrom mmdet.apis import train_detector\nfrom mmdet.apis import inference_detector, init_detector, show_result_pyplot, set_random_seed","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-15T13:39:27.80264Z","iopub.execute_input":"2021-12-15T13:39:27.802894Z","iopub.status.idle":"2021-12-15T13:40:00.814043Z","shell.execute_reply.started":"2021-12-15T13:39:27.802864Z","shell.execute_reply":"2021-12-15T13:40:00.813025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ..","metadata":{"execution":{"iopub.status.busy":"2021-12-15T13:40:00.815496Z","iopub.execute_input":"2021-12-15T13:40:00.815744Z","iopub.status.idle":"2021-12-15T13:40:00.822294Z","shell.execute_reply.started":"2021-12-15T13:40:00.815716Z","shell.execute_reply":"2021-12-15T13:40:00.821349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_WIDTH = 704\nIMG_HEIGHT = 520","metadata":{"execution":{"iopub.status.busy":"2021-12-15T13:40:00.824733Z","iopub.execute_input":"2021-12-15T13:40:00.82496Z","iopub.status.idle":"2021-12-15T13:40:00.841116Z","shell.execute_reply.started":"2021-12-15T13:40:00.824933Z","shell.execute_reply":"2021-12-15T13:40:00.840271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Helper Functions**","metadata":{}},{"cell_type":"code","source":"def rle_decode(mask_rle, shape):\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)\n\ndef rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    return ' '.join(str(x) for x in runs)\n\n\ndef flatten_l_o_l(nested_list):\n    \"\"\" Flatten a list of lists \"\"\"\n    return [item for sublist in nested_list for item in sublist]\n\n\ndef load_json_to_dict(json_path):\n    \"\"\" tbd \"\"\"\n    with open(json_path) as json_file:\n        data = json.load(json_file)\n    return data","metadata":{"execution":{"iopub.status.busy":"2021-12-15T13:40:00.84275Z","iopub.execute_input":"2021-12-15T13:40:00.843026Z","iopub.status.idle":"2021-12-15T13:40:00.858017Z","shell.execute_reply.started":"2021-12-15T13:40:00.842974Z","shell.execute_reply":"2021-12-15T13:40:00.857035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_img_and_mask(img_path, annotation, width, height):\n    \"\"\" Capture the relevant image array as well as the image mask \"\"\"\n    img_mask = np.zeros((height, width), dtype=np.uint8)\n    for i, annot in enumerate(annotation): \n        img_mask = np.where(rle_decode(annot, (height, width))!=0, i, img_mask)\n    #img_mask = cv2.cvtColor(img_mask, cv2.COLOR_GRAY2BGR)\n    img = cv2.imread(img_path)[..., ::-1]\n    return img[..., 0], img_mask","metadata":{"execution":{"iopub.status.busy":"2021-12-15T13:40:00.859813Z","iopub.execute_input":"2021-12-15T13:40:00.860125Z","iopub.status.idle":"2021-12-15T13:40:00.876119Z","shell.execute_reply.started":"2021-12-15T13:40:00.860095Z","shell.execute_reply":"2021-12-15T13:40:00.875038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def polygonFromMask(maskedArr, idx):\n  # adapted from https://github.com/hazirbas/coco-json-converter/blob/master/generate_coco_json.py\n  contours, _ = cv2.findContours(maskedArr, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n  segmentation = []\n  valid_poly = 0\n  for contour in contours:\n  # Valid polygons have >= 6 coordinates (3 points)\n     if contour.size >= 6:\n        segmentation.append(contour.astype(float).flatten().tolist())\n        valid_poly += 1\n  if valid_poly == 0:\n     raise ValueError(idx)\n  return [segmentation]","metadata":{"execution":{"iopub.status.busy":"2021-12-15T13:40:00.877383Z","iopub.execute_input":"2021-12-15T13:40:00.877714Z","iopub.status.idle":"2021-12-15T13:40:00.890047Z","shell.execute_reply.started":"2021-12-15T13:40:00.877672Z","shell.execute_reply":"2021-12-15T13:40:00.889132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Visualizations**","metadata":{}},{"cell_type":"code","source":"from skimage import measure\n\ndef fill_hole(m):\n    filled = m.copy()\n    pad = np.pad(m, 4)\n    lb = measure.label(pad < 0.5, background=0, connectivity=1)\n    u, cc = np.unique(lb, return_counts=True)\n    if len(u) > 2:\n        #print(u, cc)\n        lb = lb[4:-4, 4:-4]\n        for uu in u[2:]:\n            filled[lb == uu] = 1\n\n    return filled\ntrain_df = pd.read_csv('../input/sartorius-cell-instance-segmentation/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-12-15T13:40:00.891526Z","iopub.execute_input":"2021-12-15T13:40:00.891932Z","iopub.status.idle":"2021-12-15T13:40:01.310436Z","shell.execute_reply.started":"2021-12-15T13:40:00.891885Z","shell.execute_reply":"2021-12-15T13:40:01.309246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lines = []\nfor f in train_df.itertuples():\n    lines.append('../input/sartorius-cell-instance-segmentation/train/' + f[1] + '.png')","metadata":{"execution":{"iopub.status.busy":"2021-12-15T13:40:01.312043Z","iopub.execute_input":"2021-12-15T13:40:01.31262Z","iopub.status.idle":"2021-12-15T13:40:01.4973Z","shell.execute_reply.started":"2021-12-15T13:40:01.312567Z","shell.execute_reply":"2021-12-15T13:40:01.496356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lins = pd.Series(lines, name='img_path')","metadata":{"execution":{"iopub.status.busy":"2021-12-15T13:40:01.499807Z","iopub.execute_input":"2021-12-15T13:40:01.500354Z","iopub.status.idle":"2021-12-15T13:40:01.515098Z","shell.execute_reply.started":"2021-12-15T13:40:01.500306Z","shell.execute_reply":"2021-12-15T13:40:01.513902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.concat([train_df, lins], axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T13:40:01.516748Z","iopub.execute_input":"2021-12-15T13:40:01.517107Z","iopub.status.idle":"2021-12-15T13:40:01.545142Z","shell.execute_reply.started":"2021-12-15T13:40:01.517051Z","shell.execute_reply":"2021-12-15T13:40:01.544416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp_df = train_df.drop_duplicates(subset=[\"id\", \"img_path\"]).reset_index(drop=True)\ntmp_df[\"annotation\"] = train_df.groupby(\"id\")[\"annotation\"].agg(list).reset_index(drop=True)\ntrain_df = tmp_df.copy()\nfiles_ = []\nfor f in train_df.itertuples():\n    files_.append(f[1])\n    \n","metadata":{"execution":{"iopub.status.busy":"2021-12-15T13:40:01.54616Z","iopub.execute_input":"2021-12-15T13:40:01.546731Z","iopub.status.idle":"2021-12-15T13:40:01.838688Z","shell.execute_reply.started":"2021-12-15T13:40:01.546697Z","shell.execute_reply":"2021-12-15T13:40:01.837717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**尝试获得clean_mask** **Try to get clean_mask**","metadata":{}},{"cell_type":"code","source":"for idx in range(len(files_)):\n    im, mk = get_img_and_mask(**train_df[[\"img_path\", \"annotation\", \"width\", \"height\"]].iloc[idx].to_dict())\n    mk = fill_hole(mk)\n    mk = cv2.cvtColor(mk, cv2.COLOR_GRAY2BGR)\n    b, g, r = cv2.split(mk)\n    b = cv2.cvtColor(b, cv2.COLOR_GRAY2BGR)\n    g = cv2.cvtColor(g, cv2.COLOR_GRAY2BGR)\n    r = cv2.cvtColor(r, cv2.COLOR_GRAY2BGR)\n    #print(r.dtype)\n    #print(mk.dtype)\n    #mk = cv2.cvtColor(mk, cv2.COLOR_GRAY2BGR)\n    #mk = mk.astype(np.uint8)\n    # print(r.dtype)\n    # print(mk.dtype)\n    mk = cv2.bitwise_and(mk, r, dst=mk, mask=None)\n    cv2.imwrite(files_[idx]+'.png', mk)","metadata":{"execution":{"iopub.status.busy":"2021-12-15T13:40:01.841626Z","iopub.execute_input":"2021-12-15T13:40:01.842042Z","iopub.status.idle":"2021-12-15T13:42:23.428241Z","shell.execute_reply.started":"2021-12-15T13:40:01.842009Z","shell.execute_reply":"2021-12-15T13:42:23.427225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /kaggle/working/mmdetection","metadata":{"execution":{"iopub.status.busy":"2021-12-15T13:42:23.429473Z","iopub.execute_input":"2021-12-15T13:42:23.429727Z","iopub.status.idle":"2021-12-15T13:42:23.437536Z","shell.execute_reply.started":"2021-12-15T13:42:23.429696Z","shell.execute_reply":"2021-12-15T13:42:23.436919Z"},"trusted":true},"execution_count":null,"outputs":[]}]}