{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":9907590,"sourceType":"datasetVersion","datasetId":6087088},{"sourceId":9921395,"sourceType":"datasetVersion","datasetId":6097556}],"dockerImageVersionId":30153,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## 0. IMPORT MODULES","metadata":{}},{"cell_type":"code","source":"import ast\nimport glob\nimport os\nimport yaml\nimport json\n\nimport numpy as np\nimport pandas as pd\n\n\nfrom IPython.display import Image, display\nfrom IPython.core.magic import register_line_cell_magic\nfrom shutil import copyfile\nfrom tqdm import tqdm\ntqdm.pandas()","metadata":{"execution":{"iopub.status.busy":"2024-11-17T03:39:20.229472Z","iopub.execute_input":"2024-11-17T03:39:20.230358Z","iopub.status.idle":"2024-11-17T03:39:20.305781Z","shell.execute_reply.started":"2024-11-17T03:39:20.230222Z","shell.execute_reply":"2024-11-17T03:39:20.305044Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 1. PREPARE DATASET","metadata":{}},{"cell_type":"code","source":"with open('/kaggle/input/train-json/train_augment.json', 'r') as f:\n    data = json.load(f)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-17T03:40:08.400194Z","iopub.execute_input":"2024-11-17T03:40:08.400483Z","iopub.status.idle":"2024-11-17T03:40:13.757023Z","shell.execute_reply.started":"2024-11-17T03:40:08.400450Z","shell.execute_reply":"2024-11-17T03:40:13.756227Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.DataFrame(data['images'])\ndf['annotations'] = df.apply(lambda x: [], axis=1)\ndf.sample(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-17T03:46:37.756712Z","iopub.execute_input":"2024-11-17T03:46:37.757025Z","iopub.status.idle":"2024-11-17T03:46:38.217120Z","shell.execute_reply.started":"2024-11-17T03:46:37.756989Z","shell.execute_reply":"2024-11-17T03:46:38.216335Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# for index, row in tqdm(df.iterrows()):\n#     file_name = row['file_name'].split('/')[-1]\n#     if file_name[:6] in ['cam_03', 'cam_05', 'cam_08']:\n#         anno_dir = '/kaggle/input/bkai-track1/dataset/train_all/label_nighttime/' \n#     else:\n#         anno_dir = '/kaggle/input/bkai-track1/dataset/train_all/label_daytime/'\n    \n#     anno_file = anno_dir + file_name[:-4] + \".txt\"\n#     if file_name[:-4] == 'cam_10_00500':\n#         anno_file = anno_dir + 'cam_10_000500' + \".txt\"\n#     with open(anno_file, \"r\") as f:\n#         bboxes = f.readlines()\n#     image_annotations = []\n#     for bbox in bboxes:\n#         args = bbox.split(\" \")\n#         category_id = int(args[0])\n#         if 'nighttime' in anno_dir:\n#             category_id -= 4\n#         center_x = float(args[1])\n#         center_y = float(args[2])\n#         bbox_w = float(args[3])\n#         bbox_h = float(args[4])\n#         image_annotations.append({'category_id': category_id, 'bbox': [center_x, center_y, bbox_w, bbox_h]})\n#     df.at[index, 'annotations'] = image_annotations","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-17T03:41:40.842538Z","iopub.execute_input":"2024-11-17T03:41:40.843506Z","iopub.status.idle":"2024-11-17T03:43:26.317069Z","shell.execute_reply.started":"2024-11-17T03:41:40.843462Z","shell.execute_reply":"2024-11-17T03:43:26.316368Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data['annotations'][0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-17T03:48:45.008562Z","iopub.execute_input":"2024-11-17T03:48:45.009275Z","iopub.status.idle":"2024-11-17T03:48:45.014930Z","shell.execute_reply.started":"2024-11-17T03:48:45.009240Z","shell.execute_reply":"2024-11-17T03:48:45.014135Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def get_yolo_format_bbox(img_w, img_h, bbox):\n    left, top, width, height = bbox\n        \n    x_center = left + int(np.round(width/2))\n    y_center = top + int(np.round(height/2)) \n\n    return [x_center/img_w, y_center/img_h, width/img_w, height/img_h]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-17T03:56:50.618593Z","iopub.execute_input":"2024-11-17T03:56:50.619276Z","iopub.status.idle":"2024-11-17T03:56:50.624446Z","shell.execute_reply.started":"2024-11-17T03:56:50.619230Z","shell.execute_reply":"2024-11-17T03:56:50.623658Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for annotation in tqdm(data['annotations']):\n    category_id = annotation['category_id']\n    image_id = annotation['image_id']\n    bbox = annotation['bbox']\n    df['annotations'][image_id].append({'category_id': category_id, 'bbox': get_yolo_format_bbox(df['width'][image_id], df['height'][image_id], bbox)})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-17T03:56:52.332496Z","iopub.execute_input":"2024-11-17T03:56:52.333152Z","iopub.status.idle":"2024-11-17T03:57:22.185615Z","shell.execute_reply.started":"2024-11-17T03:56:52.333111Z","shell.execute_reply":"2024-11-17T03:57:22.184962Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train val split\nmax_id = len(df)\nimage_ids = np.arange(0, max_id)\nnp.random.seed(42)\n\nvalid_ids = np.random.choice(\n    image_ids, size=int(200), replace=False\n)\n\ndf['is_train'] = True\ndf.loc[valid_ids, 'is_train'] = False","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-17T03:58:32.552980Z","iopub.execute_input":"2024-11-17T03:58:32.553644Z","iopub.status.idle":"2024-11-17T03:58:32.561964Z","shell.execute_reply.started":"2024-11-17T03:58:32.553605Z","shell.execute_reply":"2024-11-17T03:58:32.561225Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def add_path(row):\n    return f\"/kaggle/input/bkai-track1/dataset/{row.file_name}\"\n\ndf['path'] = df.apply(lambda row: add_path(row), axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-17T03:58:35.415190Z","iopub.execute_input":"2024-11-17T03:58:35.415937Z","iopub.status.idle":"2024-11-17T03:58:36.023507Z","shell.execute_reply.started":"2024-11-17T03:58:35.415896Z","shell.execute_reply":"2024-11-17T03:58:36.022695Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def add_new_path(row):\n    if row.is_train:\n        return f\"/kaggle/working/yolor_dataset/images/train/{row.id}.jpg\"\n    else: \n        return f\"/kaggle/working/yolor_dataset/images/valid/{row.id}.jpg\"\n    \ndf['new_path'] = df.apply(lambda row: add_new_path(row), axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-17T03:58:36.848627Z","iopub.execute_input":"2024-11-17T03:58:36.849547Z","iopub.status.idle":"2024-11-17T03:58:37.790899Z","shell.execute_reply.started":"2024-11-17T03:58:36.849506Z","shell.execute_reply":"2024-11-17T03:58:37.790276Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.sample(5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-17T03:58:38.794811Z","iopub.execute_input":"2024-11-17T03:58:38.795082Z","iopub.status.idle":"2024-11-17T03:58:38.842128Z","shell.execute_reply.started":"2024-11-17T03:58:38.795050Z","shell.execute_reply":"2024-11-17T03:58:38.841456Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 2. CREATE DATASET FILE STRUCTURE","metadata":{}},{"cell_type":"code","source":"os.makedirs(\"/kaggle/working//yolor_dataset/images/train\")\nos.makedirs(\"/kaggle/working//yolor_dataset/images/valid\")\nos.makedirs(\"/kaggle/working//yolor_dataset/labels/train\")\nos.makedirs(\"/kaggle/working//yolor_dataset/labels/valid\")\nprint(f\"Directory structure yor YoloR created\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-17T04:00:10.930338Z","iopub.execute_input":"2024-11-17T04:00:10.931101Z","iopub.status.idle":"2024-11-17T04:00:10.938786Z","shell.execute_reply.started":"2024-11-17T04:00:10.931062Z","shell.execute_reply":"2024-11-17T04:00:10.938010Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def copy_file(row):\n  copyfile(row.path, row.new_path)\n\n_ = df.progress_apply(lambda row: copy_file(row), axis=1)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 3. CREATE YoloR ANNOTATIONS","metadata":{}},{"cell_type":"code","source":"  for index, row in tqdm(df.iterrows()):\n    if row.is_train:\n        file_name = f\"/kaggle/working/yolor_dataset/labels/train/{row.id}.txt\"\n        os.makedirs(os.path.dirname(file_name), exist_ok=True)\n    else:\n        file_name = f\"/kaggle/working//yolor_dataset/labels/valid/{row.id}.txt\"\n        os.makedirs(os.path.dirname(file_name), exist_ok=True)\n        \n    with open(file_name, 'w') as f:\n        for bbox in row.annotations:\n            label = bbox['category_id']\n            bbox = [label] + bbox['bbox']\n            bbox_str = ' '.join([str(x) for x in bbox])\n            f.write(bbox_str)\n            f.write('\\n')\n                \nprint(\"Annotations in YoloR format for all images created.\")","metadata":{"execution":{"iopub.status.busy":"2024-11-16T09:22:23.732129Z","iopub.execute_input":"2024-11-16T09:22:23.732796Z","iopub.status.idle":"2024-11-16T09:22:33.231960Z","shell.execute_reply.started":"2024-11-16T09:22:23.732752Z","shell.execute_reply":"2024-11-16T09:22:33.231208Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 4. CREATE YoloR DATASET CONFIGURATION FILE","metadata":{}},{"cell_type":"code","source":"data_yaml = dict(\n    train = '/kaggle/working//yolor_dataset/images/train',\n    val = '/kaggle/working//yolor_dataset/images/valid',\n    nc = 4,\n    names = ['Bike', 'Car', 'Bus', 'Truck']\n)\n\n\nwith open('/kaggle/working//YoloR-data.yaml', 'w') as outfile:\n    yaml.dump(data_yaml, outfile, default_flow_style=True)\n\nprint(f'Dataset configuration file for YoloR created')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-16T09:19:00.543047Z","iopub.status.idle":"2024-11-16T09:19:00.543882Z","shell.execute_reply.started":"2024-11-16T09:19:00.543591Z","shell.execute_reply":"2024-11-16T09:19:00.543622Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 4. INSTALL YoloR\n\n### 4A. CLONE YoloR GIT REPOSITORY ","metadata":{}},{"cell_type":"code","source":"!git clone https://github.com/WongKinYiu/yolor.git\n%cd yolor\n!pip install -r requirements.txt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-16T09:11:50.547795Z","iopub.execute_input":"2024-11-16T09:11:50.548581Z","iopub.status.idle":"2024-11-16T09:13:12.330302Z","shell.execute_reply.started":"2024-11-16T09:11:50.548525Z","shell.execute_reply":"2024-11-16T09:13:12.329299Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 4B. INSTALL MISH CUDA","metadata":{}},{"cell_type":"code","source":"%cd ..\n!git clone https://github.com/JunnYu/mish-cuda\n%cd mish-cuda\n!git reset --hard 6f38976064cbcc4782f4212d7c0c5f6dd5e315a8\n!python setup.py build install\n%cd ..","metadata":{"_kg_hide-output":true,"trusted":true,"execution":{"iopub.status.busy":"2024-11-16T09:14:12.726985Z","iopub.execute_input":"2024-11-16T09:14:12.727832Z","iopub.status.idle":"2024-11-16T09:15:17.692935Z","shell.execute_reply.started":"2024-11-16T09:14:12.727787Z","shell.execute_reply":"2024-11-16T09:15:17.691944Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 4C. INSTALL PYTORCH WAVELETS ","metadata":{}},{"cell_type":"code","source":"!git clone https://github.com/fbcotter/pytorch_wavelets\n%cd pytorch_wavelets\n!pip install .\n%cd ..","metadata":{"_kg_hide-output":true,"trusted":true,"execution":{"iopub.status.busy":"2024-11-16T09:15:17.694841Z","iopub.execute_input":"2024-11-16T09:15:17.695082Z","iopub.status.idle":"2024-11-16T09:15:30.877392Z","shell.execute_reply.started":"2024-11-16T09:15:17.695052Z","shell.execute_reply":"2024-11-16T09:15:30.876462Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 4D. DWONLOAD LATEST CHECKPOINT FROM YoloR MODEL HUB \n\nIn this notebook we take P6 model (because I want to show only how to train YoloR model on Kaggle) but you can experiment with other YoloR models: https://github.com/WongKinYiu/yolor","metadata":{}},{"cell_type":"code","source":"!git clone https://github.com/fonzi22/AICITY2024_Track4.git","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-16T09:16:24.885264Z","iopub.execute_input":"2024-11-16T09:16:24.886082Z","iopub.status.idle":"2024-11-16T09:16:28.317703Z","shell.execute_reply.started":"2024-11-16T09:16:24.886041Z","shell.execute_reply":"2024-11-16T09:16:28.316586Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!wget https://github.com/WongKinYiu/yolor/releases/download/weights/yolor-w6-paper-555.pt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-16T09:16:35.684914Z","iopub.execute_input":"2024-11-16T09:16:35.685597Z","iopub.status.idle":"2024-11-16T09:16:57.447698Z","shell.execute_reply.started":"2024-11-16T09:16:35.685545Z","shell.execute_reply":"2024-11-16T09:16:57.446714Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 4E. CONFIGURE WEIGHTS AND BIASES FOR EXPERIMENT LOGGING ","metadata":{}},{"cell_type":"code","source":"# # more about Secrets -> https://www.kaggle.com/product-feedback/114053\n# import wandb\n# from kaggle_secrets import UserSecretsClient\n\n# user_secrets = UserSecretsClient()\n# wandb_api = user_secrets.get_secret(\"wandb_api\") \n# wandb.login(key=wandb_api)\n# wandb.login(anonymous='must')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-16T09:16:57.449832Z","iopub.execute_input":"2024-11-16T09:16:57.450107Z","iopub.status.idle":"2024-11-16T09:16:57.454477Z","shell.execute_reply.started":"2024-11-16T09:16:57.450074Z","shell.execute_reply":"2024-11-16T09:16:57.453487Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### 4F. CONFIGURE YoloR HYPERPARAMETERS ","metadata":{}},{"cell_type":"code","source":"# @register_line_cell_magic\n# def writetemplate(line, cell):\n#     with open(line, 'w') as f:\n#         f.write(cell.format(**globals()))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-16T08:13:25.159750Z","iopub.execute_input":"2024-11-16T08:13:25.160485Z","iopub.status.idle":"2024-11-16T08:13:25.163908Z","shell.execute_reply.started":"2024-11-16T08:13:25.160436Z","shell.execute_reply":"2024-11-16T08:13:25.163197Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# %%writetemplate /kaggle/working/hyp-yolor.yaml\n\n# lr0: 0.01  # initial learning rate (SGD=1E-2, Adam=1E-3)\n# lrf: 0.2  # final OneCycleLR learning rate (lr0 * lrf)\n# momentum: 0.937  # SGD momentum/Adam beta1\n# weight_decay: 0.0005  # optimizer weight decay 5e-4\n# warmup_epochs: 3.0  # warmup epochs (fractions ok)\n# warmup_momentum: 0.8  # warmup initial momentum\n# warmup_bias_lr: 0.1  # warmup initial bias lr\n# box: 0.05  # box loss gain\n# cls: 0.5  # cls loss gain\n# cls_pw: 1.0  # cls BCELoss positive_weight\n# obj: 1.0  # obj loss gain (scale with pixels)\n# obj_pw: 1.0  # obj BCELoss positive_weight\n# iou_t: 0.20  # IoU training threshold\n# anchor_t: 4.0  # anchor-multiple threshold\n# # anchors: 3  # anchors per output layer (0 to ignore)\n# fl_gamma: 0.0  # focal loss gamma (efficientDet default gamma=1.5)\n# hsv_h: 0.0  # image HSV-Hue augmentation (fraction)\n# hsv_s: 0.0  # image HSV-Saturation augmentation (fraction)\n# hsv_v: 0.0  # image HSV-Value augmentation (fraction)\n# degrees: 0.0  # image rotation (+/- deg)\n# translate: 0.5  # image translation (+/- fraction)\n# scale: 0.0  # image scale (+/- gain)\n# shear: 0.0  # image shear (+/- deg)\n# perspective: 0.0  # image perspective (+/- fraction), range 0-0.001\n# flipud: 0.0  # image flip up-down (probability)\n# fliplr: 0.5  # image flip left-right (probability)\n# mosaic: 0.95  # image mosaic (probability)\n# mixup: 0.3  # image mixup (probability)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-16T08:13:25.165025Z","iopub.execute_input":"2024-11-16T08:13:25.165446Z","iopub.status.idle":"2024-11-16T08:13:25.208700Z","shell.execute_reply.started":"2024-11-16T08:13:25.165406Z","shell.execute_reply":"2024-11-16T08:13:25.207920Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## 5. TRAIN YoloR","metadata":{}},{"cell_type":"code","source":"%cd AICITY2024_Track4/train/YoloR","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-16T09:17:05.959945Z","iopub.execute_input":"2024-11-16T09:17:05.960230Z","iopub.status.idle":"2024-11-16T09:17:05.966388Z","shell.execute_reply.started":"2024-11-16T09:17:05.960198Z","shell.execute_reply":"2024-11-16T09:17:05.965598Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!python train.py --batch-size 8 --img 1280 768 --data /kaggle/working/YoloR-data.yaml --cfg models/yolor-w6.yaml --weights /kaggle/working/yolor-w6-paper-555.pt --device 0,1 --name yolor_w6 --hyp hyp.scratch.1280.yaml --epochs 10","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-16T09:26:18.900236Z","iopub.execute_input":"2024-11-16T09:26:18.900763Z","iopub.status.idle":"2024-11-16T09:32:08.232269Z","shell.execute_reply.started":"2024-11-16T09:26:18.900713Z","shell.execute_reply":"2024-11-16T09:32:08.231430Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"We got an error - but it is connected with w&b integrations. Looking for solution.","metadata":{}},{"cell_type":"markdown","source":"## 6. INFERENCE USING YoloR ","metadata":{}}]}