{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\n!pwd\n!ls","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"#!cp -r ../input/kerasretinanet/keras-retinanet/* .\n#!pip install keras-resnet\n#!pip install . --user\n#!python setup.py build_ext --inplace","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from 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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp ../input/resnet50pretrained/* ./snapshots/\nmodel_path = os.path.join('snapshots', sorted(os.listdir('snapshots'), reverse=True)[0])\n\nmodel = models.load_model(model_path, backbone_name='resnet50')\nmodel = models.convert_model(model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def predict(image):\n    image = preprocess_image(image.copy())\n    image, scale = resize_image(image)\n    print(scale)\n    boxes, scores, labels = model.predict_on_batch(np.expand_dims(image, axis=0))\n\n    boxes /= scale\n\n    return boxes, scores, labels","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    img_path = image_row\n  \n    image = read_image_bgr(img_path)\n\n    boxes, scores, labels = predict(image)\n  \n    return boxes, scores, labels\n  ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"\"\"\"\nimg_path = \"/kaggle/input/globalwheatdetection/test/cc3532ff6.jpg\"\nboxes, scores, labels = show_detected_objects(img_path)\nboxes, scores, labels = boxes[0], scores[0], labels[0]\nprint(boxes.shape)\nprint(scores.shape)\n\nsc = len(scores[scores > 0.5])\nsc = 5\nb = boxes[:sc, :]\ns = scores[:sc]\nbs = []\nfor i, el in enumerate(b):\n    el = el.astype(int)\n    el[2] = el[2] - el[0] # Convert to x1, y1, w, h\n    el[3] = el[3] - el[1]\n    el = list(el)\n    el.insert(0, s[i])\n    print(el)\n    bs.append(el)\nrl = list(itertools.chain.from_iterable(bs))\nprint(rl)\nrs = ' '.join(str(e) for e in rl)\nprint(rs)\ndres = {\n            'image_id': img_path.split('.')[0],\n            'PredictionString': rs\n            }\nres = []\nres.append(dres)\nprint(res)\ntest_df = pd.DataFrame(res, columns=['image_id', 'PredictionString'])\ntest_df.head()\n\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TEST_PATH = '/kaggle/input/global-wheat-detection/test/'\ntest_ids = os.listdir(TEST_PATH)\nimport itertools","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"res = []\nfor idx, row in enumerate(test_ids):\n    img_path = TEST_PATH + row\n    boxes, scores, labels = show_detected_objects(img_path)\n    boxes, scores, labels = boxes[0], scores[0], labels[0]\n\n    sc = len(scores[scores > 0.5])\n    b = boxes[:sc,:]\n    s = scores[:sc]\n    \n    bs = []\n    for i, el in enumerate(b):\n        el = el.astype(int)\n        el[2] = el[2] - el[0] # Convert to x1, y1, w, h\n        el[3] = el[3] - el[1]\n        el = list(el)\n        el.insert(0, s[i])\n        bs.append(el)\n    \n    rl = list(itertools.chain.from_iterable(bs))\n    rs = ' '.join(str(e) for e in rl)\n    dres = {\n            'image_id': row.split('.')[0],\n            'PredictionString': rs\n            }\n    res.append(dres)\ntest_df = pd.DataFrame(res, columns=['image_id', 'PredictionString'])\ntest_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_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}