{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"none","dataSources":[{"sourceId":67836,"databundleVersionId":7527339,"sourceType":"competition"}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false},"papermill":{"default_parameters":{},"duration":1012.214546,"end_time":"2024-01-20T13:05:55.388071","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2024-01-20T12:49:03.173525","version":"2.4.0"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# [DL Enigma 1.0 - SUST CSE Carnival 2024](https://www.kaggle.com/competitions/dl-enigma-10-sust-cse-carnival-2024/overview)\n**Bangladesh Road Object Detection for Autonomous Vehicles Challenge**\n\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-user-content/o/inbox%2F5328059%2F9b3067b3ff9d867fd242ccb1b6bd9634%2Fsdasdasdas.jpg?generation=1705692279083086&alt=media\">\n","metadata":{"papermill":{"duration":0.006768,"end_time":"2024-01-20T12:49:06.697217","exception":false,"start_time":"2024-01-20T12:49:06.690449","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"# 🔏 Key Points\n* Use of **external datasets** to train the model is **prohibited**. Publicly available **pre-training models** are **allowed**, the data used for pre-training the models must be disclose.\n* The dataset comprises 9825 images covering 9 districts in Bangladesh, with 78,943 objects annotated with rectangular bounding boxes across **13 classes**, presenting diverse road types for algorithmic challenges in various driving contexts.\n* The competition focuses on evaluating **mAP50** with IoU set at 0.5, emphasizing the significance of detecting all 13 classes for optimal performance.","metadata":{}},{"cell_type":"markdown","source":"# 🏗️ Install Libraries","metadata":{}},{"cell_type":"code","source":"!pip install ultralytics","metadata":{"_kg_hide-input":false,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-02-12T16:05:58.930224Z","iopub.execute_input":"2024-02-12T16:05:58.930596Z","iopub.status.idle":"2024-02-12T16:06:14.549892Z","shell.execute_reply.started":"2024-02-12T16:05:58.930567Z","shell.execute_reply":"2024-02-12T16:06:14.548763Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 📥 Import Libraries","metadata":{}},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nfrom tqdm import tqdm\n\nimport random\n\nfrom os import listdir\nfrom os.path import isfile,join\n\nfrom ultralytics import RTDETR\n\n\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nfrom PIL import Image","metadata":{"papermill":{"duration":0.832795,"end_time":"2024-01-20T12:49:07.54759","exception":false,"start_time":"2024-01-20T12:49:06.714795","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-12T16:06:14.551889Z","iopub.execute_input":"2024-02-12T16:06:14.552227Z","iopub.status.idle":"2024-02-12T16:06:22.535212Z","shell.execute_reply.started":"2024-02-12T16:06:14.552195Z","shell.execute_reply":"2024-02-12T16:06:22.534042Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 🐋 A Dive into MetaData\n* `train/images`- Folder contains training images of format `{District}_{Location/Time}_{image_id}.jpg`\n* `train/labels`- Folder contains training labels of format `{District}_{Location/Time}_{image_id}.txt`. Labels are in **YOLO** format so this `.txt` files contain classes and bounding boxes accordingly.\n* `District`- 9 districts in Bangladesh: Sylhet, Dhaka, Rajshahi, Mymensingh, Maowa, Chittagong, Sirajganj, Sherpur, and Khulna.\n* `Location/Time`- Specific Location or time the photo was taken.\n* `image_id`- ID code for the image.\n* **YOLO Format**-  one *.txt file per image. The *.txt file should be formatted with one row per object in class x_center y_center width height format. Box coordinates must be in normalized xywh format (from 0 to 1).","metadata":{}},{"cell_type":"markdown","source":"# 🛠️ Essential Functions\n* `showim_xyxy(path, boxes, classes, scores)`- here,\n   * path - absolute path of the image file\n   * boxes - list containing bounding boxes, here boxes contains all the rectangular box with x_min,\n     y_min, x_max, y_max points\n   * classes - list containing class of each box\n   * scores - list containing confidence score of each box\n   \n  **This function plots image with predicted bounding boxes**\n*`get_boxes( boxes )`- here,\n  * boxes - bounding boxes with coordinate xywh (x_center, y_center, width, height) format\n  \n **This function returns value in xyxy (x_min, y_min, x_max, y_max) format**","metadata":{}},{"cell_type":"code","source":"def showim_xyxy(path,boxes,classes,scores):\n    image = plt.imread(path)\n    fig, ax = plt.subplots()\n    ax.imshow(image)\n    i=0\n    for i in range(len(boxes)):\n        x1, y1, x2, y2 = boxes[i]\n        imge = Image.open(path) \n        w = imge.width \n        h = imge.height\n        x1=x1*w\n        y1=y1*h\n        x2=x2*w\n        y2=y2*h\n        rect = patches.Rectangle((x1, y1), x2 - x1, y2 - y1, linewidth=2, edgecolor='red', facecolor='none')\n        ax.add_patch(rect)\n        ax.text(x1, y1 - 5, f\"{classes[i]} {scores[i]:.2f}\", fontsize=8, color='red')\n    plt.axis('off')  # Turn off axes for a cleaner look\n    plt.title('Image with Bounding Boxes and Labels')\n    plt.show()\n\n\ndef get_boxes(boxes):\n    box_in_img = []\n    for i, score in enumerate(boxes):\n        single_pred_str = \"\"\n        \n        x_center , y_center, width,height = boxes[i]\n        x1 = float(x_center) - (float(width) / 2)\n        y1 = float(y_center) - (float(height) / 2)\n        width = float(width)\n        height = float(height)\n        box_in_img.append([x1,y1,x1+width,y1+height])\n    return box_in_img","metadata":{"execution":{"iopub.status.busy":"2024-02-12T16:06:28.417751Z","iopub.execute_input":"2024-02-12T16:06:28.418599Z","iopub.status.idle":"2024-02-12T16:06:28.429439Z","shell.execute_reply.started":"2024-02-12T16:06:28.418564Z","shell.execute_reply":"2024-02-12T16:06:28.428379Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 🕵 Exploratory Data Analysis","metadata":{}},{"cell_type":"code","source":"ipath = '/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/images/train'\nimages = [join(ipath,f) for f in listdir(ipath) if isfile(join(ipath, f))]\nlpath = '/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/labels/train'\nlabels = [join(lpath,f) for f in listdir(lpath) if isfile(join(lpath, f))]\nimages.sort()\nlabels.sort()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-02-12T16:06:34.359474Z","iopub.execute_input":"2024-02-12T16:06:34.35984Z","iopub.status.idle":"2024-02-12T16:06:52.567115Z","shell.execute_reply.started":"2024-02-12T16:06:34.359812Z","shell.execute_reply":"2024-02-12T16:06:52.566302Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#randomly printing an images with labels\nrandom.seed(10)\nid = random.randint(0, 5000)\nimg = images[id]\nlab = labels[id]\nboxes = []\nclasses = []\nscores = []\nwith open(lab,'r') as file:\n    content = file.read().split('\\n')\nfor i in range(len(content)):\n    if not content[i].strip():\n            continue\n    single_box = [float(num) for num in content[i].split(' ')]\n    boxes.append(single_box[1:])\n    classes.append(single_box[0])\n    scores.append(float(1.0))\nshowim_xyxy(img,get_boxes(boxes),classes,scores) #we have to first convert boxes from xywh form to xyxy","metadata":{"execution":{"iopub.status.busy":"2024-02-12T16:06:52.568812Z","iopub.execute_input":"2024-02-12T16:06:52.569076Z","iopub.status.idle":"2024-02-12T16:06:53.201129Z","shell.execute_reply.started":"2024-02-12T16:06:52.569053Z","shell.execute_reply":"2024-02-12T16:06:53.200086Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Class And Bounding Boxes","metadata":{}},{"cell_type":"code","source":"class_id_to_name = {\n    0: 'auto_rickshaw',\n    1: 'bicycle',\n    2: 'bus',\n    3: 'car',\n    4: 'cart_vehicle',\n    5: 'construction_vehicle',\n    6: 'motorbike',\n    7: 'person',\n    8: 'priority_vehicle',\n    9: 'three_wheeler',\n    10: 'train',\n    11: 'truck',\n    12: 'wheelchair'\n}\n\n# Define the label folder path\nlabel_folder = \"/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/labels/train\"\n\n# Initialize a dictionary to store class counts\nclass_counts = {class_id: 0 for class_id in class_id_to_name}\n\n# Iterate through each text file in the label folder\nfor filename in os.listdir(label_folder):\n    if filename.endswith(\".txt\"):\n        file_path = os.path.join(label_folder, filename)\n        \n        # Read the content of the label file and count class occurrences\n        with open(file_path, 'r') as file:\n            lines = file.readlines()\n\n        for line in lines:\n            parts = line.strip().split()\n            if parts:  # Check if the line is not empty\n                class_id = int(parts[0])\n                if class_id in class_counts:\n                    class_counts[class_id] += 1\n\n# Print the class counts\nfor class_id, count in class_counts.items():\n    class_name = class_id_to_name[class_id]\n    print(f'Class {class_id} ({class_name}): {count} instances')\nimport numpy as np\n# Create a bar plot\nclass_ids = np.array(list(class_counts.keys()))\n# print(class_ids)\nclass_names = [class_id_to_name[class_id] for class_id in class_ids]\ncounts = np.array(list(class_counts.values()))\ncolors = plt.cm.viridis(np.linspace(0, 1, len(class_ids)))\n\nplt.bar(class_names, counts, color=colors)\nplt.xlabel('Class')\nplt.ylabel('Number of Instances')\nplt.title('Number of Instances in Each Class')\nplt.xticks(rotation=45, ha=\"right\")  # Rotate x-axis labels for better visibility\nplt.tight_layout()\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-02-12T16:06:53.202352Z","iopub.execute_input":"2024-02-12T16:06:53.202638Z","iopub.status.idle":"2024-02-12T16:07:17.687534Z","shell.execute_reply.started":"2024-02-12T16:06:53.202612Z","shell.execute_reply":"2024-02-12T16:07:17.686595Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Day/Night Image","metadata":{}},{"cell_type":"code","source":"# pie plot of distribution of day and night images\nnight_images = [filename for filename in images if 'night' in filename.lower()]\nnight_labels = [filename for filename in labels if 'night' in filename.lower()]\nlen(night_images)\nlabel = ['Day', 'Night']\nsizes = [len(images)-len(night_images),len(night_images)]\nplt.pie(sizes, labels=label, autopct='%1.1f%%', startangle=90)\nplt.title('Distribution of Day and Night')\nplt.show()\n\n# class distribution of day and night images\n\nnight_class_counts = {class_id: 0 for class_id in class_id_to_name}\n\nfor file_path in night_labels:\n    # Read the content of the label file and count class occurrences\n    with open(file_path, 'r') as file:\n        lines = file.readlines()\n\n    for line in lines:\n        parts = line.strip().split()\n        if parts:  # Check if the line is not empty\n            class_id = int(parts[0])\n            if class_id in night_class_counts:\n                night_class_counts[class_id] += 1\n\nnight_counts = np.array(list(night_class_counts.values()))\nday_counts = counts-night_counts\n\nfig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 5))\ncolors = plt.cm.viridis(np.linspace(0, 1, len(class_ids)))\n\naxes[0].bar(class_names, night_counts, color=colors)\naxes[0].set_xlabel('Class')\naxes[0].set_ylabel('Number of Instances')\naxes[0].set_title('Number of Instances in Each Class')\naxes[0].set_xticklabels(class_names, rotation=45, ha=\"right\")\n\naxes[0].set_ylim(0, 17000)\naxes[0].set_ylim(0, 17000)\n\n# Bar plot for day counts\naxes[1].bar(class_names, day_counts, color=colors)\naxes[1].set_xlabel('Class')\naxes[1].set_ylabel('Number of Instances')\naxes[1].set_title('Number of Instances in Each Class (Day)')\naxes[1].set_xticklabels(class_names, rotation=45, ha=\"right\")  # Rotate x-axis labels for better visibility\nplt.tight_layout()\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-02-12T16:07:17.689119Z","iopub.execute_input":"2024-02-12T16:07:17.68942Z","iopub.status.idle":"2024-02-12T16:07:18.968908Z","shell.execute_reply.started":"2024-02-12T16:07:17.689394Z","shell.execute_reply":"2024-02-12T16:07:18.96789Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 📈 WandB\nWeights & Biases (WandB) is a machine learning development platform that allows users to track and visualize various aspects of their model training process in real-time. Utilize W&B to accelerate model development by incorporating experiment tracking, dataset versioning, and efficient model management. Some of them are:\n* Monitor, contrast, and depict machine learning experiments.\n* Access real-time metrics, terminal logs, and system statistics on a centralized dashboard as they are continuously streamed.\n* Display improvement graphs for different versions, discuss and address bugs, and showcase progress towards milestones.","metadata":{}},{"cell_type":"code","source":"import wandb\n\ntry:\n    from kaggle_secrets import UserSecretsClient\n    user_secrets = UserSecretsClient()\n    api_key = user_secrets.get_secret(\"WANDB\")\n    wandb.login(key=api_key,relogin=True)\n    anonymous = None\nexcept:\n    wandb.login(anonymous='must')\n    print('To use your W&B account,\\nGo to Add-ons -> Secrets and provide your W&B access token. Use the Label name as WANDB. \\nGet your W&B access token from here: https://wandb.ai/authorize')","metadata":{"papermill":{"duration":14.771006,"end_time":"2024-01-20T12:49:22.325184","exception":false,"start_time":"2024-01-20T12:49:07.554178","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-12T16:12:49.138847Z","iopub.execute_input":"2024-02-12T16:12:49.139297Z","iopub.status.idle":"2024-02-12T16:12:49.364974Z","shell.execute_reply.started":"2024-02-12T16:12:49.139265Z","shell.execute_reply":"2024-02-12T16:12:49.364213Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 📦Dataset","metadata":{}},{"cell_type":"markdown","source":"## Splitting into Train and Val\nWe will use the full dataset for training as it gives the best score and 20% of total data for validation.","metadata":{}},{"cell_type":"code","source":"import os\nimport shutil\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import tqdm\n\ndef copy_data(images_path, labels_path, destination_path, train_ratio=0.8, random_seed=42):\n    # Create new directories for train and val in the destination path\n    train_dir = os.path.join(destination_path, 'train')\n    val_dir = os.path.join(destination_path, 'val')\n    os.makedirs(train_dir, exist_ok=True)\n    os.makedirs(val_dir, exist_ok=True)\n\n    # Create subdirectories for 'images' and 'labels' within 'train' and 'val'\n    train_images_dir = os.path.join(train_dir, 'images')\n    train_labels_dir = os.path.join(train_dir, 'labels')\n    val_images_dir = os.path.join(val_dir, 'images')\n    val_labels_dir = os.path.join(val_dir, 'labels')\n    os.makedirs(train_images_dir, exist_ok=True)\n    os.makedirs(train_labels_dir, exist_ok=True)\n    os.makedirs(val_images_dir, exist_ok=True)\n    os.makedirs(val_labels_dir, exist_ok=True)\n\n    # List all image and label files\n    image_files = os.listdir(os.path.join(images_path, 'train'))\n    label_files = os.listdir(os.path.join(labels_path, 'train'))\n\n    # Split files into train and val\n    image_train, image_val, label_train, label_val = train_test_split(\n        image_files, label_files, test_size=(1 - train_ratio), random_state=random_seed\n    )\n\n    # Copy images and labels to 'images' and 'labels' folders in train\n    for image_file in tqdm(image_files, desc='Copying train images'):\n        shutil.copy2(os.path.join(images_path, 'train', image_file), os.path.join(train_images_dir, image_file))\n    for label_file in tqdm(label_files, desc='Copying train labels'):\n        shutil.copy2(os.path.join(labels_path, 'train', label_file), os.path.join(train_labels_dir, label_file))\n\n    # Copy remaining images and labels to 'images' and 'labels' folders in val\n    for image_file in tqdm(image_val, desc='Copying val images'):\n        shutil.copy2(os.path.join(images_path, 'train', image_file), os.path.join(val_images_dir, image_file))\n    for label_file in tqdm(label_val, desc='Copying val labels'):\n        shutil.copy2(os.path.join(labels_path, 'train', label_file), os.path.join(val_labels_dir, label_file))\n\nif __name__ == \"__main__\":\n    images_folder = '/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/images'\n    labels_folder = '/kaggle/input/dl-enigma-10-sust-cse-carnival-2024/dlenigma1/BadODD/labels'\n    destination_folder = '/kaggle/working/datasets/badodd/'\n\n    copy_data(images_folder, labels_folder, destination_folder)","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-02-12T16:10:33.858086Z","iopub.execute_input":"2024-02-12T16:10:33.858465Z","iopub.status.idle":"2024-02-12T16:11:53.540335Z","shell.execute_reply.started":"2024-02-12T16:10:33.858434Z","shell.execute_reply":"2024-02-12T16:11:53.539344Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport matplotlib.pyplot as plt\n\n# Define the paths to the image folders\ntrain_path = \"/kaggle/working/datasets/badodd/train/images\"\nval_path = \"/kaggle/working/datasets/badodd/val/images\"\n\n# Function to count the number of images in a folder\ndef count_images(folder_path):\n    return len([f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))])\n\n# Count the number of images in each folder\ntrain_images_count = count_images(train_path)\nval_images_count = count_images(val_path)\n\n# Bar plot\nfolders = ['Train', 'Validation']\nimage_counts = [train_images_count, val_images_count]\n\nplt.bar(folders, image_counts, color=['blue', 'orange'])\nplt.xlabel('Folders')\nplt.ylabel('Number of Images')\nplt.title('Number of Images in Train and Validation Folders')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-12T16:11:53.541848Z","iopub.execute_input":"2024-02-12T16:11:53.542403Z","iopub.status.idle":"2024-02-12T16:11:53.767247Z","shell.execute_reply.started":"2024-02-12T16:11:53.542375Z","shell.execute_reply":"2024-02-12T16:11:53.766122Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 🔩 Configuration","metadata":{}},{"cell_type":"code","source":"file_content = \"\"\"\npath: /kaggle/working/datasets/badodd  # dataset root dir\ntrain: train  # train images\nval: val  # val images\n\n# Classes\nnames:\n    0: auto_rickshaw\n    1: bicycle\n    2: bus\n    3: car\n    4: cart_vehicle\n    5: construction_vehicle\n    6: motorbike\n    7: person\n    8: priority_vehicle\n    9: three_wheeler\n    10: train\n    11: truck\n    12: wheelchair\n\"\"\"\n\nwith open(\"rtdetr.yaml\", mode=\"w\") as f:\n    f.write(file_content)","metadata":{"papermill":{"duration":0.016909,"end_time":"2024-01-20T12:49:34.432163","exception":false,"start_time":"2024-01-20T12:49:34.415254","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2024-02-12T16:11:53.768432Z","iopub.execute_input":"2024-02-12T16:11:53.76874Z","iopub.status.idle":"2024-02-12T16:11:53.77534Z","shell.execute_reply.started":"2024-02-12T16:11:53.768713Z","shell.execute_reply":"2024-02-12T16:11:53.774228Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 🏋️‍♂️Training\n### Some notes about training\n\n* We use **RTDETR** for this competetion.\n* Train model1 for 100 epochs and model2 for 60 epochs.\n* Then we combine this two models with **WBF**\n* Using **GPU T4x2** as **Accelerator** given by Kaggle for faster training\n\n### RTDETR\n\nReal-Time Detection Transformer (RT-DETR), developed by Baidu, is a cutting-edge end-to-end object detector that provides real-time performance while maintaining high accuracy. It leverages the power of Vision Transformers (ViT) to efficiently process multiscale features by decoupling intra-scale interaction and cross-scale fusion. RT-DETR is highly adaptable, supporting flexible adjustment of inference speed using different decoder layers without retraining. The model excels on accelerated backends like CUDA with TensorRT, outperforming many other real-time object detectors.\n<img src=\"https://user-images.githubusercontent.com/26833433/238963168-90e8483f-90aa-4eb6-a5e1-0d408b23dd33.png\">\n\n### Parameters\n * `epochs` : number of epochs\n * `imgsz` : image_size\n * `batch` : batch_size\n * `pretrained` : using pretrained weight (True/False)\n * `data` : config of dataset\n * `seed` : random seed for regeneration\n * `device` : Accelerator id for training\n * `cache` : load all images into the memory, will speed up the training\n * `Augmentations & others` : degrees, translate, scale, etc.(Here we prefered to use default augmentations mostly)","metadata":{}},{"cell_type":"markdown","source":"### model1 with 100 epochs","metadata":{}},{"cell_type":"code","source":"model1 = RTDETR('rtdetr-x.pt')\nmodel1.train(\n    \n    #loading data and model parameters\n    data='/kaggle/working/rtdetr.yaml',\n    pretrained=True,\n    cache=True, \n    device=[0, 1],\n    \n    #training parameteres\n    epochs=100,\n    batch=16, \n    imgsz=640, \n    save=True,\n    \n    #random seed\n    seed = 0,\n    \n    # Augmentation\n    degrees=0.0, \n    translate=0.1,\n    scale=0.5, \n    shear=0.0, \n    perspective=0.0,\n    flipud=0.0, \n    fliplr=0.5, \n    mosaic=1.0, \n    mixup=0.0, \n\n    # Optimization parameters\n    lr0=0.018,\n    patience=3,\n    optimizer=\"auto\",\n    momentum=0.947,\n    weight_decay=0.0005,\n    warmup_momentum=0.8,\n)","metadata":{"papermill":{"duration":1.445596,"end_time":"2024-01-20T12:49:35.884406","exception":false,"start_time":"2024-01-20T12:49:34.43881","status":"completed"},"tags":[],"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-02-12T16:13:08.803995Z","iopub.execute_input":"2024-02-12T16:13:08.804382Z","iopub.status.idle":"2024-02-12T17:20:52.47572Z","shell.execute_reply.started":"2024-02-12T16:13:08.804348Z","shell.execute_reply":"2024-02-12T17:20:52.474847Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### model2 with 60 epochs","metadata":{}},{"cell_type":"code","source":"model2 = RTDETR('rtdetr-x.pt')\nmodel2.train(\n    \n    #loading data and model parameters\n    data='/kaggle/working/rtdetr.yaml',\n    pretrained=True,\n    cache=True, \n    device=[0, 1],\n    \n    #training parameteres\n    epochs=60,\n    batch=16, \n    imgsz=640, \n    save=True,\n    \n    #random seed\n    seed = 0,\n    \n    # Augmentation\n    degrees=0.0, \n    translate=0.1,\n    scale=0.5, \n    shear=0.0, \n    perspective=0.0,\n    flipud=0.0, \n    fliplr=0.5, \n    mosaic=1.0, \n    mixup=0.0, \n\n    # Optimization parameters\n    lr0=0.018,\n    patience=3,\n    optimizer=\"auto\",\n    momentum=0.947,\n    weight_decay=0.0005,\n    warmup_momentum=0.8,\n)","metadata":{"execution":{"iopub.execute_input":"2024-01-20T13:05:48.77935Z","iopub.status.busy":"2024-01-20T13:05:48.778957Z","iopub.status.idle":"2024-01-20T13:05:48.785209Z","shell.execute_reply":"2024-01-20T13:05:48.784298Z"},"papermill":{"duration":0.368677,"end_time":"2024-01-20T13:05:48.787504","exception":false,"start_time":"2024-01-20T13:05:48.418827","status":"completed"},"tags":[]},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 🔭 Results","metadata":{}},{"cell_type":"markdown","source":"### Batch Image","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (10,10))\nplt.axis('off')\nplt.imshow(plt.imread(f'/kaggle/working/runs/detect/train/labels.jpg'));","metadata":{"execution":{"iopub.status.busy":"2024-02-12T17:20:52.477967Z","iopub.execute_input":"2024-02-12T17:20:52.478752Z","iopub.status.idle":"2024-02-12T17:20:53.184202Z","shell.execute_reply.started":"2024-02-12T17:20:52.478711Z","shell.execute_reply":"2024-02-12T17:20:53.183209Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Class Distribution","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (10,10))\nplt.axis('off')\nplt.imshow(plt.imread(f'/kaggle/working/runs/detect/train/labels_correlogram.jpg'));","metadata":{"execution":{"iopub.status.busy":"2024-02-12T17:20:53.185454Z","iopub.execute_input":"2024-02-12T17:20:53.185789Z","iopub.status.idle":"2024-02-12T17:20:54.08462Z","shell.execute_reply.started":"2024-02-12T17:20:53.185759Z","shell.execute_reply":"2024-02-12T17:20:54.083735Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Score Vs Epoch","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread(f'/kaggle/working/runs/detect/train/results.png'));","metadata":{"execution":{"iopub.status.busy":"2024-02-12T17:20:54.0866Z","iopub.execute_input":"2024-02-12T17:20:54.086908Z","iopub.status.idle":"2024-02-12T17:20:55.572791Z","shell.execute_reply.started":"2024-02-12T17:20:54.08688Z","shell.execute_reply":"2024-02-12T17:20:55.57178Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Confusion Matrix","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(12,10))\nplt.axis('off')\nplt.imshow(plt.imread(f'/kaggle/working/runs/detect/train/confusion_matrix.png'));","metadata":{"execution":{"iopub.status.busy":"2024-02-12T17:20:55.57411Z","iopub.execute_input":"2024-02-12T17:20:55.574509Z","iopub.status.idle":"2024-02-12T17:20:57.219132Z","shell.execute_reply.started":"2024-02-12T17:20:55.574474Z","shell.execute_reply":"2024-02-12T17:20:57.218234Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Metrics","metadata":{}},{"cell_type":"code","source":"for metric in ['F1', 'PR', 'P', 'R']:\n    print(f'Metric: {metric}')\n    plt.figure(figsize=(12,10))\n    plt.axis('off')\n    plt.imshow(plt.imread(f'/kaggle/working/runs/detect/train/{metric}_curve.png'));\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-12T17:20:57.220574Z","iopub.execute_input":"2024-02-12T17:20:57.220923Z","iopub.status.idle":"2024-02-12T17:20:59.874278Z","shell.execute_reply.started":"2024-02-12T17:20:57.220894Z","shell.execute_reply":"2024-02-12T17:20:59.873288Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 📢 References and Acknowledgement\n* Some ideas of this notebook is form [Great-Barrier-Reef: YOLOv5 [train] 🌊](https://www.kaggle.com/awsaf49/great-barrier-reef-yolov5-train)\n* Image 1 is taken from the [Competition](https://www.kaggle.com/competitions/dl-enigma-10-sust-cse-carnival-2024/overview) main page\n* Image 2 is taken from ultralytics [RTDETR Docs](https://docs.ultralytics.com/models/rtdetr/)\n* Image 3 is from [This blog](https://m.somewhereinblog.net/)\n\n<div style=\"text-align: center;\">\n    <img src=\"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoHCA8PDw8PDxEQDw8PEg8QDw8QERERERESGBQZHBgaGBkcIS4mHB4rLRgYJzgrKzAxNTc1GiQ7QDszSC40QzEBDAwMEA8PHhISHzQnJSE0MTQxMTE6PTQ0MTQxMTE0NDExND8xMTQ0NDo0ND80NDExMTE/NDExMTExMTExMTExMf/AABEIAKQBNAMBIgACEQEDEQH/xAAcAAEAAwEBAQEBAAAAAAAAAAAABQYHAQQDAgj/xABLEAABAwEDBggKBgkEAgMAAAABAAIDBAUGEQcSFiGT0jE1QVFTVHGzExQVFyJhgZGSsjQ2VXN00SMyUmJyhJShsTNCdfAIwSZFw//EABgBAQADAQAAAAAAAAAAAAAAAAABAgME/8QAHhEBAQADAQADAQEAAAAAAAAAAAECERIxITJBUQP/2gAMAwEAAhEDEQA/APbdm71HUUcM00RfI8y5zvCStxzZHtGprgOABSmiVndAdtPvpczi6n7Z++kU6ufLK7aSTSC0Ss7oDtp99NErO6A7affU6ijqp1EFolZ3QHbT76aJWd0B20++p1E6pqILRKzugO2n300Ss7oDtp99TqJ1TUQWiVndAdtPvpolZ3QHbT76nUTqmogtErO6A7affTRKzugO2n31OonVNRBaJWd0B20++miVndAdtPvqdROqaiC0Ss7oDtZ99NErP6A7affU6idU1EFolZ/QHbT76aJWf0B20++p1E6pqILRKz+gO2n300Ss/oDtp99TqJ1TUQWiVn9AdtPvpolZ/QHbT76nUTqmogtErP6A7affTRKz+gO2n31OonVNRBaJWf0B20++miVn9AdtPvqdROqaiC0Ss/oDtp99NErP6A7affU6idU1EFolZ3QHbT76aJWf0B20++p1E6pqILRKzugO2n300Ss/oDtp99TqJ1TUQWiVn9AdtPvpolZ/QHbT76nUTqmogtErO6A7affTRKzugO2n31OonVNRBaJWd0B20++miVndAdtPvqdROqajJryUsdPWzwxNzI2GPNbi52GMbSdbnY8JKL7Xw4xqe2HuWLi1VXe5nF1P2z99Ip1QVzOLqftn76RTqyy9WngiIoBERAREQEREBERAREQEREBERAREUAiIgIiICIiAiIgIiICIiAiIgIiKQRERLLL4cY1PbD3LFxdvhxjU9sPcsRbTxRdrmcXU/bP30inVBXM4up+2fvpFOrLL1aeCIigEREBeC1rXpqKPwlTI2Nv+0E+k88zW8JPYvvXVbIIpJpDgyJjnvPqAWXXasae89dLVVb3MooD6QacMBwiNnNq1k/8ARfDHpGV0lZ8q1G1xDKeoe0H9YmNmPrAxKmrBv3Z9c4RhzqeU6msnzW5x5muBwJUlc6usGqnqaGipIG+Las50MZ8O0anODjiXDHn7VG5R8ndK+mlraGJtPUwNdI6OIBkcrRgXeiBqcACRhgtOMVeqtSKq5N7Ulr7JncSX1dGHsbn6xIQwujzuU83sVa06tsf/AFbthUrPjJPUaeizDTu2vst2wqU07tr7LdsKlOMjqNPRZhp3bX2W7YVKad219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