{"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":"# Version\n\n* `v12`: Fold4\n* `v11`: Fold3\n* `v10`: Fold2\n* `v08`: Fold1\n* `v07`: Fold0\n","metadata":{}},{"cell_type":"markdown","source":"# [Training Notebook](https://www.kaggle.com/awsaf49/vinbigdata-cxr-ad-yolov5-14-class)\n* Select `GPU` as the **Accelerator**","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2021-07-02T09:34:52.991654Z","iopub.execute_input":"2021-07-02T09:34:52.991983Z","iopub.status.idle":"2021-07-02T09:34:53.894434Z","shell.execute_reply.started":"2021-07-02T09:34:52.991946Z","shell.execute_reply":"2021-07-02T09:34:53.893706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dim = 512 #1024, 256, 'original'\ntest_dir = f'/kaggle/input/vinbigdata-{dim}-image-dataset/vinbigdata/test'\nweights_dir = '/kaggle/input/vinbigdata-cxr-ad-yolov5-14-class-train/yolov5/runs/train/exp/weights/best.pt'","metadata":{"execution":{"iopub.status.busy":"2021-07-02T09:34:53.896547Z","iopub.execute_input":"2021-07-02T09:34:53.896943Z","iopub.status.idle":"2021-07-02T09:34:53.901275Z","shell.execute_reply.started":"2021-07-02T09:34:53.896898Z","shell.execute_reply":"2021-07-02T09:34:53.900246Z"},"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":{"execution":{"iopub.status.busy":"2021-07-02T09:34:53.902618Z","iopub.execute_input":"2021-07-02T09:34:53.902979Z","iopub.status.idle":"2021-07-02T09:34:53.952154Z","shell.execute_reply.started":"2021-07-02T09:34:53.902941Z","shell.execute_reply":"2021-07-02T09:34:53.951188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# YOLOv5 Stuff","metadata":{}},{"cell_type":"code","source":"shutil.copytree('/kaggle/input/yolov5-official-v31-dataset/yolov5', '/kaggle/working/yolov5')\nos.chdir('/kaggle/working/yolov5') # install dependencies\n\nimport torch\nfrom IPython.display import Image, clear_output  # to display images\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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2021-07-02T09:34:53.953546Z","iopub.execute_input":"2021-07-02T09:34:53.953892Z","iopub.status.idle":"2021-07-02T09:34:56.212994Z","shell.execute_reply.started":"2021-07-02T09:34:53.953854Z","shell.execute_reply":"2021-07-02T09:34:56.21181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{}},{"cell_type":"code","source":"!python detect.py --weights $weights_dir\\\n--img 640\\\n--conf 0.01\\\n--iou 0.4\\\n--source $test_dir\\\n--save-txt --save-conf --exist-ok","metadata":{"_kg_hide-input":false,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-07-02T09:34:56.215569Z","iopub.execute_input":"2021-07-02T09:34:56.215982Z","iopub.status.idle":"2021-07-02T09:39:36.263196Z","shell.execute_reply.started":"2021-07-02T09:34:56.215937Z","shell.execute_reply":"2021-07-02T09:39:36.26197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom mpl_toolkits.axes_grid1 import ImageGrid\nimport numpy as np\nimport random\nimport cv2\nfrom glob import glob\nfrom tqdm import tqdm\n\nfiles = glob('runs/detect/exp/*png')\nfor _ in range(3):\n    row = 4\n    col = 4\n    grid_files = random.sample(files, row*col)\n    images     = []\n    for image_path in tqdm(grid_files):\n        img          = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)\n        images.append(img)\n\n    fig = plt.figure(figsize=(col*5, row*5))\n    grid = ImageGrid(fig, 111,  # similar to subplot(111)\n                     nrows_ncols=(col, row),  # creates 2x2 grid of axes\n                     axes_pad=0.05,  # pad between axes in inch.\n                     )\n\n    for ax, im in zip(grid, images):\n        # Iterating over the grid returns the Axes.\n        ax.imshow(im)\n        ax.set_xticks([])\n        ax.set_yticks([])\n    plt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-07-02T09:39:36.271909Z","iopub.execute_input":"2021-07-02T09:39:36.272823Z","iopub.status.idle":"2021-07-02T09:39:41.825048Z","shell.execute_reply.started":"2021-07-02T09:39:36.272775Z","shell.execute_reply":"2021-07-02T09:39:41.82403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Process Submission","metadata":{}},{"cell_type":"code","source":"def yolo2voc(image_height, image_width, bboxes):\n    \"\"\"\n    yolo => [xmid, ymid, w, h] (normalized)\n    voc  => [x1, y1, x2, y1]\n    \n    \"\"\" \n    bboxes = bboxes.copy().astype(float) # otherwise all value will be 0 as voc_pascal dtype is np.int\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,"execution":{"iopub.status.busy":"2021-07-02T09:39:41.826673Z","iopub.execute_input":"2021-07-02T09:39:41.827012Z","iopub.status.idle":"2021-07-02T09:39:41.838042Z","shell.execute_reply.started":"2021-07-02T09:39:41.826976Z","shell.execute_reply":"2021-07-02T09:39:41.836583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_ids = []\nPredictionStrings = []\n\nfor file_path in tqdm(glob('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    image_ids.append(image_id)\n    PredictionStrings.append(' '.join(bboxes))","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2021-07-02T09:39:41.839908Z","iopub.execute_input":"2021-07-02T09:39:41.840315Z","iopub.status.idle":"2021-07-02T09:39:47.811387Z","shell.execute_reply.started":"2021-07-02T09:39:41.84028Z","shell.execute_reply":"2021-07-02T09:39:47.810522Z"},"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":{"execution":{"iopub.status.busy":"2021-07-02T09:39:47.813048Z","iopub.execute_input":"2021-07-02T09:39:47.813437Z","iopub.status.idle":"2021-07-02T09:39:48.300143Z","shell.execute_reply.started":"2021-07-02T09:39:47.813375Z","shell.execute_reply":"2021-07-02T09:39:48.299283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree('/kaggle/working/yolov5')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2021-07-02T09:42:29.012572Z","iopub.execute_input":"2021-07-02T09:42:29.013016Z","iopub.status.idle":"2021-07-02T09:42:29.033513Z","shell.execute_reply.started":"2021-07-02T09:42:29.012973Z","shell.execute_reply":"2021-07-02T09:42:29.032225Z"},"trusted":true},"execution_count":null,"outputs":[]}]}