{"cells":[{"metadata":{},"cell_type":"markdown","source":"# There is the latest SOTA object detection model DetectoRS naive model\nIt is based on the mmdetection frame. There is the naive model without any tricks for training (40 epochs)\n\nHere is the paper.DetectoRS: [Detecting Objects with Recursive Feature Pyramid and Switchable Atrous Convolution](http://https://arxiv.org/abs/2006.02334)\n\nsource code https://github.com/JianqiuChen/DetectoRS.git\n\nmyconfig file https://github.com/JianqiuChen/Kaggle-wheat-detection.git \n\ncoco-format dataset for wheat detection https://www.kaggle.com/jqeric/wheatcoco","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## 最新的目标检测模型DetectoRS，单模型版本。无网安装\n\n基于mmdetection 框架，使用原始参数训练迭代40轮。未引入其他训练技巧\n\n后续会逐渐加入一些方法以提升效果。 此方法希望可以帮到有需要的人！\n\n写notebook不容易，麻烦大家支持一下点个up！\n\n更多的训练细节和内容我会在github上上传 智星云团队敬上！\n\n为了方便大家使用学习，我已将coco格式的训练数据上传！\nhttps://www.kaggle.com/jqeric/wheatcoco","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# install package without internet\nprovided by 智星云（智星AI）","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"# install orderedset\n!cp -r ../input/orderedset/ordered-set-4.0.2 ./\n%cd ordered-set-4.0.2/\n!pip install -e .\n%cd ..","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"!pip install ../input/cu101torch140/torch-1.4.0-cp37-cp37m-linux_x86_64.whl\n!pip install ../input/torchvision050cp37/torchvision-0.5.0-cp37-cp37m-linux_x86_64.whl\n!pip install ../input/mmcvwhl/addict-2.2.1-py3-none-any.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\n!pip install ../input/pytestrunner/pytest_runner-5.2-py2.py3-none-any.whl # new\n!pip install ../input/cityscapesscripts150/cityscapesScripts-1.5.0-py3-none-any.whl\n!pip install ../input/imagecorruptions/imagecorruptions-1.1.0-py3-none-any.whl\n!pip install ../input/asynctest/asynctest-0.13.0-py3-none-any.whl\n!pip install ../input/codecov/codecov-2.1.7-py2.py3-none-any.whl\n!pip install ../input/ubelt9/ubelt-0.9.1-py3-none-any.whl\n!pip install ../input/kwarray/kwarray-0.5.8-py2.py3-none-any.whl\n!pip install ../input/xdoctest/xdoctest-0.12.0-py2.py3-none-any.whl","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"install mmcv 0.6.0","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"!cp -r ../input/mmcv60 ./\n%cd mmcv60/\n!pip install -e .\n%cd ..","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import sys\nsys.path.append('mmcv60') # To find local version","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp -r ../input/detors ./mmdetection","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## install pycoco and setup the mmdet","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"!rm -rf ./mmdetection/mmdet/apis/inference.py\n!cp  ../input/mmdetapisinference/inference.py ./mmdetection/mmdet/apis/inference.py","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%cd mmdetection\n!cp -r ../../input/mmdetection20-5-13/cocoapi/cocoapi .\n%cd cocoapi/PythonAPI\n!make\n!make install\n!python setup.py install\n%cd ../..\n!pip install -v -e .\n\n%cd ../\nsys.path.append('mmdetection') # To find local version","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# add to sys python path for pycocotools\nsys.path.append('/opt/conda/lib/python3.7/site-packages/pycocotools-2.0-py3.7-linux-x86_64.egg') # To find local version","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# start to inference","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import mmcv\n\nfrom mmdet.apis import init_detector, inference_detector, show_result_pyplot\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\nimport matplotlib.pyplot as plt\nimport torch","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp  ../input/detectors2/WheatDetectoRS_mstrain_400_1200_r50_40e.py ./mmdetection/configs/DetectoRS/","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"copy the training config file and checkpoint ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"config = './mmdetection/configs/DetectoRS/WheatDetectoRS_mstrain_400_1200_r50_40e.py'\ncheckpoint = '/kaggle/input/detectors2/epoch_40.pth'\nmodel = init_detector(config, checkpoint, device='cuda:0')\n# model = init_detector(config, checkpoint, device='cpu')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\nimg = '/kaggle/input/global-wheat-detection/test/2fd875eaa.jpg'\nresult = inference_detector(model, img)\n# img = cv2.imread(img)\nprint(type(img))\nshow_result_pyplot(img, result,['wheat'], score_thr=0.3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"result[0][0].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!mkdir  mmdetection/data/\n!mkdir  mmdetection/data/Wheatdetection\n!mkdir  mmdetection/data/Wheatdetection/annotations\n!cp -r ../input/global-wheat-detection/test mmdetection/data/Wheatdetection/test\n","execution_count":null,"outputs":[]},{"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)\ndef gen_test_annotation(test_data_path, annotation_path):\n    test_anno_dict = {}\n    test_anno_dict[\"info\"] = \"jianqiu created\"\n    test_anno_dict[\"license\"] = [\"license\"]\n    id = 0\n    test_anno_list = []\n    for img in os.listdir(test_data_path):\n        if img.endswith('jpg'):\n            id += 1\n            img_info = {}\n            img_size = Image.open(os.path.join(test_data_path, img)).size\n            img_info['height'] = img_size[1]\n            img_info['width'] = img_size[0]\n            img_info['id'] = id            \n            img_info['file_name'] = img\n            test_anno_list.append(img_info)\n    test_anno_dict[\"images\"] = test_anno_list\n    test_anno_dict[\"categories\"] = [\n    {\n      \"id\": 1,\n      \"name\": \"wheat\"\n    }\n  ]\n    with open(annotation_path, 'w+') as f:\n        json.dump(test_anno_dict, 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/detectors2'\nWEIGHTS_FILE = f'{DIR_WEIGHTS}/epoch_40.pth'\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/input/detestorstest/WheatDetectoRS_mstrain_400_1200_r50_40e.py'\ncfg = Config.fromfile(config_file)\ncfg.data.test.test_mode = True\n\ndistributed = False\ndataset = build_dataset(cfg.data.test)\ndata_loader = build_dataloader(\n    dataset,\n    imgs_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":"!mkdir -p /root/.cache/torch/checkpoints/","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"install pretrain model","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp ../input/resnet50/resnet50-19c8e357.pth /root/.cache/torch/checkpoints/resnet50-19c8e357.pth","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Wbf\n!pip install --no-deps '../input/timm-package/timm-0.1.26-py3-none-any.whl' > /dev/null\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import sys\nsys.path.insert(0, \"../input/weightedboxesfusion\")\nfrom ensemble_boxes import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\ndef run_wbf(prediction, image_size=1024, iou_thr=0.43, skip_box_thr=0.43, weights=None):\n    boxes = [(prediction[:, :4]/(image_size-1)).tolist()]\n    scores = [(prediction[:,4]).tolist()]\n    labels = [(np.ones(prediction[:,4].shape[0])).tolist() ]\n\n    boxes, scores, labels = nms(boxes, scores, labels, weights=None, iou_thr=iou_thr)\n    boxes, scores, labels = weighted_boxes_fusion([boxes], [scores], [labels], weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)\n    boxes = boxes*(image_size-1)\n    return boxes, scores, labels","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## inference ","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"device = 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') # 'cuda:0'\n\nmodel.CLASSES = dataset.CLASSES\n\nmodel = MMDataParallel(model, device_ids=[0])\noutputs = single_gpu_test(model, data_loader, False)\n\nresults = []\n\nfor images_info, result in zip(dataset.img_infos, outputs):\n    boxes, scores, labels = run_wbf(result[0][0])\n#     boxes = result[0][0][:, :4]\n#     scores = result[0][0][:, 4]\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)\ntest_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!rm -rf mmdetection/\n!rm -rf mmcv60/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!rm -rf ordered-set-4.0.2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(results[4]['PredictionString'].split(' '))//5","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}