{"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": "## \u6700\u65b0\u7684\u76ee\u6807\u68c0\u6d4b\u6a21\u578bDetectoRS\uff0c\u5355\u6a21\u578b\u7248\u672c\u3002\u65e0\u7f51\u5b89\u88c5\n\n\u57fa\u4e8emmdetection \u6846\u67b6\uff0c\u4f7f\u7528\u539f\u59cb\u53c2\u6570\u8bad\u7ec3\u8fed\u4ee340\u8f6e\u3002\u672a\u5f15\u5165\u5176\u4ed6\u8bad\u7ec3\u6280\u5de7\n\n\u540e\u7eed\u4f1a\u9010\u6e10\u52a0\u5165\u4e00\u4e9b\u65b9\u6cd5\u4ee5\u63d0\u5347\u6548\u679c\u3002 \u6b64\u65b9\u6cd5\u5e0c\u671b\u53ef\u4ee5\u5e2e\u5230\u6709\u9700\u8981\u7684\u4eba\uff01\n\n\u5199notebook\u4e0d\u5bb9\u6613\uff0c\u9ebb\u70e6\u5927\u5bb6\u652f\u6301\u4e00\u4e0b\u70b9\u4e2aup\uff01\n\n\u66f4\u591a\u7684\u8bad\u7ec3\u7ec6\u8282\u548c\u5185\u5bb9\u6211\u4f1a\u5728github\u4e0a\u4e0a\u4f20 \u667a\u661f\u4e91\u56e2\u961f\u656c\u4e0a\uff01\n\n\u4e3a\u4e86\u65b9\u4fbf\u5927\u5bb6\u4f7f\u7528\u5b66\u4e60\uff0c\u6211\u5df2\u5c06coco\u683c\u5f0f\u7684\u8bad\u7ec3\u6570\u636e\u4e0a\u4f20\uff01\nhttps://www.kaggle.com/jqeric/wheatcoco", "execution_count": null}, {"metadata": {}, "cell_type": "markdown", "source": "# install package without internet\nprovided by \u667a\u661f\u4e91\uff08\u667a\u661fAI\uff09", "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}