MORTISCOPE: YOLO12 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 YOLO12 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 pipeline designed not just for training, but also for deployment and preliminary analysis of forensic imagery:

  1. Environment & Storage Initialization: Mounts Google Drive for persistent artifact storage and installs specific dependencies, including sahi for advanced inference.
  2. Dynamic Dataset Aggregation: Downloads multiple versioned datasets (varying in resolution and environmental conditions) from Roboflow and programmatically builds a unified data.yaml configuration file to prevent path errors.
  3. Training Pipeline: Configures a YOLO12-Large model with custom augmentation strategies. It features a smart resume system that automatically detects interruptions and resumes training from the last saved checkpoint in Google Drive, alongside a custom callback to archive weights for every epoch.
  4. Comprehensive Evaluation: Generates training loss plots, a normalized confusion matrix for biological stage differentiation, and detailed inference speed benchmarks.
  5. Advanced Inference Setup: Integrates SAHI (Sliced Aided Hyper Inference) to handle high-resolution field imagery to allow the detection of small insect instars that standard resizing would miss.
  6. Deployment & Demonstration: Exports the final model to ONNX format for edge deployment and creates an interactive inference engine. This engine detects scene types (Macro vs. Field) to automatically switch between standard and sliced inference modes, applies outlier filtering, and generates publication-ready visualizations.

Tech Stack

Deep Learning and Inference Engines

  • Ultralytics YOLO12: The backbone object detection architecture. This notebook specifically uses the Large (l) variant to prioritize high accuracy for subtle morphological features of insect life stages.
  • SAHI (Sliced Aided Hyper Inference): An advanced library used to perform inference on high-resolution images by slicing them into smaller overlapping windows. This is critical for detecting small instars in wide-angle “field” shots.
  • PyTorch: The underlying tensor computation framework powering YOLO12.
  • ONNX (Open Neural Network Exchange): Used to export the trained PyTorch model into a standardized format to optimize it for deployment on various hardware platforms without Python dependencies.

Data Management and Processing

  • Roboflow: Used for versioning, hosting, preprocessing, augmenting and downloading the multi-part Chrysomya megacephala dataset.
  • Google Drive: Serves as the persistent storage layer to ensure trained weights (best.pt, last.pt) and historical logs are preserved across Google Colab sessions.
  • Pandas: Used to parse and manipulate the training logs (results.csv) for custom performance plotting.
  • PyYAML: Automates the creation of the dataset configuration file, dynamically linking downloaded image paths to the model.

Visualization and Analysis

  • OpenCV (cv2): Handles image I/O, color space conversion, and drawing bounding boxes for the inference demonstration.
  • Seaborn & Matplotlib: Used to generate high-quality statistical plots, including the smoothed loss curves and the heatmap-style confusion matrix.
  • NumPy: Performs essential array operations, particularly for calculating Intersection over Union (IoU) and filtering statistical outliers in prediction areas.

Section 1: Project Initialization and Dependencies

Purpose: This section establishes the file system structure required for the project and installs the specific external libraries needed for computer vision tasks. It ensures the notebook has access to persistent storage (Google Drive) and the necessary tools for dataset management and inference.

Key Activities:

  • Drive Mounting: Connects to Google Drive to create a persistent directory. This is crucial for saving trained weights and logs so they are not lost when the Colab session disconnects.
  • Library Installation: Installs roboflow (for downloading datasets), ultralytics (for the YOLO model), and sahi (for Sliced Aided Hyper Inference, used for detecting small objects).
  • Credential Management: Securely retrieves the Roboflow API key from Colab’s secrets manager to allow authorized access to the private datasets.
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 = "YOLO12"

# 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/YOLO12
Weight Storage:   /content/drive/MyDrive/Mortiscope Models/YOLO12/weights
# Executes the shell command to install the specified libraries.
!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.12-py3-none-any.whl.metadata (9.7 kB)
Collecting ultralytics
  Downloading ultralytics-8.4.5-py3-none-any.whl.metadata (38 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) (2026.1.4)
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.3.1
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.2
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.3.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.12-py3-none-any.whl (91 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 91.7/91.7 kB 9.0 MB/s eta 0:00:00
Downloading idna-3.7-py3-none-any.whl (66 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 66.8/66.8 kB 7.1 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 54.9 MB/s eta 0:00:00
Downloading ultralytics-8.4.5-py3-none-any.whl (1.2 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 77.9 MB/s eta 0:00:00
Downloading sahi-0.11.36-py3-none-any.whl (111 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 111.7/111.7 kB 12.0 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 41.7 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 90.4 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 134.6 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.6 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.12 sahi-0.11.36 terminaltables-3.1.10 ultralytics-8.4.5 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 data from multiple sources and format it for the YOLO12 training pipeline. Since the dataset is versioned across different projects (representing different resolutions and environmental conditions), this section consolidates them into a single training configuration.

Key Activities:

  • Dataset Download: Uses the Roboflow API to download specific versions of the Chrysomya megacephala datasets.
  • Dynamic YAML Generation: Instead of manually creating a configuration file, the script iterates through the downloaded folders to verify valid paths. It then programmatically generates a data.yaml file to ensure that the model only attempts to train on data that actually exists locally, preventing “path not found” errors.
# Accesses the specific Roboflow workspace that contains all the project datasets.
workspace = rf.workspace("mortiscope-fvkhd")

# Downloads version 1 of projects using the "yolov12" format.
decomposition_1 = workspace.project("decomposition-high-resolution-300").version(1).download("yolov12")
decomposition_2 = workspace.project("decomposition-high-resolution-250").version(1).download("yolov12")
decomposition_3 = workspace.project("decomposition-high-resolution-200").version(1).download("yolov12")
decomposition_4 = workspace.project("decomposition-standard-resolution-300").version(1).download("yolov12")
decomposition_5 = workspace.project("decomposition-standard-resolution-250").version(1).download("yolov12")
complementary = workspace.project("complementary").version(1).download("yolov12")

# 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.
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.
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%)-1
Added valid: Decomposition-(High-Resolution---300%)-1
Added train: Decomposition-(High-Resolution---250%)-1
Added valid: Decomposition-(High-Resolution---250%)-1
Added train: Decomposition-(High-Resolution---200%)-1
Skipped valid (Not found): Decomposition-(High-Resolution---200%)-1
Added train: Decomposition-(Standard-Resolution---300%)-1
Added valid: Decomposition-(Standard-Resolution---300%)-1
Added train: Decomposition-(Standard-Resolution---250%)-1
Added valid: Decomposition-(Standard-Resolution---250%)-1
Added train: Complementary-1
Added valid: Complementary-1
----------------------------------------------------------------------
Balanced configuration created at: /content/data.yaml
Total Train Folders: 6
Total Valid Folders: 5
----------------------------------------------------------------------

Section 3: Model Initialization and Callback Definition

Purpose: To prepare the architecture and define custom behaviors during the training process. This section ensures that training progress is saved frequently and securely.

Key Activities:

  • Custom Callback (on_train_epoch_end): A custom function is defined to run after every training epoch. It copies the model weights (last.pt and best.pt) from the temporary Colab runtime to the persistent Google Drive folder. It also creates a historical archive of weights for every epoch, allowing for granular analysis of model convergence later.
  • Model Instantiation: Downloads the base weights for YOLO12l (Large). The large variant is specifically selected for the training in this notebook.
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/YOLO12/weights/epoch_history
   2. Resume file active at: /content/drive/MyDrive/Mortiscope Models/YOLO12/weights/last.pt
import torch
from ultralytics import YOLO

# Defines a list of URLs for various pre-trained YOLO12 model sizes, ranging from nano (n) to extra-large (x).
urls = [
    "https://github.com/ultralytics/assets/releases/download/v8.4.0/yolo12n.pt",
    "https://github.com/ultralytics/assets/releases/download/v8.4.0/yolo12s.pt",
    "https://github.com/ultralytics/assets/releases/download/v8.4.0/yolo12m.pt",
    "https://github.com/ultralytics/assets/releases/download/v8.4.0/yolo12l.pt",
    "https://github.com/ultralytics/assets/releases/download/v8.4.0/yolo12x.pt"
]

# Selects a specific model variant to serve as the training base.
selected_url = urls[3]
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}...")
    # Uses torch.hub to download the file from the specified URL.
    torch.hub.download_url_to_file(selected_url, model_filename)
else:
    print(f"{model_filename} found locally.")

# Instantiates the YOLO model, loading the architecture and pre-trained weights from the downloaded '.pt' file.
model = YOLO(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("\nModel Architecture & Complexity Base:")
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 yolo12l.pt...100%|██████████| 51.2M/51.2M [00:09<00:00, 5.92MB/s]

Model Architecture & Complexity Base:
layer                                    name                type  gradient  parameters               shape        mu     sigma
    0                     model.0.conv.weight              Conv2d     False        1728       [64, 3, 3, 3] -0.000195    0.0632        float32
    1                       model.0.bn.weight         BatchNorm2d     False          64                [64]      2.93      1.23        float32
    1                         model.0.bn.bias         BatchNorm2d     False          64                [64]     0.444      1.26        float32
    2                             model.0.act                SiLU     False           0                  []         -         -              -
    3                     model.1.conv.weight              Conv2d     False       73728     [128, 64, 3, 3] -0.000265    0.0113        float32
    4                       model.1.bn.weight         BatchNorm2d     False         128               [128]       2.1     0.416        float32
    4                         model.1.bn.bias         BatchNorm2d     False         128               [128]     0.363     0.913        float32
    5                 model.2.cv1.conv.weight              Conv2d     False       16384    [128, 128, 1, 1]  -0.00174     0.017        float32
    6                   model.2.cv1.bn.weight         BatchNorm2d     False         128               [128]      1.21     0.457        float32
    6                     model.2.cv1.bn.bias         BatchNorm2d     False         128               [128]     0.295     0.753        float32
    7                 model.2.cv2.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -0.000836    0.0103        float32
    8                   model.2.cv2.bn.weight         BatchNorm2d     False         256               [256]     0.947     0.198        float32
    8                     model.2.cv2.bn.bias         BatchNorm2d     False         256               [256]    -0.736     0.398        float32
    9             model.2.m.0.cv1.conv.weight              Conv2d     False        2048      [32, 64, 1, 1]  -0.00171    0.0211        float32
   10               model.2.m.0.cv1.bn.weight         BatchNorm2d     False          32                [32]     0.941     0.483        float32
   10                 model.2.m.0.cv1.bn.bias         BatchNorm2d     False          32                [32]     0.687     0.901        float32
   11             model.2.m.0.cv2.conv.weight              Conv2d     False        2048      [32, 64, 1, 1]  -0.00114    0.0194        float32
   12               model.2.m.0.cv2.bn.weight         BatchNorm2d     False          32                [32]      1.23     0.328        float32
   12                 model.2.m.0.cv2.bn.bias         BatchNorm2d     False          32                [32]     0.251       0.7        float32
   13             model.2.m.0.cv3.conv.weight              Conv2d     False        4096      [64, 64, 1, 1]  -0.00131    0.0204        float32
   14               model.2.m.0.cv3.bn.weight         BatchNorm2d     False          64                [64]      1.12     0.264        float32
   14                 model.2.m.0.cv3.bn.bias         BatchNorm2d     False          64                [64]    0.0965     0.488        float32
   15         model.2.m.0.m.0.cv1.conv.weight              Conv2d     False        9216      [32, 32, 3, 3] -0.000413    0.0124        float32
   16           model.2.m.0.m.0.cv1.bn.weight         BatchNorm2d     False          32                [32]      1.25     0.374        float32
   16             model.2.m.0.m.0.cv1.bn.bias         BatchNorm2d     False          32                [32]     0.123     0.553        float32
   17         model.2.m.0.m.0.cv2.conv.weight              Conv2d     False        9216      [32, 32, 3, 3] -0.000427    0.0111        float32
   18           model.2.m.0.m.0.cv2.bn.weight         BatchNorm2d     False          32                [32]     0.752     0.224        float32
   18             model.2.m.0.m.0.cv2.bn.bias         BatchNorm2d     False          32                [32]      0.49     0.737        float32
   19         model.2.m.0.m.1.cv1.conv.weight              Conv2d     False        9216      [32, 32, 3, 3] -0.000271    0.0108        float32
   20           model.2.m.0.m.1.cv1.bn.weight         BatchNorm2d     False          32                [32]      1.09     0.291        float32
   20             model.2.m.0.m.1.cv1.bn.bias         BatchNorm2d     False          32                [32]     -0.26     0.448        float32
   21         model.2.m.0.m.1.cv2.conv.weight              Conv2d     False        9216      [32, 32, 3, 3] -0.000516      0.01        float32
   22           model.2.m.0.m.1.cv2.bn.weight         BatchNorm2d     False          32                [32]     0.787     0.303        float32
   22             model.2.m.0.m.1.cv2.bn.bias         BatchNorm2d     False          32                [32]     0.546     0.574        float32
   23             model.2.m.1.cv1.conv.weight              Conv2d     False        2048      [32, 64, 1, 1]  -0.00142    0.0178        float32
   24               model.2.m.1.cv1.bn.weight         BatchNorm2d     False          32                [32]     0.428     0.145        float32
   24                 model.2.m.1.cv1.bn.bias         BatchNorm2d     False          32                [32]      0.12     0.526        float32
   25             model.2.m.1.cv2.conv.weight              Conv2d     False        2048      [32, 64, 1, 1] -0.000978    0.0116        float32
   26               model.2.m.1.cv2.bn.weight         BatchNorm2d     False          32                [32]      1.05     0.154        float32
   26                 model.2.m.1.cv2.bn.bias         BatchNorm2d     False          32                [32]    -0.424     0.183        float32
   27             model.2.m.1.cv3.conv.weight              Conv2d     False        4096      [64, 64, 1, 1] -0.000711    0.0159        float32
   28               model.2.m.1.cv3.bn.weight         BatchNorm2d     False          64                [64]      1.25     0.143        float32
   28                 model.2.m.1.cv3.bn.bias         BatchNorm2d     False          64                [64]   -0.0111     0.294        float32
   29         model.2.m.1.m.0.cv1.conv.weight              Conv2d     False        9216      [32, 32, 3, 3] -0.000818    0.0118        float32
   30           model.2.m.1.m.0.cv1.bn.weight         BatchNorm2d     False          32                [32]     0.862      0.11        float32
   30             model.2.m.1.m.0.cv1.bn.bias         BatchNorm2d     False          32                [32]    -0.315      0.45        float32
   31         model.2.m.1.m.0.cv2.conv.weight              Conv2d     False        9216      [32, 32, 3, 3] -0.000732    0.0114        float32
   32           model.2.m.1.m.0.cv2.bn.weight         BatchNorm2d     False          32                [32]     0.616     0.167        float32
   32             model.2.m.1.m.0.cv2.bn.bias         BatchNorm2d     False          32                [32]     0.205     0.515        float32
   33         model.2.m.1.m.1.cv1.conv.weight              Conv2d     False        9216      [32, 32, 3, 3] -0.000887    0.0111        float32
   34           model.2.m.1.m.1.cv1.bn.weight         BatchNorm2d     False          32                [32]     0.731     0.106        float32
   34             model.2.m.1.m.1.cv1.bn.bias         BatchNorm2d     False          32                [32]    -0.379     0.417        float32
   35         model.2.m.1.m.1.cv2.conv.weight              Conv2d     False        9216      [32, 32, 3, 3] -0.000264    0.0115        float32
   36           model.2.m.1.m.1.cv2.bn.weight         BatchNorm2d     False          32                [32]     0.708     0.142        float32
   36             model.2.m.1.m.1.cv2.bn.bias         BatchNorm2d     False          32                [32]     0.773     0.446        float32
   37                     model.3.conv.weight              Conv2d     False      589824    [256, 256, 3, 3] -0.000154   0.00466        float32
   38                       model.3.bn.weight         BatchNorm2d     False         256               [256]     0.816     0.187        float32
   38                         model.3.bn.bias         BatchNorm2d     False         256               [256]    -0.297     0.492        float32
   39                 model.4.cv1.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -0.000486   0.00999        float32
   40                   model.4.cv1.bn.weight         BatchNorm2d     False         256               [256]     0.861     0.144        float32
   40                     model.4.cv1.bn.bias         BatchNorm2d     False         256               [256]    -0.107     0.357        float32
   41                 model.4.cv2.conv.weight              Conv2d     False      262144    [512, 512, 1, 1] -0.000506   0.00652        float32
   42                   model.4.cv2.bn.weight         BatchNorm2d     False         512               [512]      1.01     0.156        float32
   42                     model.4.cv2.bn.bias         BatchNorm2d     False         512               [512]    -0.747     0.314        float32
   43             model.4.m.0.cv1.conv.weight              Conv2d     False        8192     [64, 128, 1, 1] -0.000809    0.0116        float32
   44               model.4.m.0.cv1.bn.weight         BatchNorm2d     False          64                [64]     0.559     0.131        float32
   44                 model.4.m.0.cv1.bn.bias         BatchNorm2d     False          64                [64]     0.223     0.354        float32
   45             model.4.m.0.cv2.conv.weight              Conv2d     False        8192     [64, 128, 1, 1] -0.000288   0.00909        float32
   46               model.4.m.0.cv2.bn.weight         BatchNorm2d     False          64                [64]     0.985     0.131        float32
   46                 model.4.m.0.cv2.bn.bias         BatchNorm2d     False          64                [64]    -0.126     0.154        float32
   47             model.4.m.0.cv3.conv.weight              Conv2d     False       16384    [128, 128, 1, 1]  -0.00124     0.011        float32
   48               model.4.m.0.cv3.bn.weight         BatchNorm2d     False         128               [128]     0.932     0.124        float32
   48                 model.4.m.0.cv3.bn.bias         BatchNorm2d     False         128               [128]    -0.422     0.308        float32
   49         model.4.m.0.m.0.cv1.conv.weight              Conv2d     False       36864      [64, 64, 3, 3] -0.000392   0.00704        float32
   50           model.4.m.0.m.0.cv1.bn.weight         BatchNorm2d     False          64                [64]     0.967    0.0964        float32
   50             model.4.m.0.m.0.cv1.bn.bias         BatchNorm2d     False          64                [64]    -0.631     0.302        float32
   51         model.4.m.0.m.0.cv2.conv.weight              Conv2d     False       36864      [64, 64, 3, 3] -0.000412   0.00673        float32
   52           model.4.m.0.m.0.cv2.bn.weight         BatchNorm2d     False          64                [64]     0.744     0.141        float32
   52             model.4.m.0.m.0.cv2.bn.bias         BatchNorm2d     False          64                [64]  -0.00135     0.331        float32
   53         model.4.m.0.m.1.cv1.conv.weight              Conv2d     False       36864      [64, 64, 3, 3]  -0.00044    0.0071        float32
   54           model.4.m.0.m.1.cv1.bn.weight         BatchNorm2d     False          64                [64]     0.933    0.0963        float32
   54             model.4.m.0.m.1.cv1.bn.bias         BatchNorm2d     False          64                [64]    -0.635      0.35        float32
   55         model.4.m.0.m.1.cv2.conv.weight              Conv2d     False       36864      [64, 64, 3, 3] -0.000133   0.00668        float32
   56           model.4.m.0.m.1.cv2.bn.weight         BatchNorm2d     False          64                [64]     0.921     0.121        float32
   56             model.4.m.0.m.1.cv2.bn.bias         BatchNorm2d     False          64                [64]     0.274     0.295        float32
   57             model.4.m.1.cv1.conv.weight              Conv2d     False        8192     [64, 128, 1, 1]  -0.00104    0.0106        float32
   58               model.4.m.1.cv1.bn.weight         BatchNorm2d     False          64                [64]     0.544     0.137        float32
   58                 model.4.m.1.cv1.bn.bias         BatchNorm2d     False          64                [64]    -0.165     0.566        float32
   59             model.4.m.1.cv2.conv.weight              Conv2d     False        8192     [64, 128, 1, 1] -0.000777   0.00719        float32
   60               model.4.m.1.cv2.bn.weight         BatchNorm2d     False          64                [64]      1.16     0.128        float32
   60                 model.4.m.1.cv2.bn.bias         BatchNorm2d     False          64                [64]     -0.18    0.0961        float32
   61             model.4.m.1.cv3.conv.weight              Conv2d     False       16384    [128, 128, 1, 1]  -0.00104   0.00939        float32
   62               model.4.m.1.cv3.bn.weight         BatchNorm2d     False         128               [128]      1.05      0.14        float32
   62                 model.4.m.1.cv3.bn.bias         BatchNorm2d     False         128               [128]    -0.312     0.275        float32
   63         model.4.m.1.m.0.cv1.conv.weight              Conv2d     False       36864      [64, 64, 3, 3] -0.000634   0.00643        float32
   64           model.4.m.1.m.0.cv1.bn.weight         BatchNorm2d     False          64                [64]     0.948       0.1        float32
   64             model.4.m.1.m.0.cv1.bn.bias         BatchNorm2d     False          64                [64]    -0.634     0.376        float32
   65         model.4.m.1.m.0.cv2.conv.weight              Conv2d     False       36864      [64, 64, 3, 3] -0.000491   0.00638        float32
   66           model.4.m.1.m.0.cv2.bn.weight         BatchNorm2d     False          64                [64]     0.768     0.143        float32
   66             model.4.m.1.m.0.cv2.bn.bias         BatchNorm2d     False          64                [64]   -0.0605     0.394        float32
   67         model.4.m.1.m.1.cv1.conv.weight              Conv2d     False       36864      [64, 64, 3, 3] -0.000513   0.00651        float32
   68           model.4.m.1.m.1.cv1.bn.weight         BatchNorm2d     False          64                [64]     0.954     0.077        float32
   68             model.4.m.1.m.1.cv1.bn.bias         BatchNorm2d     False          64                [64]    -0.644     0.362        float32
   69         model.4.m.1.m.1.cv2.conv.weight              Conv2d     False       36864      [64, 64, 3, 3] -0.000226   0.00627        float32
   70           model.4.m.1.m.1.cv2.bn.weight         BatchNorm2d     False          64                [64]      1.17     0.182        float32
   70             model.4.m.1.m.1.cv2.bn.bias         BatchNorm2d     False          64                [64]     0.294     0.253        float32
   71                     model.5.conv.weight              Conv2d     False  2.3593e+06    [512, 512, 3, 3] -0.000132    0.0028        float32
   72                       model.5.bn.weight         BatchNorm2d     False         512               [512]     0.409     0.129        float32
   72                         model.5.bn.bias         BatchNorm2d     False         512               [512]    -0.716     0.354        float32
   73                 model.6.cv1.conv.weight              Conv2d     False      131072    [256, 512, 1, 1] -0.000534    0.0076        float32
   74                   model.6.cv1.bn.weight         BatchNorm2d     False         256               [256]      1.78     0.218        float32
   74                     model.6.cv1.bn.bias         BatchNorm2d     False         256               [256]     0.309     0.347        float32
   75                 model.6.cv2.conv.weight              Conv2d     False      655360   [512, 1280, 1, 1] -1.91e-05   0.00338        float32
   76                   model.6.cv2.bn.weight         BatchNorm2d     False         512               [512]       1.6     0.197        float32
   76                     model.6.cv2.bn.bias         BatchNorm2d     False         512               [512]   -0.0366     0.114        float32
   77      model.6.m.0.0.attn.qkv.conv.weight              Conv2d     False      196608    [768, 256, 1, 1] -4.97e-06   0.00415        float32
   78        model.6.m.0.0.attn.qkv.bn.weight         BatchNorm2d     False         768               [768]     0.954       0.3        float32
   78          model.6.m.0.0.attn.qkv.bn.bias         BatchNorm2d     False         768               [768]   0.00289     0.173        float32
   79              model.6.m.0.0.attn.qkv.act            Identity     False           0                  []         -         -              -
   80     model.6.m.0.0.attn.proj.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -1.85e-05   0.00656        float32
   81       model.6.m.0.0.attn.proj.bn.weight         BatchNorm2d     False         256               [256]     0.732     0.182        float32
   81         model.6.m.0.0.attn.proj.bn.bias         BatchNorm2d     False         256               [256]   2.2e-06  3.06e-05        float32
   82             model.6.m.0.0.attn.proj.act            Identity     False           0                  []         -         -              -
   83       model.6.m.0.0.attn.pe.conv.weight              Conv2d     False       12544      [256, 1, 7, 7]  -0.00133   0.00795        float32
   83         model.6.m.0.0.attn.pe.conv.bias              Conv2d     False         256               [256] -2.68e-08  3.29e-05        float32
   84         model.6.m.0.0.attn.pe.bn.weight         BatchNorm2d     False         256               [256]     0.798      0.21        float32
   84           model.6.m.0.0.attn.pe.bn.bias         BatchNorm2d     False         256               [256]  3.38e-07  1.07e-05        float32
   85               model.6.m.0.0.attn.pe.act            Identity     False           0                  []         -         -              -
   86         model.6.m.0.0.mlp.0.conv.weight              Conv2d     False       78592    [307, 256, 1, 1] -0.000548   0.00538        float32
   87           model.6.m.0.0.mlp.0.bn.weight         BatchNorm2d     False         307               [307]      1.17     0.147        float32
   87             model.6.m.0.0.mlp.0.bn.bias         BatchNorm2d     False         307               [307]    -0.478     0.156        float32
   88         model.6.m.0.0.mlp.1.conv.weight              Conv2d     False       78592    [256, 307, 1, 1]  0.000328   0.00514        float32
   89           model.6.m.0.0.mlp.1.bn.weight         BatchNorm2d     False         256               [256]     0.799     0.113        float32
   89             model.6.m.0.0.mlp.1.bn.bias         BatchNorm2d     False         256               [256]  2.23e-06  2.97e-05        float32
   90                 model.6.m.0.0.mlp.1.act            Identity     False           0                  []         -         -              -
   91      model.6.m.0.1.attn.qkv.conv.weight              Conv2d     False      196608    [768, 256, 1, 1] -1.49e-06   0.00472        float32
   92        model.6.m.0.1.attn.qkv.bn.weight         BatchNorm2d     False         768               [768]      1.09     0.216        float32
   92          model.6.m.0.1.attn.qkv.bn.bias         BatchNorm2d     False         768               [768]  -0.00225     0.188        float32
   93              model.6.m.0.1.attn.qkv.act            Identity     False           0                  []         -         -              -
   94     model.6.m.0.1.attn.proj.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -3.16e-06   0.00584        float32
   95       model.6.m.0.1.attn.proj.bn.weight         BatchNorm2d     False         256               [256]     0.755      0.14        float32
   95         model.6.m.0.1.attn.proj.bn.bias         BatchNorm2d     False         256               [256]  1.82e-06  2.69e-05        float32
   96             model.6.m.0.1.attn.proj.act            Identity     False           0                  []         -         -              -
   97       model.6.m.0.1.attn.pe.conv.weight              Conv2d     False       12544      [256, 1, 7, 7]  -0.00221    0.0075        float32
   97         model.6.m.0.1.attn.pe.conv.bias              Conv2d     False         256               [256]   1.6e-06  3.53e-05        float32
   98         model.6.m.0.1.attn.pe.bn.weight         BatchNorm2d     False         256               [256]     0.685     0.195        float32
   98           model.6.m.0.1.attn.pe.bn.bias         BatchNorm2d     False         256               [256]  -1.2e-06  1.11e-05        float32
   99               model.6.m.0.1.attn.pe.act            Identity     False           0                  []         -         -              -
  100         model.6.m.0.1.mlp.0.conv.weight              Conv2d     False       78592    [307, 256, 1, 1] -0.000482   0.00579        float32
  101           model.6.m.0.1.mlp.0.bn.weight         BatchNorm2d     False         307               [307]       1.2     0.186        float32
  101             model.6.m.0.1.mlp.0.bn.bias         BatchNorm2d     False         307               [307]    -0.531     0.178        float32
  102         model.6.m.0.1.mlp.1.conv.weight              Conv2d     False       78592    [256, 307, 1, 1]  0.000185    0.0053        float32
  103           model.6.m.0.1.mlp.1.bn.weight         BatchNorm2d     False         256               [256]      0.78     0.116        float32
  103             model.6.m.0.1.mlp.1.bn.bias         BatchNorm2d     False         256               [256]  7.48e-07  2.51e-05        float32
  104                 model.6.m.0.1.mlp.1.act            Identity     False           0                  []         -         -              -
  105      model.6.m.1.0.attn.qkv.conv.weight              Conv2d     False      196608    [768, 256, 1, 1] -1.59e-06   0.00531        float32
  106        model.6.m.1.0.attn.qkv.bn.weight         BatchNorm2d     False         768               [768]      1.21      0.15        float32
  106          model.6.m.1.0.attn.qkv.bn.bias         BatchNorm2d     False         768               [768]    0.0011     0.214        float32
  107              model.6.m.1.0.attn.qkv.act            Identity     False           0                  []         -         -              -
  108     model.6.m.1.0.attn.proj.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -3.18e-06   0.00589        float32
  109       model.6.m.1.0.attn.proj.bn.weight         BatchNorm2d     False         256               [256]     0.704     0.114        float32
  109         model.6.m.1.0.attn.proj.bn.bias         BatchNorm2d     False         256               [256]  3.11e-07  1.88e-05        float32
  110             model.6.m.1.0.attn.proj.act            Identity     False           0                  []         -         -              -
  111       model.6.m.1.0.attn.pe.conv.weight              Conv2d     False       12544      [256, 1, 7, 7]  -0.00204   0.00719        float32
  111         model.6.m.1.0.attn.pe.conv.bias              Conv2d     False         256               [256]  7.16e-07  2.38e-05        float32
  112         model.6.m.1.0.attn.pe.bn.weight         BatchNorm2d     False         256               [256]     0.688     0.146        float32
  112           model.6.m.1.0.attn.pe.bn.bias         BatchNorm2d     False         256               [256] -4.26e-08  9.58e-06        float32
  113               model.6.m.1.0.attn.pe.act            Identity     False           0                  []         -         -              -
  114         model.6.m.1.0.mlp.0.conv.weight              Conv2d     False       78592    [307, 256, 1, 1] -0.000302   0.00575        float32
  115           model.6.m.1.0.mlp.0.bn.weight         BatchNorm2d     False         307               [307]      1.15     0.116        float32
  115             model.6.m.1.0.mlp.0.bn.bias         BatchNorm2d     False         307               [307]    -0.556     0.163        float32
  116         model.6.m.1.0.mlp.1.conv.weight              Conv2d     False       78592    [256, 307, 1, 1]    0.0001   0.00528        float32
  117           model.6.m.1.0.mlp.1.bn.weight         BatchNorm2d     False         256               [256]     0.874    0.0728        float32
  117             model.6.m.1.0.mlp.1.bn.bias         BatchNorm2d     False         256               [256] -6.63e-07  1.75e-05        float32
  118                 model.6.m.1.0.mlp.1.act            Identity     False           0                  []         -         -              -
  119      model.6.m.1.1.attn.qkv.conv.weight              Conv2d     False      196608    [768, 256, 1, 1]  4.35e-06   0.00438        float32
  120        model.6.m.1.1.attn.qkv.bn.weight         BatchNorm2d     False         768               [768]      1.01     0.191        float32
  120          model.6.m.1.1.attn.qkv.bn.bias         BatchNorm2d     False         768               [768]   0.00145     0.091        float32
  121              model.6.m.1.1.attn.qkv.act            Identity     False           0                  []         -         -              -
  122     model.6.m.1.1.attn.proj.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -2.58e-05   0.00571        float32
  123       model.6.m.1.1.attn.proj.bn.weight         BatchNorm2d     False         256               [256]     0.844      0.11        float32
  123         model.6.m.1.1.attn.proj.bn.bias         BatchNorm2d     False         256               [256] -3.03e-07  1.56e-05        float32
  124             model.6.m.1.1.attn.proj.act            Identity     False           0                  []         -         -              -
  125       model.6.m.1.1.attn.pe.conv.weight              Conv2d     False       12544      [256, 1, 7, 7]  -0.00208    0.0073        float32
  125         model.6.m.1.1.attn.pe.conv.bias              Conv2d     False         256               [256] -7.94e-07  2.91e-05        float32
  126         model.6.m.1.1.attn.pe.bn.weight         BatchNorm2d     False         256               [256]      0.74     0.181        float32
  126           model.6.m.1.1.attn.pe.bn.bias         BatchNorm2d     False         256               [256]  4.02e-07  7.97e-06        float32
  127               model.6.m.1.1.attn.pe.act            Identity     False           0                  []         -         -              -
  128         model.6.m.1.1.mlp.0.conv.weight              Conv2d     False       78592    [307, 256, 1, 1] -0.000242   0.00594        float32
  129           model.6.m.1.1.mlp.0.bn.weight         BatchNorm2d     False         307               [307]      1.16     0.132        float32
  129             model.6.m.1.1.mlp.0.bn.bias         BatchNorm2d     False         307               [307]    -0.526      0.15        float32
  130         model.6.m.1.1.mlp.1.conv.weight              Conv2d     False       78592    [256, 307, 1, 1] -3.96e-05   0.00526        float32
  131           model.6.m.1.1.mlp.1.bn.weight         BatchNorm2d     False         256               [256]     0.887    0.0831        float32
  131             model.6.m.1.1.mlp.1.bn.bias         BatchNorm2d     False         256               [256]  -2.7e-07  1.31e-05        float32
  132                 model.6.m.1.1.mlp.1.act            Identity     False           0                  []         -         -              -
  133      model.6.m.2.0.attn.qkv.conv.weight              Conv2d     False      196608    [768, 256, 1, 1]  8.54e-07   0.00549        float32
  134        model.6.m.2.0.attn.qkv.bn.weight         BatchNorm2d     False         768               [768]      1.14     0.129        float32
  134          model.6.m.2.0.attn.qkv.bn.bias         BatchNorm2d     False         768               [768]  -0.00139     0.196        float32
  135              model.6.m.2.0.attn.qkv.act            Identity     False           0                  []         -         -              -
  136     model.6.m.2.0.attn.proj.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]  1.32e-05   0.00617        float32
  137       model.6.m.2.0.attn.proj.bn.weight         BatchNorm2d     False         256               [256]     0.952    0.0785        float32
  137         model.6.m.2.0.attn.proj.bn.bias         BatchNorm2d     False         256               [256] -1.63e-09  1.04e-05        float32
  138             model.6.m.2.0.attn.proj.act            Identity     False           0                  []         -         -              -
  139       model.6.m.2.0.attn.pe.conv.weight              Conv2d     False       12544      [256, 1, 7, 7]  -0.00197   0.00665        float32
  139         model.6.m.2.0.attn.pe.conv.bias              Conv2d     False         256               [256] -8.14e-07  1.79e-05        float32
  140         model.6.m.2.0.attn.pe.bn.weight         BatchNorm2d     False         256               [256]     0.643     0.133        float32
  140           model.6.m.2.0.attn.pe.bn.bias         BatchNorm2d     False         256               [256]  4.45e-07  7.52e-06        float32
  141               model.6.m.2.0.attn.pe.act            Identity     False           0                  []         -         -              -
  142         model.6.m.2.0.mlp.0.conv.weight              Conv2d     False       78592    [307, 256, 1, 1] -0.000277   0.00574        float32
  143           model.6.m.2.0.mlp.0.bn.weight         BatchNorm2d     False         307               [307]      1.13    0.0734        float32
  143             model.6.m.2.0.mlp.0.bn.bias         BatchNorm2d     False         307               [307]    -0.486     0.108        float32
  144         model.6.m.2.0.mlp.1.conv.weight              Conv2d     False       78592    [256, 307, 1, 1]  5.92e-05   0.00518        float32
  145           model.6.m.2.0.mlp.1.bn.weight         BatchNorm2d     False         256               [256]     0.944     0.075        float32
  145             model.6.m.2.0.mlp.1.bn.bias         BatchNorm2d     False         256               [256] -3.96e-08  9.36e-06        float32
  146                 model.6.m.2.0.mlp.1.act            Identity     False           0                  []         -         -              -
  147      model.6.m.2.1.attn.qkv.conv.weight              Conv2d     False      196608    [768, 256, 1, 1]  9.02e-07   0.00468        float32
  148        model.6.m.2.1.attn.qkv.bn.weight         BatchNorm2d     False         768               [768]      1.05     0.171        float32
  148          model.6.m.2.1.attn.qkv.bn.bias         BatchNorm2d     False         768               [768]   0.00296     0.104        float32
  149              model.6.m.2.1.attn.qkv.act            Identity     False           0                  []         -         -              -
  150     model.6.m.2.1.attn.proj.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -5.25e-05   0.00536        float32
  151       model.6.m.2.1.attn.proj.bn.weight         BatchNorm2d     False         256               [256]     0.864    0.0759        float32
  151         model.6.m.2.1.attn.proj.bn.bias         BatchNorm2d     False         256               [256]   1.4e-08  8.23e-06        float32
  152             model.6.m.2.1.attn.proj.act            Identity     False           0                  []         -         -              -
  153       model.6.m.2.1.attn.pe.conv.weight              Conv2d     False       12544      [256, 1, 7, 7]  -0.00183   0.00629        float32
  153         model.6.m.2.1.attn.pe.conv.bias              Conv2d     False         256               [256]  1.31e-06  2.08e-05        float32
  154         model.6.m.2.1.attn.pe.bn.weight         BatchNorm2d     False         256               [256]     0.718     0.155        float32
  154           model.6.m.2.1.attn.pe.bn.bias         BatchNorm2d     False         256               [256]  5.85e-07  4.15e-06        float32
  155               model.6.m.2.1.attn.pe.act            Identity     False           0                  []         -         -              -
  156         model.6.m.2.1.mlp.0.conv.weight              Conv2d     False       78592    [307, 256, 1, 1] -0.000271   0.00566        float32
  157           model.6.m.2.1.mlp.0.bn.weight         BatchNorm2d     False         307               [307]      1.11    0.0948        float32
  157             model.6.m.2.1.mlp.0.bn.bias         BatchNorm2d     False         307               [307]    -0.437     0.166        float32
  158         model.6.m.2.1.mlp.1.conv.weight              Conv2d     False       78592    [256, 307, 1, 1]  4.36e-05   0.00495        float32
  159           model.6.m.2.1.mlp.1.bn.weight         BatchNorm2d     False         256               [256]     0.986     0.069        float32
  159             model.6.m.2.1.mlp.1.bn.bias         BatchNorm2d     False         256               [256] -1.04e-07  7.58e-06        float32
  160                 model.6.m.2.1.mlp.1.act            Identity     False           0                  []         -         -              -
  161      model.6.m.3.0.attn.qkv.conv.weight              Conv2d     False      196608    [768, 256, 1, 1] -9.68e-06   0.00449        float32
  162        model.6.m.3.0.attn.qkv.bn.weight         BatchNorm2d     False         768               [768]     0.996     0.147        float32
  162          model.6.m.3.0.attn.qkv.bn.bias         BatchNorm2d     False         768               [768] -0.000905    0.0587        float32
  163              model.6.m.3.0.attn.qkv.act            Identity     False           0                  []         -         -              -
  164     model.6.m.3.0.attn.proj.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]  4.64e-06   0.00499        float32
  165       model.6.m.3.0.attn.proj.bn.weight         BatchNorm2d     False         256               [256]     0.956    0.0574        float32
  165         model.6.m.3.0.attn.proj.bn.bias         BatchNorm2d     False         256               [256] -6.48e-07  5.46e-06        float32
  166             model.6.m.3.0.attn.proj.act            Identity     False           0                  []         -         -              -
  167       model.6.m.3.0.attn.pe.conv.weight              Conv2d     False       12544      [256, 1, 7, 7]  -0.00159   0.00587        float32
  167         model.6.m.3.0.attn.pe.conv.bias              Conv2d     False         256               [256] -3.93e-07  1.54e-05        float32
  168         model.6.m.3.0.attn.pe.bn.weight         BatchNorm2d     False         256               [256]     0.776     0.133        float32
  168           model.6.m.3.0.attn.pe.bn.bias         BatchNorm2d     False         256               [256]   1.6e-07  2.91e-06        float32
  169               model.6.m.3.0.attn.pe.act            Identity     False           0                  []         -         -              -
  170         model.6.m.3.0.mlp.0.conv.weight              Conv2d     False       78592    [307, 256, 1, 1] -0.000211   0.00527        float32
  171           model.6.m.3.0.mlp.0.bn.weight         BatchNorm2d     False         307               [307]      1.09    0.0733        float32
  171             model.6.m.3.0.mlp.0.bn.bias         BatchNorm2d     False         307               [307]    -0.356     0.115        float32
  172         model.6.m.3.0.mlp.1.conv.weight              Conv2d     False       78592    [256, 307, 1, 1]  3.67e-05   0.00448        float32
  173           model.6.m.3.0.mlp.1.bn.weight         BatchNorm2d     False         256               [256]      1.02    0.0468        float32
  173             model.6.m.3.0.mlp.1.bn.bias         BatchNorm2d     False         256               [256]  -5.9e-07  4.92e-06        float32
  174                 model.6.m.3.0.mlp.1.act            Identity     False           0                  []         -         -              -
  175      model.6.m.3.1.attn.qkv.conv.weight              Conv2d     False      196608    [768, 256, 1, 1] -1.43e-06    0.0049        float32
  176        model.6.m.3.1.attn.qkv.bn.weight         BatchNorm2d     False         768               [768]      1.03     0.152        float32
  176          model.6.m.3.1.attn.qkv.bn.bias         BatchNorm2d     False         768               [768] -0.000359     0.118        float32
  177              model.6.m.3.1.attn.qkv.act            Identity     False           0                  []         -         -              -
  178     model.6.m.3.1.attn.proj.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -4.47e-05   0.00488        float32
  179       model.6.m.3.1.attn.proj.bn.weight         BatchNorm2d     False         256               [256]       1.3    0.0453        float32
  179         model.6.m.3.1.attn.proj.bn.bias         BatchNorm2d     False         256               [256]  7.08e-08   1.8e-06        float32
  180             model.6.m.3.1.attn.proj.act            Identity     False           0                  []         -         -              -
  181       model.6.m.3.1.attn.pe.conv.weight              Conv2d     False       12544      [256, 1, 7, 7]  -0.00193   0.00679        float32
  181         model.6.m.3.1.attn.pe.conv.bias              Conv2d     False         256               [256]  3.56e-07  1.42e-05        float32
  182         model.6.m.3.1.attn.pe.bn.weight         BatchNorm2d     False         256               [256]     0.807     0.153        float32
  182           model.6.m.3.1.attn.pe.bn.bias         BatchNorm2d     False         256               [256] -1.95e-07  3.22e-06        float32
  183               model.6.m.3.1.attn.pe.act            Identity     False           0                  []         -         -              -
  184         model.6.m.3.1.mlp.0.conv.weight              Conv2d     False       78592    [307, 256, 1, 1] -0.000124   0.00482        float32
  185           model.6.m.3.1.mlp.0.bn.weight         BatchNorm2d     False         307               [307]      1.09    0.0501        float32
  185             model.6.m.3.1.mlp.0.bn.bias         BatchNorm2d     False         307               [307]    -0.348     0.105        float32
  186         model.6.m.3.1.mlp.1.conv.weight              Conv2d     False       78592    [256, 307, 1, 1]  2.88e-05   0.00393        float32
  187           model.6.m.3.1.mlp.1.bn.weight         BatchNorm2d     False         256               [256]      1.08     0.042        float32
  187             model.6.m.3.1.mlp.1.bn.bias         BatchNorm2d     False         256               [256] -4.19e-08  1.08e-06        float32
  188                 model.6.m.3.1.mlp.1.act            Identity     False           0                  []         -         -              -
  189                     model.7.conv.weight              Conv2d     False  2.3593e+06    [512, 512, 3, 3] -9.46e-05   0.00203        float32
  190                       model.7.bn.weight         BatchNorm2d     False         512               [512]     0.629     0.146        float32
  190                         model.7.bn.bias         BatchNorm2d     False         512               [512]    -0.914     0.257        float32
  191                 model.8.cv1.conv.weight              Conv2d     False      131072    [256, 512, 1, 1] -0.000477   0.00546        float32
  192                   model.8.cv1.bn.weight         BatchNorm2d     False         256               [256]       1.5     0.187        float32
  192                     model.8.cv1.bn.bias         BatchNorm2d     False         256               [256]   -0.0135     0.103        float32
  193                 model.8.cv2.conv.weight              Conv2d     False      655360   [512, 1280, 1, 1] -4.39e-05   0.00195        float32
  194                   model.8.cv2.bn.weight         BatchNorm2d     False         512               [512]      1.27     0.219        float32
  194                     model.8.cv2.bn.bias         BatchNorm2d     False         512               [512]      0.07     0.118        float32
  195      model.8.m.0.0.attn.qkv.conv.weight              Conv2d     False      196608    [768, 256, 1, 1] -2.27e-06   0.00231        float32
  196        model.8.m.0.0.attn.qkv.bn.weight         BatchNorm2d     False         768               [768]     0.985    0.0616        float32
  196          model.8.m.0.0.attn.qkv.bn.bias         BatchNorm2d     False         768               [768]  -0.00206    0.0763        float32
  197              model.8.m.0.0.attn.qkv.act            Identity     False           0                  []         -         -              -
  198     model.8.m.0.0.attn.proj.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -1.46e-05   0.00346        float32
  199       model.8.m.0.0.attn.proj.bn.weight         BatchNorm2d     False         256               [256]     0.893      0.06        float32
  199         model.8.m.0.0.attn.proj.bn.bias         BatchNorm2d     False         256               [256]  -6.9e-07  1.01e-05        float32
  200             model.8.m.0.0.attn.proj.act            Identity     False           0                  []         -         -              -
  201       model.8.m.0.0.attn.pe.conv.weight              Conv2d     False       12544      [256, 1, 7, 7] -0.000559   0.00429        float32
  201         model.8.m.0.0.attn.pe.conv.bias              Conv2d     False         256               [256]  7.17e-07  1.45e-05        float32
  202         model.8.m.0.0.attn.pe.bn.weight         BatchNorm2d     False         256               [256]     0.961     0.099        float32
  202           model.8.m.0.0.attn.pe.bn.bias         BatchNorm2d     False         256               [256] -2.03e-08  2.05e-06        float32
  203               model.8.m.0.0.attn.pe.act            Identity     False           0                  []         -         -              -
  204         model.8.m.0.0.mlp.0.conv.weight              Conv2d     False       78592    [307, 256, 1, 1] -0.000362   0.00301        float32
  205           model.8.m.0.0.mlp.0.bn.weight         BatchNorm2d     False         307               [307]      1.06    0.0381        float32
  205             model.8.m.0.0.mlp.0.bn.bias         BatchNorm2d     False         307               [307]   -0.0936    0.0446        float32
  206         model.8.m.0.0.mlp.1.conv.weight              Conv2d     False       78592    [256, 307, 1, 1]  0.000157   0.00277        float32
  207           model.8.m.0.0.mlp.1.bn.weight         BatchNorm2d     False         256               [256]     0.951     0.035        float32
  207             model.8.m.0.0.mlp.1.bn.bias         BatchNorm2d     False         256               [256] -6.41e-07  8.62e-06        float32
  208                 model.8.m.0.0.mlp.1.act            Identity     False           0                  []         -         -              -
  209      model.8.m.0.1.attn.qkv.conv.weight              Conv2d     False      196608    [768, 256, 1, 1]  7.79e-06   0.00232        float32
  210        model.8.m.0.1.attn.qkv.bn.weight         BatchNorm2d     False         768               [768]      1.03    0.0455        float32
  210          model.8.m.0.1.attn.qkv.bn.bias         BatchNorm2d     False         768               [768]   0.00161     0.062        float32
  211              model.8.m.0.1.attn.qkv.act            Identity     False           0                  []         -         -              -
  212     model.8.m.0.1.attn.proj.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -2.88e-07   0.00276        float32
  213       model.8.m.0.1.attn.proj.bn.weight         BatchNorm2d     False         256               [256]     0.932    0.0401        float32
  213         model.8.m.0.1.attn.proj.bn.bias         BatchNorm2d     False         256               [256] -3.54e-07  8.37e-06        float32
  214             model.8.m.0.1.attn.proj.act            Identity     False           0                  []         -         -              -
  215       model.8.m.0.1.attn.pe.conv.weight              Conv2d     False       12544      [256, 1, 7, 7] -0.000863   0.00356        float32
  215         model.8.m.0.1.attn.pe.conv.bias              Conv2d     False         256               [256] -1.03e-07  1.52e-05        float32
  216         model.8.m.0.1.attn.pe.bn.weight         BatchNorm2d     False         256               [256]     0.923    0.0653        float32
  216           model.8.m.0.1.attn.pe.bn.bias         BatchNorm2d     False         256               [256]  1.99e-07  1.65e-06        float32
  217               model.8.m.0.1.attn.pe.act            Identity     False           0                  []         -         -              -
  218         model.8.m.0.1.mlp.0.conv.weight              Conv2d     False       78592    [307, 256, 1, 1] -0.000197   0.00271        float32
  219           model.8.m.0.1.mlp.0.bn.weight         BatchNorm2d     False         307               [307]      1.07    0.0967        float32
  219             model.8.m.0.1.mlp.0.bn.bias         BatchNorm2d     False         307               [307]    -0.103    0.0765        float32
  220         model.8.m.0.1.mlp.1.conv.weight              Conv2d     False       78592    [256, 307, 1, 1]  5.34e-05   0.00251        float32
  221           model.8.m.0.1.mlp.1.bn.weight         BatchNorm2d     False         256               [256]     0.947    0.0364        float32
  221             model.8.m.0.1.mlp.1.bn.bias         BatchNorm2d     False         256               [256] -4.02e-07  8.08e-06        float32
  222                 model.8.m.0.1.mlp.1.act            Identity     False           0                  []         -         -              -
  223      model.8.m.1.0.attn.qkv.conv.weight              Conv2d     False      196608    [768, 256, 1, 1] -1.16e-05   0.00274        float32
  224        model.8.m.1.0.attn.qkv.bn.weight         BatchNorm2d     False         768               [768]      1.06    0.0889        float32
  224          model.8.m.1.0.attn.qkv.bn.bias         BatchNorm2d     False         768               [768]  0.000749     0.068        float32
  225              model.8.m.1.0.attn.qkv.act            Identity     False           0                  []         -         -              -
  226     model.8.m.1.0.attn.proj.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]  1.38e-07   0.00298        float32
  227       model.8.m.1.0.attn.proj.bn.weight         BatchNorm2d     False         256               [256]     0.957    0.0275        float32
  227         model.8.m.1.0.attn.proj.bn.bias         BatchNorm2d     False         256               [256] -7.52e-08  5.35e-06        float32
  228             model.8.m.1.0.attn.proj.act            Identity     False           0                  []         -         -              -
  229       model.8.m.1.0.attn.pe.conv.weight              Conv2d     False       12544      [256, 1, 7, 7]  -0.00103    0.0035        float32
  229         model.8.m.1.0.attn.pe.conv.bias              Conv2d     False         256               [256] -3.52e-07   1.4e-05        float32
  230         model.8.m.1.0.attn.pe.bn.weight         BatchNorm2d     False         256               [256]     0.856     0.109        float32
  230           model.8.m.1.0.attn.pe.bn.bias         BatchNorm2d     False         256               [256]  3.49e-08   2.2e-06        float32
  231               model.8.m.1.0.attn.pe.act            Identity     False           0                  []         -         -              -
  232         model.8.m.1.0.mlp.0.conv.weight              Conv2d     False       78592    [307, 256, 1, 1] -0.000216   0.00296        float32
  233           model.8.m.1.0.mlp.0.bn.weight         BatchNorm2d     False         307               [307]      1.05    0.0498        float32
  233             model.8.m.1.0.mlp.0.bn.bias         BatchNorm2d     False         307               [307]   -0.0749    0.0409        float32
  234         model.8.m.1.0.mlp.1.conv.weight              Conv2d     False       78592    [256, 307, 1, 1]  8.56e-05   0.00274        float32
  235           model.8.m.1.0.mlp.1.bn.weight         BatchNorm2d     False         256               [256]     0.952    0.0251        float32
  235             model.8.m.1.0.mlp.1.bn.bias         BatchNorm2d     False         256               [256] -2.23e-07  4.68e-06        float32
  236                 model.8.m.1.0.mlp.1.act            Identity     False           0                  []         -         -              -
  237      model.8.m.1.1.attn.qkv.conv.weight              Conv2d     False      196608    [768, 256, 1, 1] -8.97e-07   0.00351        float32
  238        model.8.m.1.1.attn.qkv.bn.weight         BatchNorm2d     False         768               [768]      1.05     0.079        float32
  238          model.8.m.1.1.attn.qkv.bn.bias         BatchNorm2d     False         768               [768]  -0.00316    0.0631        float32
  239              model.8.m.1.1.attn.qkv.act            Identity     False           0                  []         -         -              -
  240     model.8.m.1.1.attn.proj.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]  8.18e-06   0.00404        float32
  241       model.8.m.1.1.attn.proj.bn.weight         BatchNorm2d     False         256               [256]      1.02     0.031        float32
  241         model.8.m.1.1.attn.proj.bn.bias         BatchNorm2d     False         256               [256]  7.45e-08  3.24e-06        float32
  242             model.8.m.1.1.attn.proj.act            Identity     False           0                  []         -         -              -
  243       model.8.m.1.1.attn.pe.conv.weight              Conv2d     False       12544      [256, 1, 7, 7]  -0.00129   0.00472        float32
  243         model.8.m.1.1.attn.pe.conv.bias              Conv2d     False         256               [256]  7.29e-07   1.4e-05        float32
  244         model.8.m.1.1.attn.pe.bn.weight         BatchNorm2d     False         256               [256]     0.819    0.0842        float32
  244           model.8.m.1.1.attn.pe.bn.bias         BatchNorm2d     False         256               [256] -2.49e-08  2.12e-06        float32
  245               model.8.m.1.1.attn.pe.act            Identity     False           0                  []         -         -              -
  246         model.8.m.1.1.mlp.0.conv.weight              Conv2d     False       78592    [307, 256, 1, 1] -6.75e-05   0.00328        float32
  247           model.8.m.1.1.mlp.0.bn.weight         BatchNorm2d     False         307               [307]      1.04    0.0312        float32
  247             model.8.m.1.1.mlp.0.bn.bias         BatchNorm2d     False         307               [307]   -0.0618    0.0231        float32
  248         model.8.m.1.1.mlp.1.conv.weight              Conv2d     False       78592    [256, 307, 1, 1]     1e-05   0.00299        float32
  249           model.8.m.1.1.mlp.1.bn.weight         BatchNorm2d     False         256               [256]      0.98     0.016        float32
  249             model.8.m.1.1.mlp.1.bn.bias         BatchNorm2d     False         256               [256] -8.61e-09  2.97e-06        float32
  250                 model.8.m.1.1.mlp.1.act            Identity     False           0                  []         -         -              -
  251      model.8.m.2.0.attn.qkv.conv.weight              Conv2d     False      196608    [768, 256, 1, 1] -3.96e-06   0.00162        float32
  252        model.8.m.2.0.attn.qkv.bn.weight         BatchNorm2d     False         768               [768]     0.999   0.00462        float32
  252          model.8.m.2.0.attn.qkv.bn.bias         BatchNorm2d     False         768               [768]  0.000138   0.00634        float32
  253              model.8.m.2.0.attn.qkv.act            Identity     False           0                  []         -         -              -
  254     model.8.m.2.0.attn.proj.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -5.58e-06   0.00179        float32
  255       model.8.m.2.0.attn.proj.bn.weight         BatchNorm2d     False         256               [256]     0.982   0.00962        float32
  255         model.8.m.2.0.attn.proj.bn.bias         BatchNorm2d     False         256               [256]  1.95e-07  2.23e-06        float32
  256             model.8.m.2.0.attn.proj.act            Identity     False           0                  []         -         -              -
  257       model.8.m.2.0.attn.pe.conv.weight              Conv2d     False       12544      [256, 1, 7, 7] -0.000346   0.00219        float32
  257         model.8.m.2.0.attn.pe.conv.bias              Conv2d     False         256               [256] -1.26e-07  7.11e-06        float32
  258         model.8.m.2.0.attn.pe.bn.weight         BatchNorm2d     False         256               [256]     0.995    0.0241        float32
  258           model.8.m.2.0.attn.pe.bn.bias         BatchNorm2d     False         256               [256] -2.26e-08  2.39e-07        float32
  259               model.8.m.2.0.attn.pe.act            Identity     False           0                  []         -         -              -
  260         model.8.m.2.0.mlp.0.conv.weight              Conv2d     False       78592    [307, 256, 1, 1] -6.91e-05    0.0027        float32
  261           model.8.m.2.0.mlp.0.bn.weight         BatchNorm2d     False         307               [307]      1.02    0.0188        float32
  261             model.8.m.2.0.mlp.0.bn.bias         BatchNorm2d     False         307               [307]   -0.0381    0.0145        float32
  262         model.8.m.2.0.mlp.1.conv.weight              Conv2d     False       78592    [256, 307, 1, 1]  3.13e-05   0.00239        float32
  263           model.8.m.2.0.mlp.1.bn.weight         BatchNorm2d     False         256               [256]     0.993    0.0111        float32
  263             model.8.m.2.0.mlp.1.bn.bias         BatchNorm2d     False         256               [256]  1.59e-07  1.87e-06        float32
  264                 model.8.m.2.0.mlp.1.act            Identity     False           0                  []         -         -              -
  265      model.8.m.2.1.attn.qkv.conv.weight              Conv2d     False      196608    [768, 256, 1, 1]  4.44e-06   0.00226        float32
  266        model.8.m.2.1.attn.qkv.bn.weight         BatchNorm2d     False         768               [768]      1.01    0.0222        float32
  266          model.8.m.2.1.attn.qkv.bn.bias         BatchNorm2d     False         768               [768]  0.000609    0.0158        float32
  267              model.8.m.2.1.attn.qkv.act            Identity     False           0                  []         -         -              -
  268     model.8.m.2.1.attn.proj.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]  4.37e-08   0.00237        float32
  269       model.8.m.2.1.attn.proj.bn.weight         BatchNorm2d     False         256               [256]     0.982   0.00935        float32
  269         model.8.m.2.1.attn.proj.bn.bias         BatchNorm2d     False         256               [256]  1.49e-07  1.54e-06        float32
  270             model.8.m.2.1.attn.proj.act            Identity     False           0                  []         -         -              -
  271       model.8.m.2.1.attn.pe.conv.weight              Conv2d     False       12544      [256, 1, 7, 7] -0.000571   0.00301        float32
  271         model.8.m.2.1.attn.pe.conv.bias              Conv2d     False         256               [256]   2.7e-07  8.83e-06        float32
  272         model.8.m.2.1.attn.pe.bn.weight         BatchNorm2d     False         256               [256]     0.966    0.0359        float32
  272           model.8.m.2.1.attn.pe.bn.bias         BatchNorm2d     False         256               [256]  2.93e-08  3.57e-07        float32
  273               model.8.m.2.1.attn.pe.act            Identity     False           0                  []         -         -              -
  274         model.8.m.2.1.mlp.0.conv.weight              Conv2d     False       78592    [307, 256, 1, 1] -1.93e-05   0.00257        float32
  275           model.8.m.2.1.mlp.0.bn.weight         BatchNorm2d     False         307               [307]      1.02     0.018        float32
  275             model.8.m.2.1.mlp.0.bn.bias         BatchNorm2d     False         307               [307]   -0.0417    0.0201        float32
  276         model.8.m.2.1.mlp.1.conv.weight              Conv2d     False       78592    [256, 307, 1, 1]  1.62e-05   0.00213        float32
  277           model.8.m.2.1.mlp.1.bn.weight         BatchNorm2d     False         256               [256]     0.993    0.0108        float32
  277             model.8.m.2.1.mlp.1.bn.bias         BatchNorm2d     False         256               [256]  1.15e-07  1.26e-06        float32
  278                 model.8.m.2.1.mlp.1.act            Identity     False           0                  []         -         -              -
  279      model.8.m.3.0.attn.qkv.conv.weight              Conv2d     False      196608    [768, 256, 1, 1]  3.03e-06   0.00181        float32
  280        model.8.m.3.0.attn.qkv.bn.weight         BatchNorm2d     False         768               [768]     0.997   0.00392        float32
  280          model.8.m.3.0.attn.qkv.bn.bias         BatchNorm2d     False         768               [768]  8.12e-05   0.00446        float32
  281              model.8.m.3.0.attn.qkv.act            Identity     False           0                  []         -         -              -
  282     model.8.m.3.0.attn.proj.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -6.97e-06   0.00201        float32
  283       model.8.m.3.0.attn.proj.bn.weight         BatchNorm2d     False         256               [256]      0.99   0.00501        float32
  283         model.8.m.3.0.attn.proj.bn.bias         BatchNorm2d     False         256               [256]  2.03e-08  6.37e-07        float32
  284             model.8.m.3.0.attn.proj.act            Identity     False           0                  []         -         -              -
  285       model.8.m.3.0.attn.pe.conv.weight              Conv2d     False       12544      [256, 1, 7, 7] -0.000474   0.00255        float32
  285         model.8.m.3.0.attn.pe.conv.bias              Conv2d     False         256               [256]  8.61e-08  7.66e-06        float32
  286         model.8.m.3.0.attn.pe.bn.weight         BatchNorm2d     False         256               [256]         1    0.0134        float32
  286           model.8.m.3.0.attn.pe.bn.bias         BatchNorm2d     False         256               [256]  1.21e-08  1.58e-07        float32
  287               model.8.m.3.0.attn.pe.act            Identity     False           0                  []         -         -              -
  288         model.8.m.3.0.mlp.0.conv.weight              Conv2d     False       78592    [307, 256, 1, 1] -2.14e-05   0.00198        float32
  289           model.8.m.3.0.mlp.0.bn.weight         BatchNorm2d     False         307               [307]      1.01   0.00799        float32
  289             model.8.m.3.0.mlp.0.bn.bias         BatchNorm2d     False         307               [307]   -0.0255     0.012        float32
  290         model.8.m.3.0.mlp.1.conv.weight              Conv2d     False       78592    [256, 307, 1, 1] -4.37e-06   0.00167        float32
  291           model.8.m.3.0.mlp.1.bn.weight         BatchNorm2d     False         256               [256]         1   0.00478        float32
  291             model.8.m.3.0.mlp.1.bn.bias         BatchNorm2d     False         256               [256]  4.38e-08  4.66e-07        float32
  292                 model.8.m.3.0.mlp.1.act            Identity     False           0                  []         -         -              -
  293      model.8.m.3.1.attn.qkv.conv.weight              Conv2d     False      196608    [768, 256, 1, 1] -4.59e-07   0.00165        float32
  294        model.8.m.3.1.attn.qkv.bn.weight         BatchNorm2d     False         768               [768]     0.997   0.00257        float32
  294          model.8.m.3.1.attn.qkv.bn.bias         BatchNorm2d     False         768               [768] -2.66e-05    0.0048        float32
  295              model.8.m.3.1.attn.qkv.act            Identity     False           0                  []         -         -              -
  296     model.8.m.3.1.attn.proj.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]  7.45e-06   0.00173        float32
  297       model.8.m.3.1.attn.proj.bn.weight         BatchNorm2d     False         256               [256]     0.995   0.00392        float32
  297         model.8.m.3.1.attn.proj.bn.bias         BatchNorm2d     False         256               [256]  2.93e-08  2.92e-07        float32
  298             model.8.m.3.1.attn.proj.act            Identity     False           0                  []         -         -              -
  299       model.8.m.3.1.attn.pe.conv.weight              Conv2d     False       12544      [256, 1, 7, 7] -0.000391   0.00269        float32
  299         model.8.m.3.1.attn.pe.conv.bias              Conv2d     False         256               [256] -2.26e-07  6.19e-06        float32
  300         model.8.m.3.1.attn.pe.bn.weight         BatchNorm2d     False         256               [256]         1    0.0129        float32
  300           model.8.m.3.1.attn.pe.bn.bias         BatchNorm2d     False         256               [256]         0  1.38e-07        float32
  301               model.8.m.3.1.attn.pe.act            Identity     False           0                  []         -         -              -
  302         model.8.m.3.1.mlp.0.conv.weight              Conv2d     False       78592    [307, 256, 1, 1] -5.34e-05    0.0022        float32
  303           model.8.m.3.1.mlp.0.bn.weight         BatchNorm2d     False         307               [307]      1.01   0.00768        float32
  303             model.8.m.3.1.mlp.0.bn.bias         BatchNorm2d     False         307               [307]   -0.0295    0.0147        float32
  304         model.8.m.3.1.mlp.1.conv.weight              Conv2d     False       78592    [256, 307, 1, 1]  2.28e-05   0.00174        float32
  305           model.8.m.3.1.mlp.1.bn.weight         BatchNorm2d     False         256               [256]         1    0.0036        float32
  305             model.8.m.3.1.mlp.1.bn.bias         BatchNorm2d     False         256               [256]  9.55e-09  1.24e-07        float32
  306                 model.8.m.3.1.mlp.1.act            Identity     False           0                  []         -         -              -
  307                                 model.9            Upsample     False           0                  []         -         -              -
  308                                model.10              Concat     False           0                  []         -         -              -
  309                model.11.cv1.conv.weight              Conv2d     False      262144   [256, 1024, 1, 1] -0.000195   0.00604        float32
  310                  model.11.cv1.bn.weight         BatchNorm2d     False         256               [256]      1.12      0.18        float32
  310                    model.11.cv1.bn.bias         BatchNorm2d     False         256               [256]    -0.375     0.354        float32
  311                model.11.cv2.conv.weight              Conv2d     False      393216    [512, 768, 1, 1] -0.000398   0.00492        float32
  312                  model.11.cv2.bn.weight         BatchNorm2d     False         512               [512]     0.964     0.158        float32
  312                    model.11.cv2.bn.bias         BatchNorm2d     False         512               [512]    -0.448     0.172        float32
  313            model.11.m.0.cv1.conv.weight              Conv2d     False       32768    [128, 256, 1, 1] -0.000436   0.00774        float32
  314              model.11.m.0.cv1.bn.weight         BatchNorm2d     False         128               [128]     0.777     0.109        float32
  314                model.11.m.0.cv1.bn.bias         BatchNorm2d     False         128               [128]    -0.333     0.362        float32
  315            model.11.m.0.cv2.conv.weight              Conv2d     False       32768    [128, 256, 1, 1] -0.000767   0.00767        float32
  316              model.11.m.0.cv2.bn.weight         BatchNorm2d     False         128               [128]      1.28     0.114        float32
  316                model.11.m.0.cv2.bn.bias         BatchNorm2d     False         128               [128]    -0.372     0.122        float32
  317            model.11.m.0.cv3.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -0.000843   0.00736        float32
  318              model.11.m.0.cv3.bn.weight         BatchNorm2d     False         256               [256]     0.938     0.114        float32
  318                model.11.m.0.cv3.bn.bias         BatchNorm2d     False         256               [256]    -0.624     0.222        float32
  319        model.11.m.0.m.0.cv1.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000313   0.00399        float32
  320          model.11.m.0.m.0.cv1.bn.weight         BatchNorm2d     False         128               [128]      1.08    0.0985        float32
  320            model.11.m.0.m.0.cv1.bn.bias         BatchNorm2d     False         128               [128]    -0.633     0.179        float32
  321        model.11.m.0.m.0.cv2.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000249   0.00377        float32
  322          model.11.m.0.m.0.cv2.bn.weight         BatchNorm2d     False         128               [128]     0.913     0.123        float32
  322            model.11.m.0.m.0.cv2.bn.bias         BatchNorm2d     False         128               [128]    -0.283     0.161        float32
  323        model.11.m.0.m.1.cv1.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000263   0.00389        float32
  324          model.11.m.0.m.1.cv1.bn.weight         BatchNorm2d     False         128               [128]      1.06     0.108        float32
  324            model.11.m.0.m.1.cv1.bn.bias         BatchNorm2d     False         128               [128]    -0.662     0.173        float32
  325        model.11.m.0.m.1.cv2.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000174   0.00365        float32
  326          model.11.m.0.m.1.cv2.bn.weight         BatchNorm2d     False         128               [128]      1.12     0.118        float32
  326            model.11.m.0.m.1.cv2.bn.bias         BatchNorm2d     False         128               [128]    -0.184      0.12        float32
  327            model.11.m.1.cv1.conv.weight              Conv2d     False       32768    [128, 256, 1, 1]   -0.0008   0.00691        float32
  328              model.11.m.1.cv1.bn.weight         BatchNorm2d     False         128               [128]     0.827    0.0837        float32
  328                model.11.m.1.cv1.bn.bias         BatchNorm2d     False         128               [128]    -0.394     0.243        float32
  329            model.11.m.1.cv2.conv.weight              Conv2d     False       32768    [128, 256, 1, 1]  -0.00052    0.0049        float32
  330              model.11.m.1.cv2.bn.weight         BatchNorm2d     False         128               [128]      1.16    0.0351        float32
  330                model.11.m.1.cv2.bn.bias         BatchNorm2d     False         128               [128]    -0.138    0.0348        float32
  331            model.11.m.1.cv3.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -0.000616   0.00557        float32
  332              model.11.m.1.cv3.bn.weight         BatchNorm2d     False         256               [256]     0.978    0.0862        float32
  332                model.11.m.1.cv3.bn.bias         BatchNorm2d     False         256               [256]    -0.439     0.114        float32
  333        model.11.m.1.m.0.cv1.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000348   0.00369        float32
  334          model.11.m.1.m.0.cv1.bn.weight         BatchNorm2d     False         128               [128]      1.08     0.089        float32
  334            model.11.m.1.m.0.cv1.bn.bias         BatchNorm2d     False         128               [128]     -0.52     0.173        float32
  335        model.11.m.1.m.0.cv2.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000216   0.00351        float32
  336          model.11.m.1.m.0.cv2.bn.weight         BatchNorm2d     False         128               [128]     0.965    0.0996        float32
  336            model.11.m.1.m.0.cv2.bn.bias         BatchNorm2d     False         128               [128]    -0.184     0.149        float32
  337        model.11.m.1.m.1.cv1.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000285   0.00352        float32
  338          model.11.m.1.m.1.cv1.bn.weight         BatchNorm2d     False         128               [128]      1.05    0.0902        float32
  338            model.11.m.1.m.1.cv1.bn.bias         BatchNorm2d     False         128               [128]    -0.464     0.135        float32
  339        model.11.m.1.m.1.cv2.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000114   0.00333        float32
  340          model.11.m.1.m.1.cv2.bn.weight         BatchNorm2d     False         128               [128]      1.16    0.0955        float32
  340            model.11.m.1.m.1.cv2.bn.bias         BatchNorm2d     False         128               [128]   -0.0482     0.094        float32
  341                                model.12            Upsample     False           0                  []         -         -              -
  342                                model.13              Concat     False           0                  []         -         -              -
  343                model.14.cv1.conv.weight              Conv2d     False      131072   [128, 1024, 1, 1] -0.000235   0.00712        float32
  344                  model.14.cv1.bn.weight         BatchNorm2d     False         128               [128]     0.616       0.2        float32
  344                    model.14.cv1.bn.bias         BatchNorm2d     False         128               [128]    0.0681     0.734        float32
  345                model.14.cv2.conv.weight              Conv2d     False       98304    [256, 384, 1, 1] -0.000215    0.0056        float32
  346                  model.14.cv2.bn.weight         BatchNorm2d     False         256               [256]     0.912     0.284        float32
  346                    model.14.cv2.bn.bias         BatchNorm2d     False         256               [256]     -0.28     0.272        float32
  347            model.14.m.0.cv1.conv.weight              Conv2d     False        8192     [64, 128, 1, 1] -0.000495     0.014        float32
  348              model.14.m.0.cv1.bn.weight         BatchNorm2d     False          64                [64]     0.394     0.118        float32
  348                model.14.m.0.cv1.bn.bias         BatchNorm2d     False          64                [64]    0.0757     0.625        float32
  349            model.14.m.0.cv2.conv.weight              Conv2d     False        8192     [64, 128, 1, 1]  0.000255   0.00937        float32
  350              model.14.m.0.cv2.bn.weight         BatchNorm2d     False          64                [64]      1.04     0.123        float32
  350                model.14.m.0.cv2.bn.bias         BatchNorm2d     False          64                [64]   -0.0874     0.148        float32
  351            model.14.m.0.cv3.conv.weight              Conv2d     False       16384    [128, 128, 1, 1] -0.000896    0.0102        float32
  352              model.14.m.0.cv3.bn.weight         BatchNorm2d     False         128               [128]     0.713     0.131        float32
  352                model.14.m.0.cv3.bn.bias         BatchNorm2d     False         128               [128]    -0.355     0.283        float32
  353        model.14.m.0.m.0.cv1.conv.weight              Conv2d     False       36864      [64, 64, 3, 3]  -0.00044   0.00787        float32
  354          model.14.m.0.m.0.cv1.bn.weight         BatchNorm2d     False          64                [64]     0.853    0.0992        float32
  354            model.14.m.0.m.0.cv1.bn.bias         BatchNorm2d     False          64                [64]    -0.548     0.419        float32
  355        model.14.m.0.m.0.cv2.conv.weight              Conv2d     False       36864      [64, 64, 3, 3] -0.000542   0.00722        float32
  356          model.14.m.0.m.0.cv2.bn.weight         BatchNorm2d     False          64                [64]     0.736     0.151        float32
  356            model.14.m.0.m.0.cv2.bn.bias         BatchNorm2d     False          64                [64]    -0.279     0.441        float32
  357        model.14.m.0.m.1.cv1.conv.weight              Conv2d     False       36864      [64, 64, 3, 3] -0.000604    0.0073        float32
  358          model.14.m.0.m.1.cv1.bn.weight         BatchNorm2d     False          64                [64]     0.817      0.11        float32
  358            model.14.m.0.m.1.cv1.bn.bias         BatchNorm2d     False          64                [64]    -0.562     0.345        float32
  359        model.14.m.0.m.1.cv2.conv.weight              Conv2d     False       36864      [64, 64, 3, 3] -0.000191   0.00665        float32
  360          model.14.m.0.m.1.cv2.bn.weight         BatchNorm2d     False          64                [64]      1.15     0.137        float32
  360            model.14.m.0.m.1.cv2.bn.bias         BatchNorm2d     False          64                [64]     0.125     0.309        float32
  361            model.14.m.1.cv1.conv.weight              Conv2d     False        8192     [64, 128, 1, 1]  -0.00125    0.0107        float32
  362              model.14.m.1.cv1.bn.weight         BatchNorm2d     False          64                [64]     0.392     0.109        float32
  362                model.14.m.1.cv1.bn.bias         BatchNorm2d     False          64                [64]    -0.273     0.404        float32
  363            model.14.m.1.cv2.conv.weight              Conv2d     False        8192     [64, 128, 1, 1] -0.000487   0.00574        float32
  364              model.14.m.1.cv2.bn.weight         BatchNorm2d     False          64                [64]      1.12    0.0984        float32
  364                model.14.m.1.cv2.bn.bias         BatchNorm2d     False          64                [64]   -0.0946     0.123        float32
  365            model.14.m.1.cv3.conv.weight              Conv2d     False       16384    [128, 128, 1, 1] -0.000569   0.00784        float32
  366              model.14.m.1.cv3.bn.weight         BatchNorm2d     False         128               [128]     0.967     0.257        float32
  366                model.14.m.1.cv3.bn.bias         BatchNorm2d     False         128               [128]    -0.182     0.311        float32
  367        model.14.m.1.m.0.cv1.conv.weight              Conv2d     False       36864      [64, 64, 3, 3] -0.000321    0.0067        float32
  368          model.14.m.1.m.0.cv1.bn.weight         BatchNorm2d     False          64                [64]     0.896     0.134        float32
  368            model.14.m.1.m.0.cv1.bn.bias         BatchNorm2d     False          64                [64]    -0.484     0.259        float32
  369        model.14.m.1.m.0.cv2.conv.weight              Conv2d     False       36864      [64, 64, 3, 3] -0.000336   0.00644        float32
  370          model.14.m.1.m.0.cv2.bn.weight         BatchNorm2d     False          64                [64]     0.837     0.144        float32
  370            model.14.m.1.m.0.cv2.bn.bias         BatchNorm2d     False          64                [64]    -0.245     0.472        float32
  371        model.14.m.1.m.1.cv1.conv.weight              Conv2d     False       36864      [64, 64, 3, 3] -0.000533   0.00615        float32
  372          model.14.m.1.m.1.cv1.bn.weight         BatchNorm2d     False          64                [64]     0.798     0.161        float32
  372            model.14.m.1.m.1.cv1.bn.bias         BatchNorm2d     False          64                [64]    -0.575     0.323        float32
  373        model.14.m.1.m.1.cv2.conv.weight              Conv2d     False       36864      [64, 64, 3, 3] -6.45e-05   0.00566        float32
  374          model.14.m.1.m.1.cv2.bn.weight         BatchNorm2d     False          64                [64]      1.18     0.254        float32
  374            model.14.m.1.m.1.cv2.bn.bias         BatchNorm2d     False          64                [64]   -0.0459     0.288        float32
  375                    model.15.conv.weight              Conv2d     False      589824    [256, 256, 3, 3] -8.55e-05   0.00184        float32
  376                      model.15.bn.weight         BatchNorm2d     False         256               [256]     0.994    0.0755        float32
  376                        model.15.bn.bias         BatchNorm2d     False         256               [256]     -0.18    0.0907        float32
  377                                model.16              Concat     False           0                  []         -         -              -
  378                model.17.cv1.conv.weight              Conv2d     False      196608    [256, 768, 1, 1] -0.000176   0.00411        float32
  379                  model.17.cv1.bn.weight         BatchNorm2d     False         256               [256]      1.14     0.124        float32
  379                    model.17.cv1.bn.bias         BatchNorm2d     False         256               [256]     -0.25     0.121        float32
  380                model.17.cv2.conv.weight              Conv2d     False      393216    [512, 768, 1, 1] -0.000127   0.00268        float32
  381                  model.17.cv2.bn.weight         BatchNorm2d     False         512               [512]      1.05     0.166        float32
  381                    model.17.cv2.bn.bias         BatchNorm2d     False         512               [512]    -0.174      0.11        float32
  382            model.17.m.0.cv1.conv.weight              Conv2d     False       32768    [128, 256, 1, 1] -0.000287    0.0059        float32
  383              model.17.m.0.cv1.bn.weight         BatchNorm2d     False         128               [128]     0.664    0.0848        float32
  383                model.17.m.0.cv1.bn.bias         BatchNorm2d     False         128               [128]    -0.175     0.244        float32
  384            model.17.m.0.cv2.conv.weight              Conv2d     False       32768    [128, 256, 1, 1] -5.04e-05   0.00337        float32
  385              model.17.m.0.cv2.bn.weight         BatchNorm2d     False         128               [128]      1.04    0.0501        float32
  385                model.17.m.0.cv2.bn.bias         BatchNorm2d     False         128               [128]   -0.0837    0.0454        float32
  386            model.17.m.0.cv3.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -0.000301    0.0044        float32
  387              model.17.m.0.cv3.bn.weight         BatchNorm2d     False         256               [256]     0.918    0.0972        float32
  387                model.17.m.0.cv3.bn.bias         BatchNorm2d     False         256               [256]    -0.198     0.123        float32
  388        model.17.m.0.m.0.cv1.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000242   0.00346        float32
  389          model.17.m.0.m.0.cv1.bn.weight         BatchNorm2d     False         128               [128]      1.06      0.12        float32
  389            model.17.m.0.m.0.cv1.bn.bias         BatchNorm2d     False         128               [128]    -0.429     0.205        float32
  390        model.17.m.0.m.0.cv2.conv.weight              Conv2d     False      147456    [128, 128, 3, 3]   -0.0002    0.0033        float32
  391          model.17.m.0.m.0.cv2.bn.weight         BatchNorm2d     False         128               [128]      1.01    0.0821        float32
  391            model.17.m.0.m.0.cv2.bn.bias         BatchNorm2d     False         128               [128]    -0.246     0.151        float32
  392        model.17.m.0.m.1.cv1.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000165   0.00333        float32
  393          model.17.m.0.m.1.cv1.bn.weight         BatchNorm2d     False         128               [128]      1.04    0.0968        float32
  393            model.17.m.0.m.1.cv1.bn.bias         BatchNorm2d     False         128               [128]    -0.369     0.166        float32
  394        model.17.m.0.m.1.cv2.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -8.61e-05   0.00309        float32
  395          model.17.m.0.m.1.cv2.bn.weight         BatchNorm2d     False         128               [128]      1.25     0.116        float32
  395            model.17.m.0.m.1.cv2.bn.bias         BatchNorm2d     False         128               [128]   -0.0778    0.0985        float32
  396            model.17.m.1.cv1.conv.weight              Conv2d     False       32768    [128, 256, 1, 1] -0.000455   0.00487        float32
  397              model.17.m.1.cv1.bn.weight         BatchNorm2d     False         128               [128]     0.746     0.082        float32
  397                model.17.m.1.cv1.bn.bias         BatchNorm2d     False         128               [128]    -0.181     0.146        float32
  398            model.17.m.1.cv2.conv.weight              Conv2d     False       32768    [128, 256, 1, 1] -0.000158   0.00218        float32
  399              model.17.m.1.cv2.bn.weight         BatchNorm2d     False         128               [128]      1.02    0.0287        float32
  399                model.17.m.1.cv2.bn.bias         BatchNorm2d     False         128               [128]   -0.0378    0.0276        float32
  400            model.17.m.1.cv3.conv.weight              Conv2d     False       65536    [256, 256, 1, 1]  -0.00024   0.00345        float32
  401              model.17.m.1.cv3.bn.weight         BatchNorm2d     False         256               [256]       1.1     0.119        float32
  401                model.17.m.1.cv3.bn.bias         BatchNorm2d     False         256               [256]    -0.126    0.0846        float32
  402        model.17.m.1.m.0.cv1.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000237   0.00272        float32
  403          model.17.m.1.m.0.cv1.bn.weight         BatchNorm2d     False         128               [128]      1.04     0.108        float32
  403            model.17.m.1.m.0.cv1.bn.bias         BatchNorm2d     False         128               [128]    -0.245     0.105        float32
  404        model.17.m.1.m.0.cv2.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000182   0.00268        float32
  405          model.17.m.1.m.0.cv2.bn.weight         BatchNorm2d     False         128               [128]     0.988    0.0975        float32
  405            model.17.m.1.m.0.cv2.bn.bias         BatchNorm2d     False         128               [128]    -0.153     0.135        float32
  406        model.17.m.1.m.1.cv1.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000184   0.00276        float32
  407          model.17.m.1.m.1.cv1.bn.weight         BatchNorm2d     False         128               [128]      1.02     0.131        float32
  407            model.17.m.1.m.1.cv1.bn.bias         BatchNorm2d     False         128               [128]    -0.323     0.132        float32
  408        model.17.m.1.m.1.cv2.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -4.97e-05   0.00257        float32
  409          model.17.m.1.m.1.cv2.bn.weight         BatchNorm2d     False         128               [128]      1.23     0.142        float32
  409            model.17.m.1.m.1.cv2.bn.bias         BatchNorm2d     False         128               [128]   -0.0806     0.105        float32
  410                    model.18.conv.weight              Conv2d     False  2.3593e+06    [512, 512, 3, 3] -4.11e-05  0.000744        float32
  411                      model.18.bn.weight         BatchNorm2d     False         512               [512]     0.912    0.0386        float32
  411                        model.18.bn.bias         BatchNorm2d     False         512               [512]     -0.13    0.0548        float32
  412                                model.19              Concat     False           0                  []         -         -              -
  413                model.20.cv1.conv.weight              Conv2d     False      524288   [512, 1024, 1, 1] -5.78e-05   0.00176        float32
  414                  model.20.cv1.bn.weight         BatchNorm2d     False         512               [512]      1.06    0.0708        float32
  414                    model.20.cv1.bn.bias         BatchNorm2d     False         512               [512]   -0.0898    0.0648        float32
  415                model.20.cv2.conv.weight              Conv2d     False      524288   [512, 1024, 1, 1] -4.66e-05   0.00157        float32
  416                  model.20.cv2.bn.weight         BatchNorm2d     False         512               [512]      1.04      0.09        float32
  416                    model.20.cv2.bn.bias         BatchNorm2d     False         512               [512]   -0.0576    0.0447        float32
  417            model.20.m.0.cv1.conv.weight              Conv2d     False       32768    [128, 256, 1, 1] -0.000159   0.00358        float32
  418              model.20.m.0.cv1.bn.weight         BatchNorm2d     False         128               [128]     0.869    0.0564        float32
  418                model.20.m.0.cv1.bn.bias         BatchNorm2d     False         128               [128]    -0.098     0.101        float32
  419            model.20.m.0.cv2.conv.weight              Conv2d     False       32768    [128, 256, 1, 1]  2.32e-05   0.00163        float32
  420              model.20.m.0.cv2.bn.weight         BatchNorm2d     False         128               [128]         1   0.00738        float32
  420                model.20.m.0.cv2.bn.bias         BatchNorm2d     False         128               [128]   -0.0152   0.00938        float32
  421            model.20.m.0.cv3.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -0.000125   0.00265        float32
  422              model.20.m.0.cv3.bn.weight         BatchNorm2d     False         256               [256]     0.983    0.0579        float32
  422                model.20.m.0.cv3.bn.bias         BatchNorm2d     False         256               [256]   -0.0727    0.0443        float32
  423        model.20.m.0.m.0.cv1.conv.weight              Conv2d     False      147456    [128, 128, 3, 3]  -0.00017   0.00213        float32
  424          model.20.m.0.m.0.cv1.bn.weight         BatchNorm2d     False         128               [128]      1.05    0.0679        float32
  424            model.20.m.0.m.0.cv1.bn.bias         BatchNorm2d     False         128               [128]    -0.166    0.0895        float32
  425        model.20.m.0.m.0.cv2.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000106   0.00208        float32
  426          model.20.m.0.m.0.cv2.bn.weight         BatchNorm2d     False         128               [128]      1.06     0.074        float32
  426            model.20.m.0.m.0.cv2.bn.bias         BatchNorm2d     False         128               [128]    -0.104    0.0706        float32
  427        model.20.m.0.m.1.cv1.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000128   0.00205        float32
  428          model.20.m.0.m.1.cv1.bn.weight         BatchNorm2d     False         128               [128]      1.04    0.0554        float32
  428            model.20.m.0.m.1.cv1.bn.bias         BatchNorm2d     False         128               [128]    -0.134    0.0653        float32
  429        model.20.m.0.m.1.cv2.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -5.59e-05   0.00193        float32
  430          model.20.m.0.m.1.cv2.bn.weight         BatchNorm2d     False         128               [128]      1.12     0.057        float32
  430            model.20.m.0.m.1.cv2.bn.bias         BatchNorm2d     False         128               [128]    -0.037    0.0484        float32
  431            model.20.m.1.cv1.conv.weight              Conv2d     False       32768    [128, 256, 1, 1] -0.000244   0.00302        float32
  432              model.20.m.1.cv1.bn.weight         BatchNorm2d     False         128               [128]     0.908    0.0766        float32
  432                model.20.m.1.cv1.bn.bias         BatchNorm2d     False         128               [128]   -0.0798    0.0518        float32
  433            model.20.m.1.cv2.conv.weight              Conv2d     False       32768    [128, 256, 1, 1] -1.94e-05  0.000763        float32
  434              model.20.m.1.cv2.bn.weight         BatchNorm2d     False         128               [128]     0.998   0.00252        float32
  434                model.20.m.1.cv2.bn.bias         BatchNorm2d     False         128               [128]  -0.00351   0.00312        float32
  435            model.20.m.1.cv3.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -0.000158   0.00223        float32
  436              model.20.m.1.cv3.bn.weight         BatchNorm2d     False         256               [256]      1.05    0.0434        float32
  436                model.20.m.1.cv3.bn.bias         BatchNorm2d     False         256               [256]   -0.0307    0.0266        float32
  437        model.20.m.1.m.0.cv1.conv.weight              Conv2d     False      147456    [128, 128, 3, 3]  -0.00013   0.00172        float32
  438          model.20.m.1.m.0.cv1.bn.weight         BatchNorm2d     False         128               [128]      1.03    0.0498        float32
  438            model.20.m.1.m.0.cv1.bn.bias         BatchNorm2d     False         128               [128]   -0.0854    0.0355        float32
  439        model.20.m.1.m.0.cv2.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000104   0.00174        float32
  440          model.20.m.1.m.0.cv2.bn.weight         BatchNorm2d     False         128               [128]      1.04     0.067        float32
  440            model.20.m.1.m.0.cv2.bn.bias         BatchNorm2d     False         128               [128]   -0.0566     0.053        float32
  441        model.20.m.1.m.1.cv1.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -0.000138   0.00174        float32
  442          model.20.m.1.m.1.cv1.bn.weight         BatchNorm2d     False         128               [128]      1.03    0.0567        float32
  442            model.20.m.1.m.1.cv1.bn.bias         BatchNorm2d     False         128               [128]    -0.112    0.0448        float32
  443        model.20.m.1.m.1.cv2.conv.weight              Conv2d     False      147456    [128, 128, 3, 3] -3.31e-05   0.00165        float32
  444          model.20.m.1.m.1.cv2.bn.weight         BatchNorm2d     False         128               [128]      1.12    0.0699        float32
  444            model.20.m.1.m.1.cv2.bn.bias         BatchNorm2d     False         128               [128]    -0.028    0.0375        float32
  445            model.21.cv2.0.0.conv.weight              Conv2d     False      147456     [64, 256, 3, 3]  -0.00012   0.00317        float32
  446              model.21.cv2.0.0.bn.weight         BatchNorm2d     False          64                [64]      0.96     0.358        float32
  446                model.21.cv2.0.0.bn.bias         BatchNorm2d     False          64                [64]     -0.19     0.563        float32
  447            model.21.cv2.0.1.conv.weight              Conv2d     False       36864      [64, 64, 3, 3]  4.75e-05   0.00593        float32
  448              model.21.cv2.0.1.bn.weight         BatchNorm2d     False          64                [64]      2.46     0.682        float32
  448                model.21.cv2.0.1.bn.bias         BatchNorm2d     False          64                [64]     0.574     0.322        float32
  449                 model.21.cv2.0.2.weight              Conv2d     False        4096      [64, 64, 1, 1] -3.29e-06    0.0328        float32
  449                   model.21.cv2.0.2.bias              Conv2d     False          64                [64]     0.996       0.8        float32
  450            model.21.cv2.1.0.conv.weight              Conv2d     False      294912     [64, 512, 3, 3] -6.78e-05   0.00204        float32
  451              model.21.cv2.1.0.bn.weight         BatchNorm2d     False          64                [64]      1.07     0.266        float32
  451                model.21.cv2.1.0.bn.bias         BatchNorm2d     False          64                [64]   -0.0429     0.394        float32
  452            model.21.cv2.1.1.conv.weight              Conv2d     False       36864      [64, 64, 3, 3]  4.68e-05   0.00533        float32
  453              model.21.cv2.1.1.bn.weight         BatchNorm2d     False          64                [64]      2.92     0.439        float32
  453                model.21.cv2.1.1.bn.bias         BatchNorm2d     False          64                [64]     0.763     0.318        float32
  454                 model.21.cv2.1.2.weight              Conv2d     False        4096      [64, 64, 1, 1]  -6.3e-07    0.0343        float32
  454                   model.21.cv2.1.2.bias              Conv2d     False          64                [64]     0.999     0.569        float32
  455            model.21.cv2.2.0.conv.weight              Conv2d     False      294912     [64, 512, 3, 3] -6.54e-05   0.00133        float32
  456              model.21.cv2.2.0.bn.weight         BatchNorm2d     False          64                [64]      1.14     0.122        float32
  456                model.21.cv2.2.0.bn.bias         BatchNorm2d     False          64                [64]    -0.152     0.155        float32
  457            model.21.cv2.2.1.conv.weight              Conv2d     False       36864      [64, 64, 3, 3]  0.000188   0.00366        float32
  458              model.21.cv2.2.1.bn.weight         BatchNorm2d     False          64                [64]       3.1     0.408        float32
  458                model.21.cv2.2.1.bn.bias         BatchNorm2d     False          64                [64]     0.621     0.243        float32
  459                 model.21.cv2.2.2.weight              Conv2d     False        4096      [64, 64, 1, 1] -1.49e-06     0.029        float32
  459                   model.21.cv2.2.2.bias              Conv2d     False          64                [64]     0.998     0.308        float32
  460          model.21.cv3.0.0.0.conv.weight              Conv2d     False        2304      [256, 1, 3, 3]    0.0044    0.0119        float32
  461            model.21.cv3.0.0.0.bn.weight         BatchNorm2d     False         256               [256]     0.975     0.184        float32
  461              model.21.cv3.0.0.0.bn.bias         BatchNorm2d     False         256               [256]     0.162     0.204        float32
  462          model.21.cv3.0.0.1.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -0.000108   0.00462        float32
  463            model.21.cv3.0.0.1.bn.weight         BatchNorm2d     False         256               [256]     0.967     0.118        float32
  463              model.21.cv3.0.0.1.bn.bias         BatchNorm2d     False         256               [256]    0.0928     0.229        float32
  464          model.21.cv3.0.1.0.conv.weight              Conv2d     False        2304      [256, 1, 3, 3]   0.00229    0.0162        float32
  465            model.21.cv3.0.1.0.bn.weight         BatchNorm2d     False         256               [256]     0.994     0.322        float32
  465              model.21.cv3.0.1.0.bn.bias         BatchNorm2d     False         256               [256]      0.15     0.263        float32
  466          model.21.cv3.0.1.1.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -0.000464   0.00526        float32
  467            model.21.cv3.0.1.1.bn.weight         BatchNorm2d     False         256               [256]      1.85      0.24        float32
  467              model.21.cv3.0.1.1.bn.bias         BatchNorm2d     False         256               [256]     0.642     0.804        float32
  468                 model.21.cv3.0.2.weight              Conv2d     False       20480     [80, 256, 1, 1]   -0.0026    0.0182        float32
  468                   model.21.cv3.0.2.bias              Conv2d     False          80                [80]       -12     0.571        float32
  469          model.21.cv3.1.0.0.conv.weight              Conv2d     False        4608      [512, 1, 3, 3]   0.00171   0.00953        float32
  470            model.21.cv3.1.0.0.bn.weight         BatchNorm2d     False         512               [512]      1.01     0.118        float32
  470              model.21.cv3.1.0.0.bn.bias         BatchNorm2d     False         512               [512]    0.0188    0.0966        float32
  471          model.21.cv3.1.0.1.conv.weight              Conv2d     False      131072    [256, 512, 1, 1] -6.43e-05   0.00295        float32
  472            model.21.cv3.1.0.1.bn.weight         BatchNorm2d     False         256               [256]      1.05    0.0891        float32
  472              model.21.cv3.1.0.1.bn.bias         BatchNorm2d     False         256               [256]    0.0292     0.166        float32
  473          model.21.cv3.1.1.0.conv.weight              Conv2d     False        2304      [256, 1, 3, 3]   0.00277    0.0147        float32
  474            model.21.cv3.1.1.0.bn.weight         BatchNorm2d     False         256               [256]      1.07     0.245        float32
  474              model.21.cv3.1.1.0.bn.bias         BatchNorm2d     False         256               [256]    0.0942     0.194        float32
  475          model.21.cv3.1.1.1.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -0.000682   0.00456        float32
  476            model.21.cv3.1.1.1.bn.weight         BatchNorm2d     False         256               [256]      1.97      0.12        float32
  476              model.21.cv3.1.1.1.bn.bias         BatchNorm2d     False         256               [256]     0.726     0.707        float32
  477                 model.21.cv3.1.2.weight              Conv2d     False       20480     [80, 256, 1, 1]   -0.0032    0.0171        float32
  477                   model.21.cv3.1.2.bias              Conv2d     False          80                [80]     -10.8     0.363        float32
  478          model.21.cv3.2.0.0.conv.weight              Conv2d     False        4608      [512, 1, 3, 3]   0.00164   0.00872        float32
  479            model.21.cv3.2.0.0.bn.weight         BatchNorm2d     False         512               [512]      1.02    0.0628        float32
  479              model.21.cv3.2.0.0.bn.bias         BatchNorm2d     False         512               [512]  -0.00346    0.0398        float32
  480          model.21.cv3.2.0.1.conv.weight              Conv2d     False      131072    [256, 512, 1, 1] -8.31e-05    0.0023        float32
  481            model.21.cv3.2.0.1.bn.weight         BatchNorm2d     False         256               [256]      1.05    0.0472        float32
  481              model.21.cv3.2.0.1.bn.bias         BatchNorm2d     False         256               [256]    0.0102    0.0799        float32
  482          model.21.cv3.2.1.0.conv.weight              Conv2d     False        2304      [256, 1, 3, 3]   0.00275    0.0134        float32
  483            model.21.cv3.2.1.0.bn.weight         BatchNorm2d     False         256               [256]       1.1     0.109        float32
  483              model.21.cv3.2.1.0.bn.bias         BatchNorm2d     False         256               [256]    0.0322    0.0875        float32
  484          model.21.cv3.2.1.1.conv.weight              Conv2d     False       65536    [256, 256, 1, 1] -0.000459   0.00369        float32
  485            model.21.cv3.2.1.1.bn.weight         BatchNorm2d     False         256               [256]      1.84    0.0763        float32
  485              model.21.cv3.2.1.1.bn.bias         BatchNorm2d     False         256               [256]     0.809     0.558        float32
  486                 model.21.cv3.2.2.weight              Conv2d     False       20480     [80, 256, 1, 1]  -0.00469    0.0142        float32
  486                   model.21.cv3.2.2.bias              Conv2d     False          80                [80]     -9.55     0.239        float32
  487                model.21.dfl.conv.weight              Conv2d     False          16       [1, 16, 1, 1]       7.5      4.76        float32

YOLOv12l summary: 488 layers, 26,454,880 parameters, 0 gradients, 89.7 GFLOPs (488, 26454880, 0, 89.74952959999999)

Section 4: Training Configuration and Execution

Purpose: This is the computational core of the notebook. It defines the hyperparameters that control how the model learns and executes the training loop.

Key Activities:

  • Hyperparameter Definition: Sets critical parameters such as epochs (100), batch_size (96), and img_size (640).
  • Augmentation Strategy: Configures geometric and color-space augmentations (Mosaic, HSV shifts, Scale) to artificially increase dataset diversity and prevent overfitting.
  • Smart Resume Logic: The script checks if a training run was interrupted. If valid weights exist in Google Drive, it automatically resumes training from the last checkpoint; otherwise, it starts a new session.
  • Training Loop: Calls model.train() to begin the backpropagation process, optimizing the weights to detect the five life stages.
# 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 of the model.
batch_size = 16
# 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 = 15

# The initial step size for the optimizer's weight updates.
learning_rate = 0.01
# The optimization algorithm to be used. 'auto' allows Ultralytics to select a suitable default.
optimizer_type = 'auto'

# Probability of applying the mosaic augmentation, which combines four images into one.
mosaic_probability = 1.0
# 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.0
# 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.0
# The gain for applying random scaling (zoom in/out) to images.
scale_gain = 0.5

# 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(weights_path, '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"{'Image Size':<25} : {img_size}")
print(f"{'Learning Rate':<25} : {learning_rate}")
print("-" * 40)
print("Augmentation Strategy")
print(f"{'Mosaic Probability':<25} : {mosaic_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("-" * 40)
print(f"{'Auto Resume Path':<25} : {auto_resume_path}")
----------------------------------------
Training Configuration
Experiment Name           : run_v1
Epochs                    : 100
Batch Size                : 16
Image Size                : 640
Learning Rate             : 0.01
----------------------------------------
Augmentation Strategy
Mosaic Probability        : 1.0
Hue Fraction              : 0.015
Saturation Fraction       : 0.7
Brightness Fraction       : 0.0
Rotation Degrees          : 0.0
Vertical Flip Prob        : 0.0
Horizontal Flip Prob      : 0.0
----------------------------------------
Auto Resume Path          : /content/drive/MyDrive/Mortiscope Models/YOLO12/weights/last.pt
import time

# 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()

# 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=weights_to_load,
        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,
        device=0,
        patience=patience,
        # Project and Checkpointing
        save=True,
        save_period=1,
        project=project_path,
        name=experiment_name,
        exist_ok=True,
        plots=True,
        # Optimizer Settings
        lr0=learning_rate,
        optimizer=optimizer_type,
        # Augmentation Parameters
        mosaic=mosaic_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
    )

# 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)
Previous run detected.

Auto-resuming from: /content/drive/MyDrive/Mortiscope Models/YOLO12/weights/last.pt

Initializing training loop...
Reloading model object from: /content/drive/MyDrive/Mortiscope Models/YOLO12/weights/last.pt
Ultralytics 8.4.5 🚀 Python-3.12.12 torch-2.9.0+cu126 CUDA:0 (NVIDIA L4, 22693MiB)
engine/trainer: agnostic_nms=False, amp=True, angle=1.0, augment=False, auto_augment=randaugment, batch=16, bgr=0.0, box=7.5, cache=False, cfg=None, classes=None, close_mosaic=10, cls=0.5, compile=False, conf=None, copy_paste=0.0, copy_paste_mode=flip, cos_lr=False, cutmix=0.0, data=/content/data.yaml, degrees=0.0, deterministic=True, device=0, dfl=1.5, dnn=False, dropout=0.0, dynamic=False, embed=None, epochs=100, erasing=0.4, exist_ok=True, fliplr=0.0, flipud=0.0, format=torchscript, fraction=1.0, freeze=None, half=False, hsv_h=0.015, hsv_s=0.7, hsv_v=0.0, imgsz=640, int8=False, iou=0.7, keras=False, kobj=1.0, line_width=None, lr0=0.01, lrf=0.01, mask_ratio=4, max_det=300, mixup=0.0, mode=train, model=/content/drive/MyDrive/Mortiscope Models/YOLO12/weights/last.pt, momentum=0.937, mosaic=1.0, multi_scale=0.0, name=run_v1, nbs=64, nms=False, opset=None, optimize=False, optimizer=auto, overlap_mask=True, patience=15, perspective=0.0, plots=True, pose=12.0, pretrained=True, profile=False, project=/content/drive/MyDrive/Mortiscope Models/YOLO12, rect=False, resume=/content/drive/MyDrive/Mortiscope Models/YOLO12/weights/last.pt, retina_masks=False, rle=1.0, save=True, save_conf=False, save_crop=False, save_dir=/content/drive/MyDrive/Mortiscope Models/YOLO12/run_v1, save_frames=False, save_json=False, save_period=1, save_txt=False, scale=0.5, 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.0, warmup_epochs=3.0, warmup_momentum=0.8, weight_decay=0.0005, workers=8, workspace=None

Downloading https://ultralytics.com/assets/Arial.ttf to '/root/.config/Ultralytics/Arial.ttf': 100% ━━━━━━━━━━━━ 755.1KB 183.5MB/s 0.0s

                   from  n    params  module                                       arguments                     
  0                  -1  1      1856  ultralytics.nn.modules.conv.Conv             [3, 64, 3, 2]                 
  1                  -1  1     73984  ultralytics.nn.modules.conv.Conv             [64, 128, 3, 2]               
  2                  -1  2    173824  ultralytics.nn.modules.block.C3k2            [128, 256, 2, True, 0.25]     
  3                  -1  1    590336  ultralytics.nn.modules.conv.Conv             [256, 256, 3, 2]              
  4                  -1  2    691712  ultralytics.nn.modules.block.C3k2            [256, 512, 2, True, 0.25]     
  5                  -1  1   2360320  ultralytics.nn.modules.conv.Conv             [512, 512, 3, 2]              
  6                  -1  4   4272944  ultralytics.nn.modules.block.A2C2f           [512, 512, 4, True, 4, True, 1.2]
  7                  -1  1   2360320  ultralytics.nn.modules.conv.Conv             [512, 512, 3, 2]              
  8                  -1  4   4272944  ultralytics.nn.modules.block.A2C2f           [512, 512, 4, True, 1, True, 1.2]
  9                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']          
 10             [-1, 6]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 11                  -1  2   2102784  ultralytics.nn.modules.block.A2C2f           [1024, 512, 2, False, -1, True, 1.2]
 12                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']          
 13             [-1, 4]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 14                  -1  2    592640  ultralytics.nn.modules.block.A2C2f           [1024, 256, 2, False, -1, True, 1.2]
 15                  -1  1    590336  ultralytics.nn.modules.conv.Conv             [256, 256, 3, 2]              
 16            [-1, 11]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 17                  -1  2   2037248  ultralytics.nn.modules.block.A2C2f           [768, 512, 2, False, -1, True, 1.2]
 18                  -1  1   2360320  ultralytics.nn.modules.conv.Conv             [512, 512, 3, 2]              
 19             [-1, 8]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 20                  -1  2   2496512  ultralytics.nn.modules.block.C3k2            [1024, 512, 2, True]          
 21        [14, 17, 20]  1   1414879  ultralytics.nn.modules.head.Detect           [5, 16, None, [256, 512, 512]]

YOLOv12l summary: 488 layers, 26,392,959 parameters, 26,392,943 gradients, 89.4 GFLOPs
Transferred 1245/1245 items from pretrained weights
Freezing layer 'model.21.dfl.conv.weight'

AMP: running Automatic Mixed Precision (AMP) checks...
Downloading https://github.com/ultralytics/assets/releases/download/v8.4.0/yolo26n.pt to 'yolo26n.pt': 100% ━━━━━━━━━━━━ 5.3MB 343.9MB/s 0.0s
AMP: checks passed ✅
train: Fast image access ✅ (ping: 0.0±0.0 ms, read: 1545.4±782.7 MB/s, size: 59.0 KB)
train: Scanning /content/Complementary-1/train/labels... 14102 images, 347 backgrounds, 0 corrupt: 100% ━━━━━━━━━━━━ 14102/14102 1.5Kit/s 9.3s
train: New cache created: /content/Complementary-1/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: 483.2±210.2 MB/s, size: 44.3 KB)
val: Scanning /content/Complementary-1/valid/labels... 896 images, 36 backgrounds, 0 corrupt: 100% ━━━━━━━━━━━━ 896/896 1.2Kit/s 0.8s
val: New cache created: /content/Complementary-1/valid/labels.cache
Plotting labels to /content/drive/MyDrive/Mortiscope Models/YOLO12/run_v1/labels.jpg... 
optimizer: 'optimizer=auto' found, ignoring 'lr0=0.01' and 'momentum=0.937' and determining best 'optimizer', 'lr0' and 'momentum' automatically... 
optimizer: MuSGD(lr=0.01, momentum=0.9) with parameter groups 2 weight(decay=0.0), 0 weight(decay=0.0005), 0 bias(decay=0.0)

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

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
      1/100      16.8G      1.623      1.873      1.456         26        640: 100% ━━━━━━━━━━━━ 882/882 1.1s/it 16:37
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.5it/s 19.1s
                   all        896       2844      0.652      0.535       0.63      0.388

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
      2/100      16.6G      1.661      1.541      1.459         38        640: 100% ━━━━━━━━━━━━ 882/882 1.0it/s 14:14
   History Saved: 002_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844      0.548      0.467      0.487      0.247

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
      3/100      17.2G      1.749      1.706      1.535         14        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:23
   History Saved: 003_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.8s
                   all        896       2844      0.847      0.353      0.473      0.285

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
      4/100      16.6G      1.744      1.675      1.557         27        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:07
   History Saved: 004_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844      0.599      0.512      0.563      0.332

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
      5/100      17.1G      1.668      1.488      1.518         27        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 005_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844      0.647       0.55      0.604      0.379

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
      6/100      16.6G      1.614      1.393      1.482         17        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 006_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844      0.753      0.514      0.618      0.395

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
      7/100      17.3G      1.581       1.33      1.459         32        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:09
   History Saved: 007_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844      0.755      0.537      0.647      0.422

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
      8/100      17.5G      1.556      1.278      1.443         39        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:07
   History Saved: 008_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844      0.771      0.526      0.609      0.396

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
      9/100      16.7G      1.535      1.235      1.426         27        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 009_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844      0.669      0.589      0.654      0.431

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     10/100      17.2G      1.509      1.187      1.417         45        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 010_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844      0.703       0.54      0.628      0.418

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     11/100        17G      1.493      1.171      1.398         44        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:09
   History Saved: 011_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844      0.693      0.642      0.691      0.455

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     12/100      17.2G      1.483      1.152      1.384         16        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 012_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844        0.7       0.59       0.66       0.44

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     13/100      16.6G      1.473       1.13      1.387          8        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 013_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844      0.728      0.605       0.68      0.457

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     14/100      16.7G      1.455      1.097      1.368         42        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 014_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844      0.725      0.595      0.683      0.458

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     15/100      17.2G      1.432      1.055      1.352         44        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 015_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844      0.718      0.626      0.678      0.458

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     16/100      16.6G      1.431      1.053      1.344         12        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:07
   History Saved: 016_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844      0.669      0.581      0.637      0.433

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     17/100      16.6G      1.425      1.039      1.347         21        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 017_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844      0.684      0.623      0.675      0.458

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     18/100      16.6G      1.407      1.013      1.332         36        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:07
   History Saved: 018_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844      0.713       0.58      0.652      0.447

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     19/100      17.1G      1.404      1.001      1.335          8        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 019_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844      0.703      0.604      0.668      0.458

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     20/100      16.5G      1.387     0.9783      1.316         13        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 020_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844      0.744      0.596      0.682      0.464

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     21/100      16.6G       1.38     0.9682      1.321         17        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 021_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844      0.705      0.601      0.666      0.459

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     22/100      17.3G      1.368     0.9522       1.31         27        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:07
   History Saved: 022_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844      0.738      0.634      0.696      0.471

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     23/100      17.2G      1.357     0.9417      1.302         14        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 023_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844      0.708       0.63      0.684      0.471

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     24/100      16.7G      1.345     0.9192      1.299         19        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:07
   History Saved: 024_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844      0.696      0.624      0.668      0.459

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     25/100      16.6G      1.346      0.914      1.291         10        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 025_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844      0.727      0.604      0.675      0.465

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     26/100      16.7G      1.336     0.8967      1.293         17        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 026_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844      0.751      0.574       0.67      0.463

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     27/100      16.6G      1.335     0.8935      1.288         28        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:07
   History Saved: 027_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844      0.713      0.621      0.685      0.475

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     28/100      17.3G      1.316     0.8721      1.279         13        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:07
   History Saved: 028_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844        0.7      0.609      0.681      0.475

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     29/100      16.6G      1.312     0.8662      1.273         14        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 029_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844      0.717      0.621      0.692      0.476

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     30/100      16.6G        1.3     0.8575      1.267         11        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:07
   History Saved: 030_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844      0.711      0.626      0.688      0.476

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     31/100      16.6G      1.295     0.8316      1.268         14        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 031_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844      0.709       0.64      0.704      0.486

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     32/100      17.3G       1.28     0.8273      1.261         29        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:08
   History Saved: 032_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844      0.732      0.618      0.687      0.476

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     33/100      17.2G      1.289     0.8248      1.259         16        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:10
   History Saved: 033_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844      0.742      0.618       0.69      0.479

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     34/100      17.2G      1.269     0.8131      1.244          9        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:09
   History Saved: 034_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844      0.701      0.631      0.679      0.474

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     35/100      16.7G      1.267     0.8021      1.248         16        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:10
   History Saved: 035_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844      0.736      0.612      0.682      0.478

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     36/100      17.3G      1.258     0.7927      1.242         34        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:09
   History Saved: 036_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844       0.73      0.628      0.695      0.484

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     37/100      17.2G      1.252     0.7802      1.237         13        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:09
   History Saved: 037_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.5s
                   all        896       2844      0.722       0.65      0.709      0.495

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     38/100      17.1G      1.241     0.7615      1.231         21        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:12
   History Saved: 038_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844      0.731      0.635      0.702      0.489

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     39/100      16.9G      1.221      0.741      1.222         26        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:35
   History Saved: 039_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.5it/s 19.2s
                   all        896       2844      0.744      0.621      0.701      0.493

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     40/100      16.6G      1.223     0.7417      1.224         38        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:19
   History Saved: 040_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844      0.742      0.623      0.695      0.488

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     41/100      17.2G      1.218     0.7394      1.223         14        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:18
   History Saved: 041_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844      0.746      0.633      0.702      0.493

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     42/100      16.7G      1.218     0.7378      1.218         27        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:17
   History Saved: 042_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844      0.732      0.619      0.688      0.483

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     43/100      16.6G      1.207     0.7196      1.213         27        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:18
   History Saved: 043_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844      0.722      0.643      0.696      0.489

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     44/100      16.6G      1.196     0.7052      1.207         17        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:17
   History Saved: 044_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844      0.736       0.62      0.691      0.487

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     45/100      17.3G      1.188     0.7032      1.201         32        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:18
   History Saved: 045_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844      0.712      0.629      0.687      0.482

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     46/100      17.8G      1.189     0.6986        1.2         39        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:17
   History Saved: 046_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844      0.697       0.65      0.691      0.487

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     47/100      16.6G      1.177     0.6866      1.194         27        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:18
   History Saved: 047_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844      0.697      0.649      0.691      0.486

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     48/100      17.2G      1.167     0.6746      1.193         45        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:18
   History Saved: 048_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844      0.711      0.647      0.695      0.489

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     49/100      17.2G      1.158     0.6698      1.182         44        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:17
   History Saved: 049_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844      0.708      0.655      0.694      0.488

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     50/100      17.2G      1.151     0.6622      1.183         16        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:17
   History Saved: 050_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844        0.7       0.66      0.695       0.49

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     51/100      16.6G       1.15     0.6557      1.183          8        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:18
   History Saved: 051_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844        0.7      0.657      0.692      0.489

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     52/100      16.7G      1.131     0.6425      1.168         42        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:18
   History Saved: 052_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844        0.7      0.653      0.691      0.489

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     53/100      17.1G      1.123     0.6327      1.164         44        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:18
   History Saved: 053_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844      0.699      0.659       0.69      0.489

      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size
     54/100      16.6G      1.121     0.6295      1.161         12        640: 100% ━━━━━━━━━━━━ 882/882 1.1it/s 13:18
   History Saved: 054_epoch.pt
                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.7s
                   all        896       2844      0.696      0.656      0.687      0.487

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

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

54 epochs completed in 12.184 hours.
Optimizer stripped from /content/drive/MyDrive/Mortiscope Models/YOLO12/run_v1/weights/last.pt, 53.5MB
Optimizer stripped from /content/drive/MyDrive/Mortiscope Models/YOLO12/run_v1/weights/best.pt, 53.5MB

Validating /content/drive/MyDrive/Mortiscope Models/YOLO12/run_v1/weights/best.pt...
Ultralytics 8.4.5 🚀 Python-3.12.12 torch-2.9.0+cu126 CUDA:0 (NVIDIA L4, 22693MiB)
YOLOv12l summary (fused): 283 layers, 26,342,927 parameters, 0 gradients, 88.6 GFLOPs

                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 28/28 1.8it/s 15.6s
                   all        896       2844      0.722       0.65      0.709      0.494
                 adult        176        593       0.76      0.529      0.603      0.337
              instar_1        222       1164       0.75      0.416      0.611      0.329
              instar_2        168        521        0.4      0.572      0.532      0.344
              instar_3        240        334      0.701      0.802      0.838      0.655
                  pupa        205        232      0.999      0.931       0.96      0.807

Speed: 0.2ms preprocess, 14.1ms inference, 0.0ms loss, 0.9ms postprocess per image
Results saved to /content/drive/MyDrive/Mortiscope Models/YOLO12/run_v1

------------------------------
Training complete.
Total Time: 12h 10m 43s
------------------------------

Section 5: Visual Performance Evaluation

Purpose: To assess the model’s accuracy and reliability using visual metrics. Raw numbers are often insufficient; visualization helps identify specific classes or scenarios where the model struggles.

Key Activities:

  • Training Metrics Plotting: Parses the results.csv log file to generate line charts for Box Loss, Classification Loss, Precision, Recall, and mAP (Mean Average Precision). This helps verify that the model is converging and not overfitting.
  • Confusion Matrix Generation: Runs a validation pass on the best-performing model to generate a normalized Confusion Matrix. This heatmap visualizes how often the model confuses one life stage with another, providing insight into biological similarities affecting the AI.
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 2x3 grid of training performance plots.

    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()

    # Creates a Matplotlib figure and an array of 2x3 subplots (axes).
    figure, axis_array = plt.subplots(2, 3, figsize=(18, 10))

    # Defines the mapping of DataFrame columns to their respective plot titles for the three primary loss functions.
    loss_map = [
        ('train/box_loss', 'val/box_loss', 'Box Loss'),
        ('train/cls_loss', 'val/cls_loss', 'Classification Loss'),
        ('train/dfl_loss', 'val/dfl_loss', 'Distribution Focal Loss')
    ]

    # Iterates through the loss map to generate the top row of plots.
    for i, (train_col, val_col, title) in enumerate(loss_map):
        axis = axis_array[0, 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)
        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 for clearer trend visualization.
        sns.lineplot(x=df.index, y=df[train_col].rolling(smoothing_window_size).mean(),
                     ax=axis, color=color_train, linewidth=2.5, label='Train')
        sns.lineplot(x=df.index, y=df[val_col].rolling(smoothing_window_size).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()

    # Precision and Recall Plot
    axis_precision_recall = axis_array[1, 0]
    sns.lineplot(x=df.index, y=df['metrics/precision(B)'].rolling(smoothing_window_size).mean(),
                 ax=axis_precision_recall, color=color_prec, label='Precision')
    sns.lineplot(x=df.index, y=df['metrics/recall(B)'].rolling(smoothing_window_size).mean(),
                 ax=axis_precision_recall, color=color_rec, label='Recall')

    axis_precision_recall.set_title('Precision & Recall')
    axis_precision_recall.set_xlabel('Epochs')
    axis_precision_recall.set_ylabel('Score')
    axis_precision_recall.set_ylim(0, 1)

    # Mean Average Precision (mAP) Plot
    axis_map = axis_array[1, 1]
    sns.lineplot(x=df.index, y=df['metrics/mAP50(B)'].rolling(smoothing_window_size).mean(),
                 ax=axis_map, color=color_map_metric, linewidth=2.5, label='mAP @ 0.50')

    axis_map.set_title('Mean Average Precision (IoU=0.50)')
    axis_map.set_xlabel('Epochs')
    axis_map.set_ylabel('Score')
    axis_map.set_ylim(0, 1)
    # Fills the area under the mAP curve to visually emphasize the performance metric.
    axis_map.fill_between(df.index, df['metrics/mAP50(B)'].rolling(smoothing_window_size).mean(),
                          color=color_map_metric, alpha=0.1)

    # Learning Rate Schedule Plot
    axis_learning_rate = axis_array[1, 2]
    sns.lineplot(x=df.index, y=df['lr/pg0'], ax=axis_learning_rate, color=color_lr, linestyle='--')

    axis_learning_rate.set_title('Learning Rate Schedule')
    axis_learning_rate.set_xlabel('Epochs')
    axis_learning_rate.set_ylabel('Learning Rate')

    # Adjusts the spacing between subplots to prevent labels from overlapping.
    plt.tight_layout(
        pad=3.0,
        w_pad=4.0,
        h_pad=5.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 YOLO model object using the best saved weights.
    validation_model = YOLO(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=batch_size,
        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/YOLO12/run_v1/weights/best.pt

Ultralytics 8.4.5 🚀 Python-3.12.12 torch-2.9.0+cu126 CUDA:0 (NVIDIA L4, 22693MiB)
YOLOv12l summary (fused): 283 layers, 26,342,927 parameters, 0 gradients, 88.6 GFLOPs
val: Fast image access ✅ (ping: 0.0±0.0 ms, read: 1327.0±383.0 MB/s, size: 36.3 KB)
val: Scanning /content/Complementary-1/valid/labels.cache... 896 images, 36 backgrounds, 0 corrupt: 100% ━━━━━━━━━━━━ 896/896 375.8Mit/s 0.0s

                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 56/56 2.2it/s 25.2s
                   all        896       2844      0.722      0.651      0.709      0.496
                 adult        176        593      0.761       0.53      0.601      0.337
              instar_1        222       1164      0.751      0.417      0.611      0.332
              instar_2        168        521        0.4      0.572      0.532      0.344
              instar_3        240        334      0.701      0.802       0.84      0.658
                  pupa        205        232      0.999      0.931       0.96      0.807

Speed: 0.8ms preprocess, 24.1ms inference, 0.0ms loss, 0.8ms postprocess per image
Results saved to /content/runs/detect/val
Confusion Matrix saved to: /content/drive/MyDrive/Mortiscope Models/YOLO12/run_v1/confusion_matrix.png

Section 6: Benchmarking and SAHI Integration

Purpose: To evaluate the model’s operational efficiency (speed) and prepare it for advanced inference scenarios involving high-resolution imagery.

Key Activities:

  • Speed Benchmarking: Runs a validation pass specifically to measure pre-processing, inference, and post-processing times. This calculates the estimated FPS (Frames Per Second) to determine if the model is suitable for real-time applications.
  • SAHI Wrapper Initialization: Initializes the Sliced Aided Hyper Inference wrapper. Standard YOLO resizing can make small insects vanish in 4K images. SAHI solves this by slicing the image into smaller overlapping windows, performing inference on each, and stitching the results back together.
# 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 YOLO object for evaluation.
    benchmark_model = YOLO(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/YOLO12/run_v1/weights/best.pt

Ultralytics 8.4.5 🚀 Python-3.12.12 torch-2.9.0+cu126 CUDA:0 (NVIDIA L4, 22693MiB)
YOLOv12l summary (fused): 283 layers, 26,342,927 parameters, 0 gradients, 88.6 GFLOPs
val: Fast image access ✅ (ping: 0.0±0.0 ms, read: 1525.1±436.0 MB/s, size: 49.3 KB)
val: Scanning /content/Complementary-1/valid/labels.cache... 896 images, 36 backgrounds, 0 corrupt: 100% ━━━━━━━━━━━━ 896/896 375.8Mit/s 0.0s

                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100% ━━━━━━━━━━━━ 56/56 2.3it/s 24.0s
                   all        896       2844      0.722      0.651      0.709      0.496
                 adult        176        593      0.761       0.53      0.601      0.337
              instar_1        222       1164      0.751      0.417      0.611      0.332
              instar_2        168        521        0.4      0.572      0.532      0.344
              instar_3        240        334      0.701      0.802       0.84      0.658
                  pupa        205        232      0.999      0.931       0.96      0.807

Speed: 0.6ms preprocess, 23.6ms inference, 0.0ms loss, 0.7ms postprocess per image

---------------------------------------------
Inference Speed Benchmark (Average per Image)
---------------------------------------------
Pre-process               : 0.64 ms
Inference (Model)         : 23.55 ms
Post-process (NMS)        : 0.70 ms
---------------------------------------------
Total Latency             : 24.89 ms
Estimated FPS             : 40.18 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='ultralytics',     # 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/YOLO12/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: To finalize the model for production deployment and provide a tangible demonstration of its capabilities on user-provided data.

Key Activities:

  • ONNX Export: Converts the PyTorch model (.pt) to the ONNX (Open Neural Network Exchange) format. This format is hardware-agnostic and optimized for deployment on edge devices or web servers.
  • Interactive Inference Pipeline: A comprehensive script that:
    1. Accepts a user-uploaded image.
    2. Detects the scene type (Macro vs. Field) to choose between Standard or Sliced inference.
    3. Filters outliers based on box area to reduce false positives.
    4. Draws bounding boxes and creates a summary legend of the detected entomological evidence.
# 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 = "yolo12_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 = YOLO(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=12            # 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/YOLO12/run_v1/weights/best.pt

Ultralytics 8.4.5 🚀 Python-3.12.12 torch-2.9.0+cu126 CPU (Intel Xeon CPU @ 2.20GHz)
💡 ProTip: Export to OpenVINO format for best performance on Intel hardware. Learn more at https://docs.ultralytics.com/integrations/openvino/
YOLOv12l summary (fused): 283 layers, 26,342,927 parameters, 0 gradients, 88.6 GFLOPs

PyTorch: starting from '/content/drive/MyDrive/Mortiscope Models/YOLO12/run_v1/weights/best.pt' with input shape (1, 3, 640, 640) BCHW and output shape(s) (1, 9, 8400) (51.1 MB)
ONNX: starting export with onnx 1.20.1 opset 12...
ONNX: slimming with onnxslim 0.1.82...
ONNX: export success ✅ 10.6s, saved as '/content/drive/MyDrive/Mortiscope Models/YOLO12/run_v1/weights/best.onnx' (100.9 MB)
Export complete (12.2s)

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

----------------------------------------------------------------------------------------------------
File Saved: /content/drive/MyDrive/Mortiscope Models/YOLO12/run_v1/weights/yolo12_mortiscope.onnx
----------------------------------------------------------------------------------------------------
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, # Uses YOLO's built-in NMS.
                verbose=False
            )

            # Manually converts the native YOLO 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
            )

        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: 7     | Avg Conf: 78.04%
----------------------------------------------------------------------