### Download ONNX to TensorRT Converter (Bash) Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7-Dynamic-Batch-TENSORRT.ipynb This snippet clones the YOLO-TensorRT8 repository from GitHub, which contains tools for converting ONNX models to TensorRT. It requires Git to be installed and an internet connection. ```bash #!/bin/bash git clone https://github.com/triple-Mu/YOLO-TensorRT8.git ``` -------------------------------- ### Download Pre-trained YOLOv5 Weights (Shell) Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5s6.ipynb This command downloads the pre-trained 'yolov5s6.pt' weights file from the YOLOv5 GitHub releases. These weights are crucial for using the YOLOv5 model for detection or further training without starting from scratch. ```shell !wget https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5s6.pt ``` -------------------------------- ### Navigate and List Directory Contents Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6.ipynb These commands change the current directory to '/content/' and then list the contents of that directory. This is useful for verifying the presence of files and directories after initial setup or downloads. ```shell %cd /content/ !ls ``` -------------------------------- ### Environment Setup and YOLOv5 Cloning (Shell) Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5s6.ipynb This snippet demonstrates how to change the directory to '/content/', list its contents, clone the YOLOv5 repository from GitHub, change into the cloned directory, checkout a specific commit, and list the contents of the YOLOv5 directory. It's essential for setting up the environment for YOLOv5 operations. ```shell %cd /content/ !ls !git clone https://github.com/ultralytics/yolov5 %cd yolov5 !git checkout 0b5ac224aef287ac3ac9ebf70ade60159450a0b1 !ls ``` -------------------------------- ### Setup YOLOv7 Docker Environment with GPU Support Source: https://context7.com/wongkinyiu/yolov7/llms.txt This snippet demonstrates how to create and configure a Docker container with NVIDIA GPU support for running YOLOv7. It includes mounting volumes for data and code, setting shared memory size, and installing necessary system and Python packages. The snippet also shows how to verify GPU availability and run object detection. ```bash # Create Docker container with NVIDIA GPU support nvidia-docker run --name yolov7 -it \ -v /path/to/coco:/coco/ \ -v /path/to/yolov7:/yolov7 \ --shm-size=64g \ nvcr.io/nvidia/pytorch:21.08-py3 # Install required system packages apt update apt install -y zip htop screen libgl1-mesa-glx # Install Python dependencies pip install seaborn thop # Navigate to code directory cd /yolov7 # Verify GPU availability python -c "import torch; print(torch.cuda.is_available())" # Run detection inside container python detect.py --weights yolov7.pt --source /coco/val2017/ ``` -------------------------------- ### Docker Installation for YOLOv7 Source: https://github.com/wongkinyiu/yolov7/blob/main/README.md This snippet outlines the steps to set up a Docker environment for YOLOv7. It includes commands to create a container, mount volumes for data and code, and install necessary system and Python packages. This is the recommended installation method for YOLOv7. ```shell # create the docker container, you can change the share memory size if you have more. nvidia-docker run --name yolov7 -it -v your_coco_path/:/coco/ -v your_code_path/:/yolov7 --shm-size=64g nvcr.io/nvidia/pytorch:21.08-py3 # apt install required packages apt update apt install -y zip htop screen libgl1-mesa-glx # pip install required packages pip install seaborn thop # go to code folder cd /yolov7 ``` -------------------------------- ### Download YOLOv5 Weights Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7e6_vs_YOLOv5x6.ipynb Downloads pre-trained YOLOv5 weights using wget. This is a prerequisite for running detection. ```python #!/usr/bin/env python # Download trained weights !wget https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5x6.pt ``` -------------------------------- ### Download YOLOv7 Weights Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7CoreML.ipynb This snippet downloads the pre-trained 'yolov7-tiny.pt' weights using wget. This is a necessary step before running inference. ```python !wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-tiny.pt ``` -------------------------------- ### Install YOLOv7 Dependencies and Check Environment (Python) Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7onnx.ipynb This snippet demonstrates the installation of the onnxruntime-gpu package and its dependencies. It also includes Python code to print the system's Python and PyTorch versions, confirming the environment setup. Warnings about running pip as root and un-warned script locations are noted. ```shell # pip install onnxruntime-gpu # ... (dependency installation output) ``` ```python import sys import torch print(f"Python version: {sys.version}, {sys.version_info} ") print(f"Pytorch version: {torch.__version__} ") ``` -------------------------------- ### Install YOLOv7 Dependencies and onnxruntime-gpu Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7trt.ipynb Installs necessary Python packages for YOLOv7, including pycuda, pytools, mako, platformdirs, and onnxruntime-gpu. It also includes warnings about running pip as root and potential PATH issues for installed scripts. ```bash pip install pycuda pytools mako platformdirs pip install onnxruntime-gpu ``` -------------------------------- ### Prepare COCO Dataset Annotations Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7e6_vs_YOLOv5x6.ipynb Creates a directory for COCO dataset annotations and downloads the annotation zip file. It then unzips the annotations into the created directory. ```bash !mkdir /content/yolov5/coco/ %cd /content/yolov5/coco/ !wget http://images.cocodataset.org/annotations/annotations_trainval2017.zip !ls !unzip -o annotations_trainval2017.zip !ls ``` -------------------------------- ### Install YOLOv7 Dependencies with Pip Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7CoreML.ipynb Installs or upgrades essential Python packages like setuptools, pip, onnx, and coremltools using pip. These packages are crucial for the functionality and deployment of YOLOv7 models. ```python !pip install --upgrade setuptools pip --user !pip install onnx !pip install coremltools>=4.1 ``` -------------------------------- ### List YOLOv7 Model Files Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7CoreML.ipynb Lists the files present in the YOLOv7 project directory. This helps in identifying available model files and scripts. ```shell # show ONNX model !ls ``` -------------------------------- ### Load TensorRT Engine and Initialize Bindings Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7trt.ipynb Loads a pre-compiled TensorRT engine from a file and initializes the necessary bindings for input and output tensors. This setup is crucial for running inference with the YOLOv7 model. ```python # Infer TensorRT Engine Binding = namedtuple('Binding', ('name', 'dtype', 'shape', 'data', 'ptr')) logger = trt.Logger(trt.Logger.INFO) trt.init_libnvinfer_plugins(logger, namespace="") with open(w, 'rb') as f, trt.Runtime(logger) as runtime: model = runtime.deserialize_cuda_engine(f.read())indings = OrderedDict() for index in range(model.num_bindings): name = model.get_binding_name(index) dtype = trt.nptype(model.get_binding_dtype(index)) shape = tuple(model.get_binding_shape(index)) data = torch.from_numpy(np.empty(shape, dtype=np.dtype(dtype))).to(device) bindings[name] = Binding(name, dtype, shape, data, int(data.data_ptr())) binding_addrs = OrderedDict((n, d.ptr) for n, d in bindings.items()) context = model.create_execution_context() ``` -------------------------------- ### Load TensorRT Engine and Prepare Images Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7-Dynamic-Batch-TENSORRT.ipynb Initializes the TensorRT inference engine by specifying the engine path and sets up the CUDA device. It then loads a list of sample images using OpenCV and duplicates them to create a batch for inference. ```python w = './yolov7-tiny-nms.trt' device = torch.device('cuda:0') imgList = [cv2.imread('../inference/images/horses.jpg'), cv2.imread('../inference/images/bus.jpg'), cv2.imread('../inference/images/zidane.jpg'), cv2.imread('../inference/images/image1.jpg'), cv2.imread('../inference/images/image2.jpg'), cv2.imread('../inference/images/image3.jpg')] # Load sample images imgList*=6 # Duplicate images to create a larger batch imgList = imgList[:32] # Ensure the batch size is 32 ``` -------------------------------- ### List Files After Model Export Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7-Dynamic-Batch-TENSORRT.ipynb This command lists all files in the current directory after the model export process. It helps verify that the ONNX model file (e.g., yolov7-tiny.onnx) and other related files have been generated or modified. ```bash !ls ``` -------------------------------- ### Download YOLOv5 Pre-trained Weights Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6.ipynb This command downloads the pre-trained YOLOv5m6 weights using wget. The weights are downloaded from a specific release URL on the YOLOv5 GitHub repository. The output confirms the download process. ```bash !# Download trained weights !wget https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5m6.pt ``` -------------------------------- ### Install YOLOv7 Dependencies using Pip Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6.ipynb Installs or upgrades essential Python packages required for YOLOv7, including setuptools, pip, PyYAML, and pycocotools. It uses pip commands to manage package installations, ensuring compatibility and functionality. ```python !pip install --upgrade setuptools pip --user !pip install --ignore-installed PyYAML !pip install pycocotools>=2.0 ``` -------------------------------- ### Test YOLOv7 Model with Batch Size 1 Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6_half.ipynb This Python command runs the `test.py` script for YOLOv7 evaluation with a batch size of 1. It configures the data, image dimensions, thresholds, device, and weights. The output shows the parsed arguments, model summary, and indicates the start of the testing process. ```python !python test.py --data data/coco.yaml --img 640 --batch 1 --conf 0.001 --iou 0.65 --device 0 --weights yolov7.pt --name yolov7_640_val ``` -------------------------------- ### Check Python and PyTorch Versions Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7CoreML.ipynb Prints the current Python version and the installed PyTorch version. This is useful for ensuring compatibility with YOLOv7 requirements and for debugging. ```python import sys import torch print(f"Python version: {sys.version}, {sys.version_info} ") print(f"Pytorch version: {torch.__version__} ") ``` -------------------------------- ### Install Triton Client and Dependencies Source: https://github.com/wongkinyiu/yolov7/blob/main/deploy/triton-inference-server/README.md Installs the necessary Python packages for interacting with the Triton Inference Server, including all client functionalities and OpenCV for image processing. ```bash pip3 install tritonclient[all] opencv-python ``` -------------------------------- ### Validate YOLOv7 Model with Batch Size 1 Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6_half.ipynb This command executes the YOLOv7 validation script (`val.py`) using a batch size of 1. Similar to the batch size 32 example, it configures the data source, image dimensions, thresholds, device, and weights. The output is saved under `yolov5m6_1280_val` (potentially in a subfolder if the previous run exists). The `--half` option is also utilized here for performance. ```bash !python val.py --data data/coco.yaml --img 1280 --batch 1 --conf 0.001 --iou 0.65 --device 0 --weights yolov5m6.pt --name yolov5m6_1280_val --half ``` -------------------------------- ### Load TensorRT Engine and Create Context Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7-Dynamic-Batch-TENSORRT.ipynb Initializes the TensorRT logger, loads a serialized CUDA engine from a file, and creates an execution context for inference. This is a prerequisite for running inference. ```python logger = trt.Logger(trt.Logger.INFO) trt.init_libnvinfer_plugins(logger, namespace="") with open(w, 'rb') as f, trt.Runtime(logger) as runtime: model = runtime.deserialize_cuda_engine(f.read()) context = model.create_execution_context() ``` -------------------------------- ### Install and Upgrade Pip and Setuptools Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7onnx.ipynb Upgrades pip and setuptools to their latest versions. This is a foundational step for managing Python packages effectively. It ensures that package installation and management commands function correctly. It may also resolve compatibility issues with other packages. ```python !pip install --upgrade setuptools pip --user ``` -------------------------------- ### Download YOLOv7 Trained Weights Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6.ipynb Downloads the pre-trained yolov7.pt weights file from the official GitHub releases. This file is essential for running inference and training the model. ```python !# Download trained weights !wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt ``` -------------------------------- ### Download YOLOv7 Trained Weights Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5s6.ipynb This code snippet downloads the pre-trained YOLOv7 weights from a GitHub release URL using wget. It's a prerequisite for running inference with the model. The downloaded file will be named 'yolov7.pt'. ```shell !wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt ``` -------------------------------- ### Return to YOLOv7 Root Directory Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6.ipynb Navigates back to the root directory of the YOLOv7 project. This command is typically used after performing operations within subdirectories like 'coco'. ```python %cd /content/yolov7/ !ls ``` -------------------------------- ### Navigate to YOLOv5 Directory Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6.ipynb This command changes the current working directory back to the root of the YOLOv5 project. It then lists the files and directories within the YOLOv5 project to show the available scripts and data. ```shell %cd /content/yolov5/ !ls ``` -------------------------------- ### YOLOv7 Batch Inference with ONNX Runtime Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7-Dynamic-Batch-ONNXRUNTIME.ipynb This snippet shows how to prepare a batch of images for inference with a YOLOv7 model using ONNX Runtime. It converts the NumPy array to a contiguous format and runs the session to get the detection outputs. The input is a NumPy array representing a batch of images, and the output is a list containing the detection results. ```python im = np.ascontiguousarray(np_batch/255) out = session.run(outname,{'images':im})[0] ``` -------------------------------- ### Download YOLOv5 Weights Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5s6.ipynb This snippet shows how to download pre-trained YOLOv5 weights (yolov5s6.pt) from GitHub. It uses wget to fetch the file and saves it to the current directory. The process involves resolving the host, connecting, and handling HTTP redirects. ```shell --2022-07-27 05:35:38-- https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5s6.pt Resolving github.com (github.com)... 20.205.243.166 Connecting to github.com (github.com)|20.205.243.166|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/264818686/e557ec4e-f775-4028-a4e3-b3dde2eaf281?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220727%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220727T053538Z&X-Amz-Expires=300&X-Amz-Signature=32aac0254eda508856f31e57ea00fadf54a3cba6bb6949f257f464c0ce6032d1&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=264818686&response-content-disposition=attachment%3B%20filename%3Dyolov5s6.pt&response-content-type=application%2Foctet-stream [following] --2022-07-27 05:35:38-- https://objects.githubusercontent.com/github-production-release-asset-2e65be/264818686/e557ec4e-f775-4028-a4e3-b3dde2eaf281?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220727%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220727T053538Z&X-Amz-Expires=300&X-Amz-Signature=32aac0254eda508856f31e57ea00fadf54a3cba6bb6949f257f464c0ce6032d1&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=264818686&response-content-disposition=attachment%3B%20filename%3Dyolov5s6.pt&response-content-type=application%2Foctet-stream Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.108.133, 185.199.110.133, 185.199.109.133, ... Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.108.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 25978837 (25M) [application/octet-stream] Saving to: ‘yolov5s6.pt’ yolov5s6.pt 100%[===================>] 24.77M 9.58MB/s in 2.6s 2022-07-27 05:35:41 (9.58 MB/s) - ‘yolov5s6.pt’ saved [25978837/25978837] ``` -------------------------------- ### Download YOLOv7-Tiny Weights (Python) Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7trt.ipynb This command downloads the pre-trained 'yolov7-tiny' weights file. These weights are essential for running object detection inference without needing to train a model from scratch. The file is saved in the current directory. ```python !# Download trained weights !wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-tiny.pt ``` -------------------------------- ### YOLOv7 Model Warmup and Inference Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7trt.ipynb This snippet shows the warmup phase for the YOLOv7 model by running inference 10 times. It then measures the time taken for a single inference pass, preparing the model for actual detection. ```python for _ in range(10): tmp = torch.randn(1,3,640,640).to(device) binding_addrs['images'] = int(tmp.data_ptr()) context.execute_v2(list(binding_addrs.values())) start = time.perf_counter() binding_addrs['images'] = int(im.data_ptr()) context.execute_v2(list(binding_addrs.values())) print(f'Cost {time.perf_counter()-start} s') ``` -------------------------------- ### Install ONNX Simplifier Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7onnx.ipynb Installs ONNX Simplifier, a tool for optimizing ONNX models. It helps reduce model size and improve inference speed by removing redundant nodes and operations. This is often used as a post-processing step after model conversion. ```python !pip install onnx-simplifier>=0.3.6 --user ``` -------------------------------- ### Download YOLOv5 Model Weights Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6.ipynb This snippet shows the process of downloading YOLOv5 model weights (yolov5m6.pt) from GitHub releases. It resolves the host, connects to the server, and saves the file with a progress indicator. The file is of type application/octet-stream and has a size of 69M. ```shell --2022-07-27 05:16:23-- https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5m6.pt Resolving github.com (github.com)... 20.205.243.166 Connecting to github.com (github.com)|20.205.243.166|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/264818686/2328d51e-5f81-49d6-b225-d704addbf92d?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220727%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220727T051623Z&X-Amz-Expires=300&X-Amz-Signature=39ada192aa6101f1189abb45c07b869b47c0e878d2dad4856e902b1e89328657&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=264818686&response-content-disposition=attachment%3B%20filename%3Dyolov5m6.pt&response-content-type=application%2Foctet-stream [following] --2022-07-27 05:16:23-- https://objects.githubusercontent.com/github-production-release-asset-2e65be/264818686/2328d51e-5f81-49d6-b225-d704addbf92d?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220727%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220727T051623Z&X-Amz-Expires=300&X-Amz-Signature=39ada192aa6101f1189abb45c07b869b47c0e878d2dad4856e902b1e89328657&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=264818686&response-content-disposition=attachment%3B%20filename%3Dyolov5m6.pt&response-content-type=application%2Foctet-stream Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ... Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.108.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 72308357 (69M) [application/octet-stream] Saving to: ‘yolov5m6.pt’ yolov5m6.pt 100%[===================>] 68.96M 374MB/s in 0.2s 2022-07-27 05:16:24 (374 MB/s) - ‘yolov5m6.pt’ saved [72308357/72308357] ``` -------------------------------- ### Clone YOLOv7 Repository and List Contents Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7CoreML.ipynb Clones the official YOLOv7 repository from GitHub and then lists the contents of the cloned directory. This step is essential for obtaining the YOLOv7 source code and associated files. ```bash !# Download YOLOv7 code !git clone https://github.com/WongKinYiu/yolov7 %cd yolov7 !ls ``` -------------------------------- ### Install ONNX and ONNX Runtime Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7onnx.ipynb Installs the ONNX (Open Neural Network Exchange) format and ONNX Runtime. ONNX is used for model interoperability, and ONNX Runtime provides an efficient inference engine. This is crucial for deploying models across different platforms. It requires specific versions of protobuf. ```python !pip install onnx !pip install onnxruntime ``` -------------------------------- ### Prepare COCO Dataset Directory Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7e6_vs_YOLOv5x6_half.ipynb Creates a directory for the COCO dataset and changes the current working directory to it. This is typically done before downloading COCO dataset annotations. ```shell #!/bin/bash !mkdir /content/yolov5/coco/ %cd /content/yolov5/coco/ ``` -------------------------------- ### Navigate and List YOLO-TensorRT8 Directory Contents (Python) Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7-Dynamic-Batch-TENSORRT.ipynb This Python snippet changes the current directory to the cloned YOLO-TensorRT8 folder and then lists its contents. This is a preparatory step before running conversion scripts. It assumes the repository has already been cloned. ```python #!/bin/bash %cd YOLO-TensorRT8 !ls ``` -------------------------------- ### Run Object Detection with YOLOv5 Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7e6_vs_YOLOv5x6_half.ipynb Executes the object detection script (`detect.py`) using downloaded YOLOv5 weights. It specifies the image to analyze, confidence threshold, and image size. The output will be saved in the `runs/detect` directory. ```python from PIL import Image # Run detection !python detect.py --weights ./yolov5x6.pt --conf 0.25 --img-size 1280 --source data/images/bus.jpg # Display the detected image Image.open('/content/yolov5/runs/detect/exp/bus.jpg') ``` -------------------------------- ### Download COCO Annotations Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6.ipynb Downloads the COCO dataset annotations zip file. This file contains annotations for training and validation sets, including instances, captions, and keypoints. ```python !wget http://images.cocodataset.org/annotations/annotations_trainval2017.zip !ls ``` -------------------------------- ### Download COCO Annotations Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7e6_vs_YOLOv5x6_half.ipynb Downloads the COCO dataset annotations using `wget`. This command fetches a zip file containing annotations for training and validation sets. The file is saved in the current directory. ```shell #!/bin/bash !wget http://images.cocodataset.org/annotations/annotations_trainval2017.zip !ls ``` -------------------------------- ### Load and Preprocess Image for Inference Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7CoreML.ipynb Loads an image using OpenCV and PIL, resizes it to a specified dimension (640x640), and prints its shape and PIL representation. This is a common preprocessing step before feeding an image into a model for inference. ```python # Load image from PIL import Image import cv2 import numpy as np im = cv2.imread('/content/yolov7/inference/images/horses.jpg') im = cv2.resize(im, (640, 640)) print(f" im = {im.shape}") b = 1 h, w, ch = im.shape im = Image.fromarray((im).astype('uint8')) print(f" im = {im}") ``` -------------------------------- ### Clone YOLOv5 Repository and Checkout Specific Commit (Shell) Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6_half.ipynb This sequence of shell commands clones the official YOLOv5 repository from GitHub and then checks out a specific commit hash. This is useful for ensuring reproducibility by using a known stable version of the codebase. ```bash # Download YOLOv5 code git clone https://github.com/ultralytics/yolov5 %cd yolov5 git checkout 0b5ac224aef287ac3ac9ebf70ade60159450a0b1 ``` -------------------------------- ### Display NVIDIA GPU Information Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7CoreML.ipynb Executes the nvidia-smi command to display detailed information about the NVIDIA GPU, including driver version, CUDA version, and GPU utilization. This helps in verifying GPU availability and performance. ```bash !nvidia-smi ``` -------------------------------- ### Run YOLOv7 Inference Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7CoreML.ipynb This snippet executes the object detection inference using the 'detect.py' script. It specifies the downloaded weights, confidence threshold, image size, and the input image source. The output will be saved in the 'runs/detect/exp' directory. ```python !python detect.py --weights ./yolov7-tiny.pt --conf 0.25 --img-size 640 --source inference/images/horses.jpg ``` -------------------------------- ### Image Preprocessing with Letterbox Function Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7-Dynamic-Batch-TENSORRT.ipynb Resizes and pads an input image to meet specified dimensions (defaulting to 640x640) while ensuring dimensions are multiples of a stride. It returns the processed image, scaling ratio, and padding values. ```python def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleup=True, stride=32): # Resize and pad image while meeting stride-multiple constraints shape = im.shape[:2] # current shape [height, width] if isinstance(new_shape, int): new_shape = (new_shape, new_shape) # Scale ratio (new / old) r = min(new_shape[0] / shape[0], new_shape[1] / shape[1]) if not scaleup: # only scale down, do not scale up (for better val mAP) r = min(r, 1.0) # Compute padding new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r)) # wh padding dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding if auto: # minimum rectangle dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding dw /= 2 # divide padding into 2 sides dh /= 2 if shape[::-1] != new_unpad: # resize im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR) top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border return im, r, (dw, dh) ``` -------------------------------- ### Initialize ONNX Runtime Inference Session Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7-Dynamic-Batch-ONNXRUNTIME.ipynb Creates an ONNX Runtime inference session. It specifies the execution providers, prioritizing CUDA if available, otherwise falling back to CPU. The session is initialized with the ONNX model file path. ```python providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if cuda else ['CPUExecutionProvider'] session = ort.InferenceSession(w, providers=providers) ``` -------------------------------- ### Setup YOLOv7 ONNX Inference Session Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7onnx.ipynb Initializes the ONNX Runtime inference session for a YOLOv7 model. It sets up the execution providers (CUDA or CPU) based on availability and loads the ONNX model. Dependencies include onnxruntime, cv2, and numpy. ```python import cv2 import time import requests import random import numpy as np import onnxruntime as ort from PIL import Image from pathlib import Path from collections import OrderedDict,namedtuple cuda = True w = "/content/yolov7/yolov7-tiny.onnx" img = cv2.imread('/content/yolov7/inference/images/horses.jpg') providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if cuda else ['CPUExecutionProvider'] session = ort.InferenceSession(w, providers=providers) ``` -------------------------------- ### Download ONNX to TensorRT Converter (Python) Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7trt.ipynb This snippet downloads the tensorrt-python repository, which contains tools for converting ONNX models to TensorRT format. It changes the directory to the parent and then clones the repository. Ensure you have git installed. ```python # Download ONNX to TensorRT converter %cd ../ !git clone https://github.com/Linaom1214/tensorrt-python.git ``` -------------------------------- ### Get ONNX Runtime Session Input/Output Names in Python Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7-Dynamic-Batch-ONNXRUNTIME.ipynb Retrieves the names of the input and output tensors from an ONNX Runtime session. This is necessary for correctly feeding data to the model and interpreting the results. Dependencies include ONNX Runtime (`onnxruntime`). ```python import onnxruntime as ort # Assuming 'session' is an initialized ONNX Runtime session object outname = [i.name for i in session.get_outputs()] print(outname) inname = [i.name for i in session.get_inputs()] print(inname) ``` -------------------------------- ### Install ONNX Runtime GPU Version Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7onnx.ipynb Installs the GPU-accelerated version of ONNX Runtime. This significantly speeds up inference on compatible hardware. It requires a system with a CUDA-enabled GPU and appropriate drivers. This installation is an alternative to the CPU version for performance-critical applications. ```python !pip install onnxruntime-gpu ``` -------------------------------- ### YOLOv7 Model Warmup and Batch Inference (Python) Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7-Dynamic-Batch-TENSORRT.ipynb This snippet demonstrates how to perform a warmup run for the YOLOv7 model to ensure accurate timing for subsequent inferences. It then proceeds to process batches of images with sizes 1, 16, and 32, measuring the inference time for each batch size. This helps in understanding the model's performance characteristics with different batch sizes. ```python bindings = getBindings(model,context,(4,3,640,640)) binding_addrs = OrderedDict((n, d.ptr) for n, d in bindings.items()) for _ in range(10): tmp = torch.randn(4,3,640,640).to(device) binding_addrs['images'] = int(tmp.data_ptr()) context.execute_v2(list(binding_addrs.values())) ``` ```python np_batch = np.concatenate([data[0] for data in resize_data]) np_batch.shape ``` ```python batch_1 = torch.from_numpy(np_batch[0:1]).to(device)/255 bindings = getBindings(model,context,(1,3,640,640)) binding_addrs = OrderedDict((n, d.ptr) for n, d in bindings.items()) print("batch==1") start = time.perf_counter() binding_addrs['images'] = int(batch_1.data_ptr()) context.execute_v2(list(binding_addrs.values())) print(f'Cost {time.perf_counter()-start} s') ``` ```python batch_16 = torch.from_numpy(np_batch[0:16]).to(device)/255 bindings = getBindings(model,context,(16,3,640,640)) binding_addrs = OrderedDict((n, d.ptr) for n, d in bindings.items()) print("batch==16") start = time.perf_counter() binding_addrs['images'] = int(batch_16.data_ptr()) context.execute_v2(list(binding_addrs.values())) print(f'Cost {time.perf_counter()-start} s') ``` ```python batch_32 = torch.from_numpy(np_batch[0:32]).to(device)/255 bindings = getBindings(model,context,(32,3,640,640)) binding_addrs = OrderedDict((n, d.ptr) for n, d in bindings.items()) print("batch==32") start = time.perf_counter() binding_addrs['images'] = int(batch_32.data_ptr()) context.execute_v2(list(binding_addrs.values())) print(f'Cost {time.perf_counter()-start} s') ``` -------------------------------- ### Run Object Detection with YOLOv5 Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6.ipynb This Python snippet executes the YOLOv5 detection script. It specifies the weights file, confidence threshold, image size, and the input image source. The output indicates the YOLOv5 version, environment details, layer fusion, and detection results for the specified image, along with inference speed and save location. ```python !python detect.py --weights ./yolov5m6.pt --conf 0.25 --img-size 1280 --source data/images/bus.jpg ``` -------------------------------- ### YOLOv7 Local Installation Requirements Source: https://context7.com/wongkinyiu/yolov7/llms.txt This snippet lists the Python dependencies required for a local installation of YOLOv7, as typically found in a requirements.txt file. It specifies version constraints for several key libraries including PyTorch, torchvision, OpenCV, and others essential for the project's functionality. ```text # Requirements for local installation (requirements.txt): # Base # matplotlib>=3.2.2 # numpy>=1.18.5,<1.24.0 # opencv-python>=4.1.1 # Pillow>=7.1.2 # PyYAML>=5.3.1 # torch>=1.7.0,!=1.12.0 # torchvision>=0.8.1,!=0.13.0 # tqdm>=4.41.0 ``` -------------------------------- ### Load and Prepare YOLOR Model for Reparameterization in Python Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/reparameterization.ipynb This snippet illustrates the initial setup for reparameterizing a YOLOR model. It involves importing necessary libraries, selecting a device, loading a trained model checkpoint, initializing a new model architecture, and copying relevant weights and parameters from the checkpoint to the new model. ```python from copy import deepcopy from models.yolo import Model import torch from utils.torch_utils import select_device, is_parallel import yaml device = select_device('0', batch_size=1) # model trained by cfg/training/*.yaml ckpt = torch.load('cfg/training/yolov7-d6_trainig.pt', map_location=device) # reparameterized model in cfg/deploy/*.yaml model = Model('cfg/deploy/yolov7-d6.yaml', ch=3, nc=80).to(device) with open('cfg/deploy/yolov7-d6.yaml') as f: yml = yaml.load(f, Loader=yaml.SafeLoader) anchors = len(yml['anchors'][0]) // 2 # copy intersect weights state_dict = ckpt['model'].float().state_dict() exclude = [] intersect_state_dict = {k: v for k, v in state_dict.items() if k in model.state_dict() and not any(x in k for x in exclude) and v.shape == model.state_dict()[k].shape} model.load_state_dict(intersect_state_dict, strict=False) model.names = ckpt['model'].names model.nc = ckpt['model'].nc idx = 162 idx2 = 166 ``` -------------------------------- ### Image Preprocessing Pipeline Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7-Dynamic-Batch-TENSORRT.ipynb Iterates through a list of images, converts them to RGB, applies the letterbox function for resizing and padding, transposes the dimensions for channel-first format, and expands dimensions for batch processing. The processed images are stored along with their scaling ratios and padding. ```python origin_RGB = [] resize_data = [] for img in imgList: img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) origin_RGB.append(img) image = img.copy() image, ratio, dwdh = letterbox(image, auto=False) image = image.transpose((2, 0, 1)) image = np.expand_dims(image, 0) image = np.ascontiguousarray(image) im = image.astype(np.float32) resize_data.append((im,ratio,dwdh)) ``` -------------------------------- ### Run YOLOv7 Inference Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7-Dynamic-Batch-TENSORRT.ipynb This command executes the YOLOv7 detection script to perform inference on specified images. It requires the path to the pre-trained weights, confidence threshold, image size, and the source directory for images. The output includes detected objects and model summary. ```bash !python detect.py --weights ./yolov7-tiny.pt --conf 0.25 --img-size 640 --source inference/images ``` -------------------------------- ### YOLOv7 Class Names Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6.ipynb This section lists the names of all object classes that the YOLOv7 model can detect. These names correspond to the numerical labels used in the dataset. ```text names: [ 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush' ] ``` -------------------------------- ### YOLOv7 Triton Client Script Help Source: https://github.com/wongkinyiu/yolov7/blob/main/deploy/triton-inference-server/README.md Displays the command-line help message for the YOLOv7 Triton client script, outlining available arguments for running inference in different modes (dummy, image, video) and configuring server connection. ```bash $ python3 client.py --help usage: client.py [-h] [-m MODEL] [--width WIDTH] [--height HEIGHT] [-u URL] [-o OUT] [-f FPS] [-i] [-v] [-t CLIENT_TIMEOUT] [-s] [-r ROOT_CERTIFICATES] [-p PRIVATE_KEY] [-x CERTIFICATE_CHAIN] {dummy,image,video} [input] positional arguments: {dummy,image,video} Run mode. 'dummy' will send an emtpy buffer to the server to test if inference works. 'image' will process an image. 'video' will process a video. input Input file to load from in image or video mode optional arguments: -h, --help show this help message and exit -m MODEL, --model MODEL Inference model name, default yolov7 --width WIDTH Inference model input width, default 640 --height HEIGHT Inference model input height, default 640 -u URL, --url URL Inference server URL, default localhost:8001 -o OUT, --out OUT Write output into file instead of displaying it -f FPS, --fps FPS Video output fps, default 24.0 FPS -i, --model-info Print model status, configuration and statistics -v, --verbose Enable verbose client output -t CLIENT_TIMEOUT, --client-timeout CLIENT_TIMEOUT Client timeout in seconds, default no timeout -s, --ssl Enable SSL encrypted channel to the server -r ROOT_CERTIFICATES, --root-certificates ROOT_CERTIFICATES File holding PEM-encoded root certificates, default none -p PRIVATE_KEY, --private-key PRIVATE_KEY File holding PEM-encoded private key, default is none -x CERTIFICATE_CHAIN, --certificate-chain CERTIFICATE_CHAIN File holding PEM-encoded certicate chain default is none ``` -------------------------------- ### Display Inference Result Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7CoreML.ipynb This snippet uses the Pillow library to open and display the resulting image after running inference. It assumes the output image is saved at '/content/yolov7/runs/detect/exp/horses.jpg'. ```python from PIL import Image Image.open('/content/yolov7/runs/detect/exp/horses.jpg') ``` -------------------------------- ### Unzip COCO Annotations Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6.ipynb Unzips the downloaded COCO annotations file. This command extracts the JSON annotation files into an 'annotations' subdirectory within the current 'coco' directory. ```python !unzip -o annotations_trainval2017.zip !ls ``` -------------------------------- ### Run YOLOv7 Test Script Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7e6_vs_YOLOv5x6_half.ipynb Executes the test.py script for YOLOv7 with specified parameters for data, image size, batch size, confidence threshold, IoU threshold, device, weights, and output name. It outputs evaluation metrics and saves predictions. ```python #!python test.py --data data/coco.yaml --img 1280 --batch 1 --conf 0.001 --iou 0.65 --device 0 --weights yolov7-e6.pt --name yolov7_e6_1280_val ``` -------------------------------- ### Postprocess Detection Boxes Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/YOLOv7-Dynamic-Batch-TENSORRT.ipynb Adjusts the detected bounding box coordinates after inference. It reverses the scaling and padding applied during preprocessing to map the boxes back to the original image dimensions. ```python def postprocess(boxes,r,dwdh): dwdh = torch.tensor(dwdh*2).to(boxes.device) boxes -= dwdh boxes /= r return boxes.clip_(0,6400) ``` -------------------------------- ### Remove YOLOv7 Cache File Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6.ipynb This command removes the cache file for the COCO validation dataset. This is often a preliminary step before re-downloading or re-processing the dataset to ensure a clean state. ```shell !rm /content/coco/val2017.cache ``` -------------------------------- ### Clone YOLOv5 Repository using Git Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6.ipynb This command clones the YOLOv5 repository from GitHub. It uses the 'git clone' command to download the entire repository content into the current directory. The output indicates the cloning process and the files present after completion. ```bash %cd /content/ !ls !# Download YOLOv5 code !git clone https://github.com/ultralytics/yolov5 %cd yolov5 !git checkout 0b5ac224aef287ac3ac9ebf70ade60159450a0b1 !ls ``` -------------------------------- ### Unzip COCO Validation Images (Shell) Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/compare_YOLOv7_vs_YOLOv5m6.ipynb This command extracts the contents of the 'val2017.zip' file into the current directory. The output indicates the progress of the extraction, showing each image file being unpacked. ```shell !unzip val2017.zip ``` -------------------------------- ### Display Detection Results (Cityscapes Example) Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/instance.ipynb Uses Matplotlib to display the image with detected masks and bounding boxes. This snippet is configured for a Cityscapes dataset context and enables inline plotting. ```python # cityscapes example %matplotlib inline plt.figure(figsize=(8,8)) plt.axis('off') plt.imshow(pnimg) plt.show() ``` -------------------------------- ### Display Detection Results (COCO Example) Source: https://github.com/wongkinyiu/yolov7/blob/main/tools/instance.ipynb Uses Matplotlib to display the image with detected masks and bounding boxes. This snippet is configured for a COCO dataset context and enables inline plotting. ```python # coco example %matplotlib inline plt.figure(figsize=(8,8)) plt.axis('off') plt.imshow(pnimg) plt.show() ```