{"cells":[{"metadata":{},"cell_type":"markdown","source":"Hi kagglers! Here I am going to show how to use a toolbox to test the object detection baseline in Global Wheat Detection problem.\n\n[MMDetection](https://github.com/open-mmlab/mmdetection) is an open source object detection toolbox based on PyTorch. It is a part of the OpenMMLab project developed by Multimedia Laboratory, CUHK.\n\n [Detectron2](https://github.com/facebookresearch/detectron2), [maskrcnn-benchmark](https://github.com/facebookresearch/maskrcnn-benchmark) and [SimpleDet](http://https://github.com/TuSimple/simpledet) are codebases for computer vision tasks, but the training speed of MMDetection is faster and updated frequency.","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"In the Global Wheat Detection Challenge we can not use Internet during inference, so it is not easy to use MMDetection as there are many dependences that are not installed in default kaggle environment.","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"First, I followed this [blog](https://www.kaggle.com/c/severstal-steel-defect-detection/discussion/113195) to install python dependences.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install ../input/mmcvwhl/addict-2.2.1-py3-none-any.whl\n!pip install ../input/mmdetection20-5-13/mmcv-0.5.1-cp37-cp37m-linux_x86_64.whl\n!pip install ../input/mmdetection20-5-13/terminal-0.4.0-py3-none-any.whl\n!pip install ../input/mmdetection20-5-13/terminaltables-3.1.0-py3-none-any.whl","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Copy the MMDetection framework to the writeable directoy.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp -r ../input/mmdetection20-5-13/mmdetection/mmdetection .","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We should prepare the data and convert to COCO format.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"!mkdir -p mmdetection/data/Wheatdetection/annotations\n!cp -r ../input/global-wheat-detection/test mmdetection/data/Wheatdetection/test\n!cp -r ../input/global-wheat-detection/sample_submission.csv mmdetection/data/Wheatdetection/\n!mkdir mmdetection/configs/wheatdetection","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Some config files for faster-rcnn implement and test data.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp ../input/mmdetfasterrcnn/config/config/faster_rcnn_r50_fpn_1x_coco_test.py mmdetection/configs/wheatdetection\n!cp ../input/mmdetfasterrcnn/config/config/wheat_detection_test.py mmdetection/configs/_base_/datasets\n!cp ../input/mmdetfasterrcnn/config/config/__init__.py mmdetection/mmdet/datasets\n!cp ../input/mmdetfasterrcnn/config/config/wheat.py mmdetection/mmdet/datasets","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cd mmdetection","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"compile the coco toolbox","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp -r ../../input/mmdetection20-5-13/cocoapi/cocoapi .","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cd cocoapi/PythonAPI","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!make","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!make install","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!python setup.py install","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pycocotools","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cd ../..","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"compile the MMDetection Framework","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install -v -e .","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cd ../","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now, we can use MMDetection for the inference.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import sys\nsys.path.append('mmdetection') # To find local version","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from mmdet.apis import init_detector, inference_detector, show_result_pyplot\nimport mmcv\nfrom mmcv import Config\nfrom mmdet.models import build_detector\nfrom mmcv.runner import load_checkpoint\nfrom mmcv.parallel import MMDataParallel\nfrom mmdet.apis import single_gpu_test\nfrom mmdet.datasets import build_dataloader, build_dataset\n\nimport pandas as pd\nimport os\nimport json\n\nfrom PIL import Image\n\nimport torch","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We define a function `gen_test_annotation`, use the hot-plug way to generate the tesing annotation to fit the final tesing environment.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def format_prediction_string(boxes, scores):\n    pred_strings = []\n    for j in zip(scores, boxes):\n        pred_strings.append(\"{0:.4f} {1} {2} {3} {4}\".format(j[0], j[1][0], j[1][1], j[1][2], j[1][3]))\n\n    return \" \".join(pred_strings)\n\n\ndef gen_test_annotation(test_data_path, annotation_path):\n    test_anno_list = []\n    for img in os.listdir(test_data_path):\n        if img.endswith('jpg'):\n            img_info = {}\n            img_info['filename'] = img\n            img_size = Image.open(os.path.join(test_data_path, img)).size\n            img_info['width'] = img_size[0]\n            img_info['height'] = img_size[1]\n            test_anno_list.append(img_info)\n    with open(annotation_path, 'w+') as f:\n        json.dump(test_anno_list, f)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"DIR_INPUT = '/kaggle/working/mmdetection/data/Wheatdetection'\nDIR_TEST = f'{DIR_INPUT}/test'\nDIR_ANNO = f'{DIR_INPUT}/annotations'\n\nDIR_WEIGHTS = '/kaggle/input/mmdetfasterrcnn'\nWEIGHTS_FILE = f'{DIR_WEIGHTS}/epoch_50.pth'\n\ntest_df = pd.read_csv(f'{DIR_INPUT}/sample_submission.csv')\n\n# prepare test data annotations\ngen_test_annotation(DIR_TEST, DIR_ANNO + '/detection_test.json')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"config_file = '/kaggle/working/mmdetection/configs/wheatdetection/faster_rcnn_r50_fpn_1x_coco_test.py'\ncfg = Config.fromfile(config_file)\ncfg.data.test.test_mode = True\n\ndistributed = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset = build_dataset(cfg.data.test)\ndata_loader = build_dataloader(\n    dataset,\n    samples_per_gpu=1,\n    workers_per_gpu=1,\n    dist=distributed,\n    shuffle=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#################################### faster rcnn ############################################\ndevice = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\n\nmodel = build_detector(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg)\ncheckpoint = load_checkpoint(model, WEIGHTS_FILE, map_location='cpu')\n\nmodel.CLASSES = dataset.CLASSES\n\nmodel = MMDataParallel(model, device_ids=[0])\noutputs = single_gpu_test(model, data_loader, False, None, 0.5)\n\nresults = []\n\nfor images_info, result in zip(dataset.data_infos, outputs):\n    boxes = result[0][:, :4]\n    scores = result[0][:, 4]\n\n    boxes[:, 2] = boxes[:, 2] - boxes[:, 0]\n    boxes[:, 3] = boxes[:, 3] - boxes[:, 1]\n\n    result = {\n        'image_id': images_info['filename'][:-4],\n        'PredictionString': format_prediction_string(boxes, scores)\n    }\n\n    results.append(result)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = pd.DataFrame(results, columns=['image_id', 'PredictionString'])\n\n# save result\ntest_df.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!rm -rf mmdetection/","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}