{"cells":[{"metadata":{},"cell_type":"markdown","source":"The trained model is present with the name : CascadeRCNN_X101 VinBigData 20Ep MMDET in the datasets. "},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport cv2\nimport random\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Check nvcc version\n!nvcc -V\n# Check GCC version\n!gcc --version","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n\nprint(\"this will take around 10 mins\")\n# install dependencies: (use cu101 because colab has CUDA 10.1)\n# !pip install -U torch==1.7.0+cu101 torchvision==0.6.1+cu101 -f https://download.pytorch.org/whl/torch_stable.html\n\n# install mmcv-full thus we could use CUDA operators\n!pip install mmcv-full\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!rm -rf mmdetection\n!git clone --branch v2.7.0 https://github.com/open-mmlab/mmdetection.git\n%cd mmdetection\n\n!pip install -e .\n\n# install Pillow 7.0.0 back in order to avoid bug in colab\n!pip install Pillow==7.0.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Check Pytorch installation\nimport torch, torchvision\nprint(torch.__version__, torch.cuda.is_available())\n\n# Check MMDetection installation\nimport mmdet\nprint(mmdet.__version__)\n\n# Check mmcv installation\nfrom mmcv.ops import get_compiling_cuda_version, get_compiler_version\nprint(get_compiling_cuda_version())\nprint(get_compiler_version())","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# CascadeRNN101X Pretrained"},{"metadata":{"trusted":true},"cell_type":"code","source":"!mkdir checkpoints\n# !wget -c http://download.openmmlab.com/mmdetection/v2.0/mask_rcnn/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco_bbox_mAP-0.408__segm_mAP-0.37_20200504_163245-42aa3d00.pth \\\n#       -O checkpoints/mask_rcnn_r50_caffe_fpn_mstrain-poly_3x_coco_bbox_mAP-0.408__segm_mAP-0.37_20200504_163245-42aa3d00.pth\n\n\n!wget -c https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/cascade_rcnn_x101_32x4d_fpn_1x_20190501-af628be5.pth \\\n      -O checkpoints/cascade_rcnn_x101_32x4d_fpn_1x_20190501-af628be5.pth\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_anno = \"../../input/vinbigdata-1024-image-dataset/vinbigdata/test\"\n\nids = os.listdir(test_anno)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(ids)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Preparing Test Annotation file"},{"metadata":{},"cell_type":"markdown","source":""},{"metadata":{"trusted":true},"cell_type":"code","source":"img_infos = []\nfor i, _id in enumerate(ids):\n    if '.png' in _id:\n        img_infos.append({\n                    \"license\": 0,\n                    \"url\": 'null',\n                    \"file_name\": _id,\n                    \"height\": 1024,\n                    \"width\": 1024,\n                    \"date_captured\": 'null',\n                    \"id\": _id\n                })","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_infos[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import json\nval_anno = '../../input/vinbigdata-coco-dataset-with-wbf-3x-downscaled/vinbigdata-coco-dataset-with-wbf-3x-downscaled/val_annotations.json'\n\nwith open(val_anno) as f:\n    dd = json.load(f)\n\ndd.keys()\ndd['annotations']=[]\ndd['images']\ndd['images'] = img_infos\nwith open('./test_ann.json', 'w') as outfile:\n    json.dump(dd, outfile)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"val_ids = os.listdir('../../input/vinbigdata-coco-dataset-with-wbf-3x-downscaled/vinbigdata-coco-dataset-with-wbf-3x-downscaled/val_images')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"_classes = (\"Aortic_enlargement\", \"Atelectasis\", \"Calcification\", \"Cardiomegaly\", \"Consolidation\", \"ILD\", \"Infiltration\", \"Lung_Opacity\", \"Nodule/Mass\", \"Other_lesion\", \"Pleural_effusion\", \"Pleural_thickening\", \"Pneumothorax\", \"Pulmonary_fibrosis\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Custom Config Options"},{"metadata":{"trusted":true},"cell_type":"code","source":"_cfg_options = {\"dataset_type\" : 'CocoDataset',\n\"classes\" : '''(\"Aortic_enlargement\", \"Atelectasis\", \"Calcification\", \"Cardiomegaly\", \"Consolidation\", \"ILD\", \"Infiltration\", \"Lung_Opacity\", \"Nodule/Mass\", \"Other_lesion\", \"Pleural_effusion\", \"Pleural_thickening\", \"Pneumothorax\", \"Pulmonary_fibrosis\")''',\n\"data.train.img_prefix\" : '../../input/vinbigdata-coco-dataset-with-wbf-3x-downscaled/vinbigdata-coco-dataset-with-wbf-3x-downscaled/',\n\"data.train.classes\" : '''(\"Aortic_enlargement\", \"Atelectasis\", \"Calcification\", \"Cardiomegaly\", \"Consolidation\", \"ILD\", \"Infiltration\", \"Lung_Opacity\", \"Nodule/Mass\", \"Other_lesion\", \"Pleural_effusion\", \"Pleural_thickening\", \"Pneumothorax\", \"Pulmonary_fibrosis\")''',\n\"data.train.ann_file\" : '../../input/vinbigdata-coco-dataset-with-wbf-3x-downscaled/vinbigdata-coco-dataset-with-wbf-3x-downscaled/train_annotations.json',\n\"data.train.type\" : 'CocoDataset',\n\"data.val.img_prefix\" : '../../input/vinbigdata-coco-dataset-with-wbf-3x-downscaled/vinbigdata-coco-dataset-with-wbf-3x-downscaled/',\n\"data.val.classes\" : '''(\"Aortic_enlargement\", \"Atelectasis\", \"Calcification\", \"Cardiomegaly\", \"Consolidation\", \"ILD\", \"Infiltration\", \"Lung_Opacity\", \"Nodule/Mass\", \"Other_lesion\", \"Pleural_effusion\", \"Pleural_thickening\", \"Pneumothorax\", \"Pulmonary_fibrosis\")''',\n\"data.val.ann_file\" : '../../input/vinbigdata-coco-dataset-with-wbf-3x-downscaled/vinbigdata-coco-dataset-with-wbf-3x-downscaled/val_annotations.json',\n\"data.val.type\" : 'CocoDataset',\n\"data.test.img_prefix\" : '../../input/vinbigdata-1024-image-dataset/vinbigdata/test/',\n\"data.test.classes\" : '''(\"Aortic_enlargement\", \"Atelectasis\", \"Calcification\", \"Cardiomegaly\", \"Consolidation\", \"ILD\", \"Infiltration\", \"Lung_Opacity\", \"Nodule/Mass\", \"Other_lesion\", \"Pleural_effusion\", \"Pleural_thickening\", \"Pneumothorax\", \"Pulmonary_fibrosis\")''',\n\"data.test.ann_file\" : './test_ann.json',\n\"data.test.type\":'CocoDataset',\n\"data.train.type\" : 'CocoDataset',\n\"data.val.type\" : 'CocoDataset',\n\"data.test.type\" : 'CocoDataset',\n\"log_config.interval\" : 10,\n\"evaluation.metric\" : 'bbox',\n\"load_from\" : './checkpoints/cascade_rcnn_x101_32x4d_fpn_1x_20190501-af628be5.pth',\n\"work_dir\" : \"../vinbig_output\",\n\"total_epochs\" : '21'}\n\n\n# \"model.roi_head.bbox_head[0].num_classes\" : '14',\n# \"model.roi_head.bbox_head[1].num_classes\" : '14',\n# \"model.roi_head.bbox_head[2].num_classes\" : '14',\n\ncfg_op = \"\"\nfor k, v in _cfg_options.items():\n    cfg_op+=f\"{k}='{v}' \"\nprint(cfg_op)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Loading the 20ep trained model"},{"metadata":{"trusted":true},"cell_type":"code","source":"from mmdet.apis import inference_detector, init_detector, show_result_pyplot\nfrom mmcv import Config\n\n# Choose to use a config and initialize the detector\n# config = 'configs/faster_rcnn/faster_rcnn_r50_caffe_fpn_mstrain_1x_coco.py'\nconfig = Config.fromfile('./configs/cascade_rcnn/cascade_rcnn_x101_32x4d_fpn_1x_coco.py')\nconfig.model.roi_head.bbox_head[0].num_classes = 14\nconfig.model.roi_head.bbox_head[1].num_classes = 14\nconfig.model.roi_head.bbox_head[2].num_classes = 14\ncheckpoint = '../../input/cascadercnnx-vinbigdata-20ep-1024/epoch_20.pth'\n# initialize the detector\n\nmodel = init_detector(config, checkpoint, device='cuda:0', cfg_options=_cfg_options)\nmodel.CLASSES = _classes\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Option 1 for inference - Using only 10 images for demo\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"_id = random.randint(1,1098)\n# Use the detector to do inference\n# img = f'../../input/vinbigdata-coco-dataset-with-wbf-3x-downscaled/vinbigdata-coco-dataset-with-wbf-3x-downscaled/val_images/{val_ids[_id]}'\nfrom tqdm import tqdm\ntest_img_ids = os.listdir(\"../../input/vinbigdata-1024-image-dataset/vinbigdata/test\")\nresult = {}\nfor _id in tqdm(test_img_ids, total=len(test_img_ids)):\n    img_path = \"../../input/vinbigdata-1024-image-dataset/vinbigdata/test/\" + f\"{_id}\"\n    pred = inference_detector(model, img_path)\n    result[_id] = pred\n# img = \"../../input/vinbigdata-1024-image-dataset/vinbigdata/test/002a34c58c5b758217ed1f584ccbcfe9.png\"\n# result = inference_detector(model, img)\n# show_result_pyplot(model, img, result, score_thr=0.3)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Option 2 for inference"},{"metadata":{"trusted":true},"cell_type":"code","source":"# !python tools/test.py ./configs/cascade_rcnn/cascade_rcnn_x101_32x4d_fpn_1x_coco.py ../../input/cascadercnnx-vinbigdata-20ep-1024/epoch_20.pth --cfg-options dataset_type='CocoDataset' classes='(\"Aortic_enlargement\", \"Atelectasis\", \"Calcification\", \"Cardiomegaly\", \"Consolidation\", \"ILD\", \"Infiltration\", \"Lung_Opacity\", \"Nodule/Mass\", \"Other_lesion\", \"Pleural_effusion\", \"Pleural_thickening\", \"Pneumothorax\", \"Pulmonary_fibrosis\")' data.train.img_prefix='../../input/vinbigdata-coco-dataset-with-wbf-3x-downscaled/vinbigdata-coco-dataset-with-wbf-3x-downscaled/' data.train.classes='(\"Aortic_enlargement\", \"Atelectasis\", \"Calcification\", \"Cardiomegaly\", \"Consolidation\", \"ILD\", \"Infiltration\", \"Lung_Opacity\", \"Nodule/Mass\", \"Other_lesion\", \"Pleural_effusion\", \"Pleural_thickening\", \"Pneumothorax\", \"Pulmonary_fibrosis\")' data.train.ann_file='../../input/vinbigdata-coco-dataset-with-wbf-3x-downscaled/vinbigdata-coco-dataset-with-wbf-3x-downscaled/train_annotations.json' data.train.type='CocoDataset' data.val.img_prefix='../../input/vinbigdata-coco-dataset-with-wbf-3x-downscaled/vinbigdata-coco-dataset-with-wbf-3x-downscaled/' data.val.classes='(\"Aortic_enlargement\", \"Atelectasis\", \"Calcification\", \"Cardiomegaly\", \"Consolidation\", \"ILD\", \"Infiltration\", \"Lung_Opacity\", \"Nodule/Mass\", \"Other_lesion\", \"Pleural_effusion\", \"Pleural_thickening\", \"Pneumothorax\", \"Pulmonary_fibrosis\")' data.val.ann_file='../../input/vinbigdata-coco-dataset-with-wbf-3x-downscaled/vinbigdata-coco-dataset-with-wbf-3x-downscaled/val_annotations.json' data.val.type='CocoDataset' data.test.img_prefix='../../input/vinbigdata-1024-image-dataset/vinbigdata/test/' data.test.classes='(\"Aortic_enlargement\", \"Atelectasis\", \"Calcification\", \"Cardiomegaly\", \"Consolidation\", \"ILD\", \"Infiltration\", \"Lung_Opacity\", \"Nodule/Mass\", \"Other_lesion\", \"Pleural_effusion\", \"Pleural_thickening\", \"Pneumothorax\", \"Pulmonary_fibrosis\")' data.test.ann_file='./test_ann.json' data.test.type='CocoDataset' log_config.interval='10' evaluation.metric='bbox' load_from='./checkpoints/cascade_rcnn_x101_32x4d_fpn_1x_20190501-af628be5.pth' work_dir='../vinbig_output' total_epochs='21' --out preds_cascadex.pkl","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir(\"./\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# import pickle\n\n# with open('./preds_cascadex.pkl', 'rb') as f:\n#     data = pickle.load(f)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nimport json\nwith open('./test_ann.json', 'rb') as f:\n    ann = json.load(f)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# file_ids = [file_name.get('file_name').split('.png')[0] for file_name in ann.get('images')]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install ensemble_boxes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\ntest_df = pd.read_csv('../../input/vinbigdata-original-image-dataset/vinbigdata/test.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from ensemble_boxes import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# ann_with_pred = zip(file_ids, data)\nsubmission_vals = []\n# for _id, preds in ann_with_pred\n\n\n# this method is for option1\nfor _id, preds in result.items():\n    boxes = []\n    scores = []\n    labels = []\n    _id = _id.split('.png')[0]\n    width = test_df[test_df.image_id==_id]['width'].iloc[0]\n    height = test_df[test_df.image_id==_id]['height'].iloc[0]  \n    for i, pred in enumerate(preds):\n        if len(pred):\n            for p in pred:\n                box = p[:4]/1024\n                boxes.append(box)\n                score = p[4].astype(float)\n                scores.append(score)\n                labels.append(i)\n    boxes, scores, labels = weighted_boxes_fusion([boxes], [scores], [labels], iou_thr=0.4, skip_box_thr=0.4)\n    boxes[:, 0] = boxes[:, 0]*height\n    boxes[:, 2] = boxes[:, 2]*height\n    boxes[:, 1] = boxes[:, 1]*width\n    boxes[:, 3] = boxes[:, 3]*width\n    \n    scaled_boxes = boxes.astype(int)\n    labels = labels.astype(int)\n    _id_preds = []\n    if len(scaled_boxes):\n        for i in range(len(scaled_boxes)):\n            _id_preds.append(str(labels[i]))\n            _id_preds.append(str(scores[i].round(2)))\n            _id_preds.append(str(scaled_boxes[i][0]))\n            _id_preds.append(str(scaled_boxes[i][1]))\n            _id_preds.append(str(scaled_boxes[i][2]))\n            _id_preds.append(str(scaled_boxes[i][3]))\n        pred_str = \" \".join(_id_preds)\n    else:\n        pred_str = '14 1 0 0 1 1'\n    submission_vals.append([_id, pred_str])\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.DataFrame(submission_vals, columns = ['image_id','PredictionString'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result['e94fde220360e4b769921e16059cc6af.png']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\nresult['b3f67ac077531f44dd06275af31edbd9.png']\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df[df.PredictionString==\"14 1 0 0 1 1\"].count()\n                                             ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.tail(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.iloc[2222]['PredictionString']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.to_csv('submission.csv', header=True, index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}