{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Library details - https://github.com/Tessellate-Imaging/Monk_Object_Detection","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Training code - https://github.com/Tessellate-Imaging/Monk_Object_Detection/tree/master/application_model_zoo\n    \n### - See Example 18","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Installation \n## Individual files to be installed since kernel cannot use internet","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport sys","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir(\"/kaggle/input/kaggle-monk-pytorch-efficientdet/kaggle_monk_pytorch_efficientdet\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir(\"/kaggle/input/global-wheat-detection\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! cp -r /kaggle/input/kaggle-monk-pytorch-efficientdet/kaggle_monk_pytorch_efficientdet .","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! cd kaggle_monk_pytorch_efficientdet/cocoapi/PythonAPI/ && pip install -e .","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sys.path.append(\"kaggle_monk_pytorch_efficientdet/cocoapi/PythonAPI/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! pip install kaggle_monk_pytorch_efficientdet/webcolors-1.11.1-py3-none-any.whl","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Inference","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"## Import\n\nimport os\nimport sys\nsys.path.append(\"kaggle_monk_pytorch_efficientdet/Monk_Object_Detection/10_pytorch_efficientdet/lib/\");\n\nfrom infer_detector import Infer\n\ngtf = Infer();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## Load model\n\nmodel_path = \"kaggle_monk_pytorch_efficientdet/trained_weights/custom/efficientdet-d4_trained.pth\";\nclasses_list = [\"wheat\"];\ngtf.load_model(model_path, classes_list, use_gpu=True);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_list = os.listdir(\"/kaggle/input/global-wheat-detection/test/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_path = \"/kaggle/input/global-wheat-detection/test/\" + img_list[0];\nscores, labels, boxes = gtf.predict(img_path, threshold=0.3);\nfrom IPython.display import Image\nImage(filename='output.jpg') ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Run in loop for for submission\n\nfrom tqdm import tqdm\ncombined = [];\nfor i in tqdm(range(len(img_list))):\n    img_id = img_list[i].split(\".\")[0];\n    img_path = \"/kaggle/input/global-wheat-detection/test/\" + img_list[i];\n    scores, labels, boxes = gtf.predict(img_path, threshold=0.3);\n    \n    wr = \"\";\n    \n    for j in range(len(scores)):\n        score = float(scores[j])\n        x1 = int(boxes[j][0]);\n        y1 = int(boxes[j][1]);\n        x2 = int(boxes[j][2]);\n        y2 = int(boxes[j][3]);\n        w = x2-x1;\n        h = y2-y1;\n        #print(type(score), type(x1), type(y1), type(w), type(h))\n        wr += str(score) + \" \" + str(x1) + \" \" + str(y1) + \" \" + str(w) + \" \" + str(h) + \" \";\n    wr = wr[:len(wr)-1];\n    combined.append([img_id, wr]);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\n\ndf = pd.DataFrame(combined, columns = ['image_id', 'PredictionString']);\ndf.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! rm -r kaggle_monk_pytorch_efficientdet","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.iloc[0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir(\"./\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}