{"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":"code","source":"from pathlib import Path\nimport os\nimport pandas as pd\nfrom  shutil import copyfile\nfrom tqdm import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-29T18:35:47.30794Z","iopub.execute_input":"2021-07-29T18:35:47.308306Z","iopub.status.idle":"2021-07-29T18:35:47.319766Z","shell.execute_reply.started":"2021-07-29T18:35:47.308227Z","shell.execute_reply":"2021-07-29T18:35:47.318977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Run `detect.py` script for inference","metadata":{}},{"cell_type":"code","source":"input_path = '../input/global-wheat-detection/test'\nos.makedirs(input_path, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T18:35:47.334952Z","iopub.execute_input":"2021-07-29T18:35:47.335212Z","iopub.status.idle":"2021-07-29T18:35:47.341913Z","shell.execute_reply.started":"2021-07-29T18:35:47.335189Z","shell.execute_reply":"2021-07-29T18:35:47.341056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# For testing large datasets\n# for file in os.listdir('../input/global-wheat-detection/test'):\n#     copyfile(f'../input/global-wheat-detection/test/{file}', f'./test_set/{file}')\n\n# for file in os.listdir('../input/mnistasjpg/testSample/testSample'):\n#     copyfile(f'../input/mnistasjpg/testSample/testSample/{file}', f'./test_set/{file}')","metadata":{"execution":{"iopub.status.busy":"2021-07-29T18:35:47.372044Z","iopub.execute_input":"2021-07-29T18:35:47.372288Z","iopub.status.idle":"2021-07-29T18:35:47.376983Z","shell.execute_reply.started":"2021-07-29T18:35:47.372265Z","shell.execute_reply":"2021-07-29T18:35:47.37621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python ../input/yolov5ultralytics/yolov5/detect.py --source {input_path} --weights ../input/yolov5-wheat-weights/runs/train/wheat-detector2/weights/best.pt --imgsz 1024 --save-txt","metadata":{"execution":{"iopub.status.busy":"2021-07-29T18:35:47.39677Z","iopub.execute_input":"2021-07-29T18:35:47.397041Z","iopub.status.idle":"2021-07-29T18:35:58.995758Z","shell.execute_reply.started":"2021-07-29T18:35:47.397019Z","shell.execute_reply":"2021-07-29T18:35:58.994846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Denormalize inferences","metadata":{}},{"cell_type":"code","source":"final_path = os.listdir('./runs/detect')[-1]\noutput_path = rf'runs/detect/{final_path}/labels'\noutput = pd.DataFrame(columns=['image_id', 'PredictionString'])\n\ndef denormalize(label):\n    label = label[2:-1]\n    label_lst = label.split(' ')\n    label_float_lst = [int(float(i)*1024) for i in label_lst]\n    xc, yc, w, h = label_float_lst\n    label_float_lst = map(lambda x: str(int(x)), [xc - w/2, yc - h/2, w, h])\n    denormalized_label_str = (' ').join(label_float_lst)\n    label = '0.5 ' + denormalized_label_str\n    return label\n\nfor file in os.listdir(input_path):\n    image_id = Path(file).stem\n    try:\n        with open(output_path + f\"/{image_id}.txt\", \"r\") as file:\n            content = file.readlines()\n        denormalized_labels = [denormalize(line) for line in content]\n        labels = (' ').join(denormalized_labels)\n        output = output.append({'image_id': image_id, 'PredictionString': labels}, ignore_index=True)\n    except FileNotFoundError:\n        output = output.append({'image_id': image_id, 'PredictionString': ''}, ignore_index=True)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T18:55:29.56686Z","iopub.execute_input":"2021-07-29T18:55:29.567284Z","iopub.status.idle":"2021-07-29T18:55:29.629045Z","shell.execute_reply.started":"2021-07-29T18:55:29.567245Z","shell.execute_reply":"2021-07-29T18:55:29.628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create output file","metadata":{}},{"cell_type":"code","source":"print('made it here!!!')\nprint(output.head())","metadata":{"execution":{"iopub.status.busy":"2021-07-29T18:35:59.126241Z","iopub.execute_input":"2021-07-29T18:35:59.126573Z","iopub.status.idle":"2021-07-29T18:35:59.14008Z","shell.execute_reply.started":"2021-07-29T18:35:59.12654Z","shell.execute_reply":"2021-07-29T18:35:59.139311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r runs\noutput.to_csv('submission.csv', index=False)\nprint('finished!')","metadata":{"execution":{"iopub.status.busy":"2021-07-29T18:35:59.142778Z","iopub.execute_input":"2021-07-29T18:35:59.143078Z","iopub.status.idle":"2021-07-29T18:35:59.150995Z","shell.execute_reply.started":"2021-07-29T18:35:59.143053Z","shell.execute_reply":"2021-07-29T18:35:59.149817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'submission.csv' in os.listdir('.')","metadata":{"execution":{"iopub.status.busy":"2021-07-29T18:35:59.152567Z","iopub.execute_input":"2021-07-29T18:35:59.153021Z","iopub.status.idle":"2021-07-29T18:35:59.161076Z","shell.execute_reply.started":"2021-07-29T18:35:59.152987Z","shell.execute_reply":"2021-07-29T18:35:59.160242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from matplotlib import pyplot as plt\n# from matplotlib import patches\n# import numpy as np\n# from PIL import Image","metadata":{"execution":{"iopub.status.busy":"2021-07-29T18:35:59.162386Z","iopub.execute_input":"2021-07-29T18:35:59.162884Z","iopub.status.idle":"2021-07-29T18:35:59.169883Z","shell.execute_reply.started":"2021-07-29T18:35:59.162844Z","shell.execute_reply":"2021-07-29T18:35:59.169067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with open('./runs/detect/exp/labels/51b3e36ab.txt', 'r') as file:\n#     content = file.read()\n# str_boxes = [box[2:] for box in content.split('\\n')][:-1]\n# int_boxes = [[int(float(x)*1024) for x in str_box.split(' ')] for str_box in str_boxes]\n# int_boxes\n\n# fig, ax = plt.subplots()\n# for box in int_boxes:\n#     xc, yc, w, h = box\n#     x = xc - w/2\n#     y = yc - h/2\n#     ax.add_patch(patches.Rectangle((x, y), w, h, linewidth=1, edgecolor='r', facecolor='none'))\n# ax.imshow(np.array(Image.open('../input/global-wheat-detection/test/51b3e36ab.jpg')))","metadata":{"execution":{"iopub.status.busy":"2021-07-29T18:46:19.689603Z","iopub.execute_input":"2021-07-29T18:46:19.689985Z","iopub.status.idle":"2021-07-29T18:46:20.193621Z","shell.execute_reply.started":"2021-07-29T18:46:19.689949Z","shell.execute_reply":"2021-07-29T18:46:20.192844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# for i, row in output.iterrows():\n#     fig, ax = plt.subplots()\n#     image_id = row.image_id\n#     bbox = row.PredictionString\n# #     with open(f'./runs/detect/exp/labels/{image_id}.txt', 'r') as file:\n# #         content = file.read()\n# #     str_boxes = [box[2:] for box in content.split('\\n')][:-1]\n# #     int_boxes = [[int(float(x)*1024) for x in str_box.split(' ')] for str_box in str_boxes]\n# #     int_boxes\n#     bboxes_int = [int(n) for i, n in enumerate(bbox.split(' ')) if i % 5 != 0] \n#     bboxes = [bboxes_int[i:i+4] for i in range(0, len(bboxes_int)-5, 4)]\n#     for box in bboxes:\n#         xc, yc, w, h = box;\n#         x = xc - w/2\n#         y = yc - h/2\n#         ax.add_patch(patches.Rectangle((x, y), w, h, linewidth=1, edgecolor='r', facecolor='none'))\n#     ax.imshow(np.array(Image.open(f'{input_path}/{image_id}.jpg')))\n#     plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T18:48:13.056439Z","iopub.execute_input":"2021-07-29T18:48:13.056763Z","iopub.status.idle":"2021-07-29T18:48:15.885805Z","shell.execute_reply.started":"2021-07-29T18:48:13.056733Z","shell.execute_reply":"2021-07-29T18:48:15.885051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}