### Install YOLO via Git Source: https://github.com/multimediatechlab/yolo/blob/main/docs/0_get_start/0_quick_start.rst Clones the YOLO repository and installs development dependencies. It's recommended to work within the cloned directory for further modifications. ```bash git clone https://github.com/WongKinYiu/YOLO.git cd YOLO pip install -r requirements-dev.txt ``` -------------------------------- ### Install YOLO via Pip Source: https://github.com/multimediatechlab/yolo/blob/main/docs/0_get_start/0_quick_start.rst Installs YOLO directly from its GitHub repository using pip. This is a convenient option for users who plan to make simple changes or use YOLO without extensive code modifications. ```bash pip install git+https://github.com/WongKinYiu/YOLO.git ``` -------------------------------- ### Train YOLO Model with Custom Dataset and Model Source: https://github.com/multimediatechlab/yolo/blob/main/docs/0_get_start/0_quick_start.rst Trains the YOLO model using a specified dataset configuration file and a particular model architecture (e.g., v9-m). This allows for fine-tuning the model on custom data. ```bash python yolo/lazy.py task=train dataset=AYamlFilePath model=v9-m ``` ```bash yolo task=train dataset=AYamlFilePath model=v9-m ``` -------------------------------- ### Enable Fast Inference Modes in YOLO Source: https://github.com/multimediatechlab/yolo/blob/main/docs/0_get_start/0_quick_start.rst Enables accelerated inference modes for YOLO, such as ONNX or TensorRT, for optimized deployment. This can significantly improve performance on supported hardware. ```bash python yolo/lazy.py task.fast_inference={onnx, trt, deploy} ``` ```bash yolo task.fast_inference={onnx, trt, deploy} ``` -------------------------------- ### Train YOLO Model Source: https://github.com/multimediatechlab/yolo/blob/main/docs/0_get_start/0_quick_start.rst Initiates the training process for the YOLO model. Users can customize the dataset and model architecture by overriding parameters. The command can be run directly or via the pip-installed package. ```bash python yolo/lazy.py task=train ``` ```bash yolo task=train ``` -------------------------------- ### YOLO Config Setup with Hydra (Decorator) Source: https://github.com/multimediatechlab/yolo/blob/main/docs/1_tutorials/1_setup.rst Demonstrates setting up YOLO configuration using Hydra's decorator approach. It initializes the configuration and creates a progress logger instance. ```python import hydra from yolo import ProgressLogger from yolo.config.config import Config @hydra.main(config_path="config", config_name="config", version_base=None) def main(cfg: Config): progress = ProgressLogger(cfg, exp_name=cfg.name) pass ``` -------------------------------- ### Perform YOLO Inference with Custom Parameters (PyPI) Source: https://github.com/multimediatechlab/yolo/blob/main/docs/1_tutorials/0_allIn1.rst This example demonstrates running YOLO model inference after installing via PyPI. It configures the model, NMS confidence threshold, data source (webcam ID 0), and enables fast inference with ONNX. ```bash yolo model=v9-m task.nms.min_confidence=0.1 task.data.source=0 task.fast_inference=onnx ``` -------------------------------- ### Clone YOLO Repository Source: https://github.com/multimediatechlab/yolo/blob/main/docs/0_get_start/2_installations.rst Clones the YOLO repository from GitHub to your local machine. This is the first step for setting up YOLO, allowing you to access the project files. ```bash git clone https://github.com/WongKinYiu/YOLO.git ``` -------------------------------- ### YOLO Config Setup with Hydra (Initialize & Compose) Source: https://github.com/multimediatechlab/yolo/blob/main/docs/1_tutorials/1_setup.rst Illustrates setting up YOLO configuration using Hydra's initialize and compose methods. This approach allows for more explicit control over configuration loading and overrides. ```python from hydra import compose, initialize from yolo import ProgressLogger from yolo.config.config import Config with initialize(config_path="config", version_base=None): cfg = compose(config_name="config", overrides=["task=train", "model=v9-c"]) progress = ProgressLogger(cfg, exp_name=cfg.name) ``` -------------------------------- ### Install YOLO Requirements Source: https://github.com/multimediatechlab/yolo/blob/main/docs/0_get_start/2_installations.rst Installs the necessary Python packages for YOLO. It provides options for minimal requirements using 'requirements.txt' and a full installation with 'requirements-dev.txt'. Ensure pip is installed. ```bash # For the minimal requirements, use: pip install -r requirements.txt # For a full installation, use: pip install -r requirements-dev.txt ``` -------------------------------- ### Import YOLO Dependencies Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_smallobject.ipynb Imports essential libraries for YOLO object detection, including PyTorch, Hydra for configuration management, and PIL for image manipulation. It also sets up the project root and appends it to the system path. ```Python import sys from pathlib import Path import torch from hydra import compose, initialize from PIL import Image # Ensure that the necessary repository is cloned and installed. You may need to run: # git clone git@github.com:WongKinYiu/YOLO.git # cd YOLO # pip install . project_root = Path().resolve().parent sys.path.append(str(project_root)) from yolo import ( AugmentationComposer, Config, NMSConfig, PostProccess, bbox_nms, create_model, create_converter, custom_logger, draw_bboxes, ) ``` -------------------------------- ### Perform YOLO Inference Source: https://github.com/multimediatechlab/yolo/blob/main/docs/0_get_start/0_quick_start.rst Runs inference using the YOLO model with a specified data source. This command is used for object detection on new images or videos. It can be executed directly or via the pip-installed package. ```bash python yolo/lazy.py task.data.source=AnySource ``` ```bash yolo task.data.source=AnySource ``` -------------------------------- ### Install YOLO via GitHub using Pip Source: https://github.com/multimediatechlab/yolo/blob/main/docs/0_get_start/2_installations.rst Installs YOLO directly from its GitHub repository using pip. This method is recommended when the PyPI package name is unavailable or for the latest updates. Requires git and pip3. ```bash pip install git+https://github.com/WongKinYiu/YOLO.git ``` -------------------------------- ### Clone YOLO Repository and Install Dependencies Source: https://github.com/multimediatechlab/yolo/blob/main/README.md Clone the official YOLO repository from GitHub and install all necessary Python dependencies using the provided requirements file. This is the recommended setup for using YOLOv9's developer mode. ```Shell git clone git@github.com:WongKinYiu/YOLO.git cd YOLO pip install -r requirements.txt ``` -------------------------------- ### YOLO Dataset Configuration Source: https://github.com/multimediatechlab/yolo/blob/main/docs/1_tutorials/0_allIn1.rst Example YAML configuration file structure for defining datasets in the YOLO project. Supports auto-download functionality and specifies paths for images, labels, and annotations. ```YAML dataset: path: ../datasets/coco128 # dataset root dir train: train2017 # train images (relative to 'path') 128 images val: val2017 # val images (relative to 'path') 5000 images test: # Classes nc: 80 names: ['person', 'bicycle', 'car', ...] # class names # Download data (optional) # auto_download: https://github.com/ultralytics/yolov5/releases/download/v1.0/coco128.zip ``` -------------------------------- ### Load and Transform Input Image Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_smallobject.ipynb Opens an input image using PIL and applies transformations, including resizing and augmentation, using the configured AugmentationComposer. The transformed image and bounding box information are prepared for the model. ```Python pil_image = Image.open(IMAGE_PATH) image, bbox, rev_tensor = transform(pil_image) image = image.to(device)[None] rev_tensor = rev_tensor.to(device)[None] ``` -------------------------------- ### Configure YOLO Inference Parameters Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_smallobject.ipynb Defines configuration parameters for YOLO inference, including the path to configuration files, model name, device (CUDA or CPU), number of classes, and the input image path. It also initializes a custom logger and sets the device. ```Python CONFIG_PATH = "../yolo/config" CONFIG_NAME = "config" MODEL = "v9-c" DEVICE = 'cuda:0' CLASS_NUM = 80 IMAGE_PATH = '../image.png' SLIDE = 4 custom_logger() device = torch.device(DEVICE) ``` -------------------------------- ### Run YOLO with NVIDIA Docker Source: https://github.com/multimediatechlab/yolo/blob/main/docs/0_get_start/2_installations.rst Pulls the YOLO Docker image and runs it with GPU support using NVIDIA Docker. This method is suitable for users who prefer containerized environments and have the NVIDIA Docker toolkit installed. ```bash docker pull henrytsui000/yolo docker run --gpus all -it henrytsui000/yolo ``` -------------------------------- ### Initialize and Compose YOLO Model Configuration Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_smallobject.ipynb Initializes Hydra to manage configurations and composes the configuration for YOLO inference. It then creates the YOLO model, augmentation composer, converter, and post-processing objects based on the loaded configuration. ```Python with initialize(config_path=CONFIG_PATH, version_base=None, job_name="notebook_job"): cfg: Config = compose(config_name=CONFIG_NAME, overrides=["task=inference", f"task.data.source={IMAGE_PATH}", f"model={MODEL}"]) model = create_model(cfg.model, class_num=CLASS_NUM).to(device) transform = AugmentationComposer([], cfg.image_size) converter = create_converter(cfg.model.name, model, cfg.model.anchor, cfg.image_size, device) post_proccess = PostProccess(converter, NMSConfig(0.5, 0.9)) ``` -------------------------------- ### Install YOLO via pip Source: https://github.com/multimediatechlab/yolo/blob/main/README.md Quickly install the YOLO library using pip and git. This allows for immediate use of YOLO functionalities, such as running inference on a specified data source. ```Shell pip install git+https://github.com/WongKinYiu/YOLO.git yolo task.data.source=0 ``` -------------------------------- ### Draw Bounding Boxes on Image Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_smallobject.ipynb Visualizes the detected objects by drawing bounding boxes on the original input image. The bounding boxes and their corresponding class labels are retrieved from the inference results. ```Python draw_bboxes(pil_image, predict_box, idx2label=cfg.dataset.class_list) ``` -------------------------------- ### Load Autoreload Extension Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_smallobject.ipynb Loads the autoreload extension for Jupyter notebooks, which automatically reloads modules before executing code. This is useful for iterative development. ```Python %load_ext autoreload %autoreload 2 ``` -------------------------------- ### Run Code Formatting and Sorting Source: https://github.com/multimediatechlab/yolo/blob/main/docs/CONTRIBUTING.md This snippet demonstrates how to format and sort Python code using the 'black' and 'isort' tools. These tools ensure code consistency and adhere to style guides, which are crucial for collaborative projects. ```Shell isort . black . ``` -------------------------------- ### YOLO Training, Validation, and Inference Commands Source: https://github.com/multimediatechlab/yolo/blob/main/docs/HOWTO.md Command-line examples for training, validating, and performing inference with the YOLO model. These commands utilize a lazy loading approach and allow customization through task-specific arguments. ```Shell # Train python yolo/lazy.py task=train dataset=dev use_wandb=True # Validate python yolo/lazy.py task=validation python yolo/lazy.py task=validation model=v9-s python yolo/lazy.py task=validation dataset=toy python yolo/lazy.py task=validation dataset=toy name=validation # Inference python yolo/lazy.py task=inference python yolo/lazy.py task=inference device=cpu python yolo/lazy.py task=inference +quite=True python yolo/lazy.py task=inference name=AnyNameYouWant python yolo/lazy.py task=inference image_size=[480,640] python yolo/lazy.py task=inference task.nms.min_confidence=0.1 python yolo/lazy.py task=inference task.fast_inference=deploy python yolo/lazy.py task=inference task.fast_inference=onnx device=cpu python yolo/lazy.py task=inference task.data.source=data/toy/images/train ``` -------------------------------- ### Perform YOLO Inference with Custom Parameters (Git Clone) Source: https://github.com/multimediatechlab/yolo/blob/main/docs/1_tutorials/0_allIn1.rst This example shows how to run YOLO model inference using a git-cloned repository. It specifies the model, NMS confidence threshold, data source (webcam ID 0), and fast inference mode using ONNX. ```bash python yolo/lazy.py model=v9-m task.nms.min_confidence=0.1 task.data.source=0 task.fast_inference=onnx ``` -------------------------------- ### Custom YOLO Model Architecture Configuration Source: https://github.com/multimediatechlab/yolo/blob/main/docs/HOWTO.md Example YAML configuration for defining a custom YOLO model architecture. It demonstrates how to specify layers, their arguments, and connections using tags and sources. ```YAML model: foo: - ADown: args: {out_channels: 256} - RepNCSPELAN: source: -2 args: {out_channels: 512, part_channels: 256} tags: B4 bar: - Concat: source: [-2, B4] ``` -------------------------------- ### Perform YOLO Inference and Post-processing Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_smallobject.ipynb Executes the YOLO model inference on the processed image using a sliding window approach. It then converts the model output, applies non-maximum suppression (NMS) to filter overlapping bounding boxes, and adjusts the bounding box coordinates. ```Python with torch.no_grad(): total_image, total_shift = slide_image(image) predict = model(total_image) pred_class, _, pred_bbox = converter(predict["Main"]) pred_bbox[1:] = (pred_bbox[1: ] + total_shift[:, None]) / SLIDE pred_bbox = pred_bbox.view(1, -1, 4) pred_class = pred_class.view(1, -1, 80) pred_bbox = (pred_bbox - rev_tensor[:, None, 1:]) / rev_tensor[:, 0:1, None] predict_box = bbox_nms(pred_class, pred_bbox, NMSConfig(0.3, 0.5)) ``` -------------------------------- ### Train YOLO Model with ModelTrainer Source: https://github.com/multimediatechlab/yolo/blob/main/docs/1_tutorials/4_train.rst Initializes and uses the ModelTrainer to manage the model training process. It requires configuration, model, converter, progress logger, device, and distributed data parallel settings. The progress logger must be started before calling the solve function. ```python from yolo import ModelTrainer solver = ModelTrainer(cfg, model, converter, progress, device, use_ddp) progress.start() solver.solve(dataloader) ``` -------------------------------- ### Implement Sliding Window for Image Processing Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_smallobject.ipynb Defines a function `slide_image` that performs a sliding window operation on an input image. It upscales the image and then divides it into smaller overlapping segments, returning the concatenated segments and their corresponding shifts. ```Python def slide_image(image, slide = 4, device = device): up_image = torch.nn.functional.interpolate(image, scale_factor=slide) image_list = [image] shift_list = [] *_, w, h = up_image.shape for x_slide in range(slide): for y_slide in range(slide): left_w, right_w = w // slide * x_slide, w // slide * (x_slide + 1) left_h, right_h = h // slide * y_slide, h // slide * (y_slide + 1) slide_image = up_image[:, :, left_w: right_w, left_h: right_h] image_list.append(slide_image) shift_list.append(torch.Tensor([left_h, left_w, left_h, left_w])) total_image = torch.concat(image_list) total_shift = torch.stack(shift_list).to(device) return total_image, total_shift ``` -------------------------------- ### Validate YOLO Model with ModelValidator Source: https://github.com/multimediatechlab/yolo/blob/main/docs/1_tutorials/4_train.rst Initializes and uses the ModelValidator to manage the model validation process. Similar to the trainer, it requires configuration, model, converter, progress logger, device, and distributed data parallel settings. The progress logger must be started before calling the solve function. ```python from yolo import ModelValidator solver = ModelValidator(cfg, model, converter, progress, device, use_ddp) progress.start() solver.solve(dataloader) ``` -------------------------------- ### Create and Load YOLO Model Source: https://github.com/multimediatechlab/yolo/blob/main/docs/1_tutorials/2_buildmodel.rst Creates a YOLO model instance using the provided configuration and loads specified weights. The model is then moved to the appropriate device (e.g., GPU). Dependencies include the YOLO model configuration and device availability. ```python model = create_model(cfg.model, class_num=cfg.dataset.class_num, weight_path=cfg.weight) model = model.to(device) ``` -------------------------------- ### Auto Download Dataset Configuration (YAML) Source: https://github.com/multimediatechlab/yolo/blob/main/docs/1_tutorials/3_dataset.rst This YAML snippet illustrates the configuration for automatically downloading and verifying a dataset. It specifies the source URL prefix and postfix, the expected number of files, and the generation of cache files for faster loading. ```yaml auto_download: true prefix: "http://example.com/datasets" postfix: "dataset.zip" file_num: 1000 ``` -------------------------------- ### YOLO Training Configuration Source: https://github.com/multimediatechlab/yolo/blob/main/docs/6_function_docs/3_config.rst Sets up the training process, including optimizer, loss function, and learning rate scheduler. It defines parameters for model training and optimization. ```python class OptimizerArgs: lr: float weight_decay: float class OptimizerConfig: type: str args: OptimizerArgs class MatcherConfig: iou: str topk: int factor: Dict[str] class LossConfig: objective: Dict[str] aux: Union[bool, bool] matcher: MatcherConfig class SchedulerConfig: type: str warmup: Dict[str] args: Dict[str] class EMAConfig: enabled: bool decay: float class TrainConfig: task: str epoch: int data: DataConfig optimizer: OptimizerConfig loss: LossConfig scheduler: SchedulerConfig ema: EMAConfig validation: ValidationConfig ``` -------------------------------- ### Multi-GPU Training with DDP Source: https://github.com/multimediatechlab/yolo/blob/main/docs/1_tutorials/0_allIn1.rst Instructions for training YOLO models across multiple GPUs using PyTorch's Distributed Data Parallel (DDP). Specifies the command structure for launching distributed training. ```bash torchrun --nproc_per_node=2 yolo/lazy.py task=train device=[0,1] ``` ```zsh torchrun --nproc_per_node=2 yolo/lazy.py task=train device=\[0,1\] ``` -------------------------------- ### Configure YOLOv9 Model and Input Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_TensorRT.ipynb Sets up configuration variables for the YOLOv9 model, including the model name, device (CUDA), weight paths, model configuration file, and the input image path and size. It also initializes a custom logger and the PyTorch device. ```Python MODEL = "v9-c" DEVICE = "cuda:0" WEIGHT_PATH = f"../weights/{MODEL}.pt" TRT_WEIGHT_PATH = f"../weights/{MODEL}.trt" MODEL_CONFIG = f"../yolo/config/model/{MODEL}.yaml" IMAGE_PATH = "../demo/images/inference/image.png" IMAGE_SIZE = (640, 640) custom_logger() device = torch.device(DEVICE) image = Image.open(IMAGE_PATH) ``` -------------------------------- ### YOLO General Configuration Source: https://github.com/multimediatechlab/yolo/blob/main/docs/6_function_docs/3_config.rst Defines general project settings, such as device, output paths, and logging preferences. It provides overall control for the YOLO project execution. ```python class GeneralConfig: name: str device: Union[str, str] cpu_num: int class_idx_id: List[int] image_size: List[int] out_path: str exist_ok: bool lucky_number: int use_wandb: bool use_TensorBoard: bool weight: Optional[str] ``` -------------------------------- ### Train YOLO Model Source: https://github.com/multimediatechlab/yolo/blob/main/docs/1_tutorials/0_allIn1.rst Command to initiate the training process for a YOLO model. Allows customization of various training parameters like model backbone, batch size, image size, and experiment tracking. ```bash python yolo/lazy.py task=train ``` ```bash python yolo/lazy.py task=train task.data.batch_size=12 image_size=1280 ``` -------------------------------- ### YOLO Dataset Configuration Source: https://github.com/multimediatechlab/yolo/blob/main/docs/6_function_docs/3_config.rst Specifies the configuration for datasets, including paths, class information, and download options. It details how to manage and load data for training and inference. ```python class DownloadDetail: url: str file_size: int class DownloadOptions: details: Dict[DownloadDetail] class DatasetConfig: path: str class_num: int class_list: List[str] auto_download: Optional[DownloadOptions] ``` -------------------------------- ### YOLOv7 Model Download Link Source: https://github.com/multimediatechlab/yolo/blob/main/docs/2_model_zoo/0_object_detection.rst Provides a direct download link for the YOLOv7 pre-trained model weights. This is essential for users who want to quickly set up and use the YOLOv7 model for object detection tasks. ```Python https://github.com/WongKinYiu/YOLO/releases/download/v1.0-alpha/v7.pt ``` -------------------------------- ### Configure Inference Parameters Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_inference.ipynb Sets up essential parameters for the YOLO inference process, including configuration paths, model name, device (GPU or CPU), and the number of classes. It also initializes a custom logger and the PyTorch device. ```Python CONFIG_PATH = "../yolo/config" CONFIG_NAME = "config" MODEL = "v9-c" DEVICE = 'cuda:0' CLASS_NUM = 80 IMAGE_PATH = '../demo/images/inference/image.png' custom_logger() device = torch.device(DEVICE) ``` -------------------------------- ### Initialize YOLO Model and Components Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_inference.ipynb Initializes the YOLO model, augmentation pipeline, and post-processing components using Hydra for configuration. It loads the model, sets up the image transformation, and creates a converter and post-processor for predictions. ```Python with initialize(config_path=CONFIG_PATH, version_base=None, job_name="notebook_job"): cfg: Config = compose(config_name=CONFIG_NAME, overrides=["task=inference", f"task.data.source={IMAGE_PATH}", f"model={MODEL}"]) model = create_model(cfg.model, class_num=CLASS_NUM).to(device) transform = AugmentationComposer([], cfg.image_size) converter = create_converter(cfg.model.name, model, cfg.model.anchor, cfg.image_size, device) post_proccess = PostProccess(converter, cfg.task.nms) ``` -------------------------------- ### Import YOLOv9 Libraries Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_TensorRT.ipynb Imports essential Python libraries for YOLOv9 object detection, including PyTorch, PIL for image processing, Loguru for logging, and OmegaConf for configuration management. It also sets up the project root and imports custom YOLO modules. ```Python import os import sys from pathlib import Path import torch from PIL import Image from loguru import logger from omegaconf import OmegaConf project_root = Path().resolve().parent sys.path.append(str(project_root)) from yolo import ( AugmentationComposer, bbox_nms, create_model, custom_logger, create_converter, draw_bboxes, Vec2Box ) from yolo.config.config import NMSConfig ``` -------------------------------- ### Autoload Converter for YOLO Models Source: https://github.com/multimediatechlab/yolo/blob/main/docs/1_tutorials/2_buildmodel.rst Automatically loads a converter based on the YOLO model type (v7 or v9). It requires the model name, anchor configuration, model object, image size, and device to correctly instantiate either 'Vec2Box' or 'Anc2Box' converters. ```python converter = create_converter(cfg.model.name, model, cfg.model.anchor, cfg.image_size, device) ``` -------------------------------- ### YOLOv9-M Model Download Link Source: https://github.com/multimediatechlab/yolo/blob/main/docs/2_model_zoo/0_object_detection.rst Provides a direct download link for the YOLOv9-M pre-trained model weights. This model offers a balance between accuracy and speed for object detection. ```Python https://github.com/WongKinYiu/YOLO/releases/download/v1.0-alpha/v9-m.pt ``` -------------------------------- ### Create YOLO Dataloader (Python) Source: https://github.com/multimediatechlab/yolo/blob/main/docs/1_tutorials/3_dataset.rst This snippet demonstrates how to create a dataloader for YOLO tasks using the `create_dataloader` function. It requires configuration objects for data and the dataset, along with the task name and a flag for distributed data parallel. ```python from yolo import create_dataloader dataloader = create_dataloader(cfg.task.data, cfg.dataset, cfg.task.task, use_ddp) ``` -------------------------------- ### YOLOv9-S Model Download Link Source: https://github.com/multimediatechlab/yolo/blob/main/docs/2_model_zoo/0_object_detection.rst Provides a direct download link for the YOLOv9-S pre-trained model weights. This model is optimized for performance and efficiency in object detection tasks. ```Python https://github.com/WongKinYiu/YOLO/releases/download/v1.0-alpha/v9-s.pt ``` -------------------------------- ### Load or Create TensorRT YOLOv9 Model Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_TensorRT.ipynb Loads a pre-existing TensorRT model if available, or creates and optimizes a new one using `torch2trt`. This involves loading the model configuration, checking for the TensorRT weight path, and converting the PyTorch model to TensorRT format for faster inference. ```Python with open(MODEL_CONFIG) as stream: cfg_model = OmegaConf.load(stream) if os.path.exists(TRT_WEIGHT_PATH): from torch2trt import TRTModule model_trt = TRTModule() model_trt.load_state_dict(torch.load(TRT_WEIGHT_PATH)) else: from torch2trt import torch2trt model = create_model(cfg_model, weight_path=WEIGHT_PATH) model = model.to(device).eval() dummy_input = torch.ones((1, 3, 640, 640)).to(device) logger.info(f"♻️ Creating TensorRT model") model_trt = torch2trt(model, [dummy_input]) torch.save(model_trt.state_dict(), TRT_WEIGHT_PATH) logger.info(f"📥 TensorRT model saved to oonx.pt") transform = AugmentationComposer([], IMAGE_SIZE) converter = create_converter(cfg_model.name, model_trt, cfg_model.anchor, IMAGE_SIZE, device) ``` -------------------------------- ### Import Dependencies for YOLO Inference Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_inference.ipynb Imports necessary libraries for YOLO inference, including PyTorch for model operations, Hydra for configuration management, and PIL for image handling. It also sets up the project path for module imports. ```Python import sys from pathlib import Path import torch from hydra import compose, initialize from PIL import Image project_root = Path().resolve().parent sys.path.append(str(project_root)) from yolo ( AugmentationComposer, Config, PostProccess, create_converter, create_model, custom_logger, draw_bboxes, ) ``` -------------------------------- ### Deploy YOLO Model for Fast Inference Source: https://github.com/multimediatechlab/yolo/blob/main/docs/1_tutorials/2_buildmodel.rst Loads a YOLO model optimized for fast inference by removing auxiliary branches. If ONNX and TensorRT are configured, it will load or compile the model into these formats. This is crucial for deployment scenarios requiring high performance. ```python model = FastModelLoader(cfg).load_model(device) ``` -------------------------------- ### YOLO Data Handling Configuration Source: https://github.com/multimediatechlab/yolo/blob/main/docs/6_function_docs/3_config.rst Configures data loading and augmentation parameters, such as batch size, image size, and shuffling. It defines how data is processed before being fed into the model. ```python class DataConfig: shuffle: bool batch_size: int pin_memory: bool cpu_num: int image_size: List[int] data_augment: Dict[int] source: Optional[Union[str, str]] ``` -------------------------------- ### YOLOv9-C Model Download Link Source: https://github.com/multimediatechlab/yolo/blob/main/docs/2_model_zoo/0_object_detection.rst Provides a direct download link for the YOLOv9-C pre-trained model weights. This model is designed for high accuracy in object detection tasks. ```Python https://github.com/WongKinYiu/YOLO/releases/download/v1.0-alpha/v9-c.pt ``` -------------------------------- ### Preprocess Image for YOLOv9 Inference Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_TensorRT.ipynb Prepares the input image for YOLOv9 inference by applying transformations and resizing. It converts the image to a PyTorch tensor and moves it to the specified device. ```Python image, bbox, rev_tensor = transform(image, torch.zeros(0, 5)) image = image.to(device)[None] ``` -------------------------------- ### YOLO Main Configuration Source: https://github.com/multimediatechlab/yolo/blob/main/docs/6_function_docs/3_config.rst The primary configuration class that aggregates dataset, model, training, and inference settings. It serves as the central configuration hub for the YOLO project. ```python class Config: task: Union[ValidationConfig, str] dataset: DatasetConfig model: ModelConfig model: GeneralConfig ``` -------------------------------- ### YOLO Model Configuration Source: https://github.com/multimediatechlab/yolo/blob/main/docs/6_function_docs/3_config.rst Defines the configuration for the YOLO model, including anchor settings and block definitions. It specifies parameters for model layers and their connections. ```python class AnchorConfig: strides: List[int] reg_max: Optional[int] anchor_num: Optional[int] anchor: List[List[int]] class LayerConfig: args: Dict source: Union[List[int], str] tags: str class BlockConfig: block: List[Dict[LayerConfig]] class ModelConfig: name: Optional[str] anchor: AnchorConfig model: Dict[BlockConfig] ``` -------------------------------- ### YOLOv9-T Model Download Link Source: https://github.com/multimediatechlab/yolo/blob/main/docs/2_model_zoo/0_object_detection.rst Provides a direct download link for the YOLOv9-T pre-trained model weights. This model is part of the YOLOv9 family and can be used for object detection. ```Python https://github.com/WongKinYiu/YOLO/releases/download/v1.0-alpha/v9-t.pt ``` -------------------------------- ### Preprocess Image for Inference Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_inference.ipynb Loads an image using PIL, applies the defined transformations, and moves the processed image and associated tensors to the specified device (GPU or CPU). This prepares the input for the YOLO model. ```Python pil_image = Image.open(IMAGE_PATH) image, bbox, rev_tensor = transform(pil_image) image = image.to(device)[None] rev_tensor = rev_tensor.to(device)[None] ``` -------------------------------- ### YOLO Inference Configuration Source: https://github.com/multimediatechlab/yolo/blob/main/docs/6_function_docs/3_config.rst Configures the inference process, including non-maximum suppression (NMS) and output settings. It defines parameters for running predictions on new data. ```python class NMSConfig: min_confidence: int min_iou: int class InferenceConfig: task: str nms: NMSConfig data: DataConfig fast_inference: Optional[None] save_predict: bool ``` -------------------------------- ### Train YOLO Model Source: https://github.com/multimediatechlab/yolo/blob/main/README.md Train YOLO models using the provided lazy script. Users can specify the task, dataset configuration, batch size, model version, and whether to use Weights & Biases for tracking. The script supports training from scratch or using pre-trained weights. ```Python python yolo/lazy.py task=train dataset=** use_wandb=True python yolo/lazy.py task=train task.data.batch_size=8 model=v9-c weight=False ``` -------------------------------- ### YOLO Validation Configuration Source: https://github.com/multimediatechlab/yolo/blob/main/docs/6_function_docs/3_config.rst Specifies configuration for model validation, including NMS and data settings. It defines how the model's performance is evaluated. ```python class ValidationConfig: task: str nms: NMSConfig data: DataConfig ``` -------------------------------- ### Perform Inference and Visualize Results Source: https://github.com/multimediatechlab/yolo/blob/main/examples/notebook_inference.ipynb Executes the YOLO model inference on the preprocessed image and applies post-processing to obtain bounding box predictions. It then draws these bounding boxes on the original image using the class labels. ```Python with torch.no_grad(): predict = model(image) pred_bbox = post_proccess(predict, rev_tensor) draw_bboxes(pil_image, pred_bbox, idx2label=cfg.dataset.class_list) ``` -------------------------------- ### Run Tests with Pytest Source: https://github.com/multimediatechlab/yolo/blob/main/docs/CONTRIBUTING.md This command executes all tests in the project using the pytest framework. Ensuring all tests pass is a critical step before submitting a pull request to maintain code quality and stability. ```Shell pytest ``` -------------------------------- ### Transfer Learning with YOLOv9 Source: https://github.com/multimediatechlab/yolo/blob/main/README.md Perform transfer learning with YOLOv9 by specifying the model version, dataset configuration, and the target device (CPU, MPS, or CUDA). This allows leveraging pre-trained weights for faster and more efficient training on custom datasets. ```Python python yolo/lazy.py task=train task.data.batch_size=8 model=v9-c dataset={dataset_config} device={cpu, mps, cuda} ```