### Setup YOLOv5 Environment Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb Clones the YOLOv5 repository, installs necessary dependencies, and checks PyTorch and GPU availability. It ensures the environment is ready for running YOLOv5. ```python !git clone https://github.com/ultralytics/yolov5 # clone repo %cd yolov5 %pip install -qr requirements.txt # install dependencies import torch from IPython.display import Image, clear_output # to display images clear_output() print(f"Setup complete. Using torch {torch.__version__} ({torch.cuda.get_device_properties(0).name if torch.cuda.is_available() else 'CPU'})") ``` -------------------------------- ### Install TPH-YOLOv5 Source: https://github.com/cv516buaa/tph-yolov5/blob/main/README.md Clones the repository, changes the directory, and installs the required dependencies using pip. ```bash git clone https://github.com/cv516Buaa/tph-yolov5 cd tph-yolov5 pip install -r requirements.txt ``` -------------------------------- ### Install and Login to Weights & Biases Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb This Python code installs the Weights & Biases library quietly ('-q') and imports it. It then prompts the user to log in to their Weights & Biases account, which is necessary for tracking and visualizing experiment results. ```python # Weights & Biases (optional) %pip install -q wandb import wandb wandb.login() ``` -------------------------------- ### Install Flask for REST API Source: https://github.com/cv516buaa/tph-yolov5/blob/main/utils/flask_rest_api/README.md This command installs the Flask framework, which is required to run the REST API. Flask is a lightweight web framework for Python. ```shell pip install Flask ``` -------------------------------- ### Download and Prepare COCO Test Dataset Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb This Python snippet details the process of downloading the COCO test2017 dataset and its labels. It uses `torch.hub.download_url_to_file` to get the label zip file, then `unzip` to extract it. It also uses `curl` to download the dataset images and `unzip` to extract them into the specified directory. This prepares the data for model testing. ```python # Download COCO test-dev2017 torch.hub.download_url_to_file('https://ultralytics.com/assets/coco2017labels.zip', 'tmp.zip') !unzip -q tmp.zip -d ../datasets && rm tmp.zip !f="test2017.zip" && curl http://images.cocodataset.org/zips/$f -o $f && unzip -q $f -d ../datasets/coco/images ``` -------------------------------- ### Train YOLOv5 with COCO128 Dataset Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb This command trains a YOLOv5s model on the COCO128 dataset. It can start from pretrained weights ('yolov5s.pt') or from randomly initialized weights using a specified configuration file ('yolov5s.yaml'). Training results are saved in the 'runs/train/' directory. ```bash python train.py --data coco128.yaml --weights yolov5s.pt --cfg yolov5s.yaml ``` -------------------------------- ### Run Flask REST API Server Source: https://github.com/cv516buaa/tph-yolov5/blob/main/utils/flask_rest_api/README.md This command starts the Flask REST API server. It specifies the Python script to run and the port number to listen on. The API will then be accessible at the specified port. ```shell python3 restapi.py --port 5000 ``` -------------------------------- ### Train TPH-YOLOv5++ Model Source: https://github.com/cv516buaa/tph-yolov5/blob/main/README.md Starts training for the TPH-YOLOv5++ model, an improved version with a cross-layer asymmetric transformer. Configuration options are similar to TPH-YOLOv5 training. ```bash python train.py --img 1536 --adam --batch 4 --epochs 80 --data ./data/VisDrone.yaml --weights yolov5l.pt --hy data/hyps/hyp.VisDrone.yaml --cfg models/yolov5l-tph-plus.yaml --name v5l-tph-plus ``` -------------------------------- ### Python Script for Making REST API Requests Source: https://github.com/cv516buaa/tph-yolov5/blob/main/utils/flask_rest_api/README.md An example Python script using the 'requests' library to send an image to the Flask REST API for object detection. It handles the file upload and prints the JSON response from the server. ```python import requests url = "http://localhost:5000/v1/object-detection/yolov5s" files = { 'image': open('zidane.jpg', 'rb'), } response = requests.post(url, files=files) print(response.json()) ``` -------------------------------- ### Train YOLOv5 with Random Initialization Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb This command trains a YOLOv5s model from scratch using a specified configuration file ('yolov5s.yaml') and no pretrained weights. Training results are saved in the 'runs/train/' directory. ```bash python train.py --data coco128.yaml --weights '' --cfg yolov5s.yaml ``` -------------------------------- ### Model Evolution and Upload with Shell Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb This shell script initiates an evolutionary training process for YOLOv5 models and optionally uploads the evolution results to a cloud storage bucket. It includes commands to copy evolution artifacts, zip them, and use `gsutil` for uploading. ```shell # Evolve !python train.py --img 640 --batch 64 --epochs 100 --data coco128.yaml --weights yolov5s.pt --cache --noautoanchor --evolve !d=runs/train/evolve && cp evolve.* $d && zip -r evolve.zip $d && gsutil mv evolve.zip gs://bucket # upload results (optional) ``` -------------------------------- ### Finetuning YOLOv5 on VOC Dataset with Shell Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb This shell script demonstrates finetuning YOLOv5 models on the VOC dataset with different batch sizes and model configurations. It specifies epochs, image size, caching, and finetuning hyperparameters, and saves results under specific project and name conventions. ```shell # VOC for b, m in zip([64, 48, 32, 16], ['yolov5s', 'yolov5m', 'yolov5l', 'yolov5x']): # zip(batch_size, model) !python train.py --batch {b} --weights {m}.pt --data VOC.yaml --epochs 50 --cache --img 512 --nosave --hyp hyp.finetune.yaml --project VOC --name {m} ``` -------------------------------- ### CI Checks and Model Operations with Shell Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb This snippet demonstrates shell commands for setting up the Python path, cleaning previous runs, and executing training, validation, detection, and export scripts for YOLOv5 models. It covers training from pre-trained weights and from scratch, validating official and custom models, detecting objects, and exporting models to different formats. ```shell export PYTHONPATH="$PWD" # to run *.py. files in subdirectories rm -rf runs # remove runs/ for m in yolov5n; do # models python train.py --img 64 --batch 32 --weights $m.pt --epochs 1 --device 0 # train pretrained python train.py --img 64 --batch 32 --weights '' --cfg $m.yaml --epochs 1 --device 0 # train scratch for d in 0 cpu; do # devices python val.py --weights $m.pt --device $d # val official python val.py --weights runs/train/exp/weights/best.pt --device $d # val custom python detect.py --weights $m.pt --device $d # detect official python detect.py --weights runs/train/exp/weights/best.pt --device $d # detect custom done python hubconf.py # hub python models/yolo.py --cfg $m.yaml # build PyTorch model python models/tf.py --weights $m.pt # build TensorFlow model python export.py --img 64 --batch 1 --weights $m.pt --include torchscript onnx # export done ``` -------------------------------- ### Load TensorBoard Extension Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb This Python code snippet loads the TensorBoard extension for use in an environment like Jupyter notebooks or IPython. It then configures TensorBoard to log training results from the 'runs/train' directory, allowing for visualization of training metrics. ```python # Tensorboard (optional) %load_ext tensorboard %tensorboard --logdir runs/train ``` -------------------------------- ### Run YOLOv5 Inference Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb Performs object detection inference using YOLOv5 on specified sources like images, videos, or streams. Results are saved to the 'runs/detect' directory. Requires the 'detect.py' script. ```shell python detect.py --source 0 # webcam file.jpg # image file.mp4 # video path/ # directory path/*.jpg # glob 'https://youtu.be/NUsoVlDFqZg' # YouTube 'rtsp://example.com/media.mp4' # RTSP, RTMP, HTTP stream ``` -------------------------------- ### Train YOLOv5s on COCO128 Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb This command initiates the training process for a YOLOv5s model. It specifies the image size, batch size, number of epochs, dataset configuration file, pre-trained weights, and enables image caching for faster loading. The output includes training hyperparameters, model summary, and data scanning progress. ```python !python train.py --img 640 --batch 16 --epochs 3 --data coco128.yaml --weights yolov5s.pt --cache ``` -------------------------------- ### Load YOLOv5 Model via PyTorch Hub Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb Loads a YOLOv5 model (e.g., 'yolov5s') using PyTorch Hub and performs inference on a batch of images. Results can be printed, shown, or saved. ```python # PyTorch Hub import torch # Model model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # Images dir = 'https://ultralytics.com/images/' imgs = [dir + f for f in ('zidane.jpg', 'bus.jpg')] # batch of images # Inference results = model(imgs) results.print() # or .show(), .save() ``` -------------------------------- ### Execute YOLOv5 Inference with Python Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb Executes the YOLOv5 inference script 'detect.py' with specific weights, image size, confidence threshold, and source directory. It then displays a detected image. This is a Python wrapper for shell command execution. ```python !python detect.py --weights yolov5s.pt --img 640 --conf 0.25 --source data/images Image(filename='runs/detect/exp/zidane.jpg', width=600) ``` -------------------------------- ### Run YOLOv5x on COCO Test Dataset Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb This snippet shows how to execute the YOLOv5 validation script in test mode. It uses the yolov5x model, COCO dataset configuration, and specifies image size, IoU threshold, and half-precision. This command is used to evaluate the model's performance on the test-dev set of COCO. ```shell # Run YOLOv5x on COCO test !python val.py --weights yolov5x.pt --data coco.yaml --img 640 --iou 0.65 --half --task test ``` -------------------------------- ### Run YOLOv5x on COCO Validation Dataset Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb This snippet demonstrates how to run the YOLOv5 validation script using the yolov5x model on the COCO dataset's validation set. It specifies parameters such as weights, data configuration, image size, IoU threshold, and enables half-precision inference. The output includes detailed evaluation metrics and saves predictions to a JSON file. ```shell # Run YOLOv5x on COCO val !python val.py --weights yolov5x.pt --data coco.yaml --img 640 --iou 0.65 --half ``` -------------------------------- ### Download COCO Validation Dataset Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb Downloads the COCO validation dataset (2017 version) using torch.hub.download_url_to_file and then extracts it to a specified directory. The temporary zip file is removed afterwards. ```python # Download COCO val torch.hub.download_url_to_file('https://ultralytics.com/assets/coco2017val.zip', 'tmp.zip') !unzip -q tmp.zip -d ../datasets && rm tmp.zip ``` -------------------------------- ### YOLOv5 Training via Python API Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt Configures and initiates the training process for a YOLOv5 model using the Python API. It sets various training parameters such as weights, configuration files, dataset, hyperparameters, epochs, batch size, image size, device, and optimizer. ```python # Training via Python API from train import train, parse_opt # Configure training options opt = parse_opt(known=False) opt.weights = 'yolov5l.pt' opt.cfg = 'models/yolov5l-tph-plus.yaml' opt.data = 'data/VisDrone.yaml' opt.hyp = 'data/hyps/hyp.VisDrone.yaml' opt.epochs = 100 opt.batch_size = 8 opt.imgsz = 1536 opt.device = '0' opt.adam = True # Start training from utils.callbacks import Callbacks from utils.torch_utils import select_device device = select_device(opt.device) results = train(opt.hyp, opt, device, Callbacks()) # results contains: (P, R, mAP@.5, mAP@.5-.95, box_loss, obj_loss, cls_loss) print(f"Final mAP@0.5: {results[2]:.3f}") print(f"Final mAP@0.5:0.95: {results[3]:.3f}") ``` -------------------------------- ### Model Profiling with Python Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb This Python snippet uses the `profile` utility from `utils.torch_utils` to measure the performance of custom activation functions (m1 and m2) on random input data. It helps in understanding the computational cost of different model components. ```python # Profile from utils.torch_utils import profile m1 = lambda x: x * torch.sigmoid(x) m2 = torch.nn.SiLU() results = profile(input=torch.randn(16, 3, 640, 640), ops=[m1, m2], n=100) ``` -------------------------------- ### Run YOLOv5 Validation Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb Executes YOLOv5 validation script for different model sizes ('yolov5s' to 'yolov5x') with specified data, image size, confidence, and IoU thresholds. Used for evaluating model performance. ```python # Reproduce for x in 'yolov5s', 'yolov5m', 'yolov5l', 'yolov5x': !python val.py --weights {x}.pt --data coco.yaml --img 640 --conf 0.25 --iou 0.45 # speed !python val.py --weights {x}.pt --data coco.yaml --img 640 --conf 0.001 --iou 0.65 # mAP ``` -------------------------------- ### Run TPH-YOLOv5 Inference with Python Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt These commands show how to perform object detection inference on images, video streams, or from a file list using a trained TPH-YOLOv5 model. Options include specifying weights, source data, image size, confidence thresholds, saving results, and filtering by class. ```python # Detect objects in images using trained TPH-YOLOv5 model python detect.py \ --weights ./weights/yolov5l-xs-1.pt \ --source ./data/images \ --img 640 \ --conf-thres 0.25 \ --iou-thres 0.45 \ --device 0 \ --save-txt \ --save-conf \ --name detect_results # Inference on video stream python detect.py \ --weights ./weights/yolov5l-xs-2.pt \ --source 0 # webcam --img 640 \ --view-img # Inference with augmentation for better accuracy python detect.py \ --weights ./weights/yolov5l-xs-1.pt \ --source ./test_images/ \ --img 640 \ --augment \ --save-crop \ --classes 0 3 4 # Filter: only pedestrian, car, van --line-thickness 2 # Batch inference from file list python detect.py \ --weights ./weights/yolov5l-xs-2.pt \ --source ./image_list.txt \ --img 1280 \ --save-txt \ --nosave # Don't save images, only labels ``` -------------------------------- ### Train TPH-YOLOv5 Model Source: https://github.com/cv516buaa/tph-yolov5/blob/main/README.md Initiates training for the TPH-YOLOv5 model using specified hyperparameters, data configuration, and YOLOv5 weights. Allows customization of image size, optimizer, batch size, and epochs. ```bash python train.py --img 1536 --adam --batch 4 --epochs 80 --data ./data/VisDrone.yaml --weights yolov5l.pt --hy data/hyps/hyp.VisDrone.yaml --cfg models/yolov5l-xs-tph.yaml --name v5l-xs-tph ``` -------------------------------- ### Train TPH-YOLOv5 Models with Python Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt These commands demonstrate how to train TPH-YOLOv5 models from scratch or resume interrupted training. They support single-GPU, multi-GPU distributed training, and various configuration options including dataset, weights, hyperparameters, and model configuration. ```python python train.py \ --img 1536 \ --adam \ --batch 4 \ --epochs 80 \ --data ./data/VisDrone.yaml \ --weights yolov5l.pt \ --hyp data/hyps/hyp.VisDrone.yaml \ --cfg models/yolov5l-xs-tph.yaml \ --name v5l-xs-tph \ --device 0 # Train TPH-YOLOv5++ (improved version with better efficiency) python train.py \ --img 1536 \ --adam \ --batch 4 \ --epochs 80 \ --data ./data/VisDrone.yaml \ --weights yolov5l.pt \ --hyp data/hyps/hyp.VisDrone.yaml \ --cfg models/yolov5l-tph-plus.yaml \ --name v5l-tph-plus \ --device 0 \ --save-period 10 # Multi-GPU distributed training python -m torch.distributed.launch \ --nproc_per_node 4 \ train.py \ --img 1536 \ --batch 16 \ --epochs 80 \ --data ./data/VisDrone.yaml \ --weights yolov5l.pt \ --cfg models/yolov5l-tph-plus.yaml \ --device 0,1,2,3 # Resume interrupted training python train.py \ --resume runs/train/v5l-tph-plus/weights/last.pt \ --device 0 ``` -------------------------------- ### Ensemble Inference Results using Weighted Boxes Fusion Source: https://github.com/cv516buaa/tph-yolov5/blob/main/README.md Combines inference results from different models using weighted boxes fusion (WBF). Users need to specify the image and text file paths within the wbf.py script. ```bash python wbf.py ``` -------------------------------- ### Custom YOLOv5 Model Architecture Configuration Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt Defines a custom YOLOv5 model architecture named 'yolov5l-tph-plus.yaml', featuring a Swin Transformer in the prediction head and a CLLA Detect head. This configuration specifies backbone, neck, and head components with layer definitions and connections. ```yaml # models/yolov5l-tph-plus.yaml - TPH-YOLOv5++ configuration # Features: Swin Transformer in prediction head, CLLA Detect head # Key architectural components: nc: 10 # number of classes (VisDrone) depth_multiple: 1.0 # model depth scaling width_multiple: 1.0 # channel scaling anchors: 3 # auto-anchors # Backbone: Standard YOLOv5 with modifications backbone: - [-1, 1, Conv, [64, 6, 2, 2]] # P1/2 - [-1, 1, Conv, [128, 3, 2]] # P2/4 - [-1, 3, C3, [128]] - [-1, 1, Conv, [256, 3, 2]] # P3/8 - [-1, 6, C3, [256]] - [-1, 1, Conv, [512, 3, 2]] # P4/16 - [-1, 9, C3, [512]] - [-1, 1, Conv, [1024, 3, 2]] # P5/32 - [-1, 3, C3, [1024]] - [-1, 1, SPPF, [1024, 5]] # Spatial Pyramid Pooling # Head: Custom TPH with Swin Transformer head: - [-1, 1, Conv, [512, 1, 1]] - [-1, 1, nn.Upsample, [None, 2, 'nearest']] - [[-1, 6], 1, Concat, [1]] # Skip connection from P4 - [-1, 3, C3, [512, False]] - [-1, 1, Conv, [256, 1, 1]] - [-1, 1, nn.Upsample, [None, 2, 'nearest']] - [[-1, 4], 1, Concat, [1]] # Skip connection from P3 - [-1, 3, C3, [256, False]] # P3 detection layer - [-1, 1, Conv, [256, 3, 2]] - [[-1, 14], 1, Concat, [1]] - [-1, 3, C3, [512, False]] # P4 detection layer - [-1, 1, Conv, [512, 3, 2]] - [[-1, 10], 1, Concat, [1]] - [-1, 1, C3STR, [1024, False]] # P5 with Swin Transformer - [[2, 17, 20, 23], 1, CLLADetect, [nc, anchors]] # Multi-scale detection ``` -------------------------------- ### Plot YOLOv5 Training Results Source: https://github.com/cv516buaa/tph-yolov5/blob/main/tutorial.ipynb Plots YOLOv5 training results from a CSV file into a PNG image. Requires the 'utils.plots' module and a 'results.csv' file as input. ```python from utils.plots import plot_results plot_results('path/to/results.csv') # plot 'results.csv' as 'results.png' ``` -------------------------------- ### YOLOv5 Model Loading and Inference with NMS Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt Loads a trained YOLOv5 model programmatically using `attempt_load` and performs inference on a dummy image. It then applies Non-Maximum Suppression (NMS) to filter detected bounding boxes based on confidence and IoU thresholds. ```python # Load model programmatically import torch from models.experimental import attempt_load # Load trained model model = attempt_load('./weights/yolov5l-xs-1.pt', device='cuda:0') model.eval() # Run inference img = torch.zeros(1, 3, 640, 640).cuda() pred = model(img, augment=False)[0] # Apply NMS from utils.general import non_max_suppression pred = non_max_suppression( pred, conf_thres=0.25, iou_thres=0.45, classes=None, agnostic=False, max_det=1000 ) # Process results for det in pred: # per image if len(det): # Detections: [x1, y1, x2, y2, conf, class] for *xyxy, conf, cls in det: print(f"Class: {int(cls)}, Conf: {conf:.3f}, Box: {xyxy}") ``` -------------------------------- ### SPPF and ASPP Spatial Pooling Initialization and Usage Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt Demonstrates the initialization and usage of two advanced spatial pooling modules: SPPF (Fast Spatial Pyramid Pooling) and ASPP (Atrous Spatial Pyramid Pooling). SPPF uses a kernel size, while ASPP uses a tuple of kernel sizes for atrous convolutions. ```python from models.common import SPPF, ASPP # Fast Spatial Pyramid Pooling sppf = SPPF(c1=1024, c2=1024, k=5) x = torch.randn(2, 1024, 32, 32) pooled = sppf(x) # Atrous Spatial Pyramid Pooling aspp = ASPP(c1=1024, c2=1024, k=(5, 9, 13)) pooled_aspp = aspp(x) ``` -------------------------------- ### Perform Inference with TPH-YOLOv5 Source: https://github.com/cv516buaa/tph-yolov5/blob/main/README.md Runs inference on the VisDrone dataset using pre-trained TPH-YOLOv5 weights. Supports various options for image size, data augmentation, saving results, and batch processing. ```bash python val.py --weights ./weights/yolov5l-xs-1.pt --img 1996 --data ./data/VisDrone.yaml yolov5l-xs-2.pt --augment --save-txt --save-conf --task val --batch-size 8 --verbose --name v5l-xs ``` -------------------------------- ### Export YOLOv5 to TorchScript for Mobile Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt Exports a YOLOv5 model to TorchScript format, optimized for mobile deployment. Requires weights, output image size, and enables optimization. ```python python export.py \ --weights ./weights/yolov5l-xs-2.pt \ --include torchscript \ --img 640 640 \ --optimize ``` -------------------------------- ### Rebase local branch to upstream master Source: https://github.com/cv516buaa/tph-yolov5/blob/main/CONTRIBUTING.md This bash script demonstrates how to update your local feature branch to be in sync with the upstream master branch. It involves adding the upstream remote, fetching changes, checking out your branch, merging upstream/master, and force-pushing to your origin. Ensure you replace 'feature' with your actual local branch name. ```bash git remote add upstream https://github.com/ultralytics/yolov5.git git fetch upstream git checkout feature # <----- replace 'feature' with local branch name git merge upstream/master git push -u origin -f ``` -------------------------------- ### Export YOLOv5 to CoreML for iOS Deployment Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt Exports a YOLOv5 model to CoreML format for iOS deployment. Requires weights, output image size, and enables half-precision. ```python python export.py \ --weights ./weights/yolov5l-xs-1.pt \ --include coreml \ --img 640 640 \ --half ``` -------------------------------- ### Using Custom Transformer Blocks in YOLOv5 Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt Illustrates the usage of custom transformer blocks within the YOLOv5 model architecture. This typically involves defining or importing these blocks in files like 'models/common.py' and referencing them in the model configuration YAML. ```python # Using custom transformer blocks in models/common.py ``` -------------------------------- ### Swin Transformer Block Initialization and Usage Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt Initializes and demonstrates the forward pass of a Swin Transformer block, a component used for spatial attention. It takes input channels, output channels, number of attention heads, transformer layers, and window size as parameters. The input shape is [batch, channels, height, width] and the output shape remains the same. ```python from models.common import SwinTransformerBlock # Initialize Swin Transformer block # c1: input channels, c2: output channels # num_heads: attention heads, num_layers: transformer depth swin_block = SwinTransformerBlock( c1=1024, c2=1024, num_heads=32, # 1024 // 32 = 32 dim per head num_layers=3, # 3 transformer layers window_size=8 # 8x8 window attention ) # Forward pass: input shape [batch, 1024, H, W] # Output shape: [batch, 1024, H, W] x = torch.randn(2, 1024, 32, 32) out = swin_block(x) ``` -------------------------------- ### CBAM Attention Module Initialization and Usage Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt Initializes and demonstrates the application of the CBAM (Convolutional Block Attention Module) for channel and spatial attention. It takes input and output channels as parameters and applies attention to the input tensor. ```python from models.common import CBAM cbam = CBAM(c1=512, c2=512) x = torch.randn(2, 512, 64, 64) attended = cbam(x) # Channel + Spatial attention ``` -------------------------------- ### Export YOLOv5 to ONNX Format Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt Exports a YOLOv5 model to the ONNX format for general deployment. Requires weights, output image size, and optionally enables dynamic axes and ONNX simplification. ```python python export.py \ --weights ./weights/yolov5l-xs-1.pt \ --include onnx \ --img 640 640 \ --dynamic \ --simplify \ --opset 13 ``` -------------------------------- ### Export YOLOv5 with INT8 Quantization for Edge Devices Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt Exports a YOLOv5 model to TFLite format with INT8 quantization, suitable for edge devices. Requires weights, data configuration, and specified image dimensions. ```python python export.py \ --weights ./weights/yolov5l-xs-2.pt \ --include tflite \ --int8 \ --data ./data/VisDrone.yaml \ --img 320 320 ``` -------------------------------- ### Export YOLOv5 to TensorFlow Formats Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt Exports a YOLOv5 model to TensorFlow formats (SavedModel and PB). Requires weights, output image size, and batch size. ```python python export.py \ --weights ./weights/yolov5l-xs-1.pt \ --include saved_model pb tflite \ --img 640 640 \ --batch-size 1 ``` -------------------------------- ### Weighted Boxes Fusion Ensemble for YOLOv5 Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt Ensembles predictions from multiple YOLOv5 models using the Weighted Boxes Fusion (WBF) algorithm to improve accuracy and reduce false positives. Requires configuration of image paths, prediction directories, output paths, and WBF parameters like IoU threshold and confidence threshold. ```python # Configure wbf.py with your paths and parameters: IMG_PATH = '/VisDrone2019-DET-test-challenge/images/' TXT_PATH = './runs/val/' # Contains subdirs for each model OUT_PATH = './runs/wbf_labels/' # WBF parameters iou_thr = 0.67 # IoU threshold for matching boxes skip_box_thr = 0.01 # Minimum confidence to include box sigma = 0.1 # Variance for weighted fusion # Run ensemble python wbf.py ``` -------------------------------- ### Validate TPH-YOLOv5 Models with Python Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt These Python commands are used to validate TPH-YOLOv5 model performance on a specified dataset. They allow for various configurations including weights, image sizes, augmentation, saving predictions, tasks (val, speed, study), and different metric thresholds. ```python # Validate TPH-YOLOv5 on VisDrone validation set python val.py \ --weights ./weights/yolov5l-xs-1.pt \ --img 1996 \ --data ./data/VisDrone.yaml \ --augment \ --save-txt \ --save-conf \ --task val \ --batch-size 8 \ --verbose \ --name v5l-xs # Validate with different IoU threshold python val.py \ --weights ./weights/yolov5l-xs-2.pt \ --img 1996 \ --data ./data/VisDrone.yaml \ --iou-thres 0.65 \ --conf-thres 0.001 \ --batch-size 8 \ --save-json # For COCO metrics # Speed benchmark on different image sizes python val.py \ --task speed \ --data ./data/VisDrone.yaml \ --weights ./weights/yolov5l-xs-1.pt \ --batch-size 1 \ --img 640 1280 1536 1996 # Study model performance across image sizes python val.py \ --task study \ --data ./data/VisDrone.yaml \ --weights ./weights/yolov5l-xs-1.pt ./weights/yolov5l-xs-2.pt \ --iou-thres 0.7 ``` -------------------------------- ### Convert VisDrone Annotations to YOLO Format Source: https://context7.com/cv516buaa/tph-yolov5/llms.txt Converts annotations from the VisDrone dataset format to the YOLO format. This script requires manual configuration of paths within the script before execution. It transforms bounding box coordinates and removes score, truncation, and occlusion information. ```python # VisDrone2YOLO_lable.py configuration YOLO_LABELS_PATH = "../datasets/VisDrone/VisDrone2019-DET-val/labels" VISANN_PATH = "../datasets/VisDrone/VisDrone2019-DET-val/annotations/" VISIMG_PATH = "../datasets/VisDrone/VisDrone2019-DET-val/images/" # Run conversion python VisDrone2YOLO_lable.py ``` -------------------------------- ### Convert VisDrone Labels to YOLO Format Source: https://github.com/cv516buaa/tph-yolov5/blob/main/README.md Converts VisDrone dataset annotations to YOLO label format using the provided Python script. Ensure the dataset path is correctly set within the script. ```bash python VisDrone2YOLO_lable.py ``` -------------------------------- ### Perform Object Detection Request using curl Source: https://github.com/cv516buaa/tph-yolov5/blob/main/utils/flask_rest_api/README.md This curl command sends a POST request with an image file to the Flask API endpoint for YOLOv5s object detection. The API processes the image and returns detection results. ```shell curl -X POST -F image=@zidane.jpg 'http://localhost:5000/v1/object-detection/yolov5s' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.