{"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":"import numpy as np, pandas as pd\nfrom glob import glob\nimport shutil, os\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import GroupKFold\nfrom tqdm.notebook import tqdm\nimport seaborn as sns","metadata":{"papermill":{"duration":0.900595,"end_time":"2021-03-12T04:43:11.973807","exception":false,"start_time":"2021-03-12T04:43:11.073212","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-12-30T06:59:47.564052Z","iopub.execute_input":"2022-12-30T06:59:47.564914Z","iopub.status.idle":"2022-12-30T06:59:48.964831Z","shell.execute_reply.started":"2022-12-30T06:59:47.56478Z","shell.execute_reply":"2022-12-30T06:59:48.963476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working\n!git clone https://github.com/train255/yolov5","metadata":{"papermill":{"duration":1.939093,"end_time":"2021-03-12T04:43:13.922315","exception":false,"start_time":"2021-03-12T04:43:11.983222","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-12-30T06:59:48.968002Z","iopub.execute_input":"2022-12-30T06:59:48.968523Z","iopub.status.idle":"2022-12-30T06:59:52.026451Z","shell.execute_reply.started":"2022-12-30T06:59:48.968474Z","shell.execute_reply":"2022-12-30T06:59:52.025017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/yolov5\n!pip install -qr requirements.txt --use-feature=2020-resolver\n%cd /kaggle/working/","metadata":{"papermill":{"duration":86.624331,"end_time":"2021-03-12T04:44:40.560176","exception":false,"start_time":"2021-03-12T04:43:13.935845","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-12-30T06:59:52.02865Z","iopub.execute_input":"2022-12-30T06:59:52.029137Z","iopub.status.idle":"2022-12-30T07:00:06.186687Z","shell.execute_reply.started":"2022-12-30T06:59:52.029091Z","shell.execute_reply":"2022-12-30T07:00:06.185134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dim = '256' #1024, 512, 256, 'original'\ntest_dir = f'/kaggle/input/vinbigdata-{dim}-image-dataset/vinbigdata/test'\nweights_dir = '/kaggle/input/weights/best.pt'","metadata":{"papermill":{"duration":0.020893,"end_time":"2021-03-12T04:44:40.595395","exception":false,"start_time":"2021-03-12T04:44:40.574502","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-12-30T07:00:06.189853Z","iopub.execute_input":"2022-12-30T07:00:06.190291Z","iopub.status.idle":"2022-12-30T07:00:06.196706Z","shell.execute_reply.started":"2022-12-30T07:00:06.19025Z","shell.execute_reply":"2022-12-30T07:00:06.195445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(f'/kaggle/input/vinbigdata-{dim}-image-dataset/vinbigdata/test.csv')\ntest_df.head()","metadata":{"papermill":{"duration":0.05735,"end_time":"2021-03-12T04:44:40.6666","exception":false,"start_time":"2021-03-12T04:44:40.60925","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir('/kaggle/working/yolov5')\n\nimport torch\nfrom IPython.display import Image, clear_output  \n\nclear_output()\nprint('Setup complete. Using torch %s %s' % (torch.__version__, torch.cuda.get_device_properties(0) if torch.cuda.is_available() else 'CPU'))","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","papermill":{"duration":0.851311,"end_time":"2021-03-12T04:44:41.561361","exception":false,"start_time":"2021-03-12T04:44:40.71005","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-12-30T07:00:06.354961Z","iopub.status.idle":"2022-12-30T07:00:06.355431Z","shell.execute_reply.started":"2022-12-30T07:00:06.355234Z","shell.execute_reply":"2022-12-30T07:00:06.355254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/yolov5\n!python train.py --weights $weights_dir\\\n--img 640\\\n--conf 0.009\\\n--iou 0.45\\\n--source $test_dir\\\n--save-conf --exist-ok","metadata":{"execution":{"iopub.status.busy":"2022-12-30T07:00:06.357265Z","iopub.status.idle":"2022-12-30T07:00:06.357866Z","shell.execute_reply.started":"2022-12-30T07:00:06.357567Z","shell.execute_reply":"2022-12-30T07:00:06.357595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd /kaggle/working/yolov5\n!python detect.py --weights $weights_dir\\\n--img 640\\\n--conf 0.009\\\n--iou 0.45\\\n--source $test_dir\\\n--save-txt --save-conf --exist-ok","metadata":{"papermill":{"duration":743.814345,"end_time":"2021-03-12T04:57:05.420672","exception":false,"start_time":"2021-03-12T04:44:41.606327","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-12-30T07:00:06.359553Z","iopub.status.idle":"2022-12-30T07:00:06.360159Z","shell.execute_reply.started":"2022-12-30T07:00:06.359839Z","shell.execute_reply":"2022-12-30T07:00:06.359867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def yolo2voc(image_height, image_width, bboxes):#for edges of the box\n    \"\"\"\n    yolo => [xmid, ymid, w, h] (normalized)\n    voc  => [x1, y1, x2, y1]\n    \n    \"\"\" \n    bboxes = bboxes.copy().astype(float) \n    \n    bboxes[..., [0, 2]] = bboxes[..., [0, 2]]* image_width\n    bboxes[..., [1, 3]] = bboxes[..., [1, 3]]* image_height\n    \n    bboxes[..., [0, 1]] = bboxes[..., [0, 1]] - bboxes[..., [2, 3]]/2\n    bboxes[..., [2, 3]] = bboxes[..., [0, 1]] + bboxes[..., [2, 3]]\n    \n    return bboxes","metadata":{"_kg_hide-input":true,"papermill":{"duration":0.861735,"end_time":"2021-03-12T04:57:08.853745","exception":false,"start_time":"2021-03-12T04:57:07.99201","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-12-30T07:00:06.361837Z","iopub.status.idle":"2022-12-30T07:00:06.362452Z","shell.execute_reply.started":"2022-12-30T07:00:06.362158Z","shell.execute_reply":"2022-12-30T07:00:06.362188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_ids = []#preprocessing to get image id (rm extensions) ->use yolo2voc, 14 diseases, which one which is\nPredictionStrings = []\n\nfor file_path in tqdm(glob('/kaggle/working/yolov5/runs/detect/exp/labels/*txt')):\n    image_id = file_path.split('/')[-1].split('.')[0]\n    w, h = test_df.loc[test_df.image_id==image_id,['width', 'height']].values[0]\n    f = open(file_path, 'r')\n    data = np.array(f.read().replace('\\n', ' ').strip().split(' ')).astype(np.float32).reshape(-1, 6)\n    data = data[:, [0, 5, 1, 2, 3, 4]]\n    bboxes = list(np.round(np.concatenate((data[:, :2], np.round(yolo2voc(h, w, data[:, 2:]))), axis =1).reshape(-1), 1).astype(str))\n    for idx in range(len(bboxes)):\n        bboxes[idx] = str(int(float(bboxes[idx]))) if idx%6!=1 else bboxes[idx]\n    if str(bboxes[0]) != \"15\":\n        image_ids.append(image_id)\n        if str(bboxes[0]) != \"14\":\n            PredictionStrings.append(' '.join(bboxes))\n        else:\n            PredictionStrings.append(\"14 1 0 0 1 1\")","metadata":{"_kg_hide-input":false,"papermill":{"duration":7.054887,"end_time":"2021-03-12T04:57:16.738768","exception":false,"start_time":"2021-03-12T04:57:09.683881","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-12-30T07:00:06.364257Z","iopub.status.idle":"2022-12-30T07:00:06.364697Z","shell.execute_reply.started":"2022-12-30T07:00:06.364497Z","shell.execute_reply":"2022-12-30T07:00:06.364517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df = pd.DataFrame({'image_id':image_ids,\n                        'PredictionString':PredictionStrings})\nsub_df = pd.merge(test_df, pred_df, on = 'image_id', how = 'left').fillna(\"14 1 0 0 1 1\")\nsub_df = sub_df[['image_id', 'PredictionString']]\nsub_df.to_csv('/kaggle/working/submission.csv',index = False)\nsub_df.tail()","metadata":{"papermill":{"duration":1.252349,"end_time":"2021-03-12T04:57:18.825011","exception":false,"start_time":"2021-03-12T04:57:17.572662","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-12-30T07:00:06.368352Z","iopub.status.idle":"2022-12-30T07:00:06.368809Z","shell.execute_reply.started":"2022-12-30T07:00:06.368603Z","shell.execute_reply":"2022-12-30T07:00:06.368623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree('/kaggle/working/yolov5')   #shell utilities","metadata":{"_kg_hide-input":true,"papermill":{"duration":1.405444,"end_time":"2021-03-12T04:57:21.065579","exception":false,"start_time":"2021-03-12T04:57:19.660135","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-12-30T07:00:06.370647Z","iopub.status.idle":"2022-12-30T07:00:06.371367Z","shell.execute_reply.started":"2022-12-30T07:00:06.370971Z","shell.execute_reply":"2022-12-30T07:00:06.371006Z"},"trusted":true},"execution_count":null,"outputs":[]}]}