{"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":"# **KDDM 2 SARTORIUS CELL INSTANCE SEGMENTATION MMCLASSIFICATION TRAIN NOTEBOOK**","metadata":{}},{"cell_type":"markdown","source":"# **INSTALL All DEPENDENCIES**\n* **Install PyTorch version required by MMDetection**\n* **Install MMClassification and required dependencies**","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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-26T16:05:54.810239Z","iopub.execute_input":"2022-01-26T16:05:54.810608Z","iopub.status.idle":"2022-01-26T16:06:56.764529Z","shell.execute_reply.started":"2022-01-26T16:05:54.810522Z","shell.execute_reply":"2022-01-26T16:06:56.763686Z"},"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/kddm2/mmcls/mmcv_full-1.3.18-cp37-cp37m-manylinux1_x86_64.whl' --no-deps\n\n!cp -r /kaggle/input/mmdetectionv2140/pycocotools-2.0.2/* /kaggle/working/\n!cp -r /kaggle/input/mmdetectionv2140/mmpycocotools-12.0.3/* /kaggle/working/\n\n!pip install '/kaggle/working/mmpycocotools-12.0.3/' --no-deps\n!pip install '/kaggle/working/pycocotools-2.0.2/' --no-deps\n\n!rm -rf /kaggle/working/pycocotools-2.0.2/\n!rm -rf /kaggle/working/mmpycocotools-12.0.3/\n\n!rm -rf mmdetection\n\n!cp -r /kaggle/input/kddm2/mmcls/mmclassification-0.18.0/mmclassification-0.18.0 /kaggle/working/mmclassification/\n%cd /kaggle/working/mmclassification\n!pip install -e .","metadata":{"execution":{"iopub.status.busy":"2022-01-26T16:06:56.767532Z","iopub.execute_input":"2022-01-26T16:06:56.767818Z","iopub.status.idle":"2022-01-26T16:07:45.386466Z","shell.execute_reply.started":"2022-01-26T16:06:56.767778Z","shell.execute_reply":"2022-01-26T16:07:45.385297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* **Check Installation**","metadata":{}},{"cell_type":"code","source":"import torch\nprint(torch.__version__, torch.cuda.is_available(),  torch.cuda.get_device_properties(0) if torch.cuda.is_available() else 'NO CUDA DEVICE')\n!nvcc --version\n\n# Check mmcv installation\nimport mmcv\nprint(mmcv.__version__)\n\n# Check MMClassification installation\nimport mmcls\nprint(mmcls.__version__)\n\n\nfrom mmcv.ops import get_compiling_cuda_version, get_compiler_version\nprint(\"Cuda:\", get_compiling_cuda_version(), \"Compiler:\", get_compiler_version())","metadata":{"execution":{"iopub.status.busy":"2022-01-26T16:08:38.251279Z","iopub.execute_input":"2022-01-26T16:08:38.251552Z","iopub.status.idle":"2022-01-26T16:08:46.637762Z","shell.execute_reply.started":"2022-01-26T16:08:38.251517Z","shell.execute_reply":"2022-01-26T16:08:46.636231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **TRAINING**\n* **Define model and settings for training**\n* **Execute training**","metadata":{}},{"cell_type":"markdown","source":"* **Set MMDetection model config**","metadata":{}},{"cell_type":"code","source":"#choose a configuration\n\n#base_cfg = \"/kaggle/working/mmclassification/configs/resnet/resnet50_8xb32_in1k.py\"\nbase_cfg = \"/kaggle/working/mmclassification/configs/resnext/resnext50-32x4d_8xb32_in1k.py\"\n#load config\ncfg = mmcv.Config.fromfile(base_cfg)\n\n###############################################################################################################\n\n#update some parameters in the config\ncfg[\"model\"][\"head\"][\"num_classes\"] = 3\n#cfg[\"model\"][\"backbone\"][\"init_cfg\"] = dict(type='Pretrained', checkpoint=\"https://download.openmmlab.com/mmclassification/v0/resnet/resnet50_8xb32_in1k_20210831-ea4938fc.pth\", prefix='backbone')\ncfg[\"model\"][\"backbone\"][\"init_cfg\"] = dict(type='Pretrained', checkpoint=\"https://download.openmmlab.com/mmclassification/v0/resnext/resnext50_32x4d_b32x8_imagenet_20210429-56066e27.pth\", prefix='backbone')\ncfg[\"model\"][\"head\"][\"topk\"] = 1\n\ncfg[\"data\"][\"train\"][\"data_prefix\"] = \"/kaggle/input/sartorius-cell-instance-segmentation/train/\"\ncfg[\"data\"][\"val\"][\"data_prefix\"] = \"/kaggle/input/sartorius-cell-instance-segmentation/train/\"\ncfg[\"data\"][\"test\"][\"data_prefix\"] = \"/kaggle/input/sartorius-cell-instance-segmentation/train/\"\ncfg[\"data\"][\"train\"][\"ann_file\"] = \"/kaggle/input/kddm2/mmcls/cls_train.txt\"\ncfg[\"data\"][\"val\"][\"ann_file\"] = \"/kaggle/input/kddm2/mmcls/cls_val.txt\"\ncfg[\"data\"][\"test\"][\"ann_file\"] = \"/kaggle/input/kddm2/mmcls/cls_val.txt\"\n\ncfg[\"data\"][\"train\"][\"pipeline\"][1] = dict(type='RandomResizedCrop', size=300, backend='pillow')\ncfg[\"data\"][\"val\"][\"pipeline\"][1] = dict(type='Resize', size=(520, -1), backend='pillow')\ncfg[\"data\"][\"val\"][\"pipeline\"][2] = dict(type='CenterCrop', crop_size=300)\ncfg[\"data\"][\"test\"][\"pipeline\"][1] = dict(type='Resize', size=(520, -1), backend='pillow')\ncfg[\"data\"][\"test\"][\"pipeline\"][2] = dict(type='CenterCrop', crop_size=300)\n\ncfg[\"lr_config\"] = dict(policy='step', step=[65, 90])\ncfg[\"runner\"] = dict(type='EpochBasedRunner', max_epochs=140)\ncfg[\"optimizer\"] = dict(type='SGD', lr=0.08, momentum=0.9, weight_decay=0.0001)\n\ncfg[\"checkpoint_config\"] = dict(interval=5)\ncfg[\"evaluation\"] = dict(interval=5, metric='accuracy')\n\n###############################################################################################################\n\n#store updated config file\ncfg_file = \"/kaggle/working/mmcls_sartorius.py\"\nwith open(cfg_file, \"w\") as f:\n    f.write(cfg.pretty_text)\nprint(cfg.pretty_text)","metadata":{"execution":{"iopub.status.busy":"2022-01-26T16:08:46.751612Z","iopub.execute_input":"2022-01-26T16:08:46.751924Z","iopub.status.idle":"2022-01-26T16:08:53.443386Z","shell.execute_reply.started":"2022-01-26T16:08:46.751882Z","shell.execute_reply":"2022-01-26T16:08:53.442506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* **Train model**","metadata":{}},{"cell_type":"code","source":"!python tools/train.py $cfg_file","metadata":{"execution":{"iopub.status.busy":"2022-01-26T16:08:53.445197Z","iopub.execute_input":"2022-01-26T16:08:53.445464Z","iopub.status.idle":"2022-01-26T16:11:37.997159Z","shell.execute_reply.started":"2022-01-26T16:08:53.445426Z","shell.execute_reply":"2022-01-26T16:11:37.996313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **TEST INFERENCE AND PERSIST TRAINED MODEL WEIGHTS**","metadata":{}},{"cell_type":"code","source":"from mmcls.apis import init_model, inference_model\nimport os\n\n!ls /kaggle/working/mmclassification/work_dirs/mmcls_sartorius -lh\nbest_epoch = \"/kaggle/working/mmclassification/work_dirs/mmcls_sartorius/epoch_10.pth\"\nmodel = init_model(cfg_file, best_epoch)\n\ntest_dir = \"/kaggle/input/sartorius-cell-instance-segmentation/test\"\nfiles = os.listdir(test_dir)\n\nfor f in files:\n    img = os.path.join(test_dir, f)\n    res = inference_model(model, img)\n    print(\"{:s}: class {:d} [conf: {:.3f}]\".format(f, res[\"pred_label\"], res[\"pred_score\"]))\n    ","metadata":{"execution":{"iopub.status.busy":"2022-01-26T16:11:45.182773Z","iopub.execute_input":"2022-01-26T16:11:45.183430Z","iopub.status.idle":"2022-01-26T16:11:51.016223Z","shell.execute_reply.started":"2022-01-26T16:11:45.183387Z","shell.execute_reply":"2022-01-26T16:11:51.014070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* **Prepare best epoch for download**","metadata":{}},{"cell_type":"code","source":"!cp $best_epoch /kaggle/working/mmcls_resnext50_0.9918_sartorius.pth\ncfg_name = cfg.file.split(\"/\")[-1]\n!cp $cfg_file /kaggle/working/$cfg_name\n!ls /kaggle/working","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}