{"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":"!nvidia-smi","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":"!pip install --upgrade keras","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%cd /kaggle/working/keras-retinanet/\n\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":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nimport pandas as pd\nimport seaborn as sns\nfrom pylab import rcParams\nimport matplotlib.pyplot as plt\nfrom matplotlib import rc\nfrom pandas.plotting import register_matplotlib_converters\nfrom sklearn.model_selection import train_test_split\nimport urllib\nimport os\nimport csv\nimport cv2\nimport time\nfrom PIL import Image\n\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\n\n%matplotlib inline\n%config InlineBackend.figure_format='retina'\n\nregister_matplotlib_converters()\nsns.set(style='whitegrid', palette='muted', font_scale=1.5)\n\nrcParams['figure.figsize'] = 22, 10\n\nRANDOM_SEED = 42\n\nnp.random.seed(RANDOM_SEED)\ntf.random.set_seed(RANDOM_SEED)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"wheat_df = pd.read_csv('/kaggle/input/global-wheat-detection/train.csv')\nwheat_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"xmin = []\nymin = []\nwidth1 = []\nheight1 = []\nxmax = []\nymax = []\nimagepath = []\nclassname = []\nfor index, row in wheat_df.iterrows():\n    imagepath.append('/kaggle/input/global-wheat-detection/train/'+row['image_id']+'.jpg')\n    i = row['bbox']\n    i = i[1:-1]\n    i = i.split(',')\n    newI = []\n    for j in i:\n        newI.append(int(round(float(j))))\n    xmin.append(newI[0])\n    ymin.append(newI[1])\n    xmax.append(newI[0]+newI[2])\n    ymax.append(newI[1]+newI[3])\n    classname.append('wheat')\nprint(\"Listed all.\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dataset = pd.DataFrame()\ndataset[\"image_name\"] = imagepath\ndataset[\"x_min\"] = xmin\ndataset[\"y_min\"] = ymin\ndataset[\"x_max\"] = xmax\ndataset[\"y_max\"] = ymax\ndataset[\"class_name\"] = classname\n\ndataset.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_image_objects(image_row):\n\n  img_path = image_row.image_name\n  box = [\n    image_row.x_min, image_row.y_min, image_row.x_max, image_row.y_max\n  ]\n\n  image = read_image_bgr(img_path)\n\n  draw = image.copy()\n  draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)\n\n  draw_box(draw, box, color=(255, 255, 0))\n\n  plt.axis('off')\n  plt.imshow(draw)\n  plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_image_objects(dataset.iloc[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df, test_df = train_test_split(\n  dataset, \n  test_size=0.2, \n  random_state=RANDOM_SEED\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ANNOTATIONS_FILE = '/kaggle/working/annotations.csv'\nCLASSES_FILE = '/kaggle/working/classes.csv'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.to_csv(ANNOTATIONS_FILE, index=False, header=None)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"classes = set(['wheat'])\n\nwith open(CLASSES_FILE, 'w') as f:\n  for i, line in enumerate(sorted(classes)):\n    f.write('{},{}\\n'.format(line,i))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n!head /kaggle/working/classes.csv","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!head /kaggle/working/annotations.csv","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"PRETRAINED_MODEL = './snapshots/_pretrained_model.h5'\n\nURL_MODEL = 'https://github.com/fizyr/keras-retinanet/releases/download/0.5.1/resnet50_coco_best_v2.1.0.h5'\nurllib.request.urlretrieve(URL_MODEL, PRETRAINED_MODEL)\n\nprint('Downloaded pretrained model to ' + PRETRAINED_MODEL)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!keras_retinanet/bin/train.py --freeze-backbone --random-transform --weights {PRETRAINED_MODEL} --batch-size 8 --steps 100 --epochs 10 csv /kaggle/working/annotations.csv /kaggle/working/classes.csv","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls snapshots","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_path = os.path.join('snapshots', sorted(os.listdir('snapshots'), reverse=True)[0])\nprint(model_path)\n\nmodel = models.load_model(model_path, backbone_name='resnet50')\nmodel = models.convert_model(model)\n\nlabels_to_names = pd.read_csv(CLASSES_FILE, header=None).T.loc[0].to_dict()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\ndef predict(image):\n  image = preprocess_image(image.copy())\n  image, scale = resize_image(image)\n\n  boxes, scores, labels = model.predict_on_batch(\n    np.expand_dims(image, axis=0)\n  )\n\n  boxes /= scale\n\n  return boxes, scores, labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"THRES_SCORE = 0.3\n\ndef draw_detections(image, boxes, scores, labels):\n  for box, score, label in zip(boxes[0], scores[0], labels[0]):\n    if score < THRES_SCORE:\n        break\n\n    color = label_color(label)\n\n    b = box.astype(int)\n    draw_box(image, b, color=color)\n\n    caption = \"{} {:.3f}\".format(labels_to_names[label], score)\n    draw_caption(image, b, caption)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def show_detected_objects(image_row):\n  img_path = image_row.image_name\n  \n  image = read_image_bgr(img_path)\n\n  boxes, scores, labels = predict(image)\n\n  draw = image.copy()\n  draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)\n\n  true_box = [\n    image_row.x_min, image_row.y_min, image_row.x_max, image_row.y_max\n  ]\n  draw_box(draw, true_box, color=(255, 255, 0))\n\n  draw_detections(draw, boxes, scores, labels)\n\n  plt.axis('off')\n  plt.imshow(draw)\n  plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.head(n=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"show_detected_objects(test_df.iloc[3])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def test_image_bbox(imagepath):\n  image = read_image_bgr(imagepath)\n  boxes, scores, labels = predict(image)\n\n  draw = image.copy()\n  draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)\n  draw_detections(draw, boxes, scores, labels)\n\n  plt.axis('off')\n  plt.imshow(draw)\n  plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_image_bbox('/kaggle/input/global-wheat-detection/test/796707dd7.jpg')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ss = pd.read_csv('/kaggle/input/global-wheat-detection/sample_submission.csv')\nPATH = '/kaggle/input/global-wheat-detection/test/'\nlistofboxes = []\nlistofimgname = []\nfor i in range (0,10):\n    row = ss.iloc[i]\n    img = row['image_id']\n    listofimgname.append(img)\n    image_ext = img + '.jpg'\n    path = os.path.join(PATH,image_ext)\n    image = read_image_bgr(path)\n    boxes, scores, labels = predict(image)\n    finalstring = \"\"\n    for box, score, label in zip(boxes[0], scores[0], labels[0]):\n        finalstring = finalstring + str(score)+ \" \"\n        for j in box:\n            finalstring = finalstring + str(int(round(j))) + \" \"\n    print(finalstring)\n    finalstring = finalstring[:-1]\n    listofboxes.append(finalstring)\nfinaldata = pd.DataFrame()\nfinaldata['image_id'] = listofimgname\nfinaldata['PredictionString'] = listofboxes\nfinaldata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"finaldata.to_csv('/kaggle/working/submission.csv',index=False)","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}