MORTISCOPE: RT-DETR Model Training and Evaluation

Author: Mortiscope

This Jupyter Notebook serves as the core development and training environment for the object detection component of the MORTISCOPE system. The primary objective is to train a RT-DETR model to accurately detect and classify the different life stages of Chrysomya megacephala. The successful training and evaluation of this model are fundamental to the application’s ability to process images and provide accurate entomological data for PMI estimation.

Workflow Summary

This notebook follows a multi-stage pipeline specifically optimized for transformer-based object detection in forensic entomology. It transitions from raw data acquisition to a production-ready, scene-adaptive inference engine:

  1. Environment and Storage Initialization: Configures the Google Colab environment by mounting Google Drive for persistent artifact storage. It installs the specialized ultralytics framework for RT-DETR support and the sahi library for high-resolution image processing.
  2. Dataset Management: Facilitates the acquisition of multiple versioned datasets from Roboflow. It features a dynamic aggregation script that programmatically builds a master data.yaml configuration, ensuring all training paths are verified across multiple source directories.
  3. RT-DETR Transformer Pipeline: Configures and initializes the RT-DETR-Large architecture. Unlike traditional CNN-based detectors, this pipeline leverages a Vision Transformer (ViT) backbone. It uses a specialized training regimen involving the AdamW optimizer, Cosine Learning Rate decay, and a custom Epoch History Callback that archives model weights to Google Drive every epoch to ensure zero data loss during session timeouts.
  4. Specialized Performance Analysis: Provides metrics unique to transformer architectures, such as GIoU (Generalized IoU) and L1 Box Loss. It generates high-resolution visualizations, including smoothed loss curves and a normalized confusion matrix to identify morphological similarities that lead to misclassification between biological life stages.
  5. Small-Object Slicing Integration: Incorporates SAHI (Sliced Aided Hyper Inference) to overcome the limitations of standard resizing. By slicing images into overlapping windows, the notebook ensures that tiny instar larvae remain detectable.
  6. Deployment & Demonstration: Finalizes the model by exporting it to the ONNX (Open Neural Network Exchange) format using Opset 16 for maximum compatibility with transformer operators. It concludes with an Interactive Inference Engine that uses a heuristic scene detection logic to automatically toggle between standard and sliced inference modes.

Tech Stack

This project utilizes a specialized stack designed to bridge the gap between high-accuracy vision transformers and the real-time requirements of forensic field analysis.

Deep Learning and Transformer Engines

  • Ultralytics RT-DETR: The first real-time end-to-end object detector based on the transformer architecture. This notebook utilizes the Large (l) variant, which replaces traditional CNN backbones with a Vision Transformer (ViT). Key features include the AIFI (Attention-based Intra-scale Feature Interaction) and CCFM (CNN-based Cross-scale Feature Fusion Module) modules, which eliminate the need for Non-Maximum Suppression (NMS) during the core inference process, resulting in lower latency and higher precision.
  • SAHI (Sliced Aided Hyper Inference): An advanced slicing library crucial for forensic imagery. It allows the transformer to detect microscopic features by dividing the image into overlapping slices, performing inference on each, and merging the results.
  • PyTorch: The underlying deep learning framework used for tensor computations and managing the complex attention mechanisms within the RT-DETR architecture.
  • ONNX (Open Neural Network Exchange): A standard format used to export the trained transformer model. This notebook uses Opset 16 to ensure full support for the specialized operators used in transformer-based architectures.

Data Management and Processing

  • Roboflow: Acts as the central repository for the Chrysomya megacephala dataset, managing versioning, image preprocessing, and dataset health checks.
  • Google Drive: Provides a persistent storage layer, ensuring that large model checkpoints (.pt files) and historical training logs (results.csv) are preserved across Google Colab session restarts.
  • Pandas: Used to analyze and parse the training logs, specifically handling the unique loss columns generated by the RT-DETR trainer.
  • PyYAML: Automates the generation of the dataset configuration file, dynamically linking multiple distributed image directories into a single training source.

Visualization and Analysis

  • OpenCV (cv2): The primary engine for image manipulation, responsible for color space conversions, drawing high-precision bounding boxes, and handling the macro and field scene detection logic.
  • Seaborn & Matplotlib: Used to generate statistical visualizations. This includes smoothed training curves for transformer-specific metrics and the heatmap-based confusion matrix for biological stage differentiation.
  • NumPy: Facilitates high-speed mathematical operations for custom Non-Maximum Suppression (NMS) in the interactive demonstration and performs geometric calculations for outlier filtering based on object-to-image area ratios.

Section 1: Project Initialization and Dependencies

Purpose: This section establishes the foundational infrastructure required for the project. It configures the file system to ensure that all training checkpoints, logs, and exported models are saved to persistent storage. It also installs the specific suite of libraries needed to handle transformer-based architectures and specialized image slicing.

Key Activities:

  • Persistent Storage Mapping: Connects to Google Drive to create a dedicated project directory. This is for RT-DETR training, as transformer models often require longer training durations and more frequent checkpointing.
  • Advanced Library Installation: Installs ultralytics which provides the RT-DETR implementation, roboflow for data access, and sahi for Sliced Aided Hyper Inference.
  • Security & Authentication: Securely retrieves the Roboflow API key using Colab’s secret management system to access datasets without exposing credentials in the code.
import os

from google.colab import drive

# Mounts Google Drive to allow the Colab notebook to access and save files directly.
drive.mount('/content/drive')

# Defines configuration variables for the main project and the specific model folder.
root_folder = "Mortiscope Models"
model_name = "RT-DETR"

# Constructs the full, platform-independent path to the model's project directory.
project_path = os.path.join('/content/drive/MyDrive', root_folder, model_name)
# Constructs the path for a dedicated subdirectory to store model weights.
weights_path = os.path.join(project_path, 'weights')

# Creates the project and weights directories.
os.makedirs(project_path, exist_ok=True)
os.makedirs(weights_path, exist_ok=True)

# Prints the fully constructed paths to the console for user confirmation.
print(f"Project Directory: {project_path}")
print(f"Weight Storage:   {weights_path}")
Mounted at /content/drive
Project Directory: /content/drive/MyDrive/Mortiscope Models/RT-DETR
Weight Storage:   /content/drive/MyDrive/Mortiscope Models/RT-DETR/weights
# Executes the shell command to install the specified libraries using pip.
!pip install roboflow ultralytics sahi shapely

# Prints a confirmation message to the console after the installation command completes.
print("Libraries installed successfully.")
Collecting roboflow
  Downloading roboflow-1.2.11-py3-none-any.whl.metadata (9.7 kB)
Collecting ultralytics
  Downloading ultralytics-8.3.251-py3-none-any.whl.metadata (37 kB)
Collecting sahi
  Downloading sahi-0.11.36-py3-none-any.whl.metadata (19 kB)
Requirement already satisfied: shapely in /usr/local/lib/python3.12/dist-packages (2.1.2)
Requirement already satisfied: certifi in /usr/local/lib/python3.12/dist-packages (from roboflow) (2025.11.12)
Collecting idna==3.7 (from roboflow)
  Downloading idna-3.7-py3-none-any.whl.metadata (9.9 kB)
Requirement already satisfied: cycler in /usr/local/lib/python3.12/dist-packages (from roboflow) (0.12.1)
Requirement already satisfied: kiwisolver>=1.3.1 in /usr/local/lib/python3.12/dist-packages (from roboflow) (1.4.9)
Requirement already satisfied: matplotlib in /usr/local/lib/python3.12/dist-packages (from roboflow) (3.10.0)
Requirement already satisfied: numpy>=1.18.5 in /usr/local/lib/python3.12/dist-packages (from roboflow) (2.0.2)
Collecting opencv-python-headless==4.10.0.84 (from roboflow)
  Downloading opencv_python_headless-4.10.0.84-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (20 kB)
Requirement already satisfied: Pillow>=7.1.2 in /usr/local/lib/python3.12/dist-packages (from roboflow) (11.3.0)
Collecting pi-heif<2 (from roboflow)
  Downloading pi_heif-1.1.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.metadata (6.5 kB)
Collecting pillow-avif-plugin<2 (from roboflow)
  Downloading pillow_avif_plugin-1.5.2-cp312-cp312-manylinux_2_28_x86_64.whl.metadata (2.1 kB)
Requirement already satisfied: python-dateutil in /usr/local/lib/python3.12/dist-packages (from roboflow) (2.9.0.post0)
Requirement already satisfied: python-dotenv in /usr/local/lib/python3.12/dist-packages (from roboflow) (1.2.1)
Requirement already satisfied: requests in /usr/local/lib/python3.12/dist-packages (from roboflow) (2.32.4)
Requirement already satisfied: six in /usr/local/lib/python3.12/dist-packages (from roboflow) (1.17.0)
Requirement already satisfied: urllib3>=1.26.6 in /usr/local/lib/python3.12/dist-packages (from roboflow) (2.5.0)
Requirement already satisfied: tqdm>=4.41.0 in /usr/local/lib/python3.12/dist-packages (from roboflow) (4.67.1)
Requirement already satisfied: PyYAML>=5.3.1 in /usr/local/lib/python3.12/dist-packages (from roboflow) (6.0.3)
Requirement already satisfied: requests-toolbelt in /usr/local/lib/python3.12/dist-packages (from roboflow) (1.0.0)
Collecting filetype (from roboflow)
  Downloading filetype-1.2.0-py2.py3-none-any.whl.metadata (6.5 kB)
Requirement already satisfied: opencv-python>=4.6.0 in /usr/local/lib/python3.12/dist-packages (from ultralytics) (4.12.0.88)
Requirement already satisfied: scipy>=1.4.1 in /usr/local/lib/python3.12/dist-packages (from ultralytics) (1.16.3)
Requirement already satisfied: torch>=1.8.0 in /usr/local/lib/python3.12/dist-packages (from ultralytics) (2.9.0+cu126)
Requirement already satisfied: torchvision>=0.9.0 in /usr/local/lib/python3.12/dist-packages (from ultralytics) (0.24.0+cu126)
Requirement already satisfied: psutil>=5.8.0 in /usr/local/lib/python3.12/dist-packages (from ultralytics) (5.9.5)
Requirement already satisfied: polars>=0.20.0 in /usr/local/lib/python3.12/dist-packages (from ultralytics) (1.31.0)
Collecting ultralytics-thop>=2.0.18 (from ultralytics)
  Downloading ultralytics_thop-2.0.18-py3-none-any.whl.metadata (14 kB)
Requirement already satisfied: click in /usr/local/lib/python3.12/dist-packages (from sahi) (8.3.1)
Collecting fire (from sahi)
  Downloading fire-0.7.1-py3-none-any.whl.metadata (5.8 kB)
Collecting opencv-python>=4.6.0 (from ultralytics)
  Downloading opencv_python-4.11.0.86-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (20 kB)
Collecting pybboxes==0.1.6 (from sahi)
  Downloading pybboxes-0.1.6-py3-none-any.whl.metadata (9.9 kB)
Collecting terminaltables (from sahi)
  Downloading terminaltables-3.1.10-py2.py3-none-any.whl.metadata (3.5 kB)
Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib->roboflow) (1.3.3)
Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.12/dist-packages (from matplotlib->roboflow) (4.61.1)
Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.12/dist-packages (from matplotlib->roboflow) (25.0)
Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.12/dist-packages (from matplotlib->roboflow) (3.2.5)
Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (from requests->roboflow) (3.4.4)
Requirement already satisfied: filelock in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (3.20.0)
Requirement already satisfied: typing-extensions>=4.10.0 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (4.15.0)
Requirement already satisfied: setuptools in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (75.2.0)
Requirement already satisfied: sympy>=1.13.3 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (1.14.0)
Requirement already satisfied: networkx>=2.5.1 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (3.6.1)
Requirement already satisfied: jinja2 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (3.1.6)
Requirement already satisfied: fsspec>=0.8.5 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (2025.3.0)
Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (12.6.77)
Requirement already satisfied: nvidia-cuda-runtime-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (12.6.77)
Requirement already satisfied: nvidia-cuda-cupti-cu12==12.6.80 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (12.6.80)
Requirement already satisfied: nvidia-cudnn-cu12==9.10.2.21 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (9.10.2.21)
Requirement already satisfied: nvidia-cublas-cu12==12.6.4.1 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (12.6.4.1)
Requirement already satisfied: nvidia-cufft-cu12==11.3.0.4 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (11.3.0.4)
Requirement already satisfied: nvidia-curand-cu12==10.3.7.77 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (10.3.7.77)
Requirement already satisfied: nvidia-cusolver-cu12==11.7.1.2 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (11.7.1.2)
Requirement already satisfied: nvidia-cusparse-cu12==12.5.4.2 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (12.5.4.2)
Requirement already satisfied: nvidia-cusparselt-cu12==0.7.1 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (0.7.1)
Requirement already satisfied: nvidia-nccl-cu12==2.27.5 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (2.27.5)
Requirement already satisfied: nvidia-nvshmem-cu12==3.3.20 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (3.3.20)
Requirement already satisfied: nvidia-nvtx-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (12.6.77)
Requirement already satisfied: nvidia-nvjitlink-cu12==12.6.85 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (12.6.85)
Requirement already satisfied: nvidia-cufile-cu12==1.11.1.6 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (1.11.1.6)
Requirement already satisfied: triton==3.5.0 in /usr/local/lib/python3.12/dist-packages (from torch>=1.8.0->ultralytics) (3.5.0)
Requirement already satisfied: termcolor in /usr/local/lib/python3.12/dist-packages (from fire->sahi) (3.2.0)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.12/dist-packages (from sympy>=1.13.3->torch>=1.8.0->ultralytics) (1.3.0)
Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.12/dist-packages (from jinja2->torch>=1.8.0->ultralytics) (3.0.3)
Downloading roboflow-1.2.11-py3-none-any.whl (89 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 89.9/89.9 kB 8.9 MB/s eta 0:00:00
Downloading idna-3.7-py3-none-any.whl (66 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 66.8/66.8 kB 7.5 MB/s eta 0:00:00
Downloading opencv_python_headless-4.10.0.84-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.9 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 49.9/49.9 MB 56.3 MB/s eta 0:00:00
Downloading ultralytics-8.3.251-py3-none-any.whl (1.2 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 79.6 MB/s eta 0:00:00
Downloading sahi-0.11.36-py3-none-any.whl (111 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 111.7/111.7 kB 12.4 MB/s eta 0:00:00
Downloading pybboxes-0.1.6-py3-none-any.whl (24 kB)
Downloading opencv_python-4.11.0.86-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (63.0 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 63.0/63.0 MB 43.6 MB/s eta 0:00:00
Downloading pi_heif-1.1.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (1.4 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.4/1.4 MB 82.9 MB/s eta 0:00:00
Downloading pillow_avif_plugin-1.5.2-cp312-cp312-manylinux_2_28_x86_64.whl (4.2 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.2/4.2 MB 135.4 MB/s eta 0:00:00
Downloading ultralytics_thop-2.0.18-py3-none-any.whl (28 kB)
Downloading filetype-1.2.0-py2.py3-none-any.whl (19 kB)
Downloading fire-0.7.1-py3-none-any.whl (115 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 115.9/115.9 kB 13.3 MB/s eta 0:00:00
Downloading terminaltables-3.1.10-py2.py3-none-any.whl (15 kB)
Installing collected packages: pillow-avif-plugin, filetype, terminaltables, pybboxes, pi-heif, opencv-python-headless, opencv-python, idna, fire, ultralytics-thop, sahi, roboflow, ultralytics
  Attempting uninstall: opencv-python-headless
    Found existing installation: opencv-python-headless 4.12.0.88
    Uninstalling opencv-python-headless-4.12.0.88:
      Successfully uninstalled opencv-python-headless-4.12.0.88
  Attempting uninstall: opencv-python
    Found existing installation: opencv-python 4.12.0.88
    Uninstalling opencv-python-4.12.0.88:
      Successfully uninstalled opencv-python-4.12.0.88
  Attempting uninstall: idna
    Found existing installation: idna 3.11
    Uninstalling idna-3.11:
      Successfully uninstalled idna-3.11
Successfully installed filetype-1.2.0 fire-0.7.1 idna-3.7 opencv-python-4.11.0.86 opencv-python-headless-4.10.0.84 pi-heif-1.1.1 pillow-avif-plugin-1.5.2 pybboxes-0.1.6 roboflow-1.2.11 sahi-0.11.36 terminaltables-3.1.10 ultralytics-8.3.251 ultralytics-thop-2.0.18

Libraries installed successfully.
from google.colab import userdata
from roboflow import Roboflow

try:
    # Attempts to retrieve the 'ROBOFLOW_API_KEY' from Google Colab's secret manager.
    rf_api_key = userdata.get('ROBOFLOW_API_KEY')
    print("API Key retrieved successfully.")
except Exception as e:
    # Handles potential errors, such as the secret not being defined in the Colab environment.
    print("Error: Could not retrieve key.")
    # Re-raises the exception to halt execution if the key is essential.
    raise e

# Initializes the Roboflow client with the successfully retrieved API key.
rf = Roboflow(api_key=rf_api_key)
API Key retrieved successfully.

Section 2: Dataset Acquisition and Configuration

Purpose: To aggregate a diverse biological dataset and prepare it for the RT-DETR training engine. This section merges six distinct dataset versions into a unified training pipeline.

Key Activities:

  • API-Driven Data Retrieval: Downloads version 2 of multiple specialized datasets from the Roboflow workspace.
  • Dynamic YAML Generation: Automatically scans the downloaded directories to confirm their location on the local disk. It then generates a central data.yaml file. This is a critical step that prevents training failures by ensuring the transformer model has a master map to find images across all six distributed folders.
# Accesses the specific Roboflow workspace that contains all the project datasets.
workspace = rf.workspace("mortiscope-fvkhd")

# Downloads version 1 of several distinct projects from the workspace.
decomposition_1 = workspace.project("decomposition-high-resolution-300").version(2).download("yolov11")
decomposition_2 = workspace.project("decomposition-high-resolution-250").version(2).download("yolov11")
decomposition_3 = workspace.project("decomposition-high-resolution-200").version(2).download("yolov11")
decomposition_4 = workspace.project("decomposition-standard-resolution-300").version(2).download("yolov11")
decomposition_5 = workspace.project("decomposition-standard-resolution-250").version(2).download("yolov11")
complementary = workspace.project("complementary").version(2).download("yolov11")

# Prints a confirmation message to the console after all download operations have successfully completed.
print("\nAll datasets downloaded successfully.")
loading Roboflow workspace...
loading Roboflow project...
loading Roboflow project...
loading Roboflow project...
loading Roboflow project...
loading Roboflow project...
loading Roboflow project...

All datasets downloaded successfully.
import yaml

# A list containing all the dataset objects that were previously downloaded.
all_datasets = [
    decomposition_1,
    decomposition_2,
    decomposition_3,
    decomposition_4,
    decomposition_5,
    complementary
]

# Initializes lists to store the file paths to the training and validation image folders.
train_paths = []
val_paths = []

print("-"*70)
print("Building Dataset Configuration")

# Iterates through each dataset to locate and collect the paths to its image directories.
for ds in all_datasets:
    # Constructs the expected paths for the 'train' and 'valid' image subdirectories.
    t_path = os.path.join(ds.location, 'train', 'images')
    v_path = os.path.join(ds.location, 'valid', 'images')

    # Verifies that the training directory actually exists before adding it to the list.
    if os.path.exists(t_path):
        train_paths.append(t_path)
        print(f"Added train: {ds.location.split('/')[-1]}")
    else:
        print(f"Skipped train (Empty): {ds.location.split('/')[-1]}")

    # Verifies that the validation directory actually exists before adding it.
    if os.path.exists(v_path):
        val_paths.append(v_path)
        print(f"Added valid: {ds.location.split('/')[-1]}")
    else:
        print(f"Skipped valid (Not found): {ds.location.split('/')[-1]}")

# Defines the master configuration dictionary in the format required by Ultralytics YOLO.
data_config = {
    'names': {
        0: 'adult',
        1: 'instar_1',
        2: 'instar_2',
        3: 'instar_3',
        4: 'pupa'
    },
    'nc': 5, # Number of classes.
    'train': train_paths,
    'val': val_paths,
}

# Defines the output path for the YAML file in the current working directory.
yaml_path = os.path.join(os.getcwd(), 'data.yaml')

# Writes the configuration dictionary to the 'data.yaml' file.
with open(yaml_path, 'w') as outfile:
    yaml.dump(data_config, outfile, default_flow_style=False)

# Prints a confirmation summary, showing the location of the file and the total number of included data folders.
print("\n" + "-"*70)
print(f"Balanced configuration created at: {yaml_path}")
print(f"Total Train Folders: {len(train_paths)}")
print(f"Total Valid Folders: {len(val_paths)}")
print("-"*70)
----------------------------------------------------------------------
Building Dataset Configuration
Added train: Decomposition-(High-Resolution---300%)-2
Added valid: Decomposition-(High-Resolution---300%)-2
Added train: Decomposition-(High-Resolution---250%)-2
Added valid: Decomposition-(High-Resolution---250%)-2
Added train: Decomposition-(High-Resolution---200%)-2
Skipped valid (Not found): Decomposition-(High-Resolution---200%)-2
Added train: Decomposition-(Standard-Resolution---300%)-2
Added valid: Decomposition-(Standard-Resolution---300%)-2
Added train: Decomposition-(Standard-Resolution---250%)-2
Added valid: Decomposition-(Standard-Resolution---250%)-2
Added train: Complementary-2
Added valid: Complementary-2
----------------------------------------------------------------------
Balanced configuration created at: /content/data.yaml
Total Train Folders: 6
Total Valid Folders: 5
----------------------------------------------------------------------

Section 3: Model Initialization and Callback Definition

Purpose: This section prepares the RT-DETR architecture and establishes custom behaviors for the training loop. This section ensures the model is correctly instantiated and that its progress is backed up to persistent storage frequently.

Key Activities:

  • Persistent Weight Archiving: Implements a custom callback that monitors the training process. At the end of every epoch, it copies the latest weights (last.pt) and the best-performing weights (best.pt) to Google Drive. It also maintains a epoch_history folder for granular review of the model’s evolution across the 100-epoch span.
  • Architecture Selection: Downloads the pre-trained weights for RT-DETR-L (Large). This version is selected to provide a direct comparison to the YOLO-L models used in other training notebooks.
  • Model Registration: Instantiates the model object and registers the custom callback into the Ultralytics training engine.
import shutil

# Defines and creates a dedicated directory for storing a snapshot of the model weights at the end of each training epoch.
history_path = os.path.join(weights_path, 'epoch_history')
os.makedirs(history_path, exist_ok=True)


def on_train_epoch_end(trainer):
    """
    A callback function executed at the end of each training epoch.

    This function performs the following actions:
    1. Saves a copy of the latest model weights (`last.pt`) to a historical
       archive, named with the corresponding epoch number.
    2. Updates the primary `last.pt` file in the persistent Google Drive
       weights folder, allowing for training resumption.
    3. Updates the `best.pt` file in the persistent folder whenever the trainer
       identifies a new best-performing model.

    Args:
        trainer: The Ultralytics trainer object, which provides access to the
                 current training state, including epoch number and file paths.
    """
    # Gets the current epoch number.
    current_epoch = trainer.epoch + 1

    # Defines the source paths for the weights generated by the trainer in the temporary, session-specific output directory.
    local_last = os.path.join(trainer.save_dir, 'weights', 'last.pt')
    local_best = os.path.join(trainer.save_dir, 'weights', 'best.pt')

    # Checks for the existence of the latest epoch's weights before proceeding.
    if os.path.exists(local_last):
        # Creates a unique filename for the historical weight file.
        history_filename = f"{current_epoch:03d}_epoch.pt"
        history_dest = os.path.join(history_path, history_filename)

        # Copies the latest weights to the historical archive directory.
        shutil.copy(local_last, history_dest)
        print(f"   History Saved: {history_filename}")

        # Overwrites the main 'last.pt' file in the persistent Google Drive folder.
        resume_dest = os.path.join(weights_path, 'last.pt')
        shutil.copy(local_last, resume_dest)

    # Checks if the trainer has produced a new best-performing model weight file.
    if os.path.exists(local_best):
        # Overwrites the main 'best.pt' file in the persistent Google Drive folder.
        best_dest = os.path.join(weights_path, 'best.pt')
        shutil.copy(local_best, best_dest)

# Prints a confirmation message detailing the callback's configuration and the locations where files will be saved.
print("Callback Defined: ")
print(f"   1. History saved to: {history_path}")
print(f"   2. Resume file active at: {weights_path}/last.pt")
Callback Defined: 
   1. History saved to: /content/drive/MyDrive/Mortiscope Models/RT-DETR/weights/epoch_history
   2. Resume file active at: /content/drive/MyDrive/Mortiscope Models/RT-DETR/weights/last.pt
import os

import torch
from ultralytics import RTDETR

# Defines the specific URLs for Ultralytics RT-DETR models.
urls = [
    "https://github.com/ultralytics/assets/releases/download/v8.3.0/rtdetr-l.pt",
    "https://github.com/ultralytics/assets/releases/download/v8.3.0/rtdetr-x.pt"
]

# Selects a specific model variant to serve as the training base.
selected_url = urls[0]
model_filename = os.path.basename(selected_url)

# Prevents re-downloading the model file if it already exists locally.
if not os.path.exists(model_filename):
    print(f"Downloading {model_filename}...")
    torch.hub.download_url_to_file(selected_url, model_filename)
else:
    print(f"{model_filename} found locally.")

# Instantiates the RT-DETR model.
model = RTDETR(model_filename)

# Registers the custom `on_train_epoch_end` callback function.
model.add_callback("on_train_epoch_end", on_train_epoch_end)

# Prints a summary of the model's architecture.
print("\nRT-DETR Architecture & Complexity:")
model.info(detailed=True)
Creating new Ultralytics Settings v0.0.6 file ✅ 
View Ultralytics Settings with 'yolo settings' or at '/root/.config/Ultralytics/settings.json'
Update Settings with 'yolo settings key=value', i.e. 'yolo settings runs_dir=path/to/dir'. For help see https://docs.ultralytics.com/quickstart/#ultralytics-settings.
Downloading rtdetr-l.pt...100%|██████████| 63.4M/63.4M [00:02<00:00, 31.5MB/s]

RT-DETR Architecture & Complexity:
layer                                    name                type  gradient  parameters               shape        mu     sigma
    0               model.0.stem1.conv.weight              Conv2d     False         864       [32, 3, 3, 3]  0.000286     0.236        float32
    1                 model.0.stem1.bn.weight         BatchNorm2d     False          32                [32]     0.258     0.116        float32
    1                   model.0.stem1.bn.bias         BatchNorm2d     False          32                [32]      0.01     0.353        float32
    2                       model.0.stem1.act                ReLU     False           0                  []         -         -              -
    3              model.0.stem2a.conv.weight              Conv2d     False        2048      [16, 32, 2, 2]  -0.00379    0.0969        float32
    4                model.0.stem2a.bn.weight         BatchNorm2d     False          16                [16]     0.298    0.0581        float32
    4                  model.0.stem2a.bn.bias         BatchNorm2d     False          16                [16]     0.133     0.141        float32
    5                      model.0.stem2a.act                ReLU     False           0                  []         -         -              -
    6              model.0.stem2b.conv.weight              Conv2d     False        2048      [32, 16, 2, 2]  -0.00226    0.0979        float32
    7                model.0.stem2b.bn.weight         BatchNorm2d     False          32                [32]     0.384     0.062        float32
    7                  model.0.stem2b.bn.bias         BatchNorm2d     False          32                [32]    0.0132    0.0914        float32
    8                      model.0.stem2b.act                ReLU     False           0                  []         -         -              -
    9               model.0.stem3.conv.weight              Conv2d     False       18432      [32, 64, 3, 3]    0.0006    0.0582        float32
   10                 model.0.stem3.bn.weight         BatchNorm2d     False          32                [32]     0.262    0.0657        float32
   10                   model.0.stem3.bn.bias         BatchNorm2d     False          32                [32]     0.344     0.246        float32
   11                       model.0.stem3.act                ReLU     False           0                  []         -         -              -
   12               model.0.stem4.conv.weight              Conv2d     False        1536      [48, 32, 1, 1] -0.000254     0.138        float32
   13                 model.0.stem4.bn.weight         BatchNorm2d     False          48                [48]     0.465    0.0983        float32
   13                   model.0.stem4.bn.bias         BatchNorm2d     False          48                [48]     0.176     0.358        float32
   14                       model.0.stem4.act                ReLU     False           0                  []         -         -              -
   15                            model.0.pool           MaxPool2d     False           0                  []         -         -              -
   16                 model.1.m.0.conv.weight              Conv2d     False       20736      [48, 48, 3, 3]  -0.00125    0.0567        float32
   17                   model.1.m.0.bn.weight         BatchNorm2d     False          48                [48]     0.664     0.277        float32
   17                     model.1.m.0.bn.bias         BatchNorm2d     False          48                [48]     0.406     0.759        float32
   18                         model.1.m.0.act                ReLU     False           0                  []         -         -              -
   19                 model.1.m.1.conv.weight              Conv2d     False       20736      [48, 48, 3, 3]  -0.00177    0.0476        float32
   20                   model.1.m.1.bn.weight         BatchNorm2d     False          48                [48]     0.921     0.515        float32
   20                     model.1.m.1.bn.bias         BatchNorm2d     False          48                [48]     0.314      1.11        float32
   21                 model.1.m.2.conv.weight              Conv2d     False       20736      [48, 48, 3, 3]  -0.00167    0.0425        float32
   22                   model.1.m.2.bn.weight         BatchNorm2d     False          48                [48]     0.901     0.129        float32
   22                     model.1.m.2.bn.bias         BatchNorm2d     False          48                [48]     0.234     0.798        float32
   23                 model.1.m.3.conv.weight              Conv2d     False       20736      [48, 48, 3, 3]  -0.00119    0.0383        float32
   24                   model.1.m.3.bn.weight         BatchNorm2d     False          48                [48]     0.901     0.121        float32
   24                     model.1.m.3.bn.bias         BatchNorm2d     False          48                [48]      -0.1     0.482        float32
   25                 model.1.m.4.conv.weight              Conv2d     False       20736      [48, 48, 3, 3]  -0.00298    0.0362        float32
   26                   model.1.m.4.bn.weight         BatchNorm2d     False          48                [48]         1     0.296        float32
   26                     model.1.m.4.bn.bias         BatchNorm2d     False          48                [48]    -0.213     0.673        float32
   27                 model.1.m.5.conv.weight              Conv2d     False       20736      [48, 48, 3, 3]  -0.00274    0.0308        float32
   28                   model.1.m.5.bn.weight         BatchNorm2d     False          48                [48]      1.51     0.526        float32
   28                     model.1.m.5.bn.bias         BatchNorm2d     False          48                [48]     -1.48      1.42        float32
   29                  model.1.sc.conv.weight              Conv2d     False       21504     [64, 336, 1, 1]  0.000581    0.0604        float32
   30                    model.1.sc.bn.weight         BatchNorm2d     False          64                [64]      2.25     0.766        float32
   30                      model.1.sc.bn.bias         BatchNorm2d     False          64                [64]      1.86      2.35        float32
   31                  model.1.ec.conv.weight              Conv2d     False        8192     [128, 64, 1, 1]  -0.00113    0.0824        float32
   32                    model.1.ec.bn.weight         BatchNorm2d     False         128               [128]      1.79     0.509        float32
   32                      model.1.ec.bn.bias         BatchNorm2d     False         128               [128]    -0.275      1.02        float32
   33                     model.2.conv.weight              Conv2d     False        1152      [128, 1, 3, 3]    -0.003    0.0848        float32
   34                       model.2.bn.weight         BatchNorm2d     False         128               [128]      1.54     0.257        float32
   34                         model.2.bn.bias         BatchNorm2d     False         128               [128]   0.00827     0.826        float32
   35                             model.2.act            Identity     False           0                  []         -         -              -
   36                 model.3.m.0.conv.weight              Conv2d     False      110592     [96, 128, 3, 3] -0.000206    0.0265        float32
   37                   model.3.m.0.bn.weight         BatchNorm2d     False          96                [96]      1.91     0.561        float32
   37                     model.3.m.0.bn.bias         BatchNorm2d     False          96                [96]    -0.221      1.55        float32
   38                 model.3.m.1.conv.weight              Conv2d     False       82944      [96, 96, 3, 3] -0.000993    0.0266        float32
   39                   model.3.m.1.bn.weight         BatchNorm2d     False          96                [96]      2.01     0.595        float32
   39                     model.3.m.1.bn.bias         BatchNorm2d     False          96                [96]    -0.578      1.53        float32
   40                 model.3.m.2.conv.weight              Conv2d     False       82944      [96, 96, 3, 3]  -0.00155    0.0239        float32
   41                   model.3.m.2.bn.weight         BatchNorm2d     False          96                [96]      1.88     0.277        float32
   41                     model.3.m.2.bn.bias         BatchNorm2d     False          96                [96]     -1.04      1.04        float32
   42                 model.3.m.3.conv.weight              Conv2d     False       82944      [96, 96, 3, 3]  -0.00167    0.0224        float32
   43                   model.3.m.3.bn.weight         BatchNorm2d     False          96                [96]      1.85     0.439        float32
   43                     model.3.m.3.bn.bias         BatchNorm2d     False          96                [96]     -1.11      1.26        float32
   44                 model.3.m.4.conv.weight              Conv2d     False       82944      [96, 96, 3, 3]  -0.00151    0.0208        float32
   45                   model.3.m.4.bn.weight         BatchNorm2d     False          96                [96]      1.91     0.391        float32
   45                     model.3.m.4.bn.bias         BatchNorm2d     False          96                [96]      -1.3      1.14        float32
   46                 model.3.m.5.conv.weight              Conv2d     False       82944      [96, 96, 3, 3] -0.000755    0.0182        float32
   47                   model.3.m.5.bn.weight         BatchNorm2d     False          96                [96]      2.28      0.55        float32
   47                     model.3.m.5.bn.bias         BatchNorm2d     False          96                [96]      -1.2      1.27        float32
   48                  model.3.sc.conv.weight              Conv2d     False      180224    [256, 704, 1, 1] -0.000106    0.0303        float32
   49                    model.3.sc.bn.weight         BatchNorm2d     False         256               [256]      1.86     0.383        float32
   49                      model.3.sc.bn.bias         BatchNorm2d     False         256               [256]    0.0356      1.38        float32
   50                  model.3.ec.conv.weight              Conv2d     False      131072    [512, 256, 1, 1]  -0.00144    0.0336        float32
   51                    model.3.ec.bn.weight         BatchNorm2d     False         512               [512]      1.25      0.17        float32
   51                      model.3.ec.bn.bias         BatchNorm2d     False         512               [512]    -0.925     0.636        float32
   52                     model.4.conv.weight              Conv2d     False        4608      [512, 1, 3, 3]  0.000926    0.0554        float32
   53                       model.4.bn.weight         BatchNorm2d     False         512               [512]      1.26     0.232        float32
   53                         model.4.bn.bias         BatchNorm2d     False         512               [512] -3.34e-06  0.000236        float32
   54                             model.4.act            Identity     False           0                  []         -         -              -
   55           model.5.m.0.conv1.conv.weight              Conv2d     False       98304    [192, 512, 1, 1] -0.000119    0.0255        float32
   56             model.5.m.0.conv1.bn.weight         BatchNorm2d     False         192               [192]      1.09     0.155        float32
   56               model.5.m.0.conv1.bn.bias         BatchNorm2d     False         192               [192]    0.0193     0.503        float32
   57                   model.5.m.0.conv1.act            Identity     False           0                  []         -         -              -
   58           model.5.m.0.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5] -0.000725    0.0667        float32
   59             model.5.m.0.conv2.bn.weight         BatchNorm2d     False         192               [192]      1.67      0.39        float32
   59               model.5.m.0.conv2.bn.bias         BatchNorm2d     False         192               [192]     0.293      1.21        float32
   60           model.5.m.1.conv1.conv.weight              Conv2d     False       36864    [192, 192, 1, 1] -0.000115    0.0309        float32
   61             model.5.m.1.conv1.bn.weight         BatchNorm2d     False         192               [192]     0.967     0.132        float32
   61               model.5.m.1.conv1.bn.bias         BatchNorm2d     False         192               [192]   -0.0815     0.558        float32
   62                   model.5.m.1.conv1.act            Identity     False           0                  []         -         -              -
   63           model.5.m.1.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5] -0.000311    0.0433        float32
   64             model.5.m.1.conv2.bn.weight         BatchNorm2d     False         192               [192]      1.41     0.508        float32
   64               model.5.m.1.conv2.bn.bias         BatchNorm2d     False         192               [192]    0.0643     0.974        float32
   65           model.5.m.2.conv1.conv.weight              Conv2d     False       36864    [192, 192, 1, 1]  0.000143    0.0266        float32
   66             model.5.m.2.conv1.bn.weight         BatchNorm2d     False         192               [192]     0.894     0.159        float32
   66               model.5.m.2.conv1.bn.bias         BatchNorm2d     False         192               [192]    0.0288     0.626        float32
   67                   model.5.m.2.conv1.act            Identity     False           0                  []         -         -              -
   68           model.5.m.2.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5]  0.000371    0.0389        float32
   69             model.5.m.2.conv2.bn.weight         BatchNorm2d     False         192               [192]      1.29     0.488        float32
   69               model.5.m.2.conv2.bn.bias         BatchNorm2d     False         192               [192]    -0.168     0.988        float32
   70           model.5.m.3.conv1.conv.weight              Conv2d     False       36864    [192, 192, 1, 1]  0.000281    0.0236        float32
   71             model.5.m.3.conv1.bn.weight         BatchNorm2d     False         192               [192]     0.938     0.143        float32
   71               model.5.m.3.conv1.bn.bias         BatchNorm2d     False         192               [192]   -0.0198     0.506        float32
   72                   model.5.m.3.conv1.act            Identity     False           0                  []         -         -              -
   73           model.5.m.3.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5] -0.000389    0.0347        float32
   74             model.5.m.3.conv2.bn.weight         BatchNorm2d     False         192               [192]      1.12     0.387        float32
   74               model.5.m.3.conv2.bn.bias         BatchNorm2d     False         192               [192]    -0.243     0.753        float32
   75           model.5.m.4.conv1.conv.weight              Conv2d     False       36864    [192, 192, 1, 1]  0.000242    0.0214        float32
   76             model.5.m.4.conv1.bn.weight         BatchNorm2d     False         192               [192]     0.945     0.112        float32
   76               model.5.m.4.conv1.bn.bias         BatchNorm2d     False         192               [192]    0.0214     0.419        float32
   77                   model.5.m.4.conv1.act            Identity     False           0                  []         -         -              -
   78           model.5.m.4.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5] -0.000177    0.0297        float32
   79             model.5.m.4.conv2.bn.weight         BatchNorm2d     False         192               [192]       1.1     0.335        float32
   79               model.5.m.4.conv2.bn.bias         BatchNorm2d     False         192               [192]    -0.429     0.778        float32
   80           model.5.m.5.conv1.conv.weight              Conv2d     False       36864    [192, 192, 1, 1] -2.67e-05    0.0183        float32
   81             model.5.m.5.conv1.bn.weight         BatchNorm2d     False         192               [192]     0.836     0.178        float32
   81               model.5.m.5.conv1.bn.bias         BatchNorm2d     False         192               [192]  -0.00317     0.649        float32
   82                   model.5.m.5.conv1.act            Identity     False           0                  []         -         -              -
   83           model.5.m.5.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5]  0.000146    0.0247        float32
   84             model.5.m.5.conv2.bn.weight         BatchNorm2d     False         192               [192]      1.63     0.478        float32
   84               model.5.m.5.conv2.bn.bias         BatchNorm2d     False         192               [192]    -0.707     0.856        float32
   85                  model.5.sc.conv.weight              Conv2d     False      851968   [512, 1664, 1, 1] -0.000388    0.0188        float32
   86                    model.5.sc.bn.weight         BatchNorm2d     False         512               [512]      1.61     0.444        float32
   86                      model.5.sc.bn.bias         BatchNorm2d     False         512               [512]    -0.468      1.37        float32
   87                  model.5.ec.conv.weight              Conv2d     False      524288   [1024, 512, 1, 1]  -0.00204     0.022        float32
   88                    model.5.ec.bn.weight         BatchNorm2d     False        1024              [1024]      1.02      0.33        float32
   88                      model.5.ec.bn.bias         BatchNorm2d     False        1024              [1024]     -1.11     0.959        float32
   89           model.6.m.0.conv1.conv.weight              Conv2d     False      196608   [192, 1024, 1, 1]  9.16e-05    0.0175        float32
   90             model.6.m.0.conv1.bn.weight         BatchNorm2d     False         192               [192]      1.01     0.131        float32
   90               model.6.m.0.conv1.bn.bias         BatchNorm2d     False         192               [192]  -0.00657     0.482        float32
   91                   model.6.m.0.conv1.act            Identity     False           0                  []         -         -              -
   92           model.6.m.0.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5]  0.000143    0.0583        float32
   93             model.6.m.0.conv2.bn.weight         BatchNorm2d     False         192               [192]      0.86     0.222        float32
   93               model.6.m.0.conv2.bn.bias         BatchNorm2d     False         192               [192]     0.207      1.07        float32
   94           model.6.m.1.conv1.conv.weight              Conv2d     False       36864    [192, 192, 1, 1]   0.00012    0.0289        float32
   95             model.6.m.1.conv1.bn.weight         BatchNorm2d     False         192               [192]     0.989    0.0794        float32
   95               model.6.m.1.conv1.bn.bias         BatchNorm2d     False         192               [192]    0.0249     0.425        float32
   96                   model.6.m.1.conv1.act            Identity     False           0                  []         -         -              -
   97           model.6.m.1.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5] -0.000294    0.0481        float32
   98             model.6.m.1.conv2.bn.weight         BatchNorm2d     False         192               [192]     0.899     0.228        float32
   98               model.6.m.1.conv2.bn.bias         BatchNorm2d     False         192               [192]     -0.45     0.687        float32
   99           model.6.m.2.conv1.conv.weight              Conv2d     False       36864    [192, 192, 1, 1]  0.000113    0.0275        float32
  100             model.6.m.2.conv1.bn.weight         BatchNorm2d     False         192               [192]     0.942     0.124        float32
  100               model.6.m.2.conv1.bn.bias         BatchNorm2d     False         192               [192]   0.00908     0.527        float32
  101                   model.6.m.2.conv1.act            Identity     False           0                  []         -         -              -
  102           model.6.m.2.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5] -0.000696    0.0431        float32
  103             model.6.m.2.conv2.bn.weight         BatchNorm2d     False         192               [192]     0.801     0.215        float32
  103               model.6.m.2.conv2.bn.bias         BatchNorm2d     False         192               [192]    -0.187     0.712        float32
  104           model.6.m.3.conv1.conv.weight              Conv2d     False       36864    [192, 192, 1, 1] -0.000185     0.027        float32
  105             model.6.m.3.conv1.bn.weight         BatchNorm2d     False         192               [192]     0.933      0.13        float32
  105               model.6.m.3.conv1.bn.bias         BatchNorm2d     False         192               [192]   0.00563     0.514        float32
  106                   model.6.m.3.conv1.act            Identity     False           0                  []         -         -              -
  107           model.6.m.3.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5]  0.000308    0.0414        float32
  108             model.6.m.3.conv2.bn.weight         BatchNorm2d     False         192               [192]     0.809     0.239        float32
  108               model.6.m.3.conv2.bn.bias         BatchNorm2d     False         192               [192]    -0.191     0.726        float32
  109           model.6.m.4.conv1.conv.weight              Conv2d     False       36864    [192, 192, 1, 1] -2.36e-05    0.0251        float32
  110             model.6.m.4.conv1.bn.weight         BatchNorm2d     False         192               [192]     0.929      0.11        float32
  110               model.6.m.4.conv1.bn.bias         BatchNorm2d     False         192               [192]   -0.0466     0.471        float32
  111                   model.6.m.4.conv1.act            Identity     False           0                  []         -         -              -
  112           model.6.m.4.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5]  0.000333    0.0363        float32
  113             model.6.m.4.conv2.bn.weight         BatchNorm2d     False         192               [192]     0.934     0.282        float32
  113               model.6.m.4.conv2.bn.bias         BatchNorm2d     False         192               [192]     -0.44     0.715        float32
  114           model.6.m.5.conv1.conv.weight              Conv2d     False       36864    [192, 192, 1, 1] -0.000116    0.0215        float32
  115             model.6.m.5.conv1.bn.weight         BatchNorm2d     False         192               [192]     0.911     0.131        float32
  115               model.6.m.5.conv1.bn.bias         BatchNorm2d     False         192               [192]   -0.0148     0.487        float32
  116                   model.6.m.5.conv1.act            Identity     False           0                  []         -         -              -
  117           model.6.m.5.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5] -0.000435    0.0298        float32
  118             model.6.m.5.conv2.bn.weight         BatchNorm2d     False         192               [192]     0.958     0.331        float32
  118               model.6.m.5.conv2.bn.bias         BatchNorm2d     False         192               [192]    -0.252      0.98        float32
  119                  model.6.sc.conv.weight              Conv2d     False 1.11411e+06   [512, 2176, 1, 1] -0.000638    0.0142        float32
  120                    model.6.sc.bn.weight         BatchNorm2d     False         512               [512]      1.47     0.304        float32
  120                      model.6.sc.bn.bias         BatchNorm2d     False         512               [512]     -1.25     0.928        float32
  121                  model.6.ec.conv.weight              Conv2d     False      524288   [1024, 512, 1, 1]  -0.00196    0.0178        float32
  122                    model.6.ec.bn.weight         BatchNorm2d     False        1024              [1024]     0.936     0.323        float32
  122                      model.6.ec.bn.bias         BatchNorm2d     False        1024              [1024]    -0.899     0.697        float32
  123           model.7.m.0.conv1.conv.weight              Conv2d     False      196608   [192, 1024, 1, 1]  1.26e-05    0.0191        float32
  124             model.7.m.0.conv1.bn.weight         BatchNorm2d     False         192               [192]     0.993      0.11        float32
  124               model.7.m.0.conv1.bn.bias         BatchNorm2d     False         192               [192]  0.000583      0.45        float32
  125                   model.7.m.0.conv1.act            Identity     False           0                  []         -         -              -
  126           model.7.m.0.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5]  0.000475    0.0465        float32
  127             model.7.m.0.conv2.bn.weight         BatchNorm2d     False         192               [192]     0.941     0.424        float32
  127               model.7.m.0.conv2.bn.bias         BatchNorm2d     False         192               [192]    -0.129      1.57        float32
  128           model.7.m.1.conv1.conv.weight              Conv2d     False       36864    [192, 192, 1, 1] -0.000374    0.0266        float32
  129             model.7.m.1.conv1.bn.weight         BatchNorm2d     False         192               [192]     0.888     0.115        float32
  129               model.7.m.1.conv1.bn.bias         BatchNorm2d     False         192               [192]   -0.0736     0.572        float32
  130                   model.7.m.1.conv1.act            Identity     False           0                  []         -         -              -
  131           model.7.m.1.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5]   0.00134    0.0379        float32
  132             model.7.m.1.conv2.bn.weight         BatchNorm2d     False         192               [192]     0.791     0.248        float32
  132               model.7.m.1.conv2.bn.bias         BatchNorm2d     False         192               [192]    -0.638      0.88        float32
  133           model.7.m.2.conv1.conv.weight              Conv2d     False       36864    [192, 192, 1, 1]  0.000466    0.0242        float32
  134             model.7.m.2.conv1.bn.weight         BatchNorm2d     False         192               [192]     0.904     0.117        float32
  134               model.7.m.2.conv1.bn.bias         BatchNorm2d     False         192               [192]    0.0649     0.501        float32
  135                   model.7.m.2.conv1.act            Identity     False           0                  []         -         -              -
  136           model.7.m.2.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5]  -0.00229    0.0373        float32
  137             model.7.m.2.conv2.bn.weight         BatchNorm2d     False         192               [192]     0.659     0.178        float32
  137               model.7.m.2.conv2.bn.bias         BatchNorm2d     False         192               [192]    -0.327     0.648        float32
  138           model.7.m.3.conv1.conv.weight              Conv2d     False       36864    [192, 192, 1, 1]  0.000348    0.0236        float32
  139             model.7.m.3.conv1.bn.weight         BatchNorm2d     False         192               [192]      0.91     0.121        float32
  139               model.7.m.3.conv1.bn.bias         BatchNorm2d     False         192               [192]   0.00219     0.489        float32
  140                   model.7.m.3.conv1.act            Identity     False           0                  []         -         -              -
  141           model.7.m.3.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5] -0.000101    0.0365        float32
  142             model.7.m.3.conv2.bn.weight         BatchNorm2d     False         192               [192]     0.701      0.22        float32
  142               model.7.m.3.conv2.bn.bias         BatchNorm2d     False         192               [192]    -0.403     0.604        float32
  143           model.7.m.4.conv1.conv.weight              Conv2d     False       36864    [192, 192, 1, 1]  0.000513    0.0228        float32
  144             model.7.m.4.conv1.bn.weight         BatchNorm2d     False         192               [192]     0.929     0.102        float32
  144               model.7.m.4.conv1.bn.bias         BatchNorm2d     False         192               [192]    0.0657     0.429        float32
  145                   model.7.m.4.conv1.act            Identity     False           0                  []         -         -              -
  146           model.7.m.4.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5]  -0.00196    0.0324        float32
  147             model.7.m.4.conv2.bn.weight         BatchNorm2d     False         192               [192]     0.749     0.212        float32
  147               model.7.m.4.conv2.bn.bias         BatchNorm2d     False         192               [192]    -0.497     0.704        float32
  148           model.7.m.5.conv1.conv.weight              Conv2d     False       36864    [192, 192, 1, 1] -5.37e-05    0.0204        float32
  149             model.7.m.5.conv1.bn.weight         BatchNorm2d     False         192               [192]     0.883     0.146        float32
  149               model.7.m.5.conv1.bn.bias         BatchNorm2d     False         192               [192]    0.0296     0.493        float32
  150                   model.7.m.5.conv1.act            Identity     False           0                  []         -         -              -
  151           model.7.m.5.conv2.conv.weight              Conv2d     False        4800      [192, 1, 5, 5]  0.000219    0.0279        float32
  152             model.7.m.5.conv2.bn.weight         BatchNorm2d     False         192               [192]     0.942     0.289        float32
  152               model.7.m.5.conv2.bn.bias         BatchNorm2d     False         192               [192]    -0.448     0.773        float32
  153                  model.7.sc.conv.weight              Conv2d     False 1.11411e+06   [512, 2176, 1, 1]  -0.00086    0.0152        float32
  154                    model.7.sc.bn.weight         BatchNorm2d     False         512               [512]       1.5     0.342        float32
  154                      model.7.sc.bn.bias         BatchNorm2d     False         512               [512]     -1.75      1.25        float32
  155                  model.7.ec.conv.weight              Conv2d     False      524288   [1024, 512, 1, 1]  -0.00201    0.0192        float32
  156                    model.7.ec.bn.weight         BatchNorm2d     False        1024              [1024]      2.08     0.728        float32
  156                      model.7.ec.bn.bias         BatchNorm2d     False        1024              [1024]     -2.21     0.934        float32
  157                     model.8.conv.weight              Conv2d     False        9216     [1024, 1, 3, 3]  0.000143    0.0397        float32
  158                       model.8.bn.weight         BatchNorm2d     False        1024              [1024]     0.799    0.0685        float32
  158                         model.8.bn.bias         BatchNorm2d     False        1024              [1024]  2.96e-06  0.000225        float32
  159                             model.8.act            Identity     False           0                  []         -         -              -
  160           model.9.m.0.conv1.conv.weight              Conv2d     False      393216   [384, 1024, 1, 1]  4.09e-06    0.0185        float32
  161             model.9.m.0.conv1.bn.weight         BatchNorm2d     False         384               [384]     0.977     0.131        float32
  161               model.9.m.0.conv1.bn.bias         BatchNorm2d     False         384               [384]   -0.0094     0.515        float32
  162                   model.9.m.0.conv1.act            Identity     False           0                  []         -         -              -
  163           model.9.m.0.conv2.conv.weight              Conv2d     False        9600      [384, 1, 5, 5]  2.48e-06    0.0403        float32
  164             model.9.m.0.conv2.bn.weight         BatchNorm2d     False         384               [384]      1.43     0.528        float32
  164               model.9.m.0.conv2.bn.bias         BatchNorm2d     False         384               [384]    -0.941      1.54        float32
  165           model.9.m.1.conv1.conv.weight              Conv2d     False      147456    [384, 384, 1, 1]  0.000307    0.0216        float32
  166             model.9.m.1.conv1.bn.weight         BatchNorm2d     False         384               [384]     0.888     0.131        float32
  166               model.9.m.1.conv1.bn.bias         BatchNorm2d     False         384               [384]    0.0369      0.58        float32
  167                   model.9.m.1.conv1.act            Identity     False           0                  []         -         -              -
  168           model.9.m.1.conv2.conv.weight              Conv2d     False        9600      [384, 1, 5, 5] -0.000825    0.0331        float32
  169             model.9.m.1.conv2.bn.weight         BatchNorm2d     False         384               [384]      1.19     0.386        float32
  169               model.9.m.1.conv2.bn.bias         BatchNorm2d     False         384               [384]     -0.81       1.1        float32
  170           model.9.m.2.conv1.conv.weight              Conv2d     False      147456    [384, 384, 1, 1]  9.04e-05    0.0195        float32
  171             model.9.m.2.conv1.bn.weight         BatchNorm2d     False         384               [384]     0.889     0.137        float32
  171               model.9.m.2.conv1.bn.bias         BatchNorm2d     False         384               [384]    0.0253     0.565        float32
  172                   model.9.m.2.conv1.act            Identity     False           0                  []         -         -              -
  173           model.9.m.2.conv2.conv.weight              Conv2d     False        9600      [384, 1, 5, 5]  -0.00045    0.0337        float32
  174             model.9.m.2.conv2.bn.weight         BatchNorm2d     False         384               [384]      1.12     0.316        float32
  174               model.9.m.2.conv2.bn.bias         BatchNorm2d     False         384               [384]    -0.731      0.89        float32
  175           model.9.m.3.conv1.conv.weight              Conv2d     False      147456    [384, 384, 1, 1] -9.82e-05    0.0182        float32
  176             model.9.m.3.conv1.bn.weight         BatchNorm2d     False         384               [384]      0.88     0.127        float32
  176               model.9.m.3.conv1.bn.bias         BatchNorm2d     False         384               [384]   -0.0142     0.563        float32
  177                   model.9.m.3.conv1.act            Identity     False           0                  []         -         -              -
  178           model.9.m.3.conv2.conv.weight              Conv2d     False        9600      [384, 1, 5, 5]  0.000362    0.0287        float32
  179             model.9.m.3.conv2.bn.weight         BatchNorm2d     False         384               [384]      1.12     0.265        float32
  179               model.9.m.3.conv2.bn.bias         BatchNorm2d     False         384               [384]    -0.765     0.719        float32
  180           model.9.m.4.conv1.conv.weight              Conv2d     False      147456    [384, 384, 1, 1]  0.000138    0.0166        float32
  181             model.9.m.4.conv1.bn.weight         BatchNorm2d     False         384               [384]     0.873     0.127        float32
  181               model.9.m.4.conv1.bn.bias         BatchNorm2d     False         384               [384]    0.0154     0.529        float32
  182                   model.9.m.4.conv1.act            Identity     False           0                  []         -         -              -
  183           model.9.m.4.conv2.conv.weight              Conv2d     False        9600      [384, 1, 5, 5] -0.000647    0.0265        float32
  184             model.9.m.4.conv2.bn.weight         BatchNorm2d     False         384               [384]      1.11     0.244        float32
  184               model.9.m.4.conv2.bn.bias         BatchNorm2d     False         384               [384]    -0.987     0.728        float32
  185           model.9.m.5.conv1.conv.weight              Conv2d     False      147456    [384, 384, 1, 1] -0.000107    0.0147        float32
  186             model.9.m.5.conv1.bn.weight         BatchNorm2d     False         384               [384]     0.896     0.127        float32
  186               model.9.m.5.conv1.bn.bias         BatchNorm2d     False         384               [384]  -0.00775     0.463        float32
  187                   model.9.m.5.conv1.act            Identity     False           0                  []         -         -              -
  188           model.9.m.5.conv2.conv.weight              Conv2d     False        9600      [384, 1, 5, 5] -3.79e-05    0.0229        float32
  189             model.9.m.5.conv2.bn.weight         BatchNorm2d     False         384               [384]      1.37      0.38        float32
  189               model.9.m.5.conv2.bn.bias         BatchNorm2d     False         384               [384]    -0.764      0.94        float32
  190                  model.9.sc.conv.weight              Conv2d     False 3.40787e+06  [1024, 3328, 1, 1] -0.000582     0.013        float32
  191                    model.9.sc.bn.weight         BatchNorm2d     False        1024              [1024]      1.48     0.401        float32
  191                      model.9.sc.bn.bias         BatchNorm2d     False        1024              [1024]     -1.58      1.28        float32
  192                  model.9.ec.conv.weight              Conv2d     False 2.09715e+06  [2048, 1024, 1, 1] -0.000945    0.0142        float32
  193                    model.9.ec.bn.weight         BatchNorm2d     False        2048              [2048]      4.42     0.487        float32
  193                      model.9.ec.bn.bias         BatchNorm2d     False        2048              [2048]     -5.61       1.2        float32
  194                    model.10.conv.weight              Conv2d     False      524288   [256, 2048, 1, 1]   0.00158    0.0746        float32
  195                      model.10.bn.weight         BatchNorm2d     False         256               [256]     0.881    0.0365        float32
  195                        model.10.bn.bias         BatchNorm2d     False         256               [256]   -0.0164    0.0795        float32
  196                            model.10.act            Identity     False           0                  []         -         -              -
  197             model.11.ma.out_proj.weightNonDynamicallyQuantizableLinear     False       65536          [256, 256] -0.000238    0.0807        float32
  197               model.11.ma.out_proj.biasNonDynamicallyQuantizableLinear     False         256               [256] -0.000216     0.104        float32
  198                     model.11.fc1.weight              Linear     False      262144         [1024, 256]  8.32e-05    0.0722        float32
  198                       model.11.fc1.bias              Linear     False        1024              [1024]    -0.101    0.0442        float32
  199                     model.11.fc2.weight              Linear     False      262144         [256, 1024] -5.94e-06    0.0646        float32
  199                       model.11.fc2.bias              Linear     False         256               [256]  -0.00152     0.242        float32
  200                   model.11.norm1.weight           LayerNorm     False         256               [256]     0.948    0.0422        float32
  200                     model.11.norm1.bias           LayerNorm     False         256               [256]  -0.00571     0.183        float32
  201                   model.11.norm2.weight           LayerNorm     False         256               [256]      1.01     0.041        float32
  201                     model.11.norm2.bias           LayerNorm     False         256               [256] -8.51e-07  2.19e-05        float32
  202                        model.11.dropout             Dropout     False           0                  []         -         -              -
  203                       model.11.dropout1             Dropout     False           0                  []         -         -              -
  204                       model.11.dropout2             Dropout     False           0                  []         -         -              -
  205                            model.11.act                GELU     False           0                  []         -         -              -
  206                    model.12.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]  0.000279    0.0762        float32
  207                      model.12.bn.weight         BatchNorm2d     False         256               [256]      1.07    0.0765        float32
  207                        model.12.bn.bias         BatchNorm2d     False         256               [256]   -0.0149    0.0864        float32
  208                            model.12.act                SiLU     False           0                  []         -         -              -
  209                                model.13            Upsample     False           0                  []         -         -              -
  210                    model.14.conv.weight              Conv2d     False      262144   [256, 1024, 1, 1] -6.37e-05    0.0825        float32
  211                      model.14.bn.weight         BatchNorm2d     False         256               [256]     0.916    0.0305        float32
  211                        model.14.bn.bias         BatchNorm2d     False         256               [256] -5.09e-07  1.16e-05        float32
  212                            model.14.act            Identity     False           0                  []         -         -              -
  213                                model.15              Concat     False           0                  []         -         -              -
  214                model.16.cv1.conv.weight              Conv2d     False      131072    [256, 512, 1, 1]  0.000616    0.0724        float32
  215                  model.16.cv1.bn.weight         BatchNorm2d     False         256               [256]      1.06    0.0509        float32
  215                    model.16.cv1.bn.bias         BatchNorm2d     False         256               [256]      -0.2    0.0762        float32
  216                model.16.cv2.conv.weight              Conv2d     False      131072    [256, 512, 1, 1]  0.000428    0.0733        float32
  217                  model.16.cv2.bn.weight         BatchNorm2d     False         256               [256]      1.06    0.0674        float32
  217                    model.16.cv2.bn.bias         BatchNorm2d     False         256               [256]   -0.0468     0.109        float32
  218                        model.16.m.0.act                SiLU     False           0                  []         -         -              -
  219          model.16.m.0.conv1.conv.weight              Conv2d     False      589824    [256, 256, 3, 3]  -0.00267     0.073        float32
  220            model.16.m.0.conv1.bn.weight         BatchNorm2d     False         256               [256]      1.06    0.0692        float32
  220              model.16.m.0.conv1.bn.bias         BatchNorm2d     False         256               [256]     -0.19    0.0599        float32
  221                  model.16.m.0.conv1.act            Identity     False           0                  []         -         -              -
  222          model.16.m.0.conv2.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]  -0.00736     0.111        float32
  223            model.16.m.0.conv2.bn.weight         BatchNorm2d     False         256               [256]     0.943     0.055        float32
  223              model.16.m.0.conv2.bn.bias         BatchNorm2d     False         256               [256]     -0.19    0.0599        float32
  224                  model.16.m.0.conv2.act            Identity     False           0                  []         -         -              -
  225          model.16.m.1.conv1.conv.weight              Conv2d     False      589824    [256, 256, 3, 3]  -0.00342    0.0732        float32
  226            model.16.m.1.conv1.bn.weight         BatchNorm2d     False         256               [256]      1.07    0.0802        float32
  226              model.16.m.1.conv1.bn.bias         BatchNorm2d     False         256               [256]    -0.169    0.0594        float32
  227                  model.16.m.1.conv1.act            Identity     False           0                  []         -         -              -
  228          model.16.m.1.conv2.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]  -0.00736     0.112        float32
  229            model.16.m.1.conv2.bn.weight         BatchNorm2d     False         256               [256]      0.93    0.0527        float32
  229              model.16.m.1.conv2.bn.bias         BatchNorm2d     False         256               [256]    -0.169    0.0594        float32
  230                  model.16.m.1.conv2.act            Identity     False           0                  []         -         -              -
  231          model.16.m.2.conv1.conv.weight              Conv2d     False      589824    [256, 256, 3, 3]  -0.00284    0.0733        float32
  232            model.16.m.2.conv1.bn.weight         BatchNorm2d     False         256               [256]      1.06    0.0608        float32
  232              model.16.m.2.conv1.bn.bias         BatchNorm2d     False         256               [256]   -0.0896    0.0653        float32
  233                  model.16.m.2.conv1.act            Identity     False           0                  []         -         -              -
  234          model.16.m.2.conv2.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]  -0.00815     0.112        float32
  235            model.16.m.2.conv2.bn.weight         BatchNorm2d     False         256               [256]     0.887    0.0486        float32
  235              model.16.m.2.conv2.bn.bias         BatchNorm2d     False         256               [256]   -0.0896    0.0653        float32
  236                  model.16.m.2.conv2.act            Identity     False           0                  []         -         -              -
  237                            model.16.cv3            Identity     False           0                  []         -         -              -
  238                    model.17.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]  -0.00499    0.0768        float32
  239                      model.17.bn.weight         BatchNorm2d     False         256               [256]      1.12    0.0826        float32
  239                        model.17.bn.bias         BatchNorm2d     False         256               [256]    0.0314    0.0922        float32
  240                                model.18            Upsample     False           0                  []         -         -              -
  241                    model.19.conv.weight              Conv2d     False      131072    [256, 512, 1, 1]   2.3e-05    0.0928        float32
  242                      model.19.bn.weight         BatchNorm2d     False         256               [256]     0.819    0.0459        float32
  242                        model.19.bn.bias         BatchNorm2d     False         256               [256] -3.47e-07  1.78e-05        float32
  243                            model.19.act            Identity     False           0                  []         -         -              -
  244                                model.20              Concat     False           0                  []         -         -              -
  245                model.21.cv1.conv.weight              Conv2d     False      131072    [256, 512, 1, 1]  -0.00148    0.0734        float32
  246                  model.21.cv1.bn.weight         BatchNorm2d     False         256               [256]      1.02    0.0755        float32
  246                    model.21.cv1.bn.bias         BatchNorm2d     False         256               [256]    -0.179     0.119        float32
  247                model.21.cv2.conv.weight              Conv2d     False      131072    [256, 512, 1, 1]  -0.00244    0.0736        float32
  248                  model.21.cv2.bn.weight         BatchNorm2d     False         256               [256]     0.895    0.0842        float32
  248                    model.21.cv2.bn.bias         BatchNorm2d     False         256               [256]    0.0516     0.116        float32
  249          model.21.m.0.conv1.conv.weight              Conv2d     False      589824    [256, 256, 3, 3]  -0.00203    0.0732        float32
  250            model.21.m.0.conv1.bn.weight         BatchNorm2d     False         256               [256]      1.04    0.0755        float32
  250              model.21.m.0.conv1.bn.bias         BatchNorm2d     False         256               [256]    -0.251    0.0989        float32
  251                  model.21.m.0.conv1.act            Identity     False           0                  []         -         -              -
  252          model.21.m.0.conv2.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]   -0.0111     0.112        float32
  253            model.21.m.0.conv2.bn.weight         BatchNorm2d     False         256               [256]     0.873    0.0523        float32
  253              model.21.m.0.conv2.bn.bias         BatchNorm2d     False         256               [256]    -0.251    0.0989        float32
  254                  model.21.m.0.conv2.act            Identity     False           0                  []         -         -              -
  255          model.21.m.1.conv1.conv.weight              Conv2d     False      589824    [256, 256, 3, 3]  -0.00389    0.0734        float32
  256            model.21.m.1.conv1.bn.weight         BatchNorm2d     False         256               [256]      1.07    0.0854        float32
  256              model.21.m.1.conv1.bn.bias         BatchNorm2d     False         256               [256]    -0.211    0.0907        float32
  257                  model.21.m.1.conv1.act            Identity     False           0                  []         -         -              -
  258          model.21.m.1.conv2.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]   -0.0136     0.112        float32
  259            model.21.m.1.conv2.bn.weight         BatchNorm2d     False         256               [256]     0.876    0.0553        float32
  259              model.21.m.1.conv2.bn.bias         BatchNorm2d     False         256               [256]    -0.211    0.0907        float32
  260                  model.21.m.1.conv2.act            Identity     False           0                  []         -         -              -
  261          model.21.m.2.conv1.conv.weight              Conv2d     False      589824    [256, 256, 3, 3]  -0.00314    0.0731        float32
  262            model.21.m.2.conv1.bn.weight         BatchNorm2d     False         256               [256]       1.1     0.125        float32
  262              model.21.m.2.conv1.bn.bias         BatchNorm2d     False         256               [256]     0.017     0.107        float32
  263                  model.21.m.2.conv1.act            Identity     False           0                  []         -         -              -
  264          model.21.m.2.conv2.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]   -0.0131     0.112        float32
  265            model.21.m.2.conv2.bn.weight         BatchNorm2d     False         256               [256]     0.951     0.127        float32
  265              model.21.m.2.conv2.bn.bias         BatchNorm2d     False         256               [256]     0.017     0.107        float32
  266                  model.21.m.2.conv2.act            Identity     False           0                  []         -         -              -
  267                            model.21.cv3            Identity     False           0                  []         -         -              -
  268                    model.22.conv.weight              Conv2d     False      589824    [256, 256, 3, 3] -0.000837    0.0666        float32
  269                      model.22.bn.weight         BatchNorm2d     False         256               [256]      1.15    0.0852        float32
  269                        model.22.bn.bias         BatchNorm2d     False         256               [256]    -0.234     0.106        float32
  270                                model.23              Concat     False           0                  []         -         -              -
  271                model.24.cv1.conv.weight              Conv2d     False      131072    [256, 512, 1, 1]  -0.00411    0.0721        float32
  272                  model.24.cv1.bn.weight         BatchNorm2d     False         256               [256]      1.07    0.0645        float32
  272                    model.24.cv1.bn.bias         BatchNorm2d     False         256               [256]    -0.204    0.0868        float32
  273                model.24.cv2.conv.weight              Conv2d     False      131072    [256, 512, 1, 1]  -0.00651    0.0725        float32
  274                  model.24.cv2.bn.weight         BatchNorm2d     False         256               [256]     0.968    0.0901        float32
  274                    model.24.cv2.bn.bias         BatchNorm2d     False         256               [256]   -0.0336    0.0746        float32
  275          model.24.m.0.conv1.conv.weight              Conv2d     False      589824    [256, 256, 3, 3]  -0.00253    0.0725        float32
  276            model.24.m.0.conv1.bn.weight         BatchNorm2d     False         256               [256]      1.08    0.0784        float32
  276              model.24.m.0.conv1.bn.bias         BatchNorm2d     False         256               [256]    -0.243    0.0816        float32
  277                  model.24.m.0.conv1.act            Identity     False           0                  []         -         -              -
  278          model.24.m.0.conv2.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]   -0.0107     0.112        float32
  279            model.24.m.0.conv2.bn.weight         BatchNorm2d     False         256               [256]     0.892    0.0653        float32
  279              model.24.m.0.conv2.bn.bias         BatchNorm2d     False         256               [256]    -0.243    0.0816        float32
  280                  model.24.m.0.conv2.act            Identity     False           0                  []         -         -              -
  281          model.24.m.1.conv1.conv.weight              Conv2d     False      589824    [256, 256, 3, 3]  -0.00306    0.0731        float32
  282            model.24.m.1.conv1.bn.weight         BatchNorm2d     False         256               [256]      1.08    0.0823        float32
  282              model.24.m.1.conv1.bn.bias         BatchNorm2d     False         256               [256]    -0.228    0.0742        float32
  283                  model.24.m.1.conv1.act            Identity     False           0                  []         -         -              -
  284          model.24.m.1.conv2.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]  -0.00876     0.112        float32
  285            model.24.m.1.conv2.bn.weight         BatchNorm2d     False         256               [256]     0.892    0.0654        float32
  285              model.24.m.1.conv2.bn.bias         BatchNorm2d     False         256               [256]    -0.228    0.0742        float32
  286                  model.24.m.1.conv2.act            Identity     False           0                  []         -         -              -
  287          model.24.m.2.conv1.conv.weight              Conv2d     False      589824    [256, 256, 3, 3]  -0.00316     0.073        float32
  288            model.24.m.2.conv1.bn.weight         BatchNorm2d     False         256               [256]       1.1    0.0971        float32
  288              model.24.m.2.conv1.bn.bias         BatchNorm2d     False         256               [256]   -0.0732    0.0904        float32
  289                  model.24.m.2.conv1.act            Identity     False           0                  []         -         -              -
  290          model.24.m.2.conv2.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]   -0.0129     0.112        float32
  291            model.24.m.2.conv2.bn.weight         BatchNorm2d     False         256               [256]     0.924     0.101        float32
  291              model.24.m.2.conv2.bn.bias         BatchNorm2d     False         256               [256]   -0.0732    0.0904        float32
  292                  model.24.m.2.conv2.act            Identity     False           0                  []         -         -              -
  293                            model.24.cv3            Identity     False           0                  []         -         -              -
  294                    model.25.conv.weight              Conv2d     False      589824    [256, 256, 3, 3]  -0.00249    0.0672        float32
  295                      model.25.bn.weight         BatchNorm2d     False         256               [256]      1.13     0.103        float32
  295                        model.25.bn.bias         BatchNorm2d     False         256               [256]    -0.117     0.115        float32
  296                                model.26              Concat     False           0                  []         -         -              -
  297                model.27.cv1.conv.weight              Conv2d     False      131072    [256, 512, 1, 1]  -0.00234     0.073        float32
  298                  model.27.cv1.bn.weight         BatchNorm2d     False         256               [256]      1.06    0.0917        float32
  298                    model.27.cv1.bn.bias         BatchNorm2d     False         256               [256]   -0.0958     0.137        float32
  299                model.27.cv2.conv.weight              Conv2d     False      131072    [256, 512, 1, 1]  -0.00179    0.0727        float32
  300                  model.27.cv2.bn.weight         BatchNorm2d     False         256               [256]     0.997      0.08        float32
  300                    model.27.cv2.bn.bias         BatchNorm2d     False         256               [256]     0.124     0.118        float32
  301          model.27.m.0.conv1.conv.weight              Conv2d     False      589824    [256, 256, 3, 3]  -0.00165     0.072        float32
  302            model.27.m.0.conv1.bn.weight         BatchNorm2d     False         256               [256]       1.1     0.101        float32
  302              model.27.m.0.conv1.bn.bias         BatchNorm2d     False         256               [256]    -0.134     0.102        float32
  303                  model.27.m.0.conv1.act            Identity     False           0                  []         -         -              -
  304          model.27.m.0.conv2.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]  -0.00495     0.112        float32
  305            model.27.m.0.conv2.bn.weight         BatchNorm2d     False         256               [256]     0.917    0.0769        float32
  305              model.27.m.0.conv2.bn.bias         BatchNorm2d     False         256               [256]    -0.134     0.102        float32
  306                  model.27.m.0.conv2.act            Identity     False           0                  []         -         -              -
  307          model.27.m.1.conv1.conv.weight              Conv2d     False      589824    [256, 256, 3, 3]  -0.00287    0.0731        float32
  308            model.27.m.1.conv1.bn.weight         BatchNorm2d     False         256               [256]      1.09     0.114        float32
  308              model.27.m.1.conv1.bn.bias         BatchNorm2d     False         256               [256]    -0.128     0.105        float32
  309                  model.27.m.1.conv1.act            Identity     False           0                  []         -         -              -
  310          model.27.m.1.conv2.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]  -0.00362     0.112        float32
  311            model.27.m.1.conv2.bn.weight         BatchNorm2d     False         256               [256]     0.924     0.105        float32
  311              model.27.m.1.conv2.bn.bias         BatchNorm2d     False         256               [256]    -0.128     0.105        float32
  312                  model.27.m.1.conv2.act            Identity     False           0                  []         -         -              -
  313          model.27.m.2.conv1.conv.weight              Conv2d     False      589824    [256, 256, 3, 3]  -0.00234    0.0731        float32
  314            model.27.m.2.conv1.bn.weight         BatchNorm2d     False         256               [256]      1.02     0.111        float32
  314              model.27.m.2.conv1.bn.bias         BatchNorm2d     False         256               [256]   -0.0278     0.127        float32
  315                  model.27.m.2.conv1.act            Identity     False           0                  []         -         -              -
  316          model.27.m.2.conv2.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]  -0.00602     0.112        float32
  317            model.27.m.2.conv2.bn.weight         BatchNorm2d     False         256               [256]     0.921     0.102        float32
  317              model.27.m.2.conv2.bn.bias         BatchNorm2d     False         256               [256]   -0.0278     0.127        float32
  318                  model.27.m.2.conv2.act            Identity     False           0                  []         -         -              -
  319                            model.27.cv3            Identity     False           0                  []         -         -              -
  320          model.28.input_proj.0.0.weight              Conv2d     False       65536    [256, 256, 1, 1] -0.000359    0.0917        float32
  321          model.28.input_proj.0.1.weight         BatchNorm2d     False         256               [256]     0.876    0.0403        float32
  321            model.28.input_proj.0.1.bias         BatchNorm2d     False         256               [256]   0.00426    0.0908        float32
  322          model.28.input_proj.1.0.weight              Conv2d     False       65536    [256, 256, 1, 1] -5.44e-05    0.0918        float32
  323          model.28.input_proj.1.1.weight         BatchNorm2d     False         256               [256]     0.873    0.0424        float32
  323            model.28.input_proj.1.1.bias         BatchNorm2d     False         256               [256]   0.00558    0.0985        float32
  324          model.28.input_proj.2.0.weight              Conv2d     False       65536    [256, 256, 1, 1]  0.000294    0.0914        float32
  325          model.28.input_proj.2.1.weight         BatchNorm2d     False         256               [256]     0.928    0.0368        float32
  325            model.28.input_proj.2.1.bias         BatchNorm2d     False         256               [256] -0.000983      0.13        float32
  326model.28.decoder.layers.0.self_attn.out_proj.weightNonDynamicallyQuantizableLinear     False       65536          [256, 256]  4.56e-05    0.0793        float32
  326model.28.decoder.layers.0.self_attn.out_proj.biasNonDynamicallyQuantizableLinear     False         256               [256]  -0.00115    0.0778        float32
  327      model.28.decoder.layers.0.dropout1             Dropout     False           0                  []         -         -              -
  328  model.28.decoder.layers.0.norm1.weight           LayerNorm     False         256               [256]     0.817    0.0852        float32
  328    model.28.decoder.layers.0.norm1.bias           LayerNorm     False         256               [256]   0.00124    0.0875        float32
  329model.28.decoder.layers.0.cross_attn.sampling_offsets.weight              Linear     False       49152          [192, 256]  0.000213    0.0505        float32
  329model.28.decoder.layers.0.cross_attn.sampling_offsets.bias              Linear     False         192               [192]   0.00267      2.38        float32
  330model.28.decoder.layers.0.cross_attn.attention_weights.weight              Linear     False       24576           [96, 256] -0.000566    0.0505        float32
  330model.28.decoder.layers.0.cross_attn.attention_weights.bias              Linear     False          96                [96]    0.0139    0.0547        float32
  331model.28.decoder.layers.0.cross_attn.value_proj.weight              Linear     False       65536          [256, 256] -0.000176    0.0868        float32
  331model.28.decoder.layers.0.cross_attn.value_proj.bias              Linear     False         256               [256]  -0.00462    0.0858        float32
  332model.28.decoder.layers.0.cross_attn.output_proj.weight              Linear     False       65536          [256, 256]  0.000337    0.0882        float32
  332model.28.decoder.layers.0.cross_attn.output_proj.bias              Linear     False         256               [256] -0.000168    0.0444        float32
  333      model.28.decoder.layers.0.dropout2             Dropout     False           0                  []         -         -              -
  334  model.28.decoder.layers.0.norm2.weight           LayerNorm     False         256               [256]      1.01    0.0409        float32
  334    model.28.decoder.layers.0.norm2.bias           LayerNorm     False         256               [256]   0.00502    0.0656        float32
  335model.28.decoder.layers.0.linear1.weight              Linear     False      262144         [1024, 256] -0.000172    0.0728        float32
  335  model.28.decoder.layers.0.linear1.bias              Linear     False        1024              [1024]   -0.0488    0.0505        float32
  336           model.28.decoder.layers.0.act                ReLU     False           0                  []         -         -              -
  337      model.28.decoder.layers.0.dropout3             Dropout     False           0                  []         -         -              -
  338model.28.decoder.layers.0.linear2.weight              Linear     False      262144         [256, 1024] -0.000146    0.0705        float32
  338  model.28.decoder.layers.0.linear2.bias              Linear     False         256               [256]   0.00136    0.0561        float32
  339      model.28.decoder.layers.0.dropout4             Dropout     False           0                  []         -         -              -
  340  model.28.decoder.layers.0.norm3.weight           LayerNorm     False         256               [256]     0.965    0.0755        float32
  340    model.28.decoder.layers.0.norm3.bias           LayerNorm     False         256               [256]    0.0113    0.0754        float32
  341model.28.decoder.layers.1.self_attn.out_proj.weightNonDynamicallyQuantizableLinear     False       65536          [256, 256]  7.86e-05    0.0748        float32
  341model.28.decoder.layers.1.self_attn.out_proj.biasNonDynamicallyQuantizableLinear     False         256               [256]  -0.00051    0.0782        float32
  342      model.28.decoder.layers.1.dropout1             Dropout     False           0                  []         -         -              -
  343  model.28.decoder.layers.1.norm1.weight           LayerNorm     False         256               [256]     0.888     0.105        float32
  343    model.28.decoder.layers.1.norm1.bias           LayerNorm     False         256               [256]  -0.00429    0.0747        float32
  344model.28.decoder.layers.1.cross_attn.sampling_offsets.weight              Linear     False       49152          [192, 256]  0.000185    0.0605        float32
  344model.28.decoder.layers.1.cross_attn.sampling_offsets.bias              Linear     False         192               [192]   0.00287      2.38        float32
  345model.28.decoder.layers.1.cross_attn.attention_weights.weight              Linear     False       24576           [96, 256] -0.000605    0.0578        float32
  345model.28.decoder.layers.1.cross_attn.attention_weights.bias              Linear     False          96                [96]    0.0109    0.0589        float32
  346model.28.decoder.layers.1.cross_attn.value_proj.weight              Linear     False       65536          [256, 256] -0.000188    0.0826        float32
  346model.28.decoder.layers.1.cross_attn.value_proj.bias              Linear     False         256               [256]   0.00208     0.059        float32
  347model.28.decoder.layers.1.cross_attn.output_proj.weight              Linear     False       65536          [256, 256]  0.000337    0.0843        float32
  347model.28.decoder.layers.1.cross_attn.output_proj.bias              Linear     False         256               [256] -0.000174     0.055        float32
  348      model.28.decoder.layers.1.dropout2             Dropout     False           0                  []         -         -              -
  349  model.28.decoder.layers.1.norm2.weight           LayerNorm     False         256               [256]         1    0.0509        float32
  349    model.28.decoder.layers.1.norm2.bias           LayerNorm     False         256               [256]  -0.00283    0.0679        float32
  350model.28.decoder.layers.1.linear1.weight              Linear     False      262144         [1024, 256] -0.000191    0.0717        float32
  350  model.28.decoder.layers.1.linear1.bias              Linear     False        1024              [1024]   -0.0557     0.049        float32
  351           model.28.decoder.layers.1.act                ReLU     False           0                  []         -         -              -
  352      model.28.decoder.layers.1.dropout3             Dropout     False           0                  []         -         -              -
  353model.28.decoder.layers.1.linear2.weight              Linear     False      262144         [256, 1024] -3.94e-05    0.0703        float32
  353  model.28.decoder.layers.1.linear2.bias              Linear     False         256               [256]  0.000974    0.0637        float32
  354      model.28.decoder.layers.1.dropout4             Dropout     False           0                  []         -         -              -
  355  model.28.decoder.layers.1.norm3.weight           LayerNorm     False         256               [256]      0.98    0.0779        float32
  355    model.28.decoder.layers.1.norm3.bias           LayerNorm     False         256               [256]  -0.00634     0.103        float32
  356model.28.decoder.layers.2.self_attn.out_proj.weightNonDynamicallyQuantizableLinear     False       65536          [256, 256]  3.51e-05      0.07        float32
  356model.28.decoder.layers.2.self_attn.out_proj.biasNonDynamicallyQuantizableLinear     False         256               [256] -0.000171    0.0767        float32
  357      model.28.decoder.layers.2.dropout1             Dropout     False           0                  []         -         -              -
  358  model.28.decoder.layers.2.norm1.weight           LayerNorm     False         256               [256]     0.874     0.102        float32
  358    model.28.decoder.layers.2.norm1.bias           LayerNorm     False         256               [256]    -0.013    0.0878        float32
  359model.28.decoder.layers.2.cross_attn.sampling_offsets.weight              Linear     False       49152          [192, 256]   0.00038    0.0596        float32
  359model.28.decoder.layers.2.cross_attn.sampling_offsets.bias              Linear     False         192               [192]   0.00298      2.38        float32
  360model.28.decoder.layers.2.cross_attn.attention_weights.weight              Linear     False       24576           [96, 256] -0.000166    0.0606        float32
  360model.28.decoder.layers.2.cross_attn.attention_weights.bias              Linear     False          96                [96]   0.00557    0.0535        float32
  361model.28.decoder.layers.2.cross_attn.value_proj.weight              Linear     False       65536          [256, 256] -0.000237     0.083        float32
  361model.28.decoder.layers.2.cross_attn.value_proj.bias              Linear     False         256               [256]  0.000856    0.0555        float32
  362model.28.decoder.layers.2.cross_attn.output_proj.weight              Linear     False       65536          [256, 256]   0.00035    0.0841        float32
  362model.28.decoder.layers.2.cross_attn.output_proj.bias              Linear     False         256               [256] -0.000166      0.06        float32
  363      model.28.decoder.layers.2.dropout2             Dropout     False           0                  []         -         -              -
  364  model.28.decoder.layers.2.norm2.weight           LayerNorm     False         256               [256]         1    0.0421        float32
  364    model.28.decoder.layers.2.norm2.bias           LayerNorm     False         256               [256]  -0.00169    0.0656        float32
  365model.28.decoder.layers.2.linear1.weight              Linear     False      262144         [1024, 256] -0.000357    0.0712        float32
  365  model.28.decoder.layers.2.linear1.bias              Linear     False        1024              [1024]    -0.054    0.0492        float32
  366           model.28.decoder.layers.2.act                ReLU     False           0                  []         -         -              -
  367      model.28.decoder.layers.2.dropout3             Dropout     False           0                  []         -         -              -
  368model.28.decoder.layers.2.linear2.weight              Linear     False      262144         [256, 1024] -0.000115    0.0699        float32
  368  model.28.decoder.layers.2.linear2.bias              Linear     False         256               [256]  0.000483    0.0591        float32
  369      model.28.decoder.layers.2.dropout4             Dropout     False           0                  []         -         -              -
  370  model.28.decoder.layers.2.norm3.weight           LayerNorm     False         256               [256]     0.964    0.0644        float32
  370    model.28.decoder.layers.2.norm3.bias           LayerNorm     False         256               [256]   0.00185    0.0863        float32
  371model.28.decoder.layers.3.self_attn.out_proj.weightNonDynamicallyQuantizableLinear     False       65536          [256, 256]  6.83e-05    0.0686        float32
  371model.28.decoder.layers.3.self_attn.out_proj.biasNonDynamicallyQuantizableLinear     False         256               [256] -0.000278    0.0708        float32
  372      model.28.decoder.layers.3.dropout1             Dropout     False           0                  []         -         -              -
  373  model.28.decoder.layers.3.norm1.weight           LayerNorm     False         256               [256]     0.864    0.0887        float32
  373    model.28.decoder.layers.3.norm1.bias           LayerNorm     False         256               [256]  -0.00481    0.0808        float32
  374model.28.decoder.layers.3.cross_attn.sampling_offsets.weight              Linear     False       49152          [192, 256] -0.000127    0.0606        float32
  374model.28.decoder.layers.3.cross_attn.sampling_offsets.bias              Linear     False         192               [192]   0.00325      2.38        float32
  375model.28.decoder.layers.3.cross_attn.attention_weights.weight              Linear     False       24576           [96, 256]  3.16e-06    0.0635        float32
  375model.28.decoder.layers.3.cross_attn.attention_weights.bias              Linear     False          96                [96]   0.00355    0.0467        float32
  376model.28.decoder.layers.3.cross_attn.value_proj.weight              Linear     False       65536          [256, 256]  -0.00017    0.0837        float32
  376model.28.decoder.layers.3.cross_attn.value_proj.bias              Linear     False         256               [256]  -0.00267    0.0531        float32
  377model.28.decoder.layers.3.cross_attn.output_proj.weight              Linear     False       65536          [256, 256]  0.000355    0.0837        float32
  377model.28.decoder.layers.3.cross_attn.output_proj.bias              Linear     False         256               [256]   0.00047    0.0577        float32
  378      model.28.decoder.layers.3.dropout2             Dropout     False           0                  []         -         -              -
  379  model.28.decoder.layers.3.norm2.weight           LayerNorm     False         256               [256]         1    0.0471        float32
  379    model.28.decoder.layers.3.norm2.bias           LayerNorm     False         256               [256]   0.00562    0.0569        float32
  380model.28.decoder.layers.3.linear1.weight              Linear     False      262144         [1024, 256] -0.000128    0.0722        float32
  380  model.28.decoder.layers.3.linear1.bias              Linear     False        1024              [1024]   -0.0523     0.046        float32
  381           model.28.decoder.layers.3.act                ReLU     False           0                  []         -         -              -
  382      model.28.decoder.layers.3.dropout3             Dropout     False           0                  []         -         -              -
  383model.28.decoder.layers.3.linear2.weight              Linear     False      262144         [256, 1024]  1.54e-06    0.0689        float32
  383  model.28.decoder.layers.3.linear2.bias              Linear     False         256               [256]   0.00104    0.0614        float32
  384      model.28.decoder.layers.3.dropout4             Dropout     False           0                  []         -         -              -
  385  model.28.decoder.layers.3.norm3.weight           LayerNorm     False         256               [256]     0.908    0.0626        float32
  385    model.28.decoder.layers.3.norm3.bias           LayerNorm     False         256               [256]   0.00996    0.0845        float32
  386model.28.decoder.layers.4.self_attn.out_proj.weightNonDynamicallyQuantizableLinear     False       65536          [256, 256]  9.06e-05    0.0656        float32
  386model.28.decoder.layers.4.self_attn.out_proj.biasNonDynamicallyQuantizableLinear     False         256               [256] -0.000194    0.0673        float32
  387      model.28.decoder.layers.4.dropout1             Dropout     False           0                  []         -         -              -
  388  model.28.decoder.layers.4.norm1.weight           LayerNorm     False         256               [256]     0.871    0.0733        float32
  388    model.28.decoder.layers.4.norm1.bias           LayerNorm     False         256               [256]   -0.0112    0.0658        float32
  389model.28.decoder.layers.4.cross_attn.sampling_offsets.weight              Linear     False       49152          [192, 256]   0.00139    0.0646        float32
  389model.28.decoder.layers.4.cross_attn.sampling_offsets.bias              Linear     False         192               [192]   -0.0153      2.38        float32
  390model.28.decoder.layers.4.cross_attn.attention_weights.weight              Linear     False       24576           [96, 256] -0.000201    0.0627        float32
  390model.28.decoder.layers.4.cross_attn.attention_weights.bias              Linear     False          96                [96]   0.00637    0.0451        float32
  391model.28.decoder.layers.4.cross_attn.value_proj.weight              Linear     False       65536          [256, 256] -0.000148    0.0831        float32
  391model.28.decoder.layers.4.cross_attn.value_proj.bias              Linear     False         256               [256]  0.000473    0.0499        float32
  392model.28.decoder.layers.4.cross_attn.output_proj.weight              Linear     False       65536          [256, 256]  0.000314     0.083        float32
  392model.28.decoder.layers.4.cross_attn.output_proj.bias              Linear     False         256               [256]  0.000257     0.056        float32
  393      model.28.decoder.layers.4.dropout2             Dropout     False           0                  []         -         -              -
  394  model.28.decoder.layers.4.norm2.weight           LayerNorm     False         256               [256]     0.995    0.0476        float32
  394    model.28.decoder.layers.4.norm2.bias           LayerNorm     False         256               [256]   0.00793    0.0543        float32
  395model.28.decoder.layers.4.linear1.weight              Linear     False      262144         [1024, 256]  7.44e-05     0.073        float32
  395  model.28.decoder.layers.4.linear1.bias              Linear     False        1024              [1024]   -0.0499    0.0424        float32
  396           model.28.decoder.layers.4.act                ReLU     False           0                  []         -         -              -
  397      model.28.decoder.layers.4.dropout3             Dropout     False           0                  []         -         -              -
  398model.28.decoder.layers.4.linear2.weight              Linear     False      262144         [256, 1024] -0.000194     0.069        float32
  398  model.28.decoder.layers.4.linear2.bias              Linear     False         256               [256]  0.000405      0.05        float32
  399      model.28.decoder.layers.4.dropout4             Dropout     False           0                  []         -         -              -
  400  model.28.decoder.layers.4.norm3.weight           LayerNorm     False         256               [256]     0.858    0.0627        float32
  400    model.28.decoder.layers.4.norm3.bias           LayerNorm     False         256               [256]     0.011    0.0775        float32
  401model.28.decoder.layers.5.self_attn.out_proj.weightNonDynamicallyQuantizableLinear     False       65536          [256, 256]  9.18e-05     0.063        float32
  401model.28.decoder.layers.5.self_attn.out_proj.biasNonDynamicallyQuantizableLinear     False         256               [256]  -0.00136    0.0455        float32
  402      model.28.decoder.layers.5.dropout1             Dropout     False           0                  []         -         -              -
  403  model.28.decoder.layers.5.norm1.weight           LayerNorm     False         256               [256]      1.03     0.056        float32
  403    model.28.decoder.layers.5.norm1.bias           LayerNorm     False         256               [256]   0.00519    0.0488        float32
  404model.28.decoder.layers.5.cross_attn.sampling_offsets.weight              Linear     False       49152          [192, 256]  0.000118    0.0567        float32
  404model.28.decoder.layers.5.cross_attn.sampling_offsets.bias              Linear     False         192               [192]   0.00523      2.37        float32
  405model.28.decoder.layers.5.cross_attn.attention_weights.weight              Linear     False       24576           [96, 256]  -0.00101     0.057        float32
  405model.28.decoder.layers.5.cross_attn.attention_weights.bias              Linear     False          96                [96]    0.0322    0.0202        float32
  406model.28.decoder.layers.5.cross_attn.value_proj.weight              Linear     False       65536          [256, 256]  2.55e-06    0.0798        float32
  406model.28.decoder.layers.5.cross_attn.value_proj.bias              Linear     False         256               [256]  -0.00323     0.034        float32
  407model.28.decoder.layers.5.cross_attn.output_proj.weight              Linear     False       65536          [256, 256]  0.000389    0.0792        float32
  407model.28.decoder.layers.5.cross_attn.output_proj.bias              Linear     False         256               [256] -0.000457    0.0513        float32
  408      model.28.decoder.layers.5.dropout2             Dropout     False           0                  []         -         -              -
  409  model.28.decoder.layers.5.norm2.weight           LayerNorm     False         256               [256]     0.981    0.0489        float32
  409    model.28.decoder.layers.5.norm2.bias           LayerNorm     False         256               [256]   0.00673    0.0493        float32
  410model.28.decoder.layers.5.linear1.weight              Linear     False      262144         [1024, 256]  0.000114    0.0731        float32
  410  model.28.decoder.layers.5.linear1.bias              Linear     False        1024              [1024]   -0.0531     0.044        float32
  411           model.28.decoder.layers.5.act                ReLU     False           0                  []         -         -              -
  412      model.28.decoder.layers.5.dropout3             Dropout     False           0                  []         -         -              -
  413model.28.decoder.layers.5.linear2.weight              Linear     False      262144         [256, 1024] -8.27e-05    0.0692        float32
  413  model.28.decoder.layers.5.linear2.bias              Linear     False         256               [256]   0.00175    0.0398        float32
  414      model.28.decoder.layers.5.dropout4             Dropout     False           0                  []         -         -              -
  415  model.28.decoder.layers.5.norm3.weight           LayerNorm     False         256               [256]     0.817    0.0731        float32
  415    model.28.decoder.layers.5.norm3.bias           LayerNorm     False         256               [256]  -0.00276    0.0747        float32
  416   model.28.denoising_class_embed.weight           Embedding     False       20480           [80, 256]   0.00374      0.97        float32
  417 model.28.query_pos_head.layers.0.weight              Linear     False        2048            [512, 4]   -0.0497     0.235        float32
  417   model.28.query_pos_head.layers.0.bias              Linear     False         512               [512]   -0.0388     0.228        float32
  418 model.28.query_pos_head.layers.1.weight              Linear     False      131072          [256, 512]  0.000355     0.121        float32
  418   model.28.query_pos_head.layers.1.bias              Linear     False         256               [256]  -0.00352    0.0439        float32
  419            model.28.enc_output.0.weight              Linear     False       65536          [256, 256]  0.000145     0.094        float32
  419              model.28.enc_output.0.bias              Linear     False         256               [256] -0.000814      0.18        float32
  420            model.28.enc_output.1.weight           LayerNorm     False         256               [256]     0.841    0.0972        float32
  420              model.28.enc_output.1.bias           LayerNorm     False         256               [256]   0.00209     0.167        float32
  421          model.28.enc_score_head.weight              Linear     False       20480           [80, 256]  0.000762    0.0777        float32
  421            model.28.enc_score_head.bias              Linear     False          80                [80]     -4.74    0.0774        float32
  422  model.28.enc_bbox_head.layers.0.weight              Linear     False       65536          [256, 256] -8.33e-05    0.0655        float32
  422    model.28.enc_bbox_head.layers.0.bias              Linear     False         256               [256]   -0.0555     0.106        float32
  423  model.28.enc_bbox_head.layers.1.weight              Linear     False       65536          [256, 256]   -0.0087    0.0613        float32
  423    model.28.enc_bbox_head.layers.1.bias              Linear     False         256               [256]   -0.0146     0.077        float32
  424  model.28.enc_bbox_head.layers.2.weight              Linear     False        1024            [4, 256]    0.0224     0.118        float32
  424    model.28.enc_bbox_head.layers.2.bias              Linear     False           4                 [4]  -0.00709    0.0372        float32
  425        model.28.dec_score_head.0.weight              Linear     False       20480           [80, 256]   0.00103     0.111        float32
  425          model.28.dec_score_head.0.bias              Linear     False          80                [80]     -4.65     0.057        float32
  426        model.28.dec_score_head.1.weight              Linear     False       20480           [80, 256] -0.000737    0.0847        float32
  426          model.28.dec_score_head.1.bias              Linear     False          80                [80]     -4.62    0.0509        float32
  427        model.28.dec_score_head.2.weight              Linear     False       20480           [80, 256] -0.000446    0.0692        float32
  427          model.28.dec_score_head.2.bias              Linear     False          80                [80]     -4.62    0.0469        float32
  428        model.28.dec_score_head.3.weight              Linear     False       20480           [80, 256]   0.00149    0.0635        float32
  428          model.28.dec_score_head.3.bias              Linear     False          80                [80]     -4.63    0.0361        float32
  429        model.28.dec_score_head.4.weight              Linear     False       20480           [80, 256]  0.000908    0.0598        float32
  429          model.28.dec_score_head.4.bias              Linear     False          80                [80]     -4.63    0.0308        float32
  430        model.28.dec_score_head.5.weight              Linear     False       20480           [80, 256] -0.000775    0.0534        float32
  430          model.28.dec_score_head.5.bias              Linear     False          80                [80]     -4.62    0.0283        float32
  431model.28.dec_bbox_head.0.layers.0.weight              Linear     False       65536          [256, 256]  -9.2e-05    0.0662        float32
  431  model.28.dec_bbox_head.0.layers.0.bias              Linear     False         256               [256]   -0.0256    0.0741        float32
  432model.28.dec_bbox_head.0.layers.1.weight              Linear     False       65536          [256, 256]   -0.0143    0.0882        float32
  432  model.28.dec_bbox_head.0.layers.1.bias              Linear     False         256               [256]  -0.00181    0.0696        float32
  433model.28.dec_bbox_head.0.layers.2.weight              Linear     False        1024            [4, 256]    0.0131     0.109        float32
  433  model.28.dec_bbox_head.0.layers.2.bias              Linear     False           4                 [4]  -0.00705   0.00811        float32
  434model.28.dec_bbox_head.1.layers.0.weight              Linear     False       65536          [256, 256] -0.000363    0.0681        float32
  434  model.28.dec_bbox_head.1.layers.0.bias              Linear     False         256               [256]   -0.0439    0.0637        float32
  435model.28.dec_bbox_head.1.layers.1.weight              Linear     False       65536          [256, 256]   -0.0127    0.0717        float32
  435  model.28.dec_bbox_head.1.layers.1.bias              Linear     False         256               [256]   -0.0275    0.0578        float32
  436model.28.dec_bbox_head.1.layers.2.weight              Linear     False        1024            [4, 256]    0.0142     0.136        float32
  436  model.28.dec_bbox_head.1.layers.2.bias              Linear     False           4                 [4]  -0.00429   0.00494        float32
  437model.28.dec_bbox_head.2.layers.0.weight              Linear     False       65536          [256, 256] -0.000684    0.0659        float32
  437  model.28.dec_bbox_head.2.layers.0.bias              Linear     False         256               [256]   -0.0563    0.0666        float32
  438model.28.dec_bbox_head.2.layers.1.weight              Linear     False       65536          [256, 256]   -0.0121    0.0687        float32
  438  model.28.dec_bbox_head.2.layers.1.bias              Linear     False         256               [256]   -0.0346    0.0619        float32
  439model.28.dec_bbox_head.2.layers.2.weight              Linear     False        1024            [4, 256]   0.00724    0.0876        float32
  439  model.28.dec_bbox_head.2.layers.2.bias              Linear     False           4                 [4]  -0.00147   0.00203        float32
  440model.28.dec_bbox_head.3.layers.0.weight              Linear     False       65536          [256, 256]  3.85e-05    0.0664        float32
  440  model.28.dec_bbox_head.3.layers.0.bias              Linear     False         256               [256]   -0.0546    0.0681        float32
  441model.28.dec_bbox_head.3.layers.1.weight              Linear     False       65536          [256, 256]   -0.0127    0.0592        float32
  441  model.28.dec_bbox_head.3.layers.1.bias              Linear     False         256               [256]    -0.046    0.0417        float32
  442model.28.dec_bbox_head.3.layers.2.weight              Linear     False        1024            [4, 256]   0.00755    0.0761        float32
  442  model.28.dec_bbox_head.3.layers.2.bias              Linear     False           4                 [4]  -0.00193   0.00232        float32
  443model.28.dec_bbox_head.4.layers.0.weight              Linear     False       65536          [256, 256]  0.000246    0.0663        float32
  443  model.28.dec_bbox_head.4.layers.0.bias              Linear     False         256               [256]   -0.0706    0.0753        float32
  444model.28.dec_bbox_head.4.layers.1.weight              Linear     False       65536          [256, 256]   -0.0126    0.0517        float32
  444  model.28.dec_bbox_head.4.layers.1.bias              Linear     False         256               [256]   -0.0403    0.0419        float32
  445model.28.dec_bbox_head.4.layers.2.weight              Linear     False        1024            [4, 256]   2.5e-05    0.0447        float32
  445  model.28.dec_bbox_head.4.layers.2.bias              Linear     False           4                 [4]  0.000239  0.000273        float32
  446model.28.dec_bbox_head.5.layers.0.weight              Linear     False       65536          [256, 256]  8.33e-05    0.0393        float32
  446  model.28.dec_bbox_head.5.layers.0.bias              Linear     False         256               [256]  0.000403    0.0617        float32
  447model.28.dec_bbox_head.5.layers.1.weight              Linear     False       65536          [256, 256]  -0.00979    0.0362        float32
  447  model.28.dec_bbox_head.5.layers.1.bias              Linear     False         256               [256]   -0.0184    0.0337        float32
  448model.28.dec_bbox_head.5.layers.2.weight              Linear     False        1024            [4, 256]  0.000198   0.00791        float32
  448  model.28.dec_bbox_head.5.layers.2.bias              Linear     False           4                 [4]  6.71e-07   2.1e-06        float32

rt-detr-l summary: 449 layers, 32,970,476 parameters, 0 gradients, 108.3 GFLOPs (449, 32970476, 0, 108.3437056)

Section 4: Training Configuration and Execution

Purpose: This section serves as the computational engine of the notebook. It requires transformer-specific optimizers and specialized learning rate schedules. This section defines those parameters and manages the execution of the training loop.

Key Activities:

  • Hyperparameter Optimization: Configures the AdamW optimizer, which is the industry standard for transformer models due to its superior weight decay handling. It also sets a Cosine Learning Rate scheduler and a warmup period to prevent the transformer’s attention gradients from exploding in early epochs.
  • Augmentation Strategy: Implements Mosaic and HSV augmentations to make the model good against varying backgrounds and lighting conditions.
  • Smart Resume Logic: Implements a multi-stage check for previous checkpoints. If a training session was interrupted, the script will automatically detect the last.pt file in Google Drive and resume training from the exact state (optimizer, epoch, and learning rate) where it left off.
  • Training Loop Execution: Launches the model.train() process to perform backpropagation and fine-tune the transformer’s object queries for insect detection.
import os

# A unique identifier for this specific training run, used for naming output folders.
experiment_name = 'run_v1'
# The total number of times the model will iterate over the entire training dataset.
epochs = 100
# The number of images processed in a single forward/backward pass.
batch_size = 16
# The nominal batch size used to simulate a larger batch via gradient accumulation.
nominal_batch_size = 64
# The resolution (in pixels) to which all input images will be resized before training.
img_size = 640
# The number of consecutive epochs with no improvement in validation metrics before training is stopped early.
patience = 30

# The initial step size for the optimizer.
learning_rate = 0.0003
# The optimization algorithm.
optimizer_type = 'AdamW'
# A boolean flag to enable Cosine Learning Rate decay for a smoother convergence curve.
use_cosine_lr = True
# The momentum factor for the optimizer.
optimizer_momentum = 0.9
# The number of epochs to slowly ramp up the learning rate to stabilize early training.
warmup_epochs = 5

# Probability of applying the mosaic augmentation, which combines four images into one.
mosaic_probability = 1.0
# Probability of applying mixup augmentation, blending two images together.
mixup_probability = 0.15
# Probability of applying copy-paste augmentation, pasting objects onto other images.
copy_paste_probability = 0.2
# The degree of random hue shift applied in HSV color-space augmentation.
hsv_hue_fraction = 0.015
# The degree of random saturation shift applied in HSV color-space augmentation.
hsv_saturation_fraction = 0.7
# The degree of random brightness shift applied in HSV color-space augmentation.
hsv_brightness_fraction = 0.4
# The range (in degrees) of random rotation applied to images.
rotation_degrees = 0.0
# The probability of vertically flipping an image.
flip_vertical_probability = 0.0
# The probability of horizontally flipping an image.
flip_horizontal_probability = 0.5
# The gain for applying random scaling (zoom in/out) to images.
scale_gain = 0.3

# The weight for classification loss to improve class discrimination.
cls_loss_weight = 1.0
# The number of final epochs during which mosaic augmentation is disabled for fine-tuning.
close_mosaic_epochs = 15

# A boolean flag to force the training to start from scratch, ignoring any existing checkpoints.
force_restart = False
# The default path to the 'last.pt' checkpoint file for automatic training resumption.
auto_resume_path = os.path.join(project_path, experiment_name, 'weights', 'last.pt')
# An optional path to a specific weight file to resume from, overriding the auto-resume logic if set.
specific_weight_path = ""

# Prints a formatted summary of the key training and augmentation configurations for user verification.
print("-" * 40)
print("Training Configuration")
print(f"{'Experiment Name':<25} : {experiment_name}")
print(f"{'Epochs':<25} : {epochs}")
print(f"{'Batch Size':<25} : {batch_size}")
print(f"{'Nominal Batch Size':<25} : {nominal_batch_size}")
print(f"{'Image Size':<25} : {img_size}")
print(f"{'Optimizer':<25} : {optimizer_type}")
print(f"{'Learning Rate':<25} : {learning_rate}")
print(f"{'Momentum':<25} : {optimizer_momentum}")
print(f"{'Cosine LR':<25} : {use_cosine_lr}")
print(f"{'Warmup Epochs':<25} : {warmup_epochs}")
print(f"{'Patience':<25} : {patience}")
print("-" * 40)
print("Augmentation Strategy")
print(f"{'Mosaic Probability':<25} : {mosaic_probability}")
print(f"{'Mixup Probability':<25} : {mixup_probability}")
print(f"{'Copy-Paste Prob':<25} : {copy_paste_probability}")
print(f"{'Hue Fraction':<25} : {hsv_hue_fraction}")
print(f"{'Saturation Fraction':<25} : {hsv_saturation_fraction}")
print(f"{'Brightness Fraction':<25} : {hsv_brightness_fraction}")
print(f"{'Rotation Degrees':<25} : {rotation_degrees}")
print(f"{'Vertical Flip Prob':<25} : {flip_vertical_probability}")
print(f"{'Horizontal Flip Prob':<25} : {flip_horizontal_probability}")
print(f"{'Scale Gain':<25} : {scale_gain}")
print("-" * 40)
print("Loss Configuration")
print(f"{'Classification Weight':<25} : {cls_loss_weight}")
print(f"{'Close Mosaic Epochs':<25} : {close_mosaic_epochs}")
print("-" * 40)
print(f"{'Auto Resume Path':<25} : {auto_resume_path}")
----------------------------------------
Training Configuration
Experiment Name           : run_v1
Epochs                    : 100
Batch Size                : 20
Nominal Batch Size        : 64
Image Size                : 640
Optimizer                 : AdamW
Learning Rate             : 0.0003
Momentum                  : 0.9
Cosine LR                 : True
Warmup Epochs             : 5
Patience                  : 30
----------------------------------------
Augmentation Strategy
Mosaic Probability        : 1.0
Mixup Probability         : 0.15
Copy-Paste Prob           : 0.2
Hue Fraction              : 0.015
Saturation Fraction       : 0.7
Brightness Fraction       : 0.4
Rotation Degrees          : 0.0
Vertical Flip Prob        : 0.0
Horizontal Flip Prob      : 0.5
Scale Gain                : 0.3
----------------------------------------
Loss Configuration
Classification Weight     : 1.0
Close Mosaic Epochs       : 15
----------------------------------------
Auto Resume Path          : /content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1/weights/last.pt
import time

from ultralytics import RTDETR

# Checkpoint Resumption Logic

# Initializes the default training mode to start a new session.
resume_mode = False
# Sets the default weights to the pre-trained model file.
weights_to_load = model_filename

# Checks for a specific, manually provided weight file to resume from.
if specific_weight_path and os.path.exists(specific_weight_path):
    print(f"Manual override detected.\nResuming from specific weight: {specific_weight_path}")
    resume_mode = True
    weights_to_load = specific_weight_path
# If no manual override is given, checks for the default auto-resume checkpoint file.
elif os.path.exists(auto_resume_path):
    # Handles the case where a checkpoint exists but a fresh start is explicitly required.
    if force_restart:
        print(f"Previous run found at {auto_resume_path}, but 'force_restart' is True.")
        print("Starting new training...")
        resume_mode = False
    # If a checkpoint exists and a fresh start is not forced, sets up auto-resumption.
    else:
        print(f"Previous run detected.\nAuto-resuming from: {auto_resume_path}")
        resume_mode = True
        weights_to_load = auto_resume_path
# If no checkpoints are found, configures the script to start a new training session.
else:
    print("No previous run found. Starting new training...")


# Training Execution
print("\nInitializing training loop...")
# Records the timestamp at the beginning of the training process to measure total duration.
start_time = time.time()

# Loads the model architecture and weights based on the resumption logic determined above.
model = RTDETR(weights_to_load)
# Registers the custom callback function for epoch-end weight archiving.
model.add_callback("on_train_epoch_end", on_train_epoch_end)

# Executes the appropriate training command based on whether a session is being resumed.
if resume_mode:
    # When resuming, the `resume` argument is used, which automatically loads the model's state, optimizer state, and last epoch from the checkpoint.
    results = model.train(
        resume=True,
        project=project_path,
        name=experiment_name
    )
else:
    # For a new run, all hyperparameters and augmentation settings are passed explicitly.
    results = model.train(
        # Core Parameters
        data=yaml_path,
        epochs=epochs,
        imgsz=img_size,
        batch=batch_size,
        nbs=nominal_batch_size,
        device=0,
        patience=patience,
        # Project and Checkpointing
        deterministic=False,
        save=True,
        save_period=1,
        project=project_path,
        name=experiment_name,
        exist_ok=True,
        plots=True,
        # Optimizer Settings
        lr0=learning_rate,
        optimizer=optimizer_type,
        momentum=optimizer_momentum,
        cos_lr=use_cosine_lr,
        warmup_epochs=warmup_epochs,
        # Augmentation Parameters
        mosaic=mosaic_probability,
        mixup=mixup_probability,
        copy_paste=copy_paste_probability,
        hsv_h=hsv_hue_fraction,
        hsv_s=hsv_saturation_fraction,
        hsv_v=hsv_brightness_fraction,
        degrees=rotation_degrees,
        flipud=flip_vertical_probability,
        fliplr=flip_horizontal_probability,
        scale=scale_gain,
        # Loss and Fine-tuning Parameters
        cls=cls_loss_weight,
        close_mosaic=close_mosaic_epochs,
        # Dataloader Settings
        workers=4
    )

# Records the timestamp at the end of the training process.
end_time = time.time()

# Calculates the total training duration and formats it into hours, minutes, and seconds.
duration_seconds = end_time - start_time
hours = int(duration_seconds // 3600)
minutes = int((duration_seconds % 3600) // 60)
seconds = int(duration_seconds % 60)

# Prints a final summary of the completed training session and its duration.
print("\n" + "-"*30)
print("Training complete.")
print(f"Total Time: {hours}h {minutes}m {seconds}s")
print("-"*30)
No previous run found. Starting new training...

Initializing training loop...
Ultralytics 8.3.251 🚀 Python-3.12.12 torch-2.9.0+cu126 CUDA:0 (NVIDIA L4, 22693MiB)
engine/trainer: agnostic_nms=False, amp=True, augment=False, auto_augment=randaugment, batch=20, bgr=0.0, box=7.5, cache=False, cfg=None, classes=None, close_mosaic=15, cls=1.0, compile=False, conf=None, copy_paste=0.2, copy_paste_mode=flip, cos_lr=True, cutmix=0.0, data=/content/data.yaml, degrees=0.0, deterministic=False, device=0, dfl=1.5, dnn=False, dropout=0.0, dynamic=False, embed=None, epochs=100, erasing=0.4, exist_ok=True, fliplr=0.5, flipud=0.0, format=torchscript, fraction=1.0, freeze=None, half=False, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, imgsz=640, int8=False, iou=0.7, keras=False, kobj=1.0, line_width=None, lr0=0.0003, lrf=0.01, mask_ratio=4, max_det=300, mixup=0.15, mode=train, model=rtdetr-l.pt, momentum=0.9, mosaic=1.0, multi_scale=False, name=run_v1, nbs=64, nms=False, opset=None, optimize=False, optimizer=AdamW, overlap_mask=True, patience=30, perspective=0.0, plots=True, pose=12.0, pretrained=True, profile=False, project=/content/drive/MyDrive/Mortiscope Models/RT-DETR, rect=False, resume=False, retina_masks=False, save=True, save_conf=False, save_crop=False, save_dir=/content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1, save_frames=False, save_json=False, save_period=1, save_txt=False, scale=0.3, seed=0, shear=0.0, show=False, show_boxes=True, show_conf=True, show_labels=True, simplify=True, single_cls=False, source=None, split=val, stream_buffer=False, task=detect, time=None, tracker=botsort.yaml, translate=0.1, val=True, verbose=True, vid_stride=1, visualize=False, warmup_bias_lr=0.1, warmup_epochs=5, warmup_momentum=0.8, weight_decay=0.0005, workers=4, workspace=None
Downloading https://ultralytics.com/assets/Arial.ttf to '/root/.config/Ultralytics/Arial.ttf': 100% ━━━━━━━━━━━━ 755.1KB 126.7MB/s 0.0s

Overriding model.yaml nc=80 with nc=5
WARNING ⚠️ no model scale passed. Assuming scale='l'.

                   from  n    params  module                                       arguments                     
  0                  -1  1     25248  ultralytics.nn.modules.block.HGStem          [3, 32, 48]                   
  1                  -1  6    155072  ultralytics.nn.modules.block.HGBlock         [48, 48, 128, 3, 6]           
  2                  -1  1      1408  ultralytics.nn.modules.conv.DWConv           [128, 128, 3, 2, 1, False]    
  3                  -1  6    839296  ultralytics.nn.modules.block.HGBlock         [128, 96, 512, 3, 6]          
  4                  -1  1      5632  ultralytics.nn.modules.conv.DWConv           [512, 512, 3, 2, 1, False]    
  5                  -1  6   1695360  ultralytics.nn.modules.block.HGBlock         [512, 192, 1024, 5, 6, True, False]
  6                  -1  6   2055808  ultralytics.nn.modules.block.HGBlock         [1024, 192, 1024, 5, 6, True, True]
  7                  -1  6   2055808  ultralytics.nn.modules.block.HGBlock         [1024, 192, 1024, 5, 6, True, True]
  8                  -1  1     11264  ultralytics.nn.modules.conv.DWConv           [1024, 1024, 3, 2, 1, False]  
  9                  -1  6   6708480  ultralytics.nn.modules.block.HGBlock         [1024, 384, 2048, 5, 6, True, False]
 10                  -1  1    524800  ultralytics.nn.modules.conv.Conv             [2048, 256, 1, 1, None, 1, 1, False]
 11                  -1  1    789760  ultralytics.nn.modules.transformer.AIFI      [256, 1024, 8]                
 12                  -1  1     66048  ultralytics.nn.modules.conv.Conv             [256, 256, 1, 1]              
 13                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']          
 14                   7  1    262656  ultralytics.nn.modules.conv.Conv             [1024, 256, 1, 1, None, 1, 1, False]
 15            [-2, -1]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 16                  -1  3   2232320  ultralytics.nn.modules.block.RepC3           [512, 256, 3]                 
 17                  -1  1     66048  ultralytics.nn.modules.conv.Conv             [256, 256, 1, 1]              
 18                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']          
 19                   3  1    131584  ultralytics.nn.modules.conv.Conv             [512, 256, 1, 1, None, 1, 1, False]
 20            [-2, -1]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 21                  -1  3   2232320  ultralytics.nn.modules.block.RepC3           [512, 256, 3]                 
 22                  -1  1    590336  ultralytics.nn.modules.conv.Conv             [256, 256, 3, 2]              
 23            [-1, 17]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 24                  -1  3   2232320  ultralytics.nn.modules.block.RepC3           [512, 256, 3]                 
 25                  -1  1    590336  ultralytics.nn.modules.conv.Conv             [256, 256, 3, 2]              
 26            [-1, 12]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 27                  -1  3   2232320  ultralytics.nn.modules.block.RepC3           [512, 256, 3]                 
 28        [21, 24, 27]  1   7312127  ultralytics.nn.modules.head.RTDETRDecoder    [5, [256, 256, 256]]          

rt-detr-l summary: 465 layers, 32,816,351 parameters, 32,816,351 gradients, 108.0 GFLOPs
Transferred 926/941 items from pretrained weights

AMP: running Automatic Mixed Precision (AMP) checks...
Downloading https://github.com/ultralytics/assets/releases/download/v8.3.0/yolo11n.pt to 'yolo11n.pt': 100% ━━━━━━━━━━━━ 5.4MB 128.9MB/s 0.0s
AMP: checks passed ✅
train: Fast image access ✅ (ping: 0.0±0.0 ms, read: 1426.5±576.1 MB/s, size: 54.4 KB)
train: Scanning /content/Complementary-2/train/labels... 14114 images, 348 backgrounds, 0 corrupt: 100% ━━━━━━━━━━━━ 14114/14114 1.5Kit/s 9.1s
train: New cache created: /content/Complementary-2/train/labels.cache
albumentations: Blur(p=0.01, blur_limit=(3, 7)), MedianBlur(p=0.01, blur_limit=(3, 7)), ToGray(p=0.01, method='weighted_average', num_output_channels=3), CLAHE(p=0.01, clip_limit=(1.0, 4.0), tile_grid_size=(8, 8))
val: Fast image access ✅ (ping: 0.0±0.0 ms, read: 541.2±106.1 MB/s, size: 51.0 KB)
val: Scanning /content/Complementary-2/valid/labels... 888 images, 28 backgrounds, 0 corrupt: 100% ━━━━━━━━━━━━ 888/888 1.2Kit/s 0.7s
val: New cache created: /content/Complementary-2/valid/labels.cache
Plotting labels to /content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1/labels.jpg... 
optimizer: AdamW(lr=0.0003, momentum=0.9) with parameter groups 143 weight(decay=0.0), 206 weight(decay=0.00046875), 226 bias(decay=0.0)

Image sizes 640 train, 640 val
Using 4 dataloader workers
Logging results to /content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1
Starting training for 100 epochs...

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
      1/100      19.8G      1.034      1.135     0.3328         33        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 10:02
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 1.7it/s 13.3s
                   all        888       2844      0.245     0.0997     0.0489     0.0184

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
      2/100      21.5G     0.7773     0.9865     0.1983         71        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:35
   History Saved: 002_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 9.9s
                   all        888       2844      0.588      0.315      0.316      0.128

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
      3/100        21G     0.7592     0.7223     0.2012         45        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:35
   History Saved: 003_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.679      0.506       0.54      0.328

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
      4/100      20.6G     0.6803     0.7182     0.1818         39        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:30
   History Saved: 004_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844       0.75       0.53      0.547      0.371

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
      5/100      15.6G     0.6286     0.6995     0.1672         58        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 005_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.803      0.494      0.527       0.37

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
      6/100        21G     0.6072      0.691     0.1657         34        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 006_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.754      0.561      0.593      0.407

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
      7/100      18.7G     0.5914      0.664     0.1566         86        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 007_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844       0.77      0.544        0.6      0.416

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
      8/100      18.7G     0.5732     0.6433     0.1495         33        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 008_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 9.9s
                   all        888       2844      0.692      0.606      0.617      0.425

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
      9/100        20G     0.5645     0.6385     0.1459         62        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 009_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 9.9s
                   all        888       2844      0.733      0.586      0.614      0.423

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     10/100      21.6G      0.564      0.629     0.1473         40        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 010_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.695      0.574      0.592      0.413

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     11/100      20.5G     0.5544     0.6236     0.1434         56        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 011_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 9.9s
                   all        888       2844      0.786      0.535      0.599      0.415

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     12/100        21G      0.543     0.6203     0.1396         71        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 012_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.703      0.579      0.607      0.424

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     13/100      20.9G     0.5387     0.6063     0.1386         38        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 013_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.718      0.615      0.629      0.438

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     14/100      18.5G     0.5287     0.5928     0.1367         53        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 014_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.729      0.608       0.61       0.43

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     15/100      19.7G     0.5264     0.5837     0.1348         32        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 015_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.733      0.601      0.627       0.44

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     16/100      20.6G      0.523      0.573     0.1313         85        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 016_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 9.9s
                   all        888       2844      0.732      0.611       0.65      0.452

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     17/100      21.4G      0.519     0.5632     0.1294         53        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 017_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.734      0.602      0.635      0.444

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     18/100        20G     0.5138     0.5638     0.1282         67        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 018_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 9.9s
                   all        888       2844      0.731      0.598      0.624      0.433

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     19/100      17.1G     0.5031      0.552     0.1275         26        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 019_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.1s
                   all        888       2844       0.73      0.599      0.631      0.445

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     20/100      20.4G     0.4985     0.5517     0.1233         43        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 020_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.734      0.593      0.622      0.436

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     21/100      18.1G     0.5031     0.5414     0.1255         78        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 021_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.738      0.618      0.633      0.445

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     22/100      18.3G     0.4959     0.5365     0.1236         53        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 022_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 9.9s
                   all        888       2844      0.711      0.605      0.631      0.445

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     23/100      20.8G     0.4945      0.534     0.1233         47        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 023_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.721      0.626      0.648      0.458

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     24/100      20.4G     0.4849     0.5245     0.1212         39        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 024_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.735      0.641      0.657      0.459

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     25/100      18.3G     0.4825     0.5209      0.121         64        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 025_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844       0.75      0.629      0.649      0.454

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     26/100      21.3G     0.4845     0.5155      0.119         66        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 026_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.723      0.623      0.637       0.45

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     27/100      21.8G     0.4803     0.5095     0.1186         23        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 027_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.729      0.631       0.65      0.457

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     28/100      20.7G     0.4762     0.5081     0.1162         67        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 028_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.724      0.591      0.628      0.445

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     29/100      17.2G     0.4693     0.5058      0.115         66        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 029_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.715      0.602       0.62      0.442

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     30/100      21.6G     0.4718     0.4985     0.1158         35        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 030_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.719      0.586      0.621      0.442

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     31/100      21.4G     0.4661     0.4982     0.1158         98        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 031_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.733      0.622       0.64      0.454

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     32/100      20.6G     0.4665     0.4924     0.1124         28        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:27
   History Saved: 032_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.723      0.616      0.634       0.45

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     33/100      20.2G     0.4651     0.4882     0.1114         24        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 033_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.714      0.626      0.638      0.451

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     34/100      21.4G     0.4551     0.4875     0.1135         53        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 034_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.718      0.625      0.642      0.451

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     35/100      20.2G     0.4553     0.4858     0.1113         40        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 035_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.738      0.618      0.641      0.451

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     36/100      20.5G       0.45     0.4805     0.1083         37        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 036_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.727      0.639      0.648      0.457

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     37/100      17.3G     0.4537     0.4784       0.11         43        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 037_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.732      0.608      0.633      0.447

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     38/100      20.1G     0.4439     0.4732     0.1082         25        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 038_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.745      0.627      0.646      0.455

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     39/100      20.9G     0.4365     0.4675     0.1068        164        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 039_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.731      0.629      0.641      0.456

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     40/100      19.3G     0.4364     0.4603     0.1071         35        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 040_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.739      0.628      0.644      0.458

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     41/100      19.7G     0.4381     0.4627     0.1077         61        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:30
   History Saved: 041_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.745      0.622      0.641      0.456

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     42/100      15.8G     0.4351     0.4611     0.1053         15        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:27
   History Saved: 042_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.751       0.62      0.645      0.457

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     43/100      18.5G     0.4361     0.4596     0.1054         73        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 043_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.763      0.616      0.645      0.456

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     44/100        21G     0.4272     0.4503      0.104         42        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 044_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.742      0.621      0.641      0.453

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     45/100      18.3G     0.4255     0.4513     0.1035         71        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 045_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.741       0.63      0.642      0.456

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     46/100        19G     0.4256     0.4495     0.1024         65        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 046_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.747      0.635      0.646      0.458

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     47/100      21.5G     0.4277     0.4503     0.1024         31        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 047_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844       0.74      0.636      0.643      0.456

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     48/100      19.2G     0.4232     0.4487    0.09971         97        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 048_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.747       0.63      0.643      0.455

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     49/100      21.3G     0.4185      0.445     0.1003         51        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 049_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.731      0.632       0.64      0.453

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     50/100      20.1G     0.4128     0.4404    0.09907         32        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:29
   History Saved: 050_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.738      0.629      0.641      0.455

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     51/100      18.4G     0.4124     0.4431    0.09765         53        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:30
   History Saved: 051_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.752      0.622      0.641      0.455

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     52/100        20G     0.4082     0.4389    0.09775         71        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 052_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.759       0.62      0.641      0.454

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     53/100        16G      0.408     0.4352    0.09687         42        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 053_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844       0.75      0.622      0.641      0.454

      Epoch    GPU_mem  giou_loss   cls_loss    l1_loss  Instances       Size
     54/100      21.7G     0.4063     0.4352    0.09572         30        640: 100% ━━━━━━━━━━━━ 706/706 1.2it/s 9:28
   History Saved: 054_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.3it/s 10.0s
                   all        888       2844      0.759      0.618       0.64      0.454

EarlyStopping: Training stopped early as no improvement observed in last 30 epochs. Best results observed at epoch 24, best model saved as best.pt.

To update EarlyStopping(patience=30) pass a new patience value, i.e. `patience=300` or use `patience=0` to disable EarlyStopping.

54 epochs completed in 8.754 hours.
Optimizer stripped from /content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1/weights/last.pt, 66.2MB
Optimizer stripped from /content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1/weights/best.pt, 66.2MB

Validating /content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1/weights/best.pt...
Ultralytics 8.3.251 🚀 Python-3.12.12 torch-2.9.0+cu126 CUDA:0 (NVIDIA L4, 22693MiB)
rt-detr-l summary: 310 layers, 31,994,015 parameters, 0 gradients, 103.5 GFLOPs

                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 23/23 2.2it/s 10.4s
                   all        888       2844      0.735      0.642      0.657      0.459
                 adult        176        593      0.819      0.525      0.595      0.328
              instar_1        222       1164      0.697      0.435      0.515      0.276
              instar_2        168        521      0.414      0.572      0.488      0.314
              instar_3        240        334      0.758      0.746      0.754      0.606
                  pupa        205        232      0.988      0.931      0.931      0.772

Speed: 0.3ms preprocess, 8.6ms inference, 0.0ms loss, 0.4ms postprocess per image
Results saved to /content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1

------------------------------
Training complete.
Total Time: 8h 45m 54s
------------------------------

Section 5: Visual Performance Evaluation

Purpose: This section provides a quantitative and visual audit of the model’s learning trajectory. Because RT-DETR is a transformer-based model, it uses specific loss functions—like GIoU (Generalized IoU) and L1 Loss that are different from YOLO. These visualizations are for verifying that the transformer’s object queries are successfully converging on the biological targets.

Key Activities:

  • Adaptive Log Analysis (plot_training_results): Parses the results.csv file and generates a grid of performance plots. The script is designed to be architecture-aware, automatically detecting and plotting RT-DETR-specific metrics alongside standard precision and recall curves.
  • Biological Classification Audit: Runs a validation pass using the best-saved weights to generate a confusion matrix. This heatmap is normalized to represent Recall (Sensitivity) to see exactly which life stages are most frequently confused by the model due to their morphological similarities.
import os

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

# Constructs the full path to the results log file.
results_csv_path = os.path.join(project_path, experiment_name, 'results.csv')

# The number of data points to average over when applying a rolling mean.
smoothing_window_size = 5

# Sets a professional and consistent visual theme for all generated plots.
sns.set_theme(style="whitegrid", context="notebook", font_scale=1.1)
# Increases the resolution of the output figures for better clarity.
plt.rcParams['figure.dpi'] = 120

# Defines a perceptually uniform colormap to derive a consistent color palette.
color_map = plt.get_cmap('plasma')

# Assigns specific colors from the colormap to different metrics for consistency.
color_train = color_map(0.0)       # Color for training metrics.
color_val   = color_map(0.6)       # Color for validation metrics.
color_prec  = color_map(0.25)      # Color for the precision curve.
color_rec   = color_map(0.75)      # Color for the recall curve.
color_map_metric = color_map(0.5)  # Color for the mAP metric.
color_lr    = color_map(0.05)      # Color for the learning rate schedule.


def plot_training_results(csv_file_path):
    """
    Reads a results.csv file and generates a grid of training performance plots.
    Automatically adapts to RT-DETR column naming conventions.

    Args:
        csv_file_path (str): The full path to the results.csv file.
    """
    # Verifies the existence of the results file before attempting to read it.
    if not os.path.exists(csv_file_path):
        print(f"Error: Could not find results at {csv_file_path}")
        return

    # Reads the CSV data into a pandas DataFrame.
    df = pd.read_csv(csv_file_path)
    # Cleans up column names by removing leading/trailing whitespace.
    df.columns = df.columns.str.strip()

    # Debug print to help identify which model architecture logs are present.
    print(f"Detected Log Columns: {df.columns.tolist()}")

    # Defines potential loss column pairs to support RT-DETR architectures.
    possible_losses = [
        # Standard RT-DETR Losses
        ('train/box_loss', 'val/box_loss', 'Box Loss'),
        ('train/cls_loss', 'val/cls_loss', 'Classification Loss'),
        ('train/dfl_loss', 'val/dfl_loss', 'DFL Loss'),
        # RT-DETR Specific Losses (GIoU and L1)
        ('train/giou_loss', 'val/giou_loss', 'GIoU Loss'),
        ('train/l1_loss', 'val/l1_loss', 'L1 Box Loss')
    ]

    # Filters the list to keep only the loss metrics that actually exist in this specific log file.
    active_losses = []
    for t_col, v_col, title in possible_losses:
        # Check for the training column presence to decide if the metric exists.
        if t_col in df.columns:
            active_losses.append((t_col, v_col, title))

    # Calculates the grid dimensions dynamically based on how many losses were found.
    num_loss_plots = len(active_losses)
    total_plots = num_loss_plots + 3

    # Configures the subplot grid layout (fixed 3 columns).
    cols = 3
    rows = (total_plots // cols) + (1 if total_plots % cols > 0 else 0)

    # Creates the figure and array of axes.
    figure, axis_array = plt.subplots(rows, cols, figsize=(18, 5 * rows))
    axes_flat = axis_array.flatten()

    # Iterates through the detected losses to generate the first row(s) of plots.
    for i, (train_col, val_col, title) in enumerate(active_losses):
        axis = axes_flat[i]

        # Plots the raw, noisy data with low opacity to serve as a background reference.
        sns.lineplot(data=df, x=df.index, y=train_col, ax=axis, color=color_train, alpha=0.15)
        # Checks if the validation column exists before plotting.
        if val_col in df.columns:
            sns.lineplot(data=df, x=df.index, y=val_col, ax=axis, color=color_val, alpha=0.15)

        # Overlays the smoothed data using a rolling mean.
        sns.lineplot(x=df.index, y=df[train_col].rolling(smoothing_window_size, min_periods=1).mean(),
                     ax=axis, color=color_train, linewidth=2.5, label='Train')

        if val_col in df.columns:
            sns.lineplot(x=df.index, y=df[val_col].rolling(smoothing_window_size, min_periods=1).mean(),
                         ax=axis, color=color_val, linewidth=2.5, label='Validation')

        # Configures the title, labels, and legend for each loss plot.
        axis.set_title(title, color='#333333')
        axis.set_xlabel('Epochs')
        axis.set_ylabel('Loss Value')
        axis.legend()

    # Tracks the current subplot index to place the remaining metrics.
    metric_idx = num_loss_plots

    # Generates the Precision and Recall Plot if the metrics exist.
    if metric_idx < len(axes_flat):
        axis = axes_flat[metric_idx]
        if 'metrics/precision(B)' in df.columns:
            sns.lineplot(x=df.index, y=df['metrics/precision(B)'].rolling(smoothing_window_size, min_periods=1).mean(),
                         ax=axis, color=color_prec, label='Precision')
            sns.lineplot(x=df.index, y=df['metrics/recall(B)'].rolling(smoothing_window_size, min_periods=1).mean(),
                         ax=axis, color=color_rec, label='Recall')
            axis.set_title('Precision & Recall')
            axis.set_ylim(0, 1)
        metric_idx += 1

    # Generates the Mean Average Precision (mAP) Plot.
    if metric_idx < len(axes_flat):
        axis = axes_flat[metric_idx]
        if 'metrics/mAP50(B)' in df.columns:
            sns.lineplot(x=df.index, y=df['metrics/mAP50(B)'].rolling(smoothing_window_size, min_periods=1).mean(),
                         ax=axis, color=color_map_metric, linewidth=2.5, label='mAP @ 0.50')
            # Fills the area under the mAP curve to visually emphasize the performance metric.
            axis.fill_between(df.index, df['metrics/mAP50(B)'].rolling(smoothing_window_size, min_periods=1).mean(),
                              color=color_map_metric, alpha=0.1)
            axis.set_title('Mean Average Precision (IoU=0.50)')
            axis.set_ylim(0, 1)
        metric_idx += 1

    # Generates the Learning Rate Schedule Plot.
    if metric_idx < len(axes_flat):
        axis = axes_flat[metric_idx]
        # Dynamically finds the LR column name (usually 'lr/pg0' but can vary).
        lr_col = 'lr/pg0' if 'lr/pg0' in df.columns else None
        if not lr_col and any('lr' in col for col in df.columns):
             lr_col = df.columns[df.columns.str.contains('lr')][0]

        if lr_col:
            sns.lineplot(x=df.index, y=df[lr_col], ax=axis, color=color_lr, linestyle='--')
            axis.set_title('Learning Rate Schedule')
            axis.set_ylabel('Learning Rate')
        metric_idx += 1

    # Hides any remaining empty subplots to keep the layout clean.
    for i in range(metric_idx, len(axes_flat)):
        axes_flat[i].axis('off')

    # Adjusts the spacing between subplots to prevent labels from overlapping.
    plt.tight_layout(pad=3.0)
    # Renders and displays the final, complete figure.
    plt.show()

# Executes the plotting function with the path to the results file.
plot_training_results(results_csv_path)

import numpy as np

# Defines the path to the best-performing model weights saved during training.
best_weight_path = os.path.join(project_path, experiment_name, 'weights', 'best.pt')
# Defines the output path for the final, publication-quality confusion matrix image.
output_image_path = os.path.join(project_path, experiment_name, 'confusion_matrix.png')

# Ensures that the script only runs if the best weight file exists.
if os.path.exists(best_weight_path):
    print(f"Loading weights from: {best_weight_path}")
    # Instantiates a new RT-DETR model object using the best saved weights.
    validation_model = RTDETR(best_weight_path)

    # Runs a validation pass on the model.
    validation_metrics = validation_model.val(
        data=yaml_path,
        split='val',
        plots=True,
        device=0,
        batch=48,
        conf=0.001
    )

    # Extracts the raw confusion matrix (a NumPy array) from the results.
    raw_matrix = validation_metrics.confusion_matrix.matrix
    num_classes = 5
    # Slices the matrix to ensure it only contains the defined classes.
    matrix_data = raw_matrix[:num_classes, :num_classes]

    # Retrieves the class names from the validation results and formats them.
    raw_names = list(validation_metrics.names.values())[:num_classes]
    class_names = [name.replace('_', ' ').title() for name in raw_names]

    # Normalizes the confusion matrix by rows to calculate recall scores.
    row_sums = matrix_data.sum(axis=1, keepdims=True)
    # Replaces zero sums with a small number to avoid division-by-zero errors for classes that may not have appeared in the validation set.
    row_sums[row_sums == 0] = 1e-9
    matrix_normalized = matrix_data / row_sums

    # Initializes a high-resolution figure for the plot.
    plt.figure(figsize=(16, 12), dpi=300)
    sns.set_theme(style="white", font_scale=1.1)

    # Creates the heatmap using Seaborn, configuring annotations, colormap, and labels.
    axis = sns.heatmap(
        matrix_normalized,
        annot=True,                 # Displays the numerical value in each cell.
        annot_kws={"size": 14},     # Sets the font size for annotations.
        fmt='.2f',                  # Formats annotations to two decimal places.
        cmap='Blues',               # Sets the color scheme.
        xticklabels=class_names,
        yticklabels=class_names,
        vmin=0.0, vmax=1.0,         # Fixes the color bar range from 0 to 1.
        square=True,                # Enforces square cells for better proportionality.
        linewidths=2.5,
        linecolor='white',
        cbar_kws={
            'shrink': 0.6,          # Adjusts the size of the color bar.
            'pad': 0.04
        }
    )

    # Configures the color bar label to clarify that the values represent recall.
    cbar = axis.collections[0].colorbar
    cbar.set_label('Recall (Sensitivity)', labelpad=30, fontsize=14)

    # Sets the main title and axis labels with appropriate padding.
    plt.title('Confusion Matrix', fontsize=20, pad=30)
    plt.xlabel('Predicted Class', fontsize=16, labelpad=25)
    plt.ylabel('Actual Class', fontsize=16, labelpad=25)

    # Adjusts tick label appearance for clarity.
    plt.xticks(rotation=0, fontsize=13)
    plt.yticks(rotation=0, fontsize=13)

    # Adjusts subplot parameters to give a tight layout.
    plt.tight_layout(pad=5.0)

    # Saves the final figure to the specified output path.
    plt.savefig(output_image_path, dpi=300, bbox_inches='tight')
    print(f"Confusion Matrix saved to: {output_image_path}")

    # Displays the plot in the notebook output.
    plt.show()

else:
    # Prints an error message if the required 'best.pt' file is not found.
    print(f"Error: Best weights not found at {best_weight_path}")
Loading weights from: /content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1/weights/best.pt

Ultralytics 8.3.251 🚀 Python-3.12.12 torch-2.9.0+cu126 CUDA:0 (NVIDIA L4, 22693MiB)
rt-detr-l summary: 310 layers, 31,994,015 parameters, 0 gradients, 103.5 GFLOPs
val: Fast image access ✅ (ping: 0.0±0.0 ms, read: 1881.6±389.0 MB/s, size: 59.0 KB)
val: Scanning /content/Complementary-2/valid/labels.cache... 888 images, 28 backgrounds, 0 corrupt: 100% ━━━━━━━━━━━━ 888/888 1.5Mit/s 0.0s

                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 19/19 1.0it/s 18.7s
                   all        888       2844      0.733      0.643      0.658      0.459
                 adult        176        593      0.819      0.527      0.599      0.327
              instar_1        222       1164      0.691      0.438      0.515      0.276
              instar_2        168        521      0.411      0.572      0.488      0.314
              instar_3        240        334      0.754      0.746      0.755      0.606
                  pupa        205        232      0.989      0.931      0.931      0.772

Speed: 1.1ms preprocess, 16.4ms inference, 0.0ms loss, 0.4ms postprocess per image
Results saved to /content/runs/detect/val
Confusion Matrix saved to: /content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1/confusion_matrix.png

Section 6: Benchmarking and SAHI Integration

Purpose: This section evaluates the model’s operational efficiency and prepares it for the specific challenges of forensic field photography. While RT-DETR is architecturally fast, forensic imagery often contains thousands of tiny larvae that are difficult to detect at standard resolutions.

Key Activities:

  • Inference Speed Benchmarking: Executes a validation pass specifically to extract timing data for pre-processing, model inference, and post-processing. This allows the calculation of the real-world Frames Per Second (FPS), ensuring the model meets the requirements for field-deployable hardware.
  • SAHI (Sliced Aided Hyper Inference) Wrapper: Initializes the SAHI library with the trained RT-DETR weights. Standard model resizing can cause small instar larvae to disappear. SAHI solves this by slicing images into overlapping windows, allowing the transformer to see microscopic biological features without losing spatial detail.
# Defines the path to the best-performing model weights from the training run.
best_weight_path = os.path.join(project_path, experiment_name, 'weights', 'best.pt')

# Ensures the script proceeds only if the required model weight file is found.
if os.path.exists(best_weight_path):
    print(f"Loading best model for benchmarking: {best_weight_path}")
    # Loads the best model weights into a RT-DETR object for evaluation.
    benchmark_model = RTDETR(best_weight_path)

    # Runs a validation pass on the specified dataset split.
    metrics = benchmark_model.val(data=yaml_path, split='val', plots=False, device=0)

    # Extracts the speed dictionary, which contains timing information for different stages of the inference pipeline.
    speed_metrics = metrics.speed

    # Displays a formatted summary of the average inference speed metrics.
    print("\n" + "-"*45)
    print("Inference Speed Benchmark (Average per Image)")
    print("-"*45)
    print(f"{'Pre-process':<25} : {speed_metrics['preprocess']:.2f} ms")
    print(f"{'Inference (Model)':<25} : {speed_metrics['inference']:.2f} ms")
    print(f"{'Post-process (NMS)':<25} : {speed_metrics['postprocess']:.2f} ms")
    print("-" * 45)

    # Calculates the total latency by summing the timings of all pipeline stages.
    total_latency = sum(speed_metrics.values())
    print(f"{'Total Latency':<25} : {total_latency:.2f} ms")

    # Estimates the throughput in Frames Per Second (FPS) based on the total latency.
    fps = 1000 / total_latency
    print(f"{'Estimated FPS':<25} : {fps:.2f} fps")
    print("-"*45)

else:
    # Prints an error message if the model weights file could not be located.
    print("Error: Best weights file not found.")
    print("Please make sure that the training completed successfully.")
Loading best model for benchmarking: /content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1/weights/best.pt

Ultralytics 8.3.251 🚀 Python-3.12.12 torch-2.9.0+cu126 CUDA:0 (NVIDIA L4, 22693MiB)
rt-detr-l summary: 310 layers, 31,994,015 parameters, 0 gradients, 103.5 GFLOPs
val: Fast image access ✅ (ping: 0.0±0.0 ms, read: 1477.3±784.3 MB/s, size: 63.7 KB)
val: Scanning /content/Complementary-2/valid/labels.cache... 888 images, 28 backgrounds, 0 corrupt: 100% ━━━━━━━━━━━━ 888/888 1.8Mit/s 0.0s

                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 56/56 3.4it/s 16.3s
                   all        888       2844      0.733      0.643      0.658      0.459
                 adult        176        593      0.819      0.527      0.599      0.327
              instar_1        222       1164       0.69      0.439      0.515      0.276
              instar_2        168        521      0.411      0.572      0.488      0.314
              instar_3        240        334      0.754      0.746      0.755      0.606
                  pupa        205        232      0.988      0.931      0.931      0.772

Speed: 0.6ms preprocess, 15.7ms inference, 0.0ms loss, 0.4ms postprocess per image

---------------------------------------------
Inference Speed Benchmark (Average per Image)
---------------------------------------------
Pre-process               : 0.61 ms
Inference (Model)         : 15.72 ms
Post-process (NMS)        : 0.37 ms
---------------------------------------------
Total Latency             : 16.70 ms
Estimated FPS             : 59.88 fps
---------------------------------------------
from sahi import AutoDetectionModel
from sahi.predict import get_sliced_prediction

# Defines the path to the best-performing model weights from the training run, which will be loaded into the SAHI wrapper.
best_weight_path = os.path.join(project_path, experiment_name, 'weights', 'best.pt')

# The height of each individual slice in pixels.
slice_height = 640
# The width of each individual slice in pixels.
slice_width = 640
# The percentage of overlap between adjacent slices vertically.
overlap_height_ratio = 0.2
# The percentage of overlap between adjacent slices horizontally.
overlap_width_ratio = 0.2

print(f"Initializing SAHI wrapper for: {best_weight_path}")

# Verifies that the model weight file exists before attempting to load it.
if os.path.exists(best_weight_path):
    # Initializes the SAHI AutoDetectionModel.
    detection_model = AutoDetectionModel.from_pretrained(
        model_type='yolov11',         # Specifies the model architecture.
        model_path=best_weight_path,  # Provides the path to the custom-trained weights.
        confidence_threshold=0.25,    # Sets the minimum confidence for a detection to be considered valid.
        device="cuda:0"               # Assigns the model to a specific GPU device for inference.
    )

    # Prints a confirmation message summarizing the SAHI configuration.
    print("\n" + "-"*45)
    print("SAHI Model Ready")
    print("-" * 45)
    print(f"{'Slice Dimensions':<20} : {slice_height}x{slice_width}")
    print(f"{'Overlap Ratio':<20} : {overlap_height_ratio * 100}%")
    print(f"{'Confidence Thresh':<20} : 0.25")
    print("-" * 45)

else:
    # Handles the case where the required weight file is not found.
    print(f"Error: Weights not found at {best_weight_path}")
    print("Cannot initialize SAHI.")
Initializing SAHI wrapper for: /content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1/weights/best.pt

---------------------------------------------
SAHI Model Ready
---------------------------------------------
Slice Dimensions     : 640x640
Overlap Ratio        : 20.0%
Confidence Thresh    : 0.25
---------------------------------------------

Section 7: Deployment Export and Interactive Demonstration

Purpose: This section orchestrates a scene-adaptive workflow that automatically adjusts the model’s behavior based on the composition of the photograph.

Key Activities:

  • User-Driven Input: Integrates an interactive file uploader for testing on new imagery.
  • Heuristic Scene Detection: Before a full analysis, the model performs a survey of the image to determine if it is a Macro (close-up) or Field (wide-angle) shot. This ensures the model uses the most efficient strategy for the specific image type.
  • Adaptive Inference Branching:
    • Macro Mode: Uses native transformer inference to prioritize speed and structural consistency when life stages are large and clearly visible.
    • Field Mode: Triggers the SAHI slicing engine to ensure that even the small instar larvae are detected.
  • Lifecycle Reporting: Generates a dual-panel visual report. One panel contains the annotated image, while the other features a summary legend showing the total count and average confidence score for each of the detected life stages of Chrysomya megacephala.
# Defines the source path for the best PyTorch model weights and the desired target path and filename for the exported ONNX model.
source_weights = os.path.join(project_path, experiment_name, 'weights', 'best.pt')
target_filename = "rtdetr_mortiscope.onnx"
target_path = os.path.join(project_path, experiment_name, 'weights', target_filename)

print(f"Loading weights from: {source_weights}")

# Verifies the existence of the source weight file before initiating the export process.
if os.path.exists(source_weights):
    # Loads the trained PyTorch model from the specified '.pt' file.
    model = RTDETR(source_weights)

    # Executes the model export process with specific configurations.
    exported_path = model.export(
        format='onnx',      # Specifies the target export format as ONNX.
        dynamic=False,      # Exports the model with fixed input/output dimensions for performance.
        simplify=True,      # Applies the ONNX-Simplifier to optimize the model graph.
        opset=16            # Sets the ONNX operator set version for broad compatibility.
    )

    # After export, the file is renamed and moved to the final target location.
    if isinstance(exported_path, str):
        # The `export` method saves the file with a default name.
        shutil.move(exported_path, target_path)
        print("\n" + "-"*100)
        print(f"File Saved: {target_path}")
        print("-"*100)
    else:
        # Handles cases where the export process does not return a valid file path.
        print("Export returned unexpected format.")

else:
    # Provides an error message if the source PyTorch model weights are not found.
    print(f"Error: Could not find weights at {source_weights}")
Loading weights from: /content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1/weights/best.pt

Ultralytics 8.3.251 🚀 Python-3.12.12 torch-2.9.0+cu126 CPU (Intel Xeon CPU @ 2.20GHz)
rt-detr-l summary: 310 layers, 31,994,015 parameters, 0 gradients, 103.5 GFLOPs

PyTorch: starting from '/content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1/weights/best.pt' with input shape (1, 3, 640, 640) BCHW and output shape(s) (1, 300, 9) (63.2 MB)
ONNX: starting export with onnx 1.20.1 opset 16...
ONNX: slimming with onnxslim 0.1.82...
ONNX: export success ✅ 8.6s, saved as '/content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1/weights/best.onnx' (125.5 MB)
Export complete (11.0s)

Results saved to /content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1/weights
Predict:         yolo predict task=detect model=/content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1/weights/best.onnx imgsz=640  
Validate:        yolo val task=detect model=/content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1/weights/best.onnx imgsz=640 data=/content/data.yaml  
Visualize:       https://netron.app

----------------------------------------------------------------------------------------------------
File Saved: /content/drive/MyDrive/Mortiscope Models/RT-DETR/run_v1/weights/rtdetr_mortiscope.onnx
----------------------------------------------------------------------------------------------------
import warnings
from collections import Counter, defaultdict

import cv2
import matplotlib.patches as mpatches
from google.colab import files
from sahi.prediction import ObjectPrediction

# Global Configuration

# The minimum confidence score required for a detection to be considered valid.
confidence_threshold = 0.25
# The target width in pixels for the final output visualization.
target_width = 3840
# The target height in pixels for the final output visualization.
target_height = 2160
# The resolution (Dots Per Inch) for the generated Matplotlib figure.
dpi = 100

# A standardized color palette for different insect life stages.
color_map = {
    "instar_1": "#eab308",
    "instar_2": "#84cc16",
    "instar_3": "#22c55e",
    "pupa":     "#f97316",
    "adult":    "#f43f5e"
}

# Defines the canonical order for presenting life stages in summaries and legends.
lifecycle_order = ["instar_1", "instar_2", "instar_3", "pupa", "adult"]


def hex_to_bgr(hex_color):
    """
    Converts a hexadecimal color string to a BGR tuple for use with OpenCV.

    Args:
        hex_color (str): The color in hexadecimal format (e.g., '#eab308').

    Returns:
        tuple: The color in BGR format (e.g., (8, 179, 234)).
    """
    hex_color = hex_color.lstrip('#')
    rgb = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
    # OpenCV uses BGR order, so the RGB tuple is reversed.
    return (rgb[2], rgb[1], rgb[0])


def format_class_name(name):
    """
    Formats an internal class name into a human-readable, title-cased string.

    Args:
        name (str): The internal class name (e.g., 'instar_1').

    Returns:
        str: The formatted name (e.g., 'Instar 1').
    """
    return name.replace("_", " ").title()


def calculate_iou(box1, box2):
    """
    Calculates the Intersection over Union (IoU) of two bounding boxes.

    Args:
        box1 (sahi.prediction.BBox): The first bounding box.
        box2 (sahi.prediction.BBox): The second bounding box.

    Returns:
        float: The IoU score, a value between 0.0 and 1.0.
    """
    # Extracts coordinates for easier calculation.
    b1 = [box1.minx, box1.miny, box1.maxx, box1.maxy]
    b2 = [box2.minx, box2.miny, box2.maxx, box2.maxy]

    # Determines the coordinates of the intersection rectangle.
    x1 = max(b1[0], b2[0])
    y1 = max(b1[1], b2[1])
    x2 = min(b1[2], b2[2])
    y2 = min(b1[3], b2[3])

    # Computes the area of intersection.
    intersection = max(0, x2 - x1) * max(0, y2 - y1)
    # Computes the area of both bounding boxes.
    area1 = (b1[2] - b1[0]) * (b1[3] - b1[1])
    area2 = (b2[2] - b2[0]) * (b2[3] - b2[1])
    # Computes the area of the union.
    union = area1 + area2 - intersection

    if union == 0:
        return 0
    return intersection / union


def apply_class_agnostic_nms(predictions, iou_threshold=0.6):
    """
    Applies a custom class-agnostic Non-Maximum Suppression (NMS) to a list
    of object predictions to filter out highly overlapping boxes.

    Args:
        predictions (list[ObjectPrediction]): A list of SAHI ObjectPrediction objects.
        iou_threshold (float): The IoU threshold above which boxes are suppressed.

    Returns:
        list[ObjectPrediction]: A filtered list of object predictions.
    """
    # Sorts predictions by confidence score in descending order.
    sorted_preds = sorted(predictions, key=lambda x: x.score.value, reverse=True)
    kept_preds = []

    for current in sorted_preds:
        should_keep = True
        for kept in kept_preds:
            iou = calculate_iou(current.bbox, kept.bbox)
            if iou > iou_threshold:
                # Suppresses the current box if it has a high IoU with an already kept box.
                should_keep = False
                break
        if should_keep:
            kept_preds.append(current)

    return kept_preds


def detect_scene_type(sahi_model, image_path):
    """
    Analyzes an image to determine if it is a 'macro' (close-up) or 'field'
    (wide-angle) scene.

    This heuristic is based on the average relative area of objects detected
    in an initial, low-resolution pass. Large average areas suggest a macro shot.

    Args:
        sahi_model (sahi.AutoDetectionModel): The initialized SAHI model.
        image_path (str): The path to the image file.

    Returns:
        str: The detected scene type, either 'macro' or 'field'.
    """
    native_model = sahi_model.model
    results = native_model.predict(image_path, imgsz=640, conf=0.25, verbose=False)
    boxes = results[0].boxes

    if len(boxes) == 0:
        return "field"

    # Calculates the normalized area (width * height) of each detected box.
    areas = boxes.xywhn[:, 2] * boxes.xywhn[:, 3]
    avg_area = torch.mean(areas).item()

    # Classifies the scene based on a predefined area threshold.
    if avg_area > 0.015:
        return "macro"
    else:
        return "field"


def run_image_analysis():
    """
    Orchestrates the main image analysis workflow.

    This function handles the user file upload, selects an inference strategy
    based on the scene type, processes the detections, applies filtering, and
    generates a final visual report with annotations and a summary legend.
    """
    print("Click button to upload image:")
    uploaded_files = files.upload()

    if not uploaded_files:
        print("No file uploaded.")
        return

    for filename in uploaded_files.keys():
        print(f"\nProcessing {filename}")

        # Determines the appropriate inference strategy for the uploaded image.
        scene_type = detect_scene_type(detection_model, filename)

        image_cv = cv2.imread(filename)
        img_h, img_w, _ = image_cv.shape
        img_area = img_w * img_h

        object_prediction_list = []

        # Scene-Adaptive Inference
        if scene_type == "macro":
            # For close-up images, use the standard, non-sliced prediction method.
            native_model = detection_model.model

            results = native_model.predict(
                filename,
                conf=0.45,
                imgsz=640,
                augment=False,
                agnostic_nms=True,
                verbose=False
            )

            # Manually converts the native results into the SAHI ObjectPrediction format.
            for r in results:
                for box in r.boxes:
                    x1, y1, x2, y2 = box.xyxy[0].tolist()
                    score = box.conf[0].item()
                    cls_id = int(box.cls[0].item())
                    cls_name = native_model.names[cls_id]

                    obj = ObjectPrediction(
                        bbox=[x1, y1, x2, y2],
                        category_id=cls_id,
                        category_name=cls_name,
                        score=score
                    )
                    object_prediction_list.append(obj)

            use_outlier_filter = False

        else:
            # For wide-angle images, use SAHI's sliced prediction method.
            if img_w < 2500 or img_h < 2500:
                current_slice_size = 160
                current_overlap = 0.35
            else:
                current_slice_size = 320
                current_overlap = 0.25

            result = get_sliced_prediction(
                filename,
                detection_model,
                slice_height=current_slice_size,
                slice_width=current_slice_size,
                overlap_height_ratio=current_overlap,
                overlap_width_ratio=current_overlap,
                postprocess_type="NMS",
                postprocess_match_metric="IOS",
                postprocess_match_threshold=0.5,
                postprocess_class_agnostic=True,
                verbose=1
            )
            object_prediction_list = result.object_prediction_list
            use_outlier_filter = True

        # Applies a final class-agnostic NMS pass to refine the results.
        object_prediction_list = apply_class_agnostic_nms(object_prediction_list, iou_threshold=0.6)

        class_counts = Counter()
        class_confidences = defaultdict(list)

        # Calculates the median area of all detections to use for outlier filtering.
        all_areas = []
        for pred in object_prediction_list:
            if pred.score.value >= confidence_threshold:
                bbox = pred.bbox
                area = (bbox.maxx - bbox.minx) * (bbox.maxy - bbox.miny)
                all_areas.append(area)

        median_area = np.median(all_areas) if all_areas else 0

        # Iterates through predictions to filter outliers and draw annotations.
        for prediction in object_prediction_list:
            if prediction.score.value < confidence_threshold:
                continue

            bbox = prediction.bbox
            box_area = (bbox.maxx - bbox.minx) * (bbox.maxy - bbox.miny)

            # Applies an outlier filter to remove unusually large detections, which are often false positives in field images.
            if use_outlier_filter and median_area > 0:
                coverage_ratio = box_area / img_area
                # Skip if box covers >5% of image.
                if coverage_ratio > 0.05:
                    continue
                # Skip if box is >15x median area.
                if box_area > (median_area * 15):
                    continue

            # Aggregates statistics for the final summary.
            class_name = prediction.category.name
            score = prediction.score.value
            class_counts[class_name] += 1
            class_confidences[class_name].append(score)

            # Draws the bounding box rectangle onto the image.
            color_hex = color_map.get(class_name, "#ffffff")
            color_bgr = hex_to_bgr(color_hex)
            x_min, y_min = int(bbox.minx), int(bbox.miny)
            x_max, y_max = int(bbox.maxx), int(bbox.maxy)
            cv2.rectangle(image_cv, (x_min, y_min), (x_max, y_max), color_bgr, 2)

        # Visualization and Reporting

        # Converts the OpenCV (BGR) image to RGB for Matplotlib display.
        img_rgb = cv2.cvtColor(image_cv, cv2.COLOR_BGR2RGB)
        fig_w_in, fig_h_in = target_width / dpi, target_height / dpi

        # Creates a two-panel figure: one for the image, one for the legend.
        fig, (ax_image, ax_legend) = plt.subplots(
            1, 2,
            figsize=(fig_w_in, fig_h_in),
            dpi=dpi,
            gridspec_kw={'width_ratios': [3, 1], 'wspace': 0.05}
        )

        ax_image.imshow(img_rgb)
        ax_image.axis('off')
        ax_legend.axis('off')

        # Constructs the legend handles from the aggregated detection data.
        legend_handles = []
        for class_key in lifecycle_order:
            if class_key in class_counts:
                count = class_counts[class_key]
                scores = class_confidences[class_key]
                avg_score = sum(scores) / len(scores) if scores else 0

                label_text = f"{format_class_name(class_key)}: {count}{avg_score * 100:.2f}%"
                patch = mpatches.Patch(color=color_map.get(class_key, "#000"), label=label_text)
                legend_handles.append(patch)

        # Renders the legend on the right-hand panel.
        if legend_handles:
            ax_legend.legend(
                handles=legend_handles,
                loc='center',
                title="Detection Summary",
                fontsize=24,
                title_fontsize=30,
                frameon=False,
                labelspacing=0.8
            )

        # Ignores the specific warning about incompatible Axes
        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", message="This figure includes Axes that are not compatible with tight_layout")
            plt.tight_layout()
        plt.show()

        # Prints a final textual summary to the console.
        print("\n" + "-" * 70)
        print(f"Image report: {filename}")
        print("-" * 70)
        for class_key in lifecycle_order:
             if class_key in class_counts:
                scores = class_confidences[class_key]
                avg = sum(scores)/len(scores)
                print(f"{format_class_name(class_key):<15} | Count: {class_counts[class_key]:<5} | Avg Conf: {avg*100:.2f}%")
        print("-" * 70)

# Executes the main analysis function.
run_image_analysis()


----------------------------------------------------------------------
Image report: 00059.jpg
----------------------------------------------------------------------
Adult           | Count: 8     | Avg Conf: 78.23%
----------------------------------------------------------------------