{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Predict bounding box and score for wheat heads","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## YOLOv3 weights\n### Trained offline and shared as dataset https://www.kaggle.com/senthilskm/yolov3-weights\n### How to add weights to input data?\n### 1. Top right under Data\n### 2. Click + Add data\n### 3. Look for https://www.kaggle.com/senthilskm/yolov3-weights\n### 4 Now it is added in /kaggle/input/yolov3-weights","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"## Use YOLOv3 TF2 implementation from https://github.com/YunYang1994/TensorFlow2.0-Examples","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\n\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\n\nimport yolov3.core.utils as utils\nfrom yolov3.core.yolov3 import YOLOv3, decode","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Paths","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"WEIGHTS = '/kaggle/input/yolov3-weights/yolov3'\nTEST_IMAGES = '/kaggle/input/global-wheat-detection/test/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"input_size = 416\n\ninput_layer = tf.keras.layers.Input([input_size, input_size, 3])\nfeature_maps = YOLOv3(input_layer)\n\nbbox_tensors = []\nfor i, fm in enumerate(feature_maps):\n    bbox_tensor = decode(fm, i)\n    bbox_tensors.append(bbox_tensor)\n\nmodel = tf.keras.Model(input_layer, bbox_tensors)\nmodel.load_weights(WEIGHTS)\nmodel.summary()\n\nimages = os.listdir(TEST_IMAGES)\nimage_id = []\npred_string = []\n\nfor image_path in images:\n    original_image = cv2.imread(os.path.join(TEST_IMAGES, image_path))\n    original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)\n    original_image_size = original_image.shape[:2]\n\n    image_data = utils.image_preporcess(np.copy(original_image), [input_size, input_size])\n    image_data = image_data[np.newaxis, ...].astype(np.float32)\n\n    pred_bbox = model.predict(image_data)\n    pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox]\n    pred_bbox = tf.concat(pred_bbox, axis=0)\n    bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.3)\n    bboxes = utils.nms(bboxes, 0.2, method='nms')\n    image_id.append(image_path.split('.')[0])\n    string = ''\n    for box in bboxes:\n        x_min, y_min = box[:2]\n        width, height = (box[2] - box[0], box[3] - box[1])\n        score = round(box[4], 4)\n        string = string + '{sc} {x_min} {y_min} {width} {height} '.format(\n            sc=score, x_min=int(x_min), y_min=int(y_min), width=int(width), height=int(height))\n    pred_string.append(string)\n\nout_df = pd.DataFrame({'image_id': image_id,\n                       'PredictionString': pred_string})\nout_df.to_csv(\"submission.csv\", index=False)","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}