{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":24800,"databundleVersionId":1831594,"sourceType":"competition"},{"sourceId":1799839,"sourceType":"datasetVersion","datasetId":1069682},{"sourceId":1800066,"sourceType":"datasetVersion","datasetId":1069809},{"sourceId":1800067,"sourceType":"datasetVersion","datasetId":1069810},{"sourceId":1800777,"sourceType":"datasetVersion","datasetId":1069787},{"sourceId":1800778,"sourceType":"datasetVersion","datasetId":1070222},{"sourceId":1800825,"sourceType":"datasetVersion","datasetId":1070245},{"sourceId":8552898,"sourceType":"datasetVersion","datasetId":5111137}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!git clone https://github.com/WongKinYiu/yolov9.git","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"papermill":{"duration":9.633907,"end_time":"2021-01-01T09:44:53.448657","exception":false,"start_time":"2021-01-01T09:44:43.81475","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T07:11:33.154381Z","iopub.execute_input":"2024-05-30T07:11:33.154718Z","iopub.status.idle":"2024-05-30T07:11:34.087136Z","shell.execute_reply.started":"2024-05-30T07:11:33.154691Z","shell.execute_reply":"2024-05-30T07:11:34.086205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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.926929,"end_time":"2021-01-01T09:44:54.403588","exception":false,"start_time":"2021-01-01T09:44:53.476659","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T07:11:34.089055Z","iopub.execute_input":"2024-05-30T07:11:34.089359Z","iopub.status.idle":"2024-05-30T07:11:36.380439Z","shell.execute_reply.started":"2024-05-30T07:11:34.089331Z","shell.execute_reply":"2024-05-30T07:11:36.379486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dim = 512 \nfold = 4","metadata":{"execution":{"iopub.status.busy":"2024-05-30T07:11:36.381662Z","iopub.execute_input":"2024-05-30T07:11:36.38213Z","iopub.status.idle":"2024-05-30T07:11:36.38808Z","shell.execute_reply.started":"2024-05-30T07:11:36.382098Z","shell.execute_reply":"2024-05-30T07:11:36.387089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(f'../input/vinbigdata-{dim}-image-dataset/vinbigdata/train.csv')\ntrain_df.head()","metadata":{"papermill":{"duration":0.262045,"end_time":"2021-01-01T09:44:54.691965","exception":false,"start_time":"2021-01-01T09:44:54.42992","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T07:11:36.390528Z","iopub.execute_input":"2024-05-30T07:11:36.390931Z","iopub.status.idle":"2024-05-30T07:11:36.659958Z","shell.execute_reply.started":"2024-05-30T07:11:36.390901Z","shell.execute_reply":"2024-05-30T07:11:36.658944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['image_path'] = f'/kaggle/input/vinbigdata-{dim}-image-dataset/vinbigdata/train/'+train_df.image_id+('.png' if dim!='original' else '.jpg')\ntrain_df.head()","metadata":{"papermill":{"duration":0.086788,"end_time":"2021-01-01T09:44:54.805857","exception":false,"start_time":"2021-01-01T09:44:54.719069","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T07:11:36.661728Z","iopub.execute_input":"2024-05-30T07:11:36.662085Z","iopub.status.idle":"2024-05-30T07:11:36.712025Z","shell.execute_reply.started":"2024-05-30T07:11:36.662052Z","shell.execute_reply":"2024-05-30T07:11:36.711196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df[train_df.class_id!=14].reset_index(drop = True)","metadata":{"papermill":{"duration":0.05543,"end_time":"2021-01-01T09:44:54.944088","exception":false,"start_time":"2021-01-01T09:44:54.888658","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T07:11:36.715223Z","iopub.execute_input":"2024-05-30T07:11:36.715508Z","iopub.status.idle":"2024-05-30T07:11:36.73344Z","shell.execute_reply.started":"2024-05-30T07:11:36.715483Z","shell.execute_reply":"2024-05-30T07:11:36.732501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Pre-Processing","metadata":{"papermill":{"duration":0.027303,"end_time":"2021-01-01T09:44:54.999199","exception":false,"start_time":"2021-01-01T09:44:54.971896","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_df['x_min'] = train_df.apply(lambda row: (row.x_min)/row.width, axis =1)\ntrain_df['y_min'] = train_df.apply(lambda row: (row.y_min)/row.height, axis =1)\n\ntrain_df['x_max'] = train_df.apply(lambda row: (row.x_max)/row.width, axis =1)\ntrain_df['y_max'] = train_df.apply(lambda row: (row.y_max)/row.height, axis =1)\n\ntrain_df['x_mid'] = train_df.apply(lambda row: (row.x_max+row.x_min)/2, axis =1)\ntrain_df['y_mid'] = train_df.apply(lambda row: (row.y_max+row.y_min)/2, axis =1)\n\ntrain_df['w'] = train_df.apply(lambda row: (row.x_max-row.x_min), axis =1)\ntrain_df['h'] = train_df.apply(lambda row: (row.y_max-row.y_min), axis =1)\n\ntrain_df['area'] = train_df['w']*train_df['h']\ntrain_df.head()","metadata":{"papermill":{"duration":7.821668,"end_time":"2021-01-01T09:45:02.854149","exception":false,"start_time":"2021-01-01T09:44:55.032481","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T07:11:36.734539Z","iopub.execute_input":"2024-05-30T07:11:36.73481Z","iopub.status.idle":"2024-05-30T07:11:44.068891Z","shell.execute_reply.started":"2024-05-30T07:11:36.734786Z","shell.execute_reply":"2024-05-30T07:11:44.068016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"features = ['x_min', 'y_min', 'x_max', 'y_max', 'x_mid', 'y_mid', 'w', 'h', 'area']\nX = train_df[features]\ny = train_df['class_id']\nX.shape, y.shape","metadata":{"papermill":{"duration":0.040387,"end_time":"2021-01-01T09:45:02.923416","exception":false,"start_time":"2021-01-01T09:45:02.883029","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T07:11:44.070256Z","iopub.execute_input":"2024-05-30T07:11:44.07064Z","iopub.status.idle":"2024-05-30T07:11:44.080887Z","shell.execute_reply.started":"2024-05-30T07:11:44.070606Z","shell.execute_reply":"2024-05-30T07:11:44.080032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_ids, class_names = list(zip(*set(zip(train_df.class_id, train_df.class_name))))\nclasses = list(np.array(class_names)[np.argsort(class_ids)])\nclasses = list(map(lambda x: str(x), classes))\nclasses","metadata":{"papermill":{"duration":0.050418,"end_time":"2021-01-01T09:45:03.002944","exception":false,"start_time":"2021-01-01T09:45:02.952526","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T07:11:44.081932Z","iopub.execute_input":"2024-05-30T07:11:44.082214Z","iopub.status.idle":"2024-05-30T07:11:44.103594Z","shell.execute_reply.started":"2024-05-30T07:11:44.082191Z","shell.execute_reply":"2024-05-30T07:11:44.102696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Split","metadata":{"papermill":{"duration":0.052492,"end_time":"2021-01-01T09:47:56.110766","exception":false,"start_time":"2021-01-01T09:47:56.058274","status":"completed"},"tags":[]}},{"cell_type":"code","source":"gkf  = GroupKFold(n_splits = 5)\ntrain_df['fold'] = -1\nfor fold, (train_idx, val_idx) in enumerate(gkf.split(train_df, groups = train_df.image_id.tolist())):\n    train_df.loc[val_idx, 'fold'] = fold\ntrain_df.head()","metadata":{"papermill":{"duration":0.134603,"end_time":"2021-01-01T09:47:56.297774","exception":false,"start_time":"2021-01-01T09:47:56.163171","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T07:11:44.156367Z","iopub.execute_input":"2024-05-30T07:11:44.156634Z","iopub.status.idle":"2024-05-30T07:11:44.239745Z","shell.execute_reply.started":"2024-05-30T07:11:44.156601Z","shell.execute_reply":"2024-05-30T07:11:44.238918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_files = []\nval_files   = []\nval_files += list(train_df[train_df.fold==fold].image_path.unique())\ntrain_files += list(train_df[train_df.fold!=fold].image_path.unique())\nlen(train_files), len(val_files)","metadata":{"papermill":{"duration":0.086817,"end_time":"2021-01-01T09:47:56.443789","exception":false,"start_time":"2021-01-01T09:47:56.356972","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T07:11:44.240686Z","iopub.execute_input":"2024-05-30T07:11:44.240952Z","iopub.status.idle":"2024-05-30T07:11:44.26574Z","shell.execute_reply.started":"2024-05-30T07:11:44.24093Z","shell.execute_reply":"2024-05-30T07:11:44.264917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Copying Files","metadata":{"papermill":{"duration":0.083752,"end_time":"2021-01-01T09:47:56.584924","exception":false,"start_time":"2021-01-01T09:47:56.501172","status":"completed"},"tags":[]}},{"cell_type":"code","source":"os.makedirs('/kaggle/working/vinbigdata/labels/train', exist_ok = True)\nos.makedirs('/kaggle/working/vinbigdata/labels/val', exist_ok = True)\nos.makedirs('/kaggle/working/vinbigdata/images/train', exist_ok = True)\nos.makedirs('/kaggle/working/vinbigdata/images/val', exist_ok = True)\nlabel_dir = '/kaggle/input/vinbigdata-yolo-labels-dataset/labels'\nfor file in tqdm(train_files):\n    shutil.copy(file, '/kaggle/working/vinbigdata/images/train')\n    filename = file.split('/')[-1].split('.')[0]\n    shutil.copy(os.path.join(label_dir, filename+'.txt'), '/kaggle/working/vinbigdata/labels/train')\n    \nfor file in tqdm(val_files):\n    shutil.copy(file, '/kaggle/working/vinbigdata/images/val')\n    filename = file.split('/')[-1].split('.')[0]\n    shutil.copy(os.path.join(label_dir, filename+'.txt'), '/kaggle/working/vinbigdata/labels/val')","metadata":{"papermill":{"duration":124.654777,"end_time":"2021-01-01T09:50:01.331041","exception":false,"start_time":"2021-01-01T09:47:56.676264","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T11:36:40.205933Z","iopub.execute_input":"2024-05-30T11:36:40.206778Z","iopub.status.idle":"2024-05-30T11:36:58.087205Z","shell.execute_reply.started":"2024-05-30T11:36:40.206744Z","shell.execute_reply":"2024-05-30T11:36:58.086357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get Class Name","metadata":{"papermill":{"duration":0.068822,"end_time":"2021-01-01T09:50:01.458337","exception":false,"start_time":"2021-01-01T09:50:01.389515","status":"completed"},"tags":[]}},{"cell_type":"code","source":"class_ids, class_names = list(zip(*set(zip(train_df.class_id, train_df.class_name))))\nclasses = list(np.array(class_names)[np.argsort(class_ids)])\nclasses = list(map(lambda x: str(x), classes))\nclasses","metadata":{"papermill":{"duration":0.082234,"end_time":"2021-01-01T09:50:01.601574","exception":false,"start_time":"2021-01-01T09:50:01.51934","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T07:13:12.795685Z","iopub.execute_input":"2024-05-30T07:13:12.795932Z","iopub.status.idle":"2024-05-30T07:13:12.816824Z","shell.execute_reply.started":"2024-05-30T07:13:12.79591Z","shell.execute_reply":"2024-05-30T07:13:12.815948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# YOLOv9 Stuff","metadata":{"papermill":{"duration":0.055699,"end_time":"2021-01-01T09:50:01.82747","exception":false,"start_time":"2021-01-01T09:50:01.771771","status":"completed"},"tags":[]}},{"cell_type":"code","source":"from os import listdir\nfrom os.path import isfile, join\nimport yaml\n\ncwd = '/kaggle/working/'\n\nwith open(join( cwd , 'train.txt'), 'w') as f:\n    for path in glob('/kaggle/working/vinbigdata/images/train/*'):\n        f.write(path+'\\n')\n            \nwith open(join( cwd , 'val.txt'), 'w') as f:\n    for path in glob('/kaggle/working/vinbigdata/images/val/*'):\n        f.write(path+'\\n')\n\ndata = dict(\n    train =  join( cwd , 'train.txt') ,\n    val   =  join( cwd , 'val.txt' ),\n    nc    = 14,\n    names = classes\n    )\n\nwith open(join( cwd , 'vinbigdata.yaml'), 'w') as outfile:\n    yaml.dump(data, outfile, default_flow_style=False)\n\nf = open(join( cwd , 'vinbigdata.yaml'), 'r')\nprint('\\nyaml:')\nprint(f.read())","metadata":{"papermill":{"duration":0.113001,"end_time":"2021-01-01T09:50:01.996448","exception":false,"start_time":"2021-01-01T09:50:01.883447","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T07:13:12.818208Z","iopub.execute_input":"2024-05-30T07:13:12.818557Z","iopub.status.idle":"2024-05-30T07:13:12.869832Z","shell.execute_reply.started":"2024-05-30T07:13:12.818523Z","shell.execute_reply":"2024-05-30T07:13:12.868976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd yolov9\n%pip install -qr requirements.txt # 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":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","papermill":{"duration":6.702428,"end_time":"2021-01-01T09:50:08.784153","exception":false,"start_time":"2021-01-01T09:50:02.081725","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T07:13:12.876258Z","iopub.execute_input":"2024-05-30T07:13:12.876578Z","iopub.status.idle":"2024-05-30T07:13:30.026368Z","shell.execute_reply.started":"2024-05-30T07:13:12.876549Z","shell.execute_reply":"2024-05-30T07:13:30.025324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train","metadata":{"papermill":{"duration":0.064553,"end_time":"2021-01-01T09:50:19.6938","exception":false,"start_time":"2021-01-01T09:50:19.629247","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!pip install ultralytics","metadata":{"execution":{"iopub.status.busy":"2024-05-30T07:13:30.027593Z","iopub.execute_input":"2024-05-30T07:13:30.028057Z","iopub.status.idle":"2024-05-30T07:13:43.144937Z","shell.execute_reply.started":"2024-05-30T07:13:30.028027Z","shell.execute_reply":"2024-05-30T07:13:43.143944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !WANDB_MODE=\"dryrun\" python train_dual.py --workers 8 --device 0 --batch 10 --data /kaggle/working/vinbigdata.yaml --img 640 --cfg models/detect/yolov9-c.yaml --weights '' --name yolov9-c --hyp hyp.scratch-high.yaml --min-items 0 --epochs 30 --close-mosaic 15\n!WANDB_MODE=\"dryrun\" python train_dual.py --workers 8 --device 0 --batch 10 --data /kaggle/working/vinbigdata.yaml --img 640 --cfg models/detect/yolov9-e.yaml --weights '' --name yolov9-e --min-items 0 --epochs 30 --close-mosaic 15","metadata":{"papermill":{"duration":19916.498298,"end_time":"2021-01-01T15:22:16.289734","exception":false,"start_time":"2021-01-01T09:50:19.791436","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T07:13:43.146371Z","iopub.execute_input":"2024-05-30T07:13:43.146688Z","iopub.status.idle":"2024-05-30T11:26:24.26284Z","shell.execute_reply.started":"2024-05-30T07:13:43.146659Z","shell.execute_reply":"2024-05-30T11:26:24.261624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Class Distribution","metadata":{"papermill":{"duration":4.919442,"end_time":"2021-01-01T15:22:26.398681","exception":false,"start_time":"2021-01-01T15:22:21.479239","status":"completed"},"tags":[]}},{"cell_type":"code","source":"plt.figure(figsize = (20,20))\nplt.axis('off')\nplt.imshow(plt.imread('runs/train/yolov9-e/labels_correlogram.jpg'));","metadata":{"papermill":{"duration":6.511035,"end_time":"2021-01-01T15:22:37.753063","exception":false,"start_time":"2021-01-01T15:22:31.242028","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T11:26:24.264351Z","iopub.execute_input":"2024-05-30T11:26:24.264638Z","iopub.status.idle":"2024-05-30T11:26:25.871125Z","shell.execute_reply.started":"2024-05-30T11:26:24.26461Z","shell.execute_reply":"2024-05-30T11:26:25.870163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20,20))\nplt.axis('off')\nplt.imshow(plt.imread('runs/train/yolov9-e/labels.jpg'));","metadata":{"papermill":{"duration":5.977042,"end_time":"2021-01-01T15:22:48.614609","exception":false,"start_time":"2021-01-01T15:22:42.637567","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T11:26:25.872376Z","iopub.execute_input":"2024-05-30T11:26:25.872665Z","iopub.status.idle":"2024-05-30T11:26:27.120972Z","shell.execute_reply.started":"2024-05-30T11:26:25.872639Z","shell.execute_reply":"2024-05-30T11:26:27.119894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Batch Image","metadata":{"papermill":{"duration":5.378338,"end_time":"2021-01-01T15:22:59.482837","exception":false,"start_time":"2021-01-01T15:22:54.104499","status":"completed"},"tags":[]}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.figure(figsize = (15, 15))\nplt.imshow(plt.imread('runs/train/yolov9-e/train_batch0.jpg'))\n\nplt.figure(figsize = (15, 15))\nplt.imshow(plt.imread('runs/train/yolov9-e/train_batch1.jpg'))\n\nplt.figure(figsize = (15, 15))\nplt.imshow(plt.imread('runs/train/yolov9-e/train_batch2.jpg'))","metadata":{"papermill":{"duration":7.317416,"end_time":"2021-01-01T15:23:11.777544","exception":false,"start_time":"2021-01-01T15:23:04.460128","status":"completed"},"tags":[],"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-05-30T11:26:27.122277Z","iopub.execute_input":"2024-05-30T11:26:27.12264Z","iopub.status.idle":"2024-05-30T11:26:30.988022Z","shell.execute_reply.started":"2024-05-30T11:26:27.122609Z","shell.execute_reply":"2024-05-30T11:26:30.987064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# GT Vs Pred","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(3, 2, figsize = (2*5,3*5), constrained_layout = True)\nfor row in range(3):\n    ax[row][0].imshow(plt.imread(f'runs/train/yolov9-e/val_batch{row}_labels.jpg'))\n    ax[row][0].set_xticks([])\n    ax[row][0].set_yticks([])\n    ax[row][0].set_title(f'runs/train/yolov9-e/val_batch{row}_labels.jpg', fontsize = 12)\n    \n    ax[row][1].imshow(plt.imread(f'runs/train/yolov9-e/val_batch{row}_pred.jpg'))\n    ax[row][1].set_xticks([])\n    ax[row][1].set_yticks([])\n    ax[row][1].set_title(f'runs/train/yolov9-e/val_batch{row}_pred.jpg', fontsize = 12)","metadata":{"papermill":{"duration":6.453975,"end_time":"2021-01-01T15:23:23.514717","exception":false,"start_time":"2021-01-01T15:23:17.060742","status":"completed"},"tags":[],"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-05-30T11:26:30.989323Z","iopub.execute_input":"2024-05-30T11:26:30.989645Z","iopub.status.idle":"2024-05-30T11:26:35.744374Z","shell.execute_reply.started":"2024-05-30T11:26:30.989617Z","shell.execute_reply":"2024-05-30T11:26:35.743434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# (Loss, Map) Vs Epoch","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread('runs/train/yolov9-e/results.png'));","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-05-30T11:26:35.750295Z","iopub.execute_input":"2024-05-30T11:26:35.750918Z","iopub.status.idle":"2024-05-30T11:26:37.224252Z","shell.execute_reply.started":"2024-05-30T11:26:35.750887Z","shell.execute_reply":"2024-05-30T11:26:37.223317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Confusion Matrix","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread('runs/train/yolov9-e/confusion_matrix.png'));","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-05-30T11:26:37.225497Z","iopub.execute_input":"2024-05-30T11:26:37.225863Z","iopub.status.idle":"2024-05-30T11:26:39.147647Z","shell.execute_reply.started":"2024-05-30T11:26:37.225822Z","shell.execute_reply":"2024-05-30T11:26:39.146207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference","metadata":{"papermill":{"duration":4.941983,"end_time":"2021-01-01T15:23:33.765831","exception":false,"start_time":"2021-01-01T15:23:28.823848","status":"completed"},"tags":[]}},{"cell_type":"code","source":"shutil.copy('/kaggle/input/general2-py/general.py', '/kaggle/working/yolov9/utils')","metadata":{"execution":{"iopub.status.busy":"2024-05-30T11:26:39.149304Z","iopub.execute_input":"2024-05-30T11:26:39.14964Z","iopub.status.idle":"2024-05-30T11:26:39.16675Z","shell.execute_reply.started":"2024-05-30T11:26:39.149612Z","shell.execute_reply":"2024-05-30T11:26:39.16593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python detect.py --weights 'runs/train/yolov9-e/weights/best.pt'\\\n--img 640\\\n--conf 0.15\\\n--iou 0.5\\\n--source /kaggle/working/vinbigdata/images/val\\\n--exist-ok","metadata":{"_kg_hide-output":true,"papermill":{"duration":10.763143,"end_time":"2021-01-01T15:23:49.800461","exception":false,"start_time":"2021-01-01T15:23:39.037318","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-05-30T11:26:39.167692Z","iopub.execute_input":"2024-05-30T11:26:39.16796Z","iopub.status.idle":"2024-05-30T11:27:44.782613Z","shell.execute_reply.started":"2024-05-30T11:26:39.167937Z","shell.execute_reply":"2024-05-30T11:27:44.781542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Inference Plot","metadata":{"papermill":{"duration":5.225725,"end_time":"2021-01-01T15:24:00.706026","exception":false,"start_time":"2021-01-01T15:23:55.480301","status":"completed"},"tags":[]}},{"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/*')\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":{"papermill":{"duration":5.31015,"end_time":"2021-01-01T15:24:11.211904","exception":false,"start_time":"2021-01-01T15:24:05.901754","status":"completed"},"tags":[],"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-05-30T11:27:44.784082Z","iopub.execute_input":"2024-05-30T11:27:44.784445Z","iopub.status.idle":"2024-05-30T11:27:52.899364Z","shell.execute_reply.started":"2024-05-30T11:27:44.784414Z","shell.execute_reply":"2024-05-30T11:27:52.898108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree('/kaggle/working/vinbigdata')\nshutil.rmtree('runs/detect')\nfor file in (glob('runs/train/exp/**/*.png', recursive = True)+glob('runs/train/exp/**/*.jpg', recursive = True)):\n    os.remove(file)","metadata":{"papermill":{"duration":5.709202,"end_time":"2021-01-01T15:24:22.413173","exception":false,"start_time":"2021-01-01T15:24:16.703971","status":"completed"},"tags":[],"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-05-30T11:27:52.900751Z","iopub.execute_input":"2024-05-30T11:27:52.901633Z","iopub.status.idle":"2024-05-30T11:27:53.346028Z","shell.execute_reply.started":"2024-05-30T11:27:52.901599Z","shell.execute_reply":"2024-05-30T11:27:53.345204Z"},"trusted":true},"execution_count":null,"outputs":[]}]}