{"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":"%cd /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2021-12-30T13:39:26.980648Z","iopub.execute_input":"2021-12-30T13:39:26.981355Z","iopub.status.idle":"2021-12-30T13:39:27.011555Z","shell.execute_reply.started":"2021-12-30T13:39:26.981252Z","shell.execute_reply":"2021-12-30T13:39:27.010684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# in order to use mmdection ,we need to uninstall pytorch-1.9 and install pytorch-1.7","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-30T13:39:27.013204Z","iopub.execute_input":"2021-12-30T13:39:27.013521Z","iopub.status.idle":"2021-12-30T13:41:34.755129Z","shell.execute_reply.started":"2021-12-30T13:39:27.013485Z","shell.execute_reply":"2021-12-30T13:41:34.754289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# we need to move mmdetection to kaggle working path.","metadata":{}},{"cell_type":"code","source":"!cp -r ../input/mmdetectionv2140/* /kaggle/working/\n!cp -r ../input/mmdetection-new/* /kaggle/working/\n#your model\n!cp -r ../input/handudu/mask_rcnn_cbv2_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco.py /kaggle/working/\n!cp -r ../input/timtim/timm-0.4.12-py3-none-any.whl /kaggle/working/\n!pip install '/kaggle/working/timm-0.4.12-py3-none-any.whl' --no-deps\n!pip install '/kaggle/working/addict-2.4.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/working/yapf-0.31.0-py2.py3-none-any.whl' --no-deps\n!pip install '/kaggle/working/terminal-0.4.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/working/terminaltables-3.1.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/working/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/working/pycocotools-2.0.2/pycocotools-2.0.2' --no-deps\n!pip install '/kaggle/working/mmpycocotools-12.0.3/mmpycocotools-12.0.3' --no-deps\n!mv /kaggle/working/CBNetV2-main  /kaggle/working/mmdetection\n%cd /kaggle/working/mmdetection\n!pip install -e .","metadata":{"execution":{"iopub.status.busy":"2021-12-30T13:41:34.756976Z","iopub.execute_input":"2021-12-30T13:41:34.757237Z","iopub.status.idle":"2021-12-30T13:45:39.292053Z","shell.execute_reply.started":"2021-12-30T13:41:34.757202Z","shell.execute_reply":"2021-12-30T13:45:39.291174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# importing some tools are basic and important","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 cupy as cp\nimport gc\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\nfrom glob import glob\nfrom tqdm.notebook import tqdm\nimport cv2\nimport re\nimport shutil\nfrom mmdet.apis import inference_detector, init_detector, show_result_pyplot, set_random_seed","metadata":{"execution":{"iopub.status.busy":"2021-12-30T13:45:39.294533Z","iopub.execute_input":"2021-12-30T13:45:39.29483Z","iopub.status.idle":"2021-12-30T13:46:01.352365Z","shell.execute_reply.started":"2021-12-30T13:45:39.294777Z","shell.execute_reply":"2021-12-30T13:46:01.351517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working","metadata":{"execution":{"iopub.status.busy":"2021-12-30T13:46:01.354112Z","iopub.execute_input":"2021-12-30T13:46:01.354364Z","iopub.status.idle":"2021-12-30T13:46:01.35956Z","shell.execute_reply.started":"2021-12-30T13:46:01.354328Z","shell.execute_reply":"2021-12-30T13:46:01.358867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# add post function,such as rle trick in cell competition","metadata":{}},{"cell_type":"code","source":"\ndef rle_encoding(x):\n    dots = np.where(x.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 ' '.join(map(str, run_lengths))\ndef get_mask_from_result(result):\n    d = {True : 1, False : 0}\n    u,inv = np.unique(result,return_inverse = True)\n    mk = cp.array([d[x] for x in u])[inv].reshape(result.shape)\n#     print(mk.shape)\n    return mk\ndef does_overlap(mask, other_masks):\n    for other_mask in other_masks:\n        if np.sum(np.logical_and(mask, other_mask)) > 0:\n            #import pdb; pdb.set_trace()\n            #print(\"Found overlapping masks!\")\n            return True\n    return False\ndef remove_overlapping_pixels(mask, other_masks):\n    for other_mask in other_masks:\n        if np.sum(np.logical_and(mask, other_mask)) > 0:\n            print(\"Overlap detected\")\n            mask[np.logical_and(mask, other_mask)] = 0\n    return mask\n\ndef one_hot(y, num_classes, dtype=cp.uint8): # GPU\n    y = cp.array(y, dtype='int')\n    input_shape = y.shape\n    if input_shape and input_shape[-1] == 1 and len(input_shape) > 1:\n        input_shape = tuple(input_shape[:-1])\n    y = y.ravel()\n    if not num_classes:\n        num_classes = cp.max(y) + 1\n    n = y.shape[0]\n    categorical = cp.zeros((n, num_classes), dtype=dtype)\n    categorical[cp.arange(n), y] = 1\n    output_shape = input_shape + (num_classes,)\n    categorical = cp.reshape(categorical, output_shape)\n    return categorical","metadata":{"execution":{"iopub.status.busy":"2021-12-30T13:46:01.360763Z","iopub.execute_input":"2021-12-30T13:46:01.361126Z","iopub.status.idle":"2021-12-30T13:46:01.401715Z","shell.execute_reply.started":"2021-12-30T13:46:01.361094Z","shell.execute_reply":"2021-12-30T13:46:01.400925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rle_encoding(x):\n    dots = np.where(x.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 ' '.join(map(str, run_lengths))\ndef get_mask_from_result(result):\n    d = {True : 1, False : 0}\n    u,inv = np.unique(result,return_inverse = True)\n    mk = cp.array([d[x] for x in u])[inv].reshape(result.shape)\n#     print(mk.shape)\n    return mk\ndef does_overlap(mask, other_masks):\n    for other_mask in other_masks:\n        if np.sum(np.logical_and(mask, other_mask)) > 0:\n            #import pdb; pdb.set_trace()\n            #print(\"Found overlapping masks!\")\n            return True\n    return False\ndef remove_overlapping_pixels(mask, other_masks):\n    for other_mask in other_masks:\n        if np.sum(np.logical_and(mask, other_mask)) > 0:\n            print(\"Overlap detected\")\n            mask[np.logical_and(mask, other_mask)] = 0\n    return mask","metadata":{"execution":{"iopub.status.busy":"2021-12-30T13:46:01.402978Z","iopub.execute_input":"2021-12-30T13:46:01.403722Z","iopub.status.idle":"2021-12-30T13:46:01.416118Z","shell.execute_reply.started":"2021-12-30T13:46:01.403677Z","shell.execute_reply":"2021-12-30T13:46:01.41537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config_file = f'mask_rcnn_cbv2_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco.py'\ncheckpoint_file = '../input/final-model-w/0317.pth' \nmodel = init_detector(config_file, checkpoint_file, device='cuda:0')","metadata":{"execution":{"iopub.status.busy":"2021-12-30T13:46:01.41774Z","iopub.execute_input":"2021-12-30T13:46:01.418036Z","iopub.status.idle":"2021-12-30T13:46:14.188141Z","shell.execute_reply.started":"2021-12-30T13:46:01.417999Z","shell.execute_reply":"2021-12-30T13:46:14.187293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"masks = []\nfiles = []\n\nMIN_PIXELS = {0: 75, 1: 75, 2: 150}\nconfidence_thresholds = {0: 0.25, 1: 0.65, 2: 0.35}\nfor imgs in tqdm(glob('../input/sartorius-cell-instance-segmentation/test/*')):\n    result = inference_detector(model, imgs)\n    \n    pred_class_ls = [len(result[0][0]), len(result[0][1]), len(result[0][2])]\n    pred_class = pred_class_ls.index(max(len(result[0][0]), len(result[0][1]), len(result[0][2])))\n    msk = []\n    for i, classe in enumerate(result[0]):\n        if classe.shape != (0, 5):\n            bbs = classe\n            sgs = result[1][i]\n            for bb, sg in zip(bbs,sgs):\n                box = bb[:4]\n                cnf = bb[4]\n                if cnf >= confidence_thresholds[pred_class]:\n                    mask = get_mask_from_result(sg)\n                    mask = remove_overlapping_pixels(mask, msk)\n                    if mask.sum() >= MIN_PIXELS[pred_class]:\n                        msk.append(mask)\n    for mk in msk:\n            rle_mask = rle_encoding(mk)\n            masks.append(rle_mask)\n            files.append(str(imgs.split('/')[4].split('.')[0]))","metadata":{"execution":{"iopub.status.busy":"2021-12-30T13:46:14.189363Z","iopub.execute_input":"2021-12-30T13:46:14.189602Z","iopub.status.idle":"2021-12-30T13:46:53.759322Z","shell.execute_reply.started":"2021-12-30T13:46:14.18957Z","shell.execute_reply":"2021-12-30T13:46:53.75869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = pd.Series(files, name='id')\npreds = pd.Series(masks, name='predicted')\nsubmission_df = pd.concat([files, preds], axis=1)\nsubmission_df.to_csv('submission.csv', index=False)\nsubmission_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-30T13:46:53.763997Z","iopub.execute_input":"2021-12-30T13:46:53.766966Z","iopub.status.idle":"2021-12-30T13:46:53.811784Z","shell.execute_reply.started":"2021-12-30T13:46:53.766925Z","shell.execute_reply":"2021-12-30T13:46:53.811188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working\n!rm -rf /kaggle/working/mmcv_full-1_3_8-cu110-torch1_7_0\n!rm -rf /kaggle/working/terminaltables-3.1.0-py3-none-any.whl\n!rm -rf /kaggle/working/mmdetection\n!rm -rf /kaggle/working/yapf-0.31.0-py2.py3-none-any.whl\n!rm -rf /kaggle/working/mmdetection-2.14.0\n!rm -rf /kaggle/working/yapf-0.31.0-py2.py3-none-any.whl\n!rm -rf /kaggle/working/mmdetection-2.14.0\n!rm -rf /kaggle/working/mmcv-1.3.8\n\n\n","metadata":{"execution":{"iopub.status.busy":"2021-12-30T13:46:53.815789Z","iopub.execute_input":"2021-12-30T13:46:53.817717Z","iopub.status.idle":"2021-12-30T13:46:55.401515Z","shell.execute_reply.started":"2021-12-30T13:46:53.817679Z","shell.execute_reply":"2021-12-30T13:46:55.400555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /kaggle/working/mmpycocotools-12.0.3\n!rm -rf /kaggle/working/addict-2.4.0-py3-none-any.whl\n!rm -rf /kaggle/working/pycocotools-2.0.2\n!rm -rf /kaggle/working/mmcv-1.3.8\n!rm -rf /kaggle/working/terminal-0.4.0-py3-none-any.whl\n!rm -rf /kaggle/working/mmpycocotools-12.0.3\n!rm -rf /kaggle/working/addict-2.4.0-py3-none-any.whl\n!rm -rf /kaggle/working/pycocotools-2.0.2","metadata":{"execution":{"iopub.status.busy":"2021-12-30T13:46:55.403155Z","iopub.execute_input":"2021-12-30T13:46:55.403427Z","iopub.status.idle":"2021-12-30T13:46:56.846258Z","shell.execute_reply.started":"2021-12-30T13:46:55.40339Z","shell.execute_reply":"2021-12-30T13:46:56.845288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n!rm -rf /kaggle/working/detectors_htc_r50_1x_coco.py\n!rm -rf /kaggle/working/mask_rcnn_cbv2_swin_tiny_patch4_window7_mstrain_480-800_adamw_3x_coco.py\n!rm -rf /kaggle/working/timm-0.4.12-py3-none-any.whl","metadata":{"execution":{"iopub.status.busy":"2021-12-30T13:46:56.847684Z","iopub.execute_input":"2021-12-30T13:46:56.847953Z","iopub.status.idle":"2021-12-30T13:46:57.387604Z","shell.execute_reply.started":"2021-12-30T13:46:56.847917Z","shell.execute_reply":"2021-12-30T13:46:57.386645Z"},"trusted":true},"execution_count":null,"outputs":[]}]}