{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\n\ndf_labels = pd.read_csv(\"/kaggle/input/global-wheat-detection/train.csv\")\ndf_labels.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"pic_ids, bboxes = [], []\nfor img in os.listdir(\"/kaggle/input/global-wheat-detection/train\"):\n    pic_id = img[:-4]\n    bbox = df_labels.loc[df_labels['image_id'] == pic_id][\"bbox\"]\n    \n    pic_ids.append(pic_id)\n    bboxes.append(bbox)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Display boxes on pictures.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from ast import literal_eval\n\ndef draw_bboxes(img, bbox):\n    for box in bbox:\n        box = literal_eval(box)\n        x, y, w, h = [int(n) for n in box]\n        cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)\n        \n    return img","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\nfrom matplotlib import rcParams\n\nrcParams[\"figure.figsize\"] = 15, 15\n\nfor i in range(4):\n    pic = pic_ids[i]\n    bbox = bboxes[i].values\n    \n    path = f\"/kaggle/input/global-wheat-detection/train/{pic}.jpg\"\n    pic = cv2.imread(path)\n    \n    pic = draw_bboxes(pic, bbox)\n    \n    plt.imshow(pic)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import csv\n\nimg_path = \"/kaggle/input/global-wheat-detection/train\"\n\nwith open(\"img.csv\", \"w\") as f:\n    for i in range(len(pic_ids)):\n        bbox = bboxes[i].values\n        for box in bbox:\n            box = literal_eval(box)\n            box = [int(b) for b in box]\n            try:\n                a, b, c, d = box[0], box[1], box[0]+box[2], box[1]+box[3]\n                s = f\"{img_path}/{pic_ids[i]}.jpg,{a},{b},{c},{d},wheat\\n\"\n    #             s = f\"{pic_ids[i]};{b}\\n\"\n\n            except:\n                continue\n            f.write(s)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_df = pd.read_csv(\"img.csv\")\nsample_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"with open(\"classes.csv\", \"w\") as f:\n    f.write(\"wheat,0\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"! wget 'https://github.com/fizyr/keras-retinanet/releases/download/0.5.1/resnet50_coco_best_v2.1.0.h5'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!git clone https://github.com/fizyr/keras-retinanet.git","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%cd keras-retinanet\n! pip install .","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!python setup.py build_ext --inplace","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## IMPORTANT!\nYou have to downgrade keras to version 2.3.1. It is necessary for the code to work.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"! pip install keras==2.3.1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!keras_retinanet/bin/train.py --freeze-backbone --random-transform --weights \"../resnet50_coco_best_v2.1.0.h5\" --lr 1e-3 --batch-size 8 --steps 100 --epochs 5  csv ../img.csv ../classes.csv","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%cd snapshots\nos.listdir(os.getcwd())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras_retinanet.models import load_model\nfrom keras_retinanet.utils.image import preprocess_image, resize_image\nfrom keras_retinanet import models\n\nmodel = load_model(\"resnet50_csv_01.h5\", backbone_name='resnet50')\nmodel = models.convert_model(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras_retinanet.utils.visualization import draw_box, draw_caption\nfrom keras_retinanet.utils.colors import label_color\n\ndef draw_boxes(img_id, img, boxes, scores, labels):\n    for box, score, label in zip(boxes[0], scores[0], labels[0]):\n        if score < 0.5:\n            break\n        box = [int(b) for b in box]\n        \n        submission.append([img_id, score, box])\n        \n        draw_box(img, box, color=label_color(label))\n        score = \"{:.3f}\".format(score)\n        draw_caption(img, box, score)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_path = \"/kaggle/input/global-wheat-detection/test\"\ntest_imgs  = [img for img in os.listdir(test_path)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%cd ..\n%cd ..\n%mkdir detected\n%cd detected","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = []\n\nfor i in range(len(test_imgs)):\n    pic_path = test_path + \"/\" + test_imgs[i]\n    img = cv2.imread(pic_path)\n    pic = preprocess_image(img)\n    \n    box, score, label = model.predict_on_batch(np.expand_dims(pic, axis=0))\n    draw_boxes(test_imgs[i], img, box, score, label)\n    \n    plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))\n    plt.savefig(test_imgs[i], )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df = pd.DataFrame(submission)\ndf.columns = [\"img\", \"prediction\", \"bbox\"]\ndf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%cd ../..","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df.to_csv(\"wheat_detected.csv\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# CONTINUATION [HERE](https://www.kaggle.com/imakaruamikurah/detect-wheat-results)","execution_count":null},{"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}