{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/global-wheat-detection/train.csv\")\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import json\ntrain_df = pd.DataFrame()\ntrain_df[\"path\"] = df[\"image_id\"].apply(lambda x: \"/kaggle/input/global-wheat-detection/train/%s.jpg\" % x)\ntrain_df[\"x1\"] = df[\"bbox\"].apply(lambda x: json.loads(x)[0]).astype(\"int\")\ntrain_df[\"y1\"] = df[\"bbox\"].apply(lambda x: json.loads(x)[1]).astype(\"int\")\ntrain_df[\"x2\"] = df[\"bbox\"].apply(lambda x: json.loads(x)[2] + json.loads(x)[0]).astype(\"int\")\ntrain_df[\"y2\"] = df[\"bbox\"].apply(lambda x: json.loads(x)[3] + json.loads(x)[1]).astype(\"int\")\ntrain_df[\"class\"] = df[\"source\"]\ntrain_df.to_csv(\"annotations.csv\", header=False, index=False)\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classnames = [i for i in set(train_df[\"class\"])]\nidx = range(len(classnames))\ntrain_classes = pd.DataFrame()\ntrain_classes[\"class_name\"] = classnames\ntrain_classes[\"class_id\"] = idx\ntrain_classes.to_csv(\"classes.csv\", header=False, index=False)\ntrain_classes.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!git clone https://github.com/fizyr/keras-retinanet.git\n!cd keras-retinanet && python setup.py build_ext --inplace\n!cd keras-retinanet && pip install .\n!cd keras-retinanet && python keras_retinanet/bin/train.py csv","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!cd keras-retinanet && python keras_retinanet/bin/train.py --steps=2000  --backbone=resnet101 --epochs=5 csv ../annotations.csv ../classes.csv","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!cd keras-retinanet && python keras_retinanet/bin/convert_model.py snapshots/resnet101_csv_05.h5  ./model.h5 ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# show images inline\n%matplotlib inline\n\n# automatically reload modules when they have changed\n%load_ext autoreload\n%autoreload 2\n\n# import keras\nimport keras\n\n# import keras_retinanet\nfrom keras_retinanet import models\nfrom keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image\nfrom keras_retinanet.utils.visualization import draw_box, draw_caption\nfrom keras_retinanet.utils.colors import label_color\nfrom keras_retinanet.utils.gpu import setup_gpu\n\n# import miscellaneous modules\nimport matplotlib.pyplot as plt\nimport cv2\nimport os\nimport numpy as np\nimport time\n\n# use this to change which GPU to use\ngpu = 0\n\n# set the modified tf session as backend in keras\nsetup_gpu(gpu)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_path = \"./keras-retinanet/model.h5\"\n\n# load retinanet model\nmodel = models.load_model(model_path, backbone_name='resnet101')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_df = pd.read_csv(\"classes.csv\", header=None)\nlabels_to_names = {}\nfor i in range(len(class_df)):\n    labels_to_names[i] =  class_df[0][i]\n    \nlabels_to_names","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_dir=\"/kaggle/input/global-wheat-detection/test\"\nfiles = os.listdir(base_dir)\nsubmission_df = pd.DataFrame()\nimage_ids = []\nsubmission_strings = []\nfor f in files:\n    image = read_image_bgr(base_dir + \"/\" + f)\n    image_ids.append(f.replace(\".jpg\", \"\"))\n    # copy to draw on\n    draw = image.copy()\n    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)\n\n    # preprocess image for network\n    image = preprocess_image(image)\n    image, scale = resize_image(image)\n\n    # process image\n    start = time.time()\n    boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))\n    print(\"processing time: \", time.time() - start)\n\n    # correct for image scale\n    boxes /= scale\n    subm_txt = \"\"\n    # visualize detections\n    for box, score, label in zip(boxes[0], scores[0], labels[0]):\n        # scores are sorted so we can break\n        if score < 0.5:\n            break\n        \n        color = label_color(label)\n\n        b = box.astype(int)\n        subm_txt += \"%s %s %s %s %s \" % (score, b[0], b[1], b[2]-b[0], b[3]-b[1])\n        draw_box(draw, b, color=color)\n\n        caption = \"{} {:.3f}\".format(labels_to_names[label], score)\n        draw_caption(draw, b, caption)\n\n    submission_strings.append(subm_txt)\n    plt.figure(figsize=(15, 15))\n    plt.axis('off')\n    plt.imshow(draw)\n    plt.show()\n\n\nsubmission_df[\"image_id\"] = image_ids\nsubmission_df[\"PredictionString\"] = submission_strings","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)\n","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}