{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<a id=\"1\"></a>\n# <p style=\"background-color:#000000;font-family:newtimeroman;color:#fff;font-size:120%;text-align:center;border-radius:20px 80px;\"> Train </p>\n### This notebook is a follow-up of [🌟🐟Detection using Keras-RetinaNet-Train Notebook](https://www.kaggle.com/mahipalsingh/detection-using-keras-retinanet-train)","metadata":{}},{"cell_type":"markdown","source":"<a id=\"2\"></a>\n# <p style=\"background-color:#000000;font-family:newtimeroman;color:#fff;font-size:120%;text-align:center;border-radius:20px 80px;\"> Import Modules </p>","metadata":{}},{"cell_type":"code","source":"import pandas as pd\n# show images inline\n%matplotlib inline\nimport keras\nimport tensorflow\n# import miscellaneous modules\nimport matplotlib.pyplot as plt\nimport cv2\nimport os\nimport numpy as np\nimport time\nimport tensorflow as tf\n\n# Create 'RetinaNet' dir for zip extraction\n%mkdir RetinaNet\n\n#%cd RetinaNet/\nfrom distutils.dir_util import copy_tree\n\nf='/kaggle/input/offlineretinanet/'\nt='/kaggle/working/RetinaNet/'\ncopy_tree(f,t)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-12-23T07:37:44.278425Z","iopub.execute_input":"2021-12-23T07:37:44.278789Z","iopub.status.idle":"2021-12-23T07:37:51.506081Z","shell.execute_reply.started":"2021-12-23T07:37:44.278694Z","shell.execute_reply":"2021-12-23T07:37:51.505321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"3\"></a>\n# <p style=\"background-color:#000000;font-family:newtimeroman;color:#fff;font-size:120%;text-align:center;border-radius:20px 80px;\"> Install RetinaNet Offline  </p>","metadata":{}},{"cell_type":"code","source":"#Clone Git Repository\n#!git clone https://github.com/fizyr/keras-retinanet.git\n#%cd keras-retinanet/\n%cd RetinaNet/\n!python setup.py build_ext --inplace\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 import models\n\n!pip install /kaggle/input/creating-whl-file-retinanet/whlfiles/keras_resnet-0.2.0-py2.py3-none-any.whl\nimport keras\nimport keras_resnet","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-12-23T07:37:51.508364Z","iopub.execute_input":"2021-12-23T07:37:51.508637Z","iopub.status.idle":"2021-12-23T07:38:26.374679Z","shell.execute_reply.started":"2021-12-23T07:37:51.508601Z","shell.execute_reply":"2021-12-23T07:38:26.37362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4\"></a>\n# <p style=\"background-color:#000000;font-family:newtimeroman;color:#fff;font-size:120%;text-align:center;border-radius:20px 80px;\"> Load Model  </p>","metadata":{}},{"cell_type":"code","source":"model_path = '/kaggle/input/v2models/bestRetinanet.h5'\n#print(model_path)\n\n# load retinanet model\nmodel = models.load_model(model_path, backbone_name='resnet50')  ## Use backbone as resnet50\nmodel = models.convert_model(model)\n\n# load label to names mapping for visualization purposes\n#labels_to_names = pd.read_csv('/kaggle/input/detection-using-keras-retinanet-train/keras-retinanet/classes.csv',header=None).T.loc[0].to_dict()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-12-23T07:38:26.376659Z","iopub.execute_input":"2021-12-23T07:38:26.376903Z","iopub.status.idle":"2021-12-23T07:38:35.329065Z","shell.execute_reply.started":"2021-12-23T07:38:26.376864Z","shell.execute_reply":"2021-12-23T07:38:35.328178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Annotation file for visualizing result\ndf_extrain_main=pd.read_csv(\"/kaggle/input/v2models/annotation.csv\",names=['image_path','x','y','width','height',\"class\"])\ndf_extrain_main","metadata":{"execution":{"iopub.status.busy":"2021-12-23T07:38:35.33134Z","iopub.execute_input":"2021-12-23T07:38:35.331615Z","iopub.status.idle":"2021-12-23T07:38:35.381751Z","shell.execute_reply.started":"2021-12-23T07:38:35.331582Z","shell.execute_reply":"2021-12-23T07:38:35.380727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"5\"></a>\n# <p style=\"background-color:#000000;font-family:newtimeroman;color:#fff;font-size:120%;text-align:center;border-radius:20px 80px;\"> Visualizing Results  </p>","metadata":{}},{"cell_type":"code","source":"THRES_SCORE = 0.5  # Set Score Threshold Value\n\ndef df_plot_orinal(drawOG,img_path,df):\n    df=df[df['image_path']==img_path]\n    for i,r in df.iterrows():\n        cv2.rectangle(drawOG, (r['x'], r['y']), (r['width'], r['height']), (255,0,0),2)\n    \n\ndef img_inference(img_path):\n  image = read_image_bgr(img_path)\n\n  # copy to draw on\n  draw = image.copy()\n  draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)\n  drawOG = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\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  df_plot_orinal(drawOG,img_path,df_extrain_main)\n  # correct for image scale\n  boxes /= scale\n  #print(boxes)\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      #print(score)\n      if score < THRES_SCORE:\n          continue\n      #print(score)\n      color = label_color(label)\n      b = box.astype(int)\n      draw_box(draw, b, color=color)\n      #caption = \"{} {:.3f}%\".format(labels_to_names[label], score*100)\n    \n  fig = plt.figure(figsize=(20, 20))\n  ax1=fig.add_subplot(1, 2, 1)\n  plt.imshow(draw)\n  ax2=fig.add_subplot(1, 2, 2)\n  plt.imshow(drawOG)\n\n  ax1.title.set_text('Predicted')\n  ax2.title.set_text('Actual')\n  plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-23T07:38:35.383019Z","iopub.execute_input":"2021-12-23T07:38:35.38333Z","iopub.status.idle":"2021-12-23T07:38:35.396128Z","shell.execute_reply.started":"2021-12-23T07:38:35.383277Z","shell.execute_reply":"2021-12-23T07:38:35.395132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=df_extrain_main.sample(n=10)  #Predict on Random 10 Image\nfor i,r in data.iterrows():\n    img_inference(r['image_path'])","metadata":{"execution":{"iopub.status.busy":"2021-12-23T07:38:35.39747Z","iopub.execute_input":"2021-12-23T07:38:35.398187Z","iopub.status.idle":"2021-12-23T07:39:24.609528Z","shell.execute_reply.started":"2021-12-23T07:38:35.39815Z","shell.execute_reply":"2021-12-23T07:39:24.608805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"5\"></a>\n# <p style=\"background-color:#000000;font-family:newtimeroman;color:#fff;font-size:120%;text-align:center;border-radius:20px 80px;\"> Make Submission  </p>","metadata":{}},{"cell_type":"code","source":"%cd /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2021-12-23T07:39:24.610718Z","iopub.execute_input":"2021-12-23T07:39:24.611301Z","iopub.status.idle":"2021-12-23T07:39:24.618017Z","shell.execute_reply.started":"2021-12-23T07:39:24.611268Z","shell.execute_reply":"2021-12-23T07:39:24.617121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import greatbarrierreef\n\nenv = greatbarrierreef.make_env()   # initialize the environment\niter_test = env.iter_test()","metadata":{"execution":{"iopub.status.busy":"2021-12-23T07:39:24.6193Z","iopub.execute_input":"2021-12-23T07:39:24.619773Z","iopub.status.idle":"2021-12-23T07:39:24.652384Z","shell.execute_reply.started":"2021-12-23T07:39:24.619729Z","shell.execute_reply":"2021-12-23T07:39:24.651659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_dict = {\n    'id': [],\n    'prediction_string': [],\n}\n\nfor (image, sample_prediction_df) in iter_test:\n  #image = read_image_bgr(Image.fromarray(image))\n  image = preprocess_image(image[:, :, ::-1])\n  image, scale = resize_image(image)\n\n  bboxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))\n  #bboxes_, scores_, labels_=bboxes[0], scores[0], labels[0]\n  # correct for image scale\n  bboxes /= scale\n  predictions = []\n  for box, score, label in zip(bboxes[0], scores[0], labels[0]):\n      # scores are sorted so we can break\n      if score < THRES_SCORE:\n          continue\n      x_min = int(box[0])  \n      y_min = int(box[1])\n      x_max = int(box[2])\n      y_max = int(box[3])\n\n      bbox_width = x_max - x_min\n      bbox_height = y_max - y_min\n      predictions.append('{:.2f} {} {} {} {}'.format(score, x_min, y_min, bbox_width, bbox_height))\n\n  prediction_str = ' '.join(predictions)\n  sample_prediction_df['annotations'] = prediction_str\n  env.predict(sample_prediction_df)\n\nprint('Prediction:', sample_prediction_df)","metadata":{"execution":{"iopub.status.busy":"2021-12-23T07:39:24.653837Z","iopub.execute_input":"2021-12-23T07:39:24.654253Z","iopub.status.idle":"2021-12-23T07:39:36.250706Z","shell.execute_reply.started":"2021-12-23T07:39:24.654207Z","shell.execute_reply":"2021-12-23T07:39:36.249634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.read_csv('submission.csv')\nsub_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-23T07:39:36.25304Z","iopub.execute_input":"2021-12-23T07:39:36.253306Z","iopub.status.idle":"2021-12-23T07:39:36.264042Z","shell.execute_reply.started":"2021-12-23T07:39:36.253272Z","shell.execute_reply":"2021-12-23T07:39:36.263298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<p style=\"font-size:220%;text-align:center\"> If you find this notebook interesting, please do upvote :) </p>\n\n# <p style=\"text-align:center\"> <img src=\"https://media.giphy.com/media/3oEdva9BUHPIs2SkGk/giphy.gif\"> </p>\n\n<p style=\"font-size:130%;text-align:center\"> Still lot of improvments is remaining!... 🛠⚙ </p>","metadata":{}},{"cell_type":"markdown","source":"<a id=\"5\"></a>\n# <p style=\"background-color:#000000;font-family:newtimeroman;color:#fff;font-size:120%;text-align:center;border-radius:20px 80px;\"> Reference  </p>\n\n## [Creating WHL file - Retinanet](https://www.kaggle.com/akhileshdkapse/creating-whl-file-retinanet)","metadata":{}}]}