{"cells":[{"metadata":{},"cell_type":"markdown","source":"Train kernel: https://www.kaggle.com/ateplyuk/gwd-starter-efficientdet-train","execution_count":null},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport sys\nimport os\nimport itertools\n\nsys.path.append('/kaggle/input/mod-efficientdet/')\nsys.path.append('/kaggle/input/eff-torch/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cp -r /kaggle/input/cocoapi/* /tmp","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cd /tmp/PythonAPI","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!make","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from infer_detector import Infer","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gtf = Infer()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gtf.Model(model_dir=\"/kaggle/input/gwd-starter-efficientdet-train/trained/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_path = \"/kaggle/input/global-wheat-detection/test/cc3532ff6.jpg\"\nduration, scores, labels, boxes = gtf.Predict(img_path, ['wheat'], vis_threshold=0.7);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from IPython.display import Image\nImage(filename='output.jpg') ","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"TEST_PATH = '/kaggle/input/global-wheat-detection/test/'\ntest_ids = os.listdir(TEST_PATH)","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"res = []\nfor idx, row in enumerate(test_ids):\n    img_path = TEST_PATH + row\n    duration, scores, labels, boxes = gtf.Predict(img_path, ['wheat'], vis_threshold=0.5)\n    \n    sc = len(scores[scores > 0.5])\n    \n    b = boxes[:sc].cpu().numpy()\n    s = scores[:sc].cpu().numpy()\n    \n    bs = []\n    for i, el in enumerate(b):\n        el = list(map(int, el)) \n        el[2] = el[2] - el[0]\n        el[3] = el[3] - el[1]\n        el.insert(0, s[i])      \n        bs.append(el)        \n\n    \n    rl = list(itertools.chain.from_iterable(bs))\n    rs = ' '.join(str(e) for e in rl)\n    \n    \n    dres = {\n            'image_id': row.split('.')[0],\n            'PredictionString': rs\n            }   \n        \n    res.append(dres)     ","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"test_df = pd.DataFrame(res, columns=['image_id', 'PredictionString'])\ntest_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"cd /kaggle/working","execution_count":null,"outputs":[]},{"metadata":{"trusted":false},"cell_type":"code","source":"test_df.to_csv('submission.csv', index=False)","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}