{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceType":"competition","sourceId":24800,"datasetId":1042002,"databundleVersionId":1831594},{"sourceType":"datasetVersion","sourceId":1799839,"datasetId":1069682,"databundleVersionId":1837296},{"sourceType":"datasetVersion","sourceId":1800066,"datasetId":1069809,"databundleVersionId":1837523},{"sourceType":"datasetVersion","sourceId":1800067,"datasetId":1069810,"databundleVersionId":1837524},{"sourceType":"datasetVersion","sourceId":1800825,"datasetId":1070245,"databundleVersionId":1838284},{"sourceType":"datasetVersion","sourceId":1800777,"datasetId":1069787,"databundleVersionId":1838236},{"sourceType":"datasetVersion","sourceId":1800778,"datasetId":1070222,"databundleVersionId":1838237}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Version\n* `v13`: Fold4\n* `v12`: Fold3\n* `v10`: Fold2\n* `v09`: Fold1\n* `v03`: Fold0","metadata":{}},{"cell_type":"code","source":"!python --version","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-15T20:15:44.833591Z","iopub.execute_input":"2025-11-15T20:15:44.833798Z","iopub.status.idle":"2025-11-15T20:15:44.960802Z","shell.execute_reply.started":"2025-11-15T20:15:44.833782Z","shell.execute_reply":"2025-11-15T20:15:44.959996Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install --upgrade pip","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-15T20:15:44.962734Z","iopub.execute_input":"2025-11-15T20:15:44.963022Z","iopub.status.idle":"2025-11-15T20:15:51.038395Z","shell.execute_reply.started":"2025-11-15T20:15:44.96299Z","shell.execute_reply":"2025-11-15T20:15:51.037489Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install --upgrade seaborn","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2025-11-15T20:15:51.039506Z","iopub.execute_input":"2025-11-15T20:15:51.039794Z","iopub.status.idle":"2025-11-15T20:15:53.533033Z","shell.execute_reply.started":"2025-11-15T20:15:51.039758Z","shell.execute_reply":"2025-11-15T20:15:53.532325Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"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":"2025-11-15T20:15:53.533893Z","iopub.execute_input":"2025-11-15T20:15:53.534083Z","iopub.status.idle":"2025-11-15T20:15:54.555751Z","shell.execute_reply.started":"2025-11-15T20:15:53.53406Z","shell.execute_reply":"2025-11-15T20:15:54.555176Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dim = 512 #512, 256, 'original'\nfold = 4","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-15T20:15:54.557435Z","iopub.execute_input":"2025-11-15T20:15:54.557725Z","iopub.status.idle":"2025-11-15T20:15:54.561543Z","shell.execute_reply.started":"2025-11-15T20:15:54.557707Z","shell.execute_reply":"2025-11-15T20:15:54.560812Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv(f'../input/vinbigdata-{dim}-image-dataset/vinbigdata/train.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2025-11-15T20:15:54.562256Z","iopub.execute_input":"2025-11-15T20:15:54.56257Z","iopub.status.idle":"2025-11-15T20:15:54.84264Z","shell.execute_reply.started":"2025-11-15T20:15:54.562545Z","shell.execute_reply":"2025-11-15T20:15:54.842015Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"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":{"execution":{"iopub.status.busy":"2025-11-15T20:15:54.843368Z","iopub.execute_input":"2025-11-15T20:15:54.843612Z","iopub.status.idle":"2025-11-15T20:15:54.879953Z","shell.execute_reply.started":"2025-11-15T20:15:54.843584Z","shell.execute_reply":"2025-11-15T20:15:54.879241Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Only 14 Class","metadata":{"papermill":{"duration":0.027478,"end_time":"2021-01-01T09:44:54.861374","exception":false,"start_time":"2021-01-01T09:44:54.833896","status":"completed"},"tags":[]}},{"cell_type":"code","source":"train_df = train_df[train_df.class_id!=14].reset_index(drop = True)","metadata":{"execution":{"iopub.status.busy":"2025-11-15T20:15:54.880741Z","iopub.execute_input":"2025-11-15T20:15:54.881093Z","iopub.status.idle":"2025-11-15T20:15:54.904097Z","shell.execute_reply.started":"2025-11-15T20:15:54.881062Z","shell.execute_reply":"2025-11-15T20:15:54.903574Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"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":{"execution":{"iopub.status.busy":"2025-11-15T20:15:54.904785Z","iopub.execute_input":"2025-11-15T20:15:54.904957Z","iopub.status.idle":"2025-11-15T20:15:57.915345Z","shell.execute_reply.started":"2025-11-15T20:15:54.904943Z","shell.execute_reply":"2025-11-15T20:15:57.914665Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"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":{"execution":{"iopub.status.busy":"2025-11-15T20:15:57.916122Z","iopub.execute_input":"2025-11-15T20:15:57.9164Z","iopub.status.idle":"2025-11-15T20:15:57.92712Z","shell.execute_reply.started":"2025-11-15T20:15:57.91637Z","shell.execute_reply":"2025-11-15T20:15:57.926341Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"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":{"execution":{"iopub.status.busy":"2025-11-15T20:15:57.927875Z","iopub.execute_input":"2025-11-15T20:15:57.92811Z","iopub.status.idle":"2025-11-15T20:15:57.947982Z","shell.execute_reply.started":"2025-11-15T20:15:57.928093Z","shell.execute_reply":"2025-11-15T20:15:57.947284Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# t-SNE Visualization","metadata":{"papermill":{"duration":0.028555,"end_time":"2021-01-01T09:45:03.060819","exception":false,"start_time":"2021-01-01T09:45:03.032264","status":"completed"},"tags":[]}},{"cell_type":"code","source":"\"\"\"\n%%time\nfrom sklearn.manifold import TSNE\n\ntsne = TSNE(n_components = 2, perplexity = 40, random_state=1, n_iter=5000)\ndata_X = X\ndata_y = y.loc[data_X.index]\nembs = tsne.fit_transform(data_X)\n# Add to dataframe for convenience\nplot_x = embs[:, 0]\nplot_y = embs[:, 1]\n\"\"\"","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2025-11-15T20:15:57.948711Z","iopub.execute_input":"2025-11-15T20:15:57.948969Z","iopub.status.idle":"2025-11-15T20:15:57.965081Z","shell.execute_reply.started":"2025-11-15T20:15:57.948947Z","shell.execute_reply":"2025-11-15T20:15:57.96453Z"},"papermill":{"duration":79.550362,"end_time":"2021-01-01T09:46:22.640073","exception":false,"start_time":"2021-01-01T09:45:03.089711","status":"completed"},"tags":[],"_kg_hide-input":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\"\"\"\nimport matplotlib.pyplot as plt\nplt.figure(figsize = (15, 15))\nplt.axis('off')\nscatter = plt.scatter(plot_x, plot_y, marker = 'o',s = 50, c=data_y.tolist(), alpha= 0.5,cmap='viridis')\nplt.legend(handles=scatter.legend_elements()[0], labels=classes)\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2025-11-15T20:15:57.965789Z","iopub.execute_input":"2025-11-15T20:15:57.966066Z","iopub.status.idle":"2025-11-15T20:15:57.98333Z","shell.execute_reply.started":"2025-11-15T20:15:57.966051Z","shell.execute_reply":"2025-11-15T20:15:57.982777Z"},"papermill":{"duration":0.674101,"end_time":"2021-01-01T09:46:23.353693","exception":false,"start_time":"2021-01-01T09:46:22.679592","status":"completed"},"tags":[],"_kg_hide-input":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# BBox Location","metadata":{"papermill":{"duration":0.043674,"end_time":"2021-01-01T09:46:23.441245","exception":false,"start_time":"2021-01-01T09:46:23.397571","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"## x_mid Vs y_mid","metadata":{"papermill":{"duration":0.041604,"end_time":"2021-01-01T09:46:23.525588","exception":false,"start_time":"2021-01-01T09:46:23.483984","status":"completed"},"tags":[]}},{"cell_type":"code","source":"\"\"\"\nfrom scipy.stats import gaussian_kde\n\n\nx_val = train_df.x_mid.values\ny_val = train_df.y_mid.values\n\n# Calculate the point density\nxy = np.vstack([x_val,y_val])\nz = gaussian_kde(xy)(xy)\n\nfig, ax = plt.subplots(figsize = (10, 10))\nax.axis('off')\nax.scatter(x_val, y_val, c=z, s=100, cmap='viridis')\n# ax.set_xlabel('x_mid')\n# ax.set_ylabel('y_mid')\nplt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2025-11-15T20:15:57.986401Z","iopub.execute_input":"2025-11-15T20:15:57.986599Z","iopub.status.idle":"2025-11-15T20:15:57.998402Z","shell.execute_reply.started":"2025-11-15T20:15:57.986585Z","shell.execute_reply":"2025-11-15T20:15:57.997771Z"},"papermill":{"duration":31.402628,"end_time":"2021-01-01T09:46:54.970613","exception":false,"start_time":"2021-01-01T09:46:23.567985","status":"completed"},"tags":[],"_kg_hide-input":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## bbox_w Vs bbox_h","metadata":{"papermill":{"duration":0.046802,"end_time":"2021-01-01T09:46:55.064644","exception":false,"start_time":"2021-01-01T09:46:55.017842","status":"completed"},"tags":[]}},{"cell_type":"code","source":"\"\"\"\nx_val = train_df.w.values\ny_val = train_df.h.values\n\n# Calculate the point density\nxy = np.vstack([x_val,y_val])\nz = gaussian_kde(xy)(xy)\n\nfig, ax = plt.subplots(figsize = (10, 10))\nax.axis('off')\nax.scatter(x_val, y_val, c=z, s=100, cmap='viridis')\n# ax.set_xlabel('bbox_width')\n# ax.set_ylabel('bbox_height')\nplt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2025-11-15T20:15:57.999113Z","iopub.execute_input":"2025-11-15T20:15:57.999305Z","iopub.status.idle":"2025-11-15T20:15:58.013918Z","shell.execute_reply.started":"2025-11-15T20:15:57.999267Z","shell.execute_reply":"2025-11-15T20:15:58.013385Z"},"papermill":{"duration":30.485279,"end_time":"2021-01-01T09:47:25.596636","exception":false,"start_time":"2021-01-01T09:46:55.111357","status":"completed"},"tags":[],"_kg_hide-input":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Image Aspect Ratio","metadata":{"papermill":{"duration":0.090619,"end_time":"2021-01-01T09:47:25.783368","exception":false,"start_time":"2021-01-01T09:47:25.692749","status":"completed"},"tags":[]}},{"cell_type":"code","source":"\"\"\"\nx_val = train_df.width.values\ny_val = train_df.height.values\n\n# Calculate the point density\nxy = np.vstack([x_val,y_val])\nz = gaussian_kde(xy)(xy)\n\nfig, ax = plt.subplots(figsize = (10, 10))\nax.axis('off')\nax.scatter(x_val, y_val, c=z, s=100, cmap='viridis')\n# ax.set_xlabel('image_width')\n# ax.set_ylabel('image_height')\nplt.show()\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2025-11-15T20:15:58.014608Z","iopub.execute_input":"2025-11-15T20:15:58.014773Z","iopub.status.idle":"2025-11-15T20:15:58.029063Z","shell.execute_reply.started":"2025-11-15T20:15:58.014761Z","shell.execute_reply":"2025-11-15T20:15:58.028343Z"},"papermill":{"duration":30.131145,"end_time":"2021-01-01T09:47:56.005954","exception":false,"start_time":"2021-01-01T09:47:25.874809","status":"completed"},"tags":[],"_kg_hide-input":true,"trusted":true},"outputs":[],"execution_count":null},{"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":{"execution":{"iopub.status.busy":"2025-11-15T20:15:58.029673Z","iopub.execute_input":"2025-11-15T20:15:58.029916Z","iopub.status.idle":"2025-11-15T20:15:58.101846Z","shell.execute_reply.started":"2025-11-15T20:15:58.029898Z","shell.execute_reply":"2025-11-15T20:15:58.101329Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"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":{"execution":{"iopub.status.busy":"2025-11-15T20:15:58.102475Z","iopub.execute_input":"2025-11-15T20:15:58.102683Z","iopub.status.idle":"2025-11-15T20:15:58.125135Z","shell.execute_reply.started":"2025-11-15T20:15:58.102668Z","shell.execute_reply":"2025-11-15T20:15:58.124545Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"\n# Data Augmentation","metadata":{}},{"cell_type":"code","source":"from matplotlib.pyplot import imshow\nfrom cv2 import imread\n\nimshow(imread(train_files[10]))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-16T00:58:18.471044Z","iopub.execute_input":"2025-11-16T00:58:18.471368Z","iopub.status.idle":"2025-11-16T00:58:18.70035Z","shell.execute_reply.started":"2025-11-16T00:58:18.471345Z","shell.execute_reply":"2025-11-16T00:58:18.699701Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_files","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nimport cv2\nimport albumentations as A\nfor file in tqdm(train_files):\n    transform = A.Compose([\n        A.ToGray(p=0.5),\n        A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.7), # Adjust brightness/contrast\n        A.GaussNoise()\n        # Add more augmentations as needed\n    ])\n    image = cv2.imread(file)\n    augmented_data = transform(image=image)\n    augmented_image = augmented_data['image']\n    cv2.imwrite(file, augmented_image)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-16T00:55:19.756974Z","iopub.execute_input":"2025-11-16T00:55:19.757512Z","iopub.status.idle":"2025-11-16T00:56:19.174053Z","shell.execute_reply.started":"2025-11-16T00:55:19.757487Z","shell.execute_reply":"2025-11-16T00:56:19.173269Z"}},"outputs":[],"execution_count":null},{"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":{"execution":{"iopub.status.busy":"2025-11-16T01:01:43.865411Z","iopub.execute_input":"2025-11-16T01:01:43.866211Z","iopub.status.idle":"2025-11-16T01:02:13.471637Z","shell.execute_reply.started":"2025-11-16T01:01:43.866186Z","shell.execute_reply":"2025-11-16T01:02:13.470874Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"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":{"execution":{"iopub.status.busy":"2025-11-15T20:17:53.316095Z","iopub.execute_input":"2025-11-15T20:17:53.316379Z","iopub.status.idle":"2025-11-15T20:17:53.328781Z","shell.execute_reply.started":"2025-11-15T20:17:53.316358Z","shell.execute_reply":"2025-11-15T20:17:53.328164Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# [YOLOv5](https://github.com/ultralytics/yolov5)\n![](https://user-images.githubusercontent.com/26833433/98699617-a1595a00-2377-11eb-8145-fc674eb9b1a7.jpg)\n![](https://user-images.githubusercontent.com/26833433/90187293-6773ba00-dd6e-11ea-8f90-cd94afc0427f.png)","metadata":{"papermill":{"duration":0.056257,"end_time":"2021-01-01T09:50:01.716608","exception":false,"start_time":"2021-01-01T09:50:01.660351","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"# YOLOv5 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":{"execution":{"iopub.status.busy":"2025-11-16T01:02:36.826822Z","iopub.execute_input":"2025-11-16T01:02:36.827134Z","iopub.status.idle":"2025-11-16T01:02:36.845934Z","shell.execute_reply.started":"2025-11-16T01:02:36.82711Z","shell.execute_reply":"2025-11-16T01:02:36.845347Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Pretrained Checkpoints:\n\n| Model | AP<sup>val</sup> | AP<sup>test</sup> | AP<sub>50</sub> | Speed<sub>GPU</sub> | FPS<sub>GPU</sub> || params | FLOPS |\n|---------- |------ |------ |------ | -------- | ------| ------ |------  |  :------: |\n| [YOLOv5s](https://github.com/ultralytics/yolov5/releases/tag/v3.0)    | 37.0     | 37.0     | 56.2     | **2.4ms** | **416** || 7.5M   | 13.2B\n| [YOLOv5m](https://github.com/ultralytics/yolov5/releases/tag/v3.0)    | 44.3     | 44.3     | 63.2     | 3.4ms     | 294     || 21.8M  | 39.4B\n| [YOLOv5l](https://github.com/ultralytics/yolov5/releases/tag/v3.0)    | 47.7     | 47.7     | 66.5     | 4.4ms     | 227     || 47.8M  | 88.1B\n| [YOLOv5x](https://github.com/ultralytics/yolov5/releases/tag/v3.0)    | **49.2** | **49.2** | **67.7** | 6.9ms     | 145     || 89.0M  | 166.4B\n| | | | | | || |\n| [YOLOv5x](https://github.com/ultralytics/yolov5/releases/tag/v3.0) + TTA|**50.8**| **50.8** | **68.9** | 25.5ms    | 39      || 89.0M  | 354.3B\n| | | | | | || |\n| [YOLOv3-SPP](https://github.com/ultralytics/yolov5/releases/tag/v3.0) | 45.6     | 45.5     | 65.2     | 4.5ms     | 222     || 63.0M  | 118.0B","metadata":{"papermill":{"duration":0.064911,"end_time":"2021-01-01T09:50:19.435746","exception":false,"start_time":"2021-01-01T09:50:19.370835","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"# Selecting Models\nIn this notebok I'm using `v5s`. To select your prefered model just replace `--cfg models/yolov5s.yaml --weights yolov5s.pt` with the following command:\n* `v5s` : `--cfg models/yolov5s.yaml --weights yolov5s.pt`\n* `v5m` : `--cfg models/yolov5m.yaml --weights yolov5m.pt`\n* `v5l` : `--cfg models/yolov5l.yaml --weights yolov5l.pt`\n* `v5x` : `--cfg models/yolov5x.yaml --weights yolov5x.pt`","metadata":{"papermill":{"duration":0.064016,"end_time":"2021-01-01T09:50:19.564859","exception":false,"start_time":"2021-01-01T09:50:19.500843","status":"completed"},"tags":[]}},{"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 numpy==1.26.4","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-15T20:17:53.378075Z","iopub.execute_input":"2025-11-15T20:17:53.378345Z","iopub.status.idle":"2025-11-15T20:19:08.175362Z","shell.execute_reply.started":"2025-11-15T20:17:53.378323Z","shell.execute_reply":"2025-11-15T20:19:08.174598Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from ultralytics import YOLO\n\n# Load a model\nmodel = YOLO(\"yolo12s.pt\")\n\n# Train the model\ntrain_results = model.train(\n    data=\"/kaggle/working/vinbigdata.yaml\",  # path to dataset YAML\n    epochs=100,  # number of training epochs\n    patience=10,\n    imgsz=640,  # training image size\n    device=0,  # device to run on, i.e. device=0 or device=0,1,2,3 or device=cpu\n)\n\npath = model.export()  # return path to exported model\nprint(path)\n\n# Evaluate model performance on the validation set\nmetrics = model.val()\n\n# Export the model to PT format","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-16T01:02:54.809867Z","iopub.execute_input":"2025-11-16T01:02:54.810585Z","iopub.status.idle":"2025-11-16T02:53:24.356728Z","shell.execute_reply.started":"2025-11-16T01:02:54.810561Z","shell.execute_reply":"2025-11-16T02:53:24.355983Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python --version","metadata":{"execution":{"iopub.status.busy":"2025-11-15T22:01:03.45Z","iopub.status.idle":"2025-11-15T22:01:03.450318Z","shell.execute_reply.started":"2025-11-15T22:01:03.450151Z","shell.execute_reply":"2025-11-15T22:01:03.450163Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x=0\nfrom time import sleep\nwhile True:\n    x+=1\n    sleep(60)\n    print(f\"refresh {float(60*x)} sec\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-16T00:38:18.024471Z","iopub.execute_input":"2025-11-16T00:38:18.025081Z","iopub.status.idle":"2025-11-16T00:41:10.808544Z","shell.execute_reply.started":"2025-11-16T00:38:18.025058Z","shell.execute_reply":"2025-11-16T00:41:10.807576Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pwd","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-16T00:33:15.587261Z","iopub.execute_input":"2025-11-16T00:33:15.587948Z","iopub.status.idle":"2025-11-16T00:33:15.783664Z","shell.execute_reply.started":"2025-11-16T00:33:15.587926Z","shell.execute_reply":"2025-11-16T00:33:15.782673Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!zip -r output.zip runs/detect","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-16T03:02:34.05436Z","iopub.execute_input":"2025-11-16T03:02:34.054729Z","iopub.status.idle":"2025-11-16T03:02:42.786601Z","shell.execute_reply.started":"2025-11-16T03:02:34.054691Z","shell.execute_reply":"2025-11-16T03:02:42.785793Z"}},"outputs":[],"execution_count":null},{"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/exp/labels_correlogram.jpg'));","metadata":{"execution":{"iopub.status.busy":"2025-11-15T22:01:03.45415Z","iopub.status.idle":"2025-11-15T22:01:03.45441Z","shell.execute_reply.started":"2025-11-15T22:01:03.454269Z","shell.execute_reply":"2025-11-15T22:01:03.454279Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize = (20,20))\nplt.axis('off')\nplt.imshow(plt.imread('runs/train/exp/labels.jpg'));","metadata":{"execution":{"iopub.status.busy":"2025-11-15T22:01:03.455801Z","iopub.status.idle":"2025-11-15T22:01:03.456072Z","shell.execute_reply.started":"2025-11-15T22:01:03.455959Z","shell.execute_reply":"2025-11-15T22:01:03.455969Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"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/exp/train_batch0.jpg'))\n\nplt.figure(figsize = (15, 15))\nplt.imshow(plt.imread('runs/train/exp/train_batch1.jpg'))\n\nplt.figure(figsize = (15, 15))\nplt.imshow(plt.imread('runs/train/exp/train_batch2.jpg'))","metadata":{"execution":{"iopub.status.busy":"2025-11-15T22:01:03.45683Z","iopub.status.idle":"2025-11-15T22:01:03.457048Z","shell.execute_reply.started":"2025-11-15T22:01:03.456947Z","shell.execute_reply":"2025-11-15T22:01:03.456957Z"},"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,"trusted":true},"outputs":[],"execution_count":null},{"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/exp/test_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/exp/test_batch{row}_labels.jpg', fontsize = 12)\n    \n    ax[row][1].imshow(plt.imread(f'runs/train/exp/test_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/exp/test_batch{row}_pred.jpg', fontsize = 12)","metadata":{"execution":{"iopub.status.busy":"2025-11-15T22:01:03.457946Z","iopub.status.idle":"2025-11-15T22:01:03.458161Z","shell.execute_reply.started":"2025-11-15T22:01:03.458059Z","shell.execute_reply":"2025-11-15T22:01:03.458068Z"},"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,"trusted":true},"outputs":[],"execution_count":null},{"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/exp/results.png'));","metadata":{"_kg_hide-input":true,"trusted":true,"execution":{"iopub.status.busy":"2025-11-15T22:01:03.459057Z","iopub.status.idle":"2025-11-15T22:01:03.459412Z","shell.execute_reply.started":"2025-11-15T22:01:03.459208Z","shell.execute_reply":"2025-11-15T22:01:03.459226Z"}},"outputs":[],"execution_count":null},{"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/exp/confusion_matrix.png'));","metadata":{"trusted":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2025-11-15T22:01:03.46066Z","iopub.status.idle":"2025-11-15T22:01:03.460902Z","shell.execute_reply.started":"2025-11-15T22:01:03.460788Z","shell.execute_reply":"2025-11-15T22:01:03.460799Z"}},"outputs":[],"execution_count":null},{"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":"!python detect.py --weights 'runs/train/exp/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,"execution":{"iopub.status.busy":"2025-11-15T22:01:03.461889Z","iopub.status.idle":"2025-11-15T22:01:03.462141Z","shell.execute_reply.started":"2025-11-15T22:01:03.462035Z","shell.execute_reply":"2025-11-15T22:01:03.462046Z"},"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":[],"trusted":true},"outputs":[],"execution_count":null},{"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":{"execution":{"iopub.status.busy":"2025-11-16T00:41:21.452359Z","iopub.execute_input":"2025-11-16T00:41:21.452637Z","iopub.status.idle":"2025-11-16T00:41:21.504352Z","shell.execute_reply.started":"2025-11-16T00:41:21.452617Z","shell.execute_reply":"2025-11-16T00:41:21.503413Z"},"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":[],"trusted":true,"_kg_hide-input":true},"outputs":[],"execution_count":null},{"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":{"execution":{"iopub.status.busy":"2025-11-15T22:01:03.464143Z","iopub.status.idle":"2025-11-15T22:01:03.464434Z","shell.execute_reply.started":"2025-11-15T22:01:03.464258Z","shell.execute_reply":"2025-11-15T22:01:03.464272Z"},"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":[],"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"outputs":[],"execution_count":null}]}