### Install YOLOv5 Dependencies Source: https://docs.ultralytics.com/yolov5/quickstart_tutorial Clones the YOLOv5 repository and installs necessary dependencies using pip. Requires Python >=3.8.0 and PyTorch >=1.8. ```shell git clone https://github.com/ultralytics/yolov5 # clone repository cd yolov5 pip install -r requirements.txt # install dependencies ``` -------------------------------- ### YOLOv5 Training Commands Source: https://docs.ultralytics.com/yolov5/quickstart_tutorial Provides example commands for training YOLOv5 models on different datasets and configurations. Supports specifying data, epochs, initial weights, model configuration, and batch size. Optimal batch sizes for V100 GPUs are suggested. ```shell # Train YOLOv5n on COCO128 for 3 epochs python train.py --data coco128.yaml --epochs 3 --weights yolov5n.pt --batch-size 128 # Train YOLOv5s on COCO for 300 epochs python train.py --data coco.yaml --epochs 300 --weights '' --cfg yolov5s.yaml --batch-size 64 # Train YOLOv5m on COCO for 300 epochs python train.py --data coco.yaml --epochs 300 --weights '' --cfg yolov5m.yaml --batch-size 40 # Train YOLOv5l on COCO for 300 epochs python train.py --data coco.yaml --epochs 300 --weights '' --cfg yolov5l.yaml --batch-size 24 ``` -------------------------------- ### Install Specific NVIDIA Container Toolkit Version on Ubuntu/Debian Source: https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial This snippet allows for the installation of a specific version of the NVIDIA Container Toolkit on Debian-based systems. It requires setting an environment variable for the desired version and then installing the toolkit and its components. ```bash export NVIDIA_CONTAINER_TOOLKIT_VERSION=1.17.8-1 sudo apt-get install -y \ nvidia-container-toolkit=${NVIDIA_CONTAINER_TOOLKIT_VERSION} \ nvidia-container-toolkit-base=${NVIDIA_CONTAINER_TOOLKIT_VERSION} \ libnvidia-container-tools=${NVIDIA_CONTAINER_TOOLKIT_VERSION} \ libnvidia-container1=${NVIDIA_CONTAINER_TOOLKIT_VERSION} ``` -------------------------------- ### Pull YOLOv5 Docker Image Source: https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial Pulls the latest YOLOv5 Docker image from Docker Hub. Ensure Docker is installed and configured. ```bash # Pull the latest YOLOv5 image from Docker Hub sudo docker pull $t ``` -------------------------------- ### Install NVIDIA Container Toolkit on RHEL/CentOS/Fedora Source: https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial This code snippet installs the NVIDIA Container Toolkit on RHEL-based systems (like CentOS, Fedora, Amazon Linux). It involves adding the NVIDIA repository, cleaning cache, checking for updates, and then installing the necessary packages. ```bash curl -s -L https://nvidia.github.io/libnvidia-container/stable/rpm/nvidia-container-toolkit.repo \ | sudo tee /etc/yum.repos.d/nvidia-container-toolkit.repo sudo dnf clean expire-cache sudo dnf check-update sudo dnf install \ nvidia-container-toolkit \ nvidia-container-toolkit-base \ lib-nvidia-container-tools \ lib-nvidia-container1 ``` -------------------------------- ### Install YOLOv5 Dependencies with Pip Source: https://docs.ultralytics.com/yolov5/environments/azureml_quickstart_tutorial This section outlines the installation of essential Python packages required for YOLOv5 functionality by referencing the 'requirements.txt' file. Additionally, it installs the ONNX library, which is crucial for exporting models to the ONNX format for deployment. ```bash pip install -r requirements.txt pip install onnx >= 1.12.0 ``` -------------------------------- ### Clone YOLOv5 and Install Dependencies (Shell) Source: https://docs.ultralytics.com/yolov5/environments/aws_quickstart_tutorial This snippet demonstrates cloning the official Ultralytics YOLOv5 repository from GitHub and installing its required Python dependencies using pip. It's essential for setting up the YOLOv5 environment on a server or local machine. Ensure you are using a Python 3.8+ environment. ```shell # Clone the YOLOv5 repository git clone https://github.com/ultralytics/yolov5 cd yolov5 # Install required packages pip install -r requirements.txt ``` -------------------------------- ### Clone YOLOv5 Repository using Git Source: https://docs.ultralytics.com/yolov5/environments/azureml_quickstart_tutorial This command clones the official Ultralytics YOLOv5 repository from GitHub to the local file system. It then navigates the user into the newly created 'yolov5' directory, preparing the environment for dependency installation. ```bash git clone https://github.com/ultralytics/yolov5 cd yolov5 ``` -------------------------------- ### Install Specific NVIDIA Container Toolkit Version on RHEL/CentOS/Fedora Source: https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial This snippet installs a specific version of the NVIDIA Container Toolkit on RHEL-based systems. It requires setting the NVIDIA_CONTAINER_TOOLKIT_VERSION environment variable and then using dnf to install the specified versions of the toolkit and its components. ```bash export NVIDIA_CONTAINER_TOOLKIT_VERSION=1.17.8-1 sudo dnf install -y \ nvidia-container-toolkit-${NVIDIA_CONTAINER_TOOLKIT_VERSION} \ nvidia-container-toolkit-base-${NVIDIA_CONTAINER_TOOLKIT_VERSION} \ lib-nvidia-container-tools-${NVIDIA_CONTAINER_TOOLKIT_VERSION} \ lib-nvidia-container1-${NVIDIA_CONTAINER_TOOLKIT_VERSION} ``` -------------------------------- ### YOLOv5 Inference with detect.py Source: https://docs.ultralytics.com/yolov5/quickstart_tutorial Utilizes the `detect.py` script for versatile inference on various sources like images, videos, directories, webcams, and streams. Automatically fetches models and saves results. ```shell python detect.py --weights yolov5s.pt --source 0 # webcam python detect.py --weights yolov5s.pt --source image.jpg # image python detect.py --weights yolov5s.pt --source video.mp4 # video python detect.py --weights yolov5s.pt --source screen # screenshot python detect.py --weights yolov5s.pt --source path/ # directory python detect.py --weights yolov5s.pt --source list.txt # list of images python detect.py --weights yolov5s.pt --source list.streams # list of streams python detect.py --weights yolov5s.pt --source 'path/*.jpg' # glob pattern python detect.py --weights yolov5s.pt --source 'https://youtu.be/LNwODJXcvt4' # YouTube video python detect.py --weights yolov5s.pt --source 'rtsp://example.com/media.mp4' # RTSP, RTMP, HTTP stream ``` -------------------------------- ### Install NVIDIA Container Toolkit on Ubuntu/Debian Source: https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial This code snippet installs the NVIDIA Container Toolkit on Debian-based systems (like Ubuntu). It involves adding the NVIDIA repository, updating package lists, and installing the necessary packages. This enables Docker containers to access NVIDIA GPUs. ```bash curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \ && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list \ | sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' \ | sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list sudo apt-get update sudo apt-get install -y \ nvidia-container-toolkit \ nvidia-container-toolkit-base libnvidia-container-tools \ libnvidia-container1 ``` -------------------------------- ### Run YOLOv5 Docker Container (GPU) Source: https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial Runs an interactive YOLOv5 Docker container with GPU support. Requires the NVIDIA Container Toolkit. Use --gpus all for all GPUs or specify devices. ```bash # Run with access to all available GPUs sudo docker run -it --runtime=nvidia --ipc=host --gpus all $t # Run with access to specific GPUs (e.g., GPUs 2 and 3) sudo docker run -it --runtime=nvidia --ipc=host --gpus '"device=2,3"' $t ``` -------------------------------- ### Run YOLOv5 Docker Container with Volume Mount Source: https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial Runs an interactive YOLOv5 Docker container with GPU support and mounts a local directory into the container. This allows access to datasets and models from the host machine. ```bash # Mount /path/on/host (your local machine) to /path/in/container (inside the container) sudo docker run -it --runtime=nvidia --ipc=host --gpus all -v /path/on/host:/path/in/container $t ``` -------------------------------- ### Create and Activate Conda Virtual Environment Source: https://docs.ultralytics.com/yolov5/environments/azureml_quickstart_tutorial This snippet demonstrates how to create a new Conda virtual environment named 'yolov5env' with Python 3.10 and then activate it. It also ensures that pip is installed within the environment, which is necessary for installing subsequent Python packages. ```bash conda create --name yolov5env -y python=3.10 conda activate yolov5env conda install pip -y ``` -------------------------------- ### YOLOv5 Inference with PyTorch Hub Source: https://docs.ultralytics.com/yolov5/quickstart_tutorial Performs object detection inference using a YOLOv5 model loaded directly from PyTorch Hub. Supports various input types including images and lists of images. Results can be displayed, saved, or processed further. ```python import torch # Model loading model = torch.hub.load("ultralytics/yolov5", "yolov5s") # Can be 'yolov5n' - 'yolov5x6', or 'custom' # Inference on images img = "https://ultralytics.com/images/zidane.jpg" # Can be a file, Path, PIL, OpenCV, numpy, or list of images # Run inference results = model(img) # Display results results.print() # Other options: .show(), .save(), .crop(), .pandas(), etc. Explore these in the Predict mode documentation. ``` -------------------------------- ### Initialize ClearML SDK Source: https://docs.ultralytics.com/yolov5/tutorials/clearml_logging_integration Connects the ClearML SDK to your ClearML server. This command guides you through creating credentials and establishing the connection, essential for ClearML to track your experiments. ```bash clearml-init ``` -------------------------------- ### Verify NVIDIA Drivers with nvidia-smi Source: https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial This command checks if NVIDIA drivers are installed correctly on the system. It is a prerequisite for GPU support in Docker containers. No specific input or output is detailed beyond successful execution. ```bash nvidia-smi ``` -------------------------------- ### Verify NVIDIA Runtime in Docker Source: https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial This command checks the Docker info to ensure that the 'nvidia' runtime is listed, confirming that the NVIDIA Container Toolkit is correctly configured and accessible by Docker. This is a verification step after setting up GPU support. ```bash docker info | grep -i runtime ``` -------------------------------- ### Download Sample Image for YOLOv5 Deployment Source: https://docs.ultralytics.com/yolov5/tutorials/neural_magic_pruning_quantization Downloads a sample image ('basilica.jpg') required for testing YOLOv5 deployment examples. This image is used as input for inference. ```bash wget -O basilica.jpg https://raw.githubusercontent.com/neuralmagic/deepsparse/main/src/deepsparse/yolo/sample_images/basilica.jpg ``` -------------------------------- ### Start YOLOv5 Training from Scratch Source: https://docs.ultralytics.com/yolov5/tutorials/tips_for_best_training_results Begin YOLOv5 training from scratch without pretrained weights. This method is suitable for large datasets. It requires specifying the model architecture YAML file and an empty string for the `--weights` argument. Requires `custom.yaml` for dataset configuration. ```python python train.py --data custom.yaml --weights "" --cfg yolov5s.yaml python train.py --data custom.yaml --weights "" --cfg yolov5m.yaml python train.py --data custom.yaml --weights "" --cfg yolov5l.yaml python train.py --data custom.yaml --weights "" --cfg yolov5x.yaml ``` -------------------------------- ### YOLOv5 Training within Docker Source: https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial Trains a YOLOv5 model using the `train.py` script inside the Docker container. Requires a dataset configuration file and optionally pre-trained weights. ```python # Train a YOLOv5 model on your custom dataset (ensure data is mounted or downloaded) python train.py --data your_dataset.yaml --weights yolov5s.pt --img 640 # Start training ``` -------------------------------- ### YOLOv5 Inference within Docker Source: https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial Performs object detection inference on images or videos using a trained YOLOv5 model with the `detect.py` script. ```python # Run inference on images or videos using a trained model python detect.py --weights yolov5s.pt --source path/to/your/images_or_videos # Perform detection ``` -------------------------------- ### Install DeepSparse for YOLOv5 Source: https://docs.ultralytics.com/yolov5/tutorials/neural_magic_pruning_quantization Installs the DeepSparse library with necessary components for server, YOLO, and ONNX Runtime integration. Recommended for use within a Python virtual environment. ```bash pip install "deepsparse[server,yolo,onnxruntime]" ``` -------------------------------- ### Start YOLOv5 Training with Pretrained Weights Source: https://docs.ultralytics.com/yolov5/tutorials/tips_for_best_training_results Initiate YOLOv5 training using pretrained weights. This is recommended for smaller to medium datasets. The script automatically downloads the specified model weights from the latest YOLOv5 release. Requires `custom.yaml` for dataset configuration. ```python python train.py --data custom.yaml --weights yolov5s.pt python train.py --data custom.yaml --weights yolov5m.pt python train.py --data custom.yaml --weights yolov5l.pt python train.py --data custom.yaml --weights yolov5x.pt python train.py --data custom.yaml --weights custom_pretrained.pt ``` -------------------------------- ### Train YOLOv5 Model (Python) Source: https://docs.ultralytics.com/yolov5/environments/aws_quickstart_tutorial This command initiates the training process for a YOLOv5 model. It specifies the dataset configuration file (`coco128.yaml`), the pre-trained weights to start from (`yolov5s.pt`), and the image input size (`640`). The necessary models and datasets are automatically downloaded. ```python python train.py --data coco128.yaml --weights yolov5s.pt --img 640 ``` -------------------------------- ### Pull YOLOv5 Docker Image Source: https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial This command pulls the latest official YOLOv5 Docker image from Docker Hub. The 'latest' tag ensures that the most recent version of the YOLOv5 repository is downloaded, providing access to the newest features and updates. ```bash # Define the image name with tag t=ultralytics/yolov5:latest ``` -------------------------------- ### Python Example Request for DeepSparse Server Source: https://docs.ultralytics.com/yolov5/tutorials/neural_magic_pruning_quantization Provides a Python code snippet using the 'requests' library to send an image to a running DeepSparse HTTP server endpoint for inference. This demonstrates how to interact with the deployed model service. ```python import json import requests ``` -------------------------------- ### Run YOLOv5 Docker Container (CPU) Source: https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial Runs an interactive YOLOv5 Docker container using only the CPU. The --ipc=host flag is crucial for shared memory access. ```bash # Run an interactive container instance using CPU sudo docker run -it --runtime=nvidia --ipc=host $t ``` -------------------------------- ### YOLOv5 Validation within Docker Source: https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial Validates the performance of a trained YOLOv5 model using the `val.py` script. Requires the path to the trained weights and the dataset configuration. ```python # Validate the trained model's performance (Precision, Recall, mAP) python val.py --weights path/to/your/best.pt --data your_dataset.yaml # Validate accuracy ``` -------------------------------- ### YOLOv5 Hyperparameter Configuration Example Source: https://docs.ultralytics.com/yolov5/tutorials/hyperparameter_evolution An example of a YOLOv5 hyperparameter configuration file (e.g., *.yaml). These parameters control various aspects of model training, such as learning rate, augmentation, and loss function weights. ```yaml # YOLOv5 🚀 by Ultralytics, AGPL-3.0 license # Hyperparameters for low-augmentation COCO training from scratch # python train.py --batch 64 --cfg yolov5n6.yaml --weights '' --data coco.yaml --img 640 --epochs 300 --linear # See tutorials for hyperparameter evolution https://github.com/ultralytics/yolov5#tutorials lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3) lrf: 0.01 # final OneCycleLR learning rate (lr0 * lrf) momentum: 0.937 # SGD momentum/Adam beta1 weight_decay: 0.0005 # optimizer weight decay 5e-4 warmup_epochs: 3.0 # warmup epochs (fractions ok) warmup_momentum: 0.8 # warmup initial momentum warmup_bias_lr: 0.1 # warmup initial bias lr box: 0.05 # box loss gain cls: 0.5 # cls loss gain cls_pw: 1.0 # cls BCELoss positive_weight obj: 1.0 # obj loss gain (scale with pixels) obj_pw: 1.0 # obj BCELoss positive_weight iou_t: 0.20 # IoU training threshold anchor_t: 4.0 # anchor-multiple threshold # anchors: 3 # anchors per output layer (0 to ignore) fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5) hsv_h: 0.015 # image HSV-Hue augmentation (fraction) hsv_s: 0.7 # image HSV-Saturation augmentation (fraction) hsv_v: 0.4 # image HSV-Value augmentation (fraction) degrees: 0.0 # image rotation (+/- deg) translate: 0.1 # image translation (+/- fraction) scale: 0.5 # image scale (+/- gain) shear: 0.0 # image shear (+/- deg) perspective: 0.0 # image perspective (+/- fraction), range 0-0.001 flipud: 0.0 # image flip up-down (probability) fliplr: 0.5 # image flip left-right (probability) mosaic: 1.0 # image mosaic (probability) mixup: 0.0 # image mixup (probability) copy_paste: 0.0 # segment copy-paste (probability) ``` -------------------------------- ### Configure Docker Runtime for NVIDIA Source: https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial This command configures the Docker runtime to use NVIDIA for GPU acceleration. After configuration, the Docker daemon must be restarted for the changes to take effect. This is a crucial step for enabling GPU support within Docker. ```bash sudo nvidia-ctk runtime configure --runtime=docker sudo systemctl restart docker ``` -------------------------------- ### Clone YOLOv5 Repo and Install Dependencies Source: https://docs.ultralytics.com/yolov5/tutorials/train_custom_data This snippet shows how to clone the YOLOv5 GitHub repository and install the necessary Python dependencies from the requirements.txt file. Ensure you have Git and pip installed. ```bash git clone https://github.com/ultralytics/yolov5 # Clone the repository cd yolov5 pip install -r requirements.txt # Install dependencies ``` -------------------------------- ### Evolve YOLOv5 Hyperparameters (Multi-GPU with Delay) Source: https://docs.ultralytics.com/yolov5/tutorials/hyperparameter_evolution Demonstrates how to run hyperparameter evolution for YOLOv5 across multiple GPUs with an optional delay between starting each process. Logs are redirected to individual files. ```shell # Multi-GPU with delay for i in {0..7}; do sleep $((30 * i)) # 30-second delay (optional) echo "Starting GPU $i..." nohup python train.py --epochs 10 --data coco128.yaml --weights yolov5s.pt --cache --device $i --evolve > "evolve_gpu_$i.log" & done ``` -------------------------------- ### Install Optuna for Hyperparameter Optimization Source: https://docs.ultralytics.com/yolov5/tutorials/clearml_logging_integration Installs the Optuna library, which can be used for hyperparameter optimization (HPO) with ClearML. If Optuna is not installed, the script defaults to using RandomSearch. Ensure you have run a training task at least once to have a template task in ClearML. ```shell # To use optuna, install it first, otherwise you can change the optimizer to just be RandomSearch pip install optuna python utils/loggers/clearml/hpo.py ``` -------------------------------- ### Install comet_ml Package Source: https://docs.ultralytics.com/yolov5/tutorials/comet_logging_integration Installs the Comet ML Python package using pip. This is the first step to enable Comet's experiment tracking capabilities. ```shell pip install comet_ml ``` -------------------------------- ### YOLOv5 Model Export within Docker Source: https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial Exports a trained YOLOv5 model to various formats (e.g., ONNX, CoreML, TFLite) for deployment using the `export.py` script. ```python # Export the trained model to various formats like ONNX, CoreML, or TFLite for deployment python export.py --weights yolov5s.pt --include onnx coreml tflite # Export model ``` -------------------------------- ### Create IPython Kernel for YOLOv5 Environment Source: https://docs.ultralytics.com/yolov5/environments/azureml_quickstart_tutorial This snippet shows how to install the ipykernel package and create a new IPython kernel linked to a Conda environment named 'yolov5env'. This is crucial for running Python code within an AzureML Notebook using the specified environment. ```shell # Ensure your Conda environment is active # conda activate yolov5env # Install ipykernel if not already present conda install ipykernel -y # Create a new kernel linked to your environment python -m ipykernel install --user --name yolov5env --display-name "Python (yolov5env)" ``` -------------------------------- ### Train YOLOv5 Model Source: https://docs.ultralytics.com/yolov5/environments/azureml_quickstart_tutorial This command initiates the training process for the YOLOv5 model. It utilizes the 'yolov5s' pretrained weights and the COCO128 dataset, specifying parameters for image size, number of epochs, and batch size. This is a core step for adapting the model to a specific task. ```python python train.py --data coco128.yaml --weights yolov5s.pt --img 640 --epochs 10 --batch 16 ``` -------------------------------- ### Run YOLOv5 Inference for Object Detection Source: https://docs.ultralytics.com/yolov5/environments/azureml_quickstart_tutorial This script executes inference using the 'yolov5s' model on images located in the 'data/images' directory. It detects objects within the specified images with a defined image size. This is used for applying a trained model to new data. ```python python detect.py --weights yolov5s.pt --source data/images --img 640 ``` -------------------------------- ### Ubuntu Dependency for OpenCV Source: https://docs.ultralytics.com/yolov5/tutorials/neural_magic_pruning_quantization Installs the 'libgl1' package on Ubuntu systems, which is a dependency for OpenCV when running in certain cloud environments that might lack graphical libraries. ```bash apt-get install libgl1 ``` -------------------------------- ### Clone YOLOv5 Repo and Install Dependencies (Bash) Source: https://docs.ultralytics.com/yolov5/tutorials/train_custom_data This snippet demonstrates how to clone the YOLOv5 repository from GitHub and install the necessary Python dependencies using pip. This is a foundational step for setting up a local YOLOv5 environment. ```bash git clone https://github.com/ultralytics/yolov5 cd yolov5 pip install -r requirements.txt ``` -------------------------------- ### Install ClearML Python Package Source: https://docs.ultralytics.com/yolov5/tutorials/clearml_logging_integration Installs the ClearML Python package, enabling integration with YOLOv5 for experiment tracking and other MLOps features. This is a prerequisite for using ClearML with YOLOv5. ```bash pip install clearml ``` -------------------------------- ### Clone YOLOv5 Repo and Install Dependencies Source: https://docs.ultralytics.com/yolov5/tutorials/transfer_learning_with_frozen_layers This snippet demonstrates how to clone the YOLOv5 repository from GitHub and install its required dependencies using pip. Ensure you have Python >=3.8.0 and PyTorch >=1.8 installed. ```bash git clone https://github.com/ultralytics/yolov5 # clone repository cd yolov5 pip install -r requirements.txt # install dependencies ``` -------------------------------- ### Train YOLOv5 Model with Custom Dataset Source: https://docs.ultralytics.com/yolov5/environments/google_cloud_quickstart_tutorial Example command to train a YOLOv5s model on a custom dataset for a specified number of epochs. Requires Python, YOLOv5 repository, and a custom dataset configuration file. ```bash python train.py --img 640 --batch 16 --epochs 100 --data custom_dataset.yaml --weights yolov5s.pt ``` -------------------------------- ### Clone YOLOv5 Repository and Install Requirements Source: https://docs.ultralytics.com/yolov5/tutorials/hyperparameter_evolution This snippet shows the basic commands to clone the YOLOv5 repository from GitHub and install the necessary Python dependencies. Ensure you have Python >=3.8.0 and PyTorch >=1.8 installed. ```shell git clone https://github.com/ultralytics/yolov5 # clone cd yolov5 pip install -r requirements.txt # install ``` -------------------------------- ### Install YOLOv5 Dependencies on GCP VM Source: https://docs.ultralytics.com/yolov5/environments/google_cloud_quickstart_tutorial This code snippet demonstrates how to clone the YOLOv5 repository from GitHub and install all necessary Python packages using pip. It ensures the environment meets the requirements for YOLOv5, such as Python version 3.8.0+ and PyTorch 1.8+. ```bash # Clone the YOLOv5 repository from GitHub git clone https://github.com/ultralytics/yolov5 # Navigate into the cloned repository directory cd yolov5 # Install the required Python packages listed in requirements.txt pip install -r requirements.txt ``` -------------------------------- ### Run YOLOv5 Validation in Bash Cell Source: https://docs.ultralytics.com/yolov5/environments/azureml_quickstart_tutorial This example demonstrates how to execute a Bash command within an AzureML Notebook cell to run the YOLOv5 validation script. It highlights the necessity of activating the Conda environment within the Bash cell before running the Python script. ```shell %%bash source activate yolov5env # Activate environment within the cell # Example: Run validation using the activated environment python val.py --weights yolov5s.pt --data coco128.yaml --img 640 ``` -------------------------------- ### HTTP Server: Deploy YOLOv5 with DeepSparse Server CLI Source: https://docs.ultralytics.com/yolov5/tutorials/neural_magic_pruning_quantization Starts a DeepSparse HTTP server to deploy a YOLOv5 model. The server is configured with the 'yolo' task and a specific model path, making the model accessible via an API endpoint. ```bash deepsparse.server \ --task yolo \ --model_path zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned65_quant-none ``` -------------------------------- ### Install and Configure Comet for YOLOv5 Source: https://docs.ultralytics.com/yolov5/tutorials/train_custom_data Integrate Comet ML for experiment tracking. This involves installing the Comet library, setting your API key as an environment variable, and then training your model. Comet automatically logs training details. ```shell pip install comet_ml export COMET_API_KEY=YOUR_API_KEY_HERE python train.py --img 640 --epochs 3 --data coco128.yaml --weights yolov5s.pt ``` -------------------------------- ### Manage Data with Google Cloud Storage (gsutil) Source: https://docs.ultralytics.com/yolov5/environments/google_cloud_quickstart_tutorial Commands to copy datasets from a Google Cloud Storage (GCS) bucket to a local VM and to copy trained model weights from a VM to a GCS bucket. Requires Google Cloud SDK installation and initialization. ```bash # Example: Copy your dataset from a GCS bucket to your VM gsutil cp -r gs://your-data-bucket/my_dataset ./datasets/ ``` ```bash # Example: Copy trained model weights from your VM to a GCS bucket gsutil cp -r ./runs/train/exp/weights gs://your-models-bucket/yolov5_custom_weights/ ``` -------------------------------- ### Run ClearML Agent Daemon Source: https://docs.ultralytics.com/yolov5/tutorials/clearml_logging_integration Starts a ClearML agent daemon that listens to specified queues for tasks. The agent can execute experiments remotely by recreating the environment and reporting results back to the ClearML experiment manager. The optional [--docker] flag enables Docker containerization for execution. ```shell clearml-agent daemon --queue QUEUES_TO_LISTEN_TO [--docker] ``` -------------------------------- ### Export YOLOv5 Model to ONNX Format Source: https://docs.ultralytics.com/yolov5/environments/azureml_quickstart_tutorial This command exports the 'yolov5s' model to the ONNX (Open Neural Network Exchange) format. ONNX is a widely supported format for deploying machine learning models across various platforms and hardware. The export is configured for a specific image size. ```python python export.py --weights yolov5s.pt --include onnx --img 640 ``` -------------------------------- ### Annotate Image using DeepSparse CLI Source: https://docs.ultralytics.com/yolov5/tutorials/neural_magic_pruning_quantization This command-line interface (CLI) example shows how to use DeepSparse to annotate an image file. The `annotate` command takes a model filepath (using a zoo path for a pruned and quantized YOLOv5 model) and a source image file. The output is an annotated image saved in an 'annotation-results' folder. ```bash deepsparse.object_detection.annotate --model_filepath zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned65_quant-none --source basilica.jpg ``` -------------------------------- ### YOLO Label Format Example Source: https://docs.ultralytics.com/yolov5/tutorials/train_custom_data Demonstrates the structure of a YOLO format label file, which contains one line per object bounding box. Each line specifies the class index and normalized coordinates (center x, center y, width, height) of the object. ```text 0 0.5 0.5 0.2 0.3 0 0.7 0.6 0.1 0.15 27 0.3 0.4 0.05 0.1 ``` -------------------------------- ### Export YOLOv5 Models Source: https://docs.ultralytics.com/yolov5/environments/aws_quickstart_tutorial This command exports a YOLOv5 model to multiple formats including ONNX, CoreML, and TFLite. It requires the path to the pre-trained weights and specifies the image size for export. No external dependencies are strictly required beyond the YOLOv5 repository itself. ```bash python export.py --weights yolov5s.pt --include onnx coreml tflite --img 640 ``` -------------------------------- ### Train YOLOv5 with Backbone Frozen Source: https://docs.ultralytics.com/yolov5/tutorials/transfer_learning_with_frozen_layers This command demonstrates training YOLOv5 with the backbone layers frozen. It is useful for comparing the effects of layer freezing on performance metrics like mAP. This specific example freezes 10 layers. ```python # Example command for training with backbone frozen python train.py --batch 48 --weights yolov5m.pt --data voc.yaml --epochs 50 --cache --img 512 --hyp hyp.finetune.yaml --freeze 10 ``` -------------------------------- ### Log Class-Level Metrics with Comet Source: https://docs.ultralytics.com/yolov5/tutorials/comet_logging_integration Enable logging of per-class metrics such as mAP, precision, recall, and f1 score in Comet. This feature is activated by setting the `COMET_LOG_PER_CLASS_METRICS` environment variable to `true`. The example shows a standard training command with this variable set. ```bash env COMET_LOG_PER_CLASS_METRICS=true python train.py \ --img 640 \ --batch 16 \ --epochs 5 \ --data coco128.yaml \ --weights yolov5s.pt ``` -------------------------------- ### Run YOLOv5 Object Detection Inference (Python) Source: https://docs.ultralytics.com/yolov5/environments/aws_quickstart_tutorial This command performs object detection inference using a trained YOLOv5 model. You need to provide the path to the model weights (`yolov5s.pt`), the source of the images or videos (`path/to/your/images_or_videos/`), and the desired image input size (`640`). ```python python detect.py --weights yolov5s.pt --source path/to/your/images_or_videos/ --img 640 ``` -------------------------------- ### Increase Swap Memory on Linux Source: https://docs.ultralytics.com/yolov5/environments/aws_quickstart_tutorial This script increases the available swap memory on a Linux system by creating a 64GB swap file. It involves allocating space, setting permissions, formatting the file as a swap area, enabling it, and verifying its activation. This is useful for managing memory limitations during training or when working with large datasets. ```bash # Allocate a 64GB swap file (adjust size as needed) sudo fallocate -l 64G /swapfile # Set correct permissions sudo chmod 600 /swapfile # Set up the file as a Linux swap area sudo mkswap /swapfile # Enable the swap file sudo swapon /swapfile # Verify the swap memory is active free -h ``` -------------------------------- ### Control Prediction Image Logging with Comet Source: https://docs.ultralytics.com/yolov5/tutorials/comet_logging_integration Adjust the maximum number of prediction images logged by Comet during YOLOv5 training. This is controlled via the `COMET_MAX_IMAGE_UPLOADS` environment variable. By default, 100 validation images are logged. This example sets the limit to 200. ```bash env COMET_MAX_IMAGE_UPLOADS=200 python train.py \ --img 640 \ --batch 16 \ --epochs 5 \ --data coco128.yaml \ --weights yolov5s.pt \ --bbox_interval 1 ``` -------------------------------- ### Validate YOLOv5 Model Performance Source: https://docs.ultralytics.com/yolov5/environments/azureml_quickstart_tutorial This command performs validation on a trained YOLOv5 model, typically 'yolov5s', using the COCO128 validation set. It evaluates the model's performance using metrics such as Precision, Recall, and mAP, which are essential for assessing model quality. ```python python val.py --weights yolov5s.pt --data coco128.yaml --img 640 ``` -------------------------------- ### Validate YOLOv5 Model Performance (Python) Source: https://docs.ultralytics.com/yolov5/environments/aws_quickstart_tutorial This Python command is used to validate the performance metrics (Precision, Recall, mAP) of a trained YOLOv5 model. It requires the path to the trained weights (`yolov5s.pt`), the dataset configuration (`coco128.yaml`), and the image input size (`640`). ```python python val.py --weights yolov5s.pt --data coco128.yaml --img 640 ``` -------------------------------- ### Sync Dataset with ClearML Source: https://docs.ultralytics.com/yolov5/tutorials/clearml_logging_integration Uploads a local dataset to ClearML as a versioned dataset. It uses the `clearml-data sync` command for a streamlined process or a sequence of `create`, `add`, and `close` commands for more control. Ensure you are in the dataset's root folder before execution. ```shell cd coco128 clearml-data sync --project YOLOv5 --name coco128 --folder . ``` ```shell # Optionally add --parent if you want to base # this version on another dataset version, so no duplicate files are uploaded! clearml-data create --name coco128 --project YOLOv5 clearml-data add --files . clearml-data close ``` -------------------------------- ### Configure Comet Credentials via .comet.config File Source: https://docs.ultralytics.com/yolov5/tutorials/comet_logging_integration Configures Comet API key and project name by creating a .comet.config file. This file should be placed in the working directory. ```ini [comet] api_key=YOUR_API_KEY project_name=YOUR_COMET_PROJECT_NAME # This will default to 'yolov5' ``` -------------------------------- ### Run YOLOv5 Training Script Source: https://docs.ultralytics.com/yolov5/tutorials/comet_logging_integration Executes the YOLOv5 training script with specified parameters for image size, batch size, epochs, data configuration, and initial weights. Comet will automatically log metrics and parameters. ```python # Train YOLOv5s on COCO128 for 5 epochs python train.py --img 640 --batch 16 --epochs 5 --data coco128.yaml --weights yolov5s.pt ``` -------------------------------- ### Benchmark YOLOv5 ONNX Runtime Performance (Shell) Source: https://docs.ultralytics.com/yolov5/tutorials/neural_magic_pruning_quantization This command benchmarks the performance of the standard dense YOLOv5s model using ONNX Runtime. It specifies the model from the DeepSparse zoo, runs in synchronous mode ('sync') with a batch size of 32, and uses 1 stream. The output provides metrics such as throughput in images per second. ```bash deepsparse.benchmark zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/base-none -s sync -b 32 -nstreams 1 -e onnxruntime # Original Model Path: zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/base-none # Batch Size: 32 # Scenario: sync # Throughput (items/sec): 41.9025 ``` -------------------------------- ### Benchmark YOLOv5 ONNX Runtime Batch 1 (Shell) Source: https://docs.ultralytics.com/yolov5/tutorials/neural_magic_pruning_quantization This command benchmarks the performance of the standard dense YOLOv5s model using ONNX Runtime in a batch size 1 scenario. It specifies the model from the DeepSparse zoo, runs in synchronous mode ('sync') with a batch size of 1, and uses 1 stream. This is relevant for latency-sensitive applications. ```bash deepsparse.benchmark zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/base-none -s sync -b 1 -nstreams 1 -e onnxruntime # Original Model Path: zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/base-none # Batch Size: 1 # Scenario: sync # Throughput (items/sec): 48.0921 ``` -------------------------------- ### Benchmark DeepSparse VNNI Optimized YOLOv5 Batch 1 (Shell) Source: https://docs.ultralytics.com/yolov5/tutorials/neural_magic_pruning_quantization This command benchmarks a YOLOv5s model optimized with VNNI instructions and block pruning for DeepSparse, running in a batch size 1 scenario. It specifies the model from the DeepSparse zoo, runs in synchronous mode ('sync') with a batch size of 1, and uses 1 stream. This showcases maximum throughput on compatible hardware. ```bash deepsparse.benchmark zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned35_quant-none-vnni -s sync -b 1 -nstreams 1 # Original Model Path: zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned35_quant-none-vnni # Batch Size: 1 # Scenario: sync # Throughput (items/sec): 179.7375 ``` -------------------------------- ### Train YOLOv5 Using ClearML Dataset Source: https://docs.ultralytics.com/yolov5/tutorials/clearml_logging_integration Trains YOLOv5 models using a dataset versioned in ClearML. This command specifies training parameters such as image size, batch size, epochs, the ClearML dataset ID, and the initial weights. The `--cache` flag enables caching for faster subsequent runs. ```python python train.py --img 640 --batch 16 --epochs 3 --data clearml://YOUR_DATASET_ID --weights yolov5s.pt --cache ``` -------------------------------- ### Python API: Deploy YOLOv5 with DeepSparse Pipeline Source: https://docs.ultralytics.com/yolov5/tutorials/neural_magic_pruning_quantization Demonstrates deploying a YOLOv5 model using DeepSparse's Python API. It creates a 'yolo' task pipeline with a specified model stub and runs inference on a list of images, returning bounding box detections. ```python from deepsparse import Pipeline # list of images in local filesystem images = ["basilica.jpg"] # create Pipeline model_stub = "zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned65_quant-none" yolo_pipeline = Pipeline.create( task="yolo", model_path=model_stub, ) # run inference on images, receive bounding boxes + classes pipeline_outputs = yolo_pipeline(images=images, iou_thres=0.6, conf_thres=0.001) print(pipeline_outputs) ``` -------------------------------- ### Allocate Swap Space on GCP VM (Optional) Source: https://docs.ultralytics.com/yolov5/environments/google_cloud_quickstart_tutorial This optional section provides bash commands to allocate and enable swap space on a GCP VM. This is useful when working with large datasets that might exceed the VM's available RAM, helping to prevent memory errors during intensive operations. ```bash # Allocate a 64GB swap file sudo fallocate -l 64G /swapfile # Set the correct permissions for the swap file sudo chmod 600 /swapfile # Set up the Linux swap area sudo mkswap /swapfile # Enable the swap file sudo swapon /swapfile # Verify the swap space allocation (should show increased swap memory) free -h ``` -------------------------------- ### Benchmark DeepSparse Sparse YOLOv5 Batch 1 (Shell) Source: https://docs.ultralytics.com/yolov5/tutorials/neural_magic_pruning_quantization This command benchmarks the performance of a pruned-and-quantized YOLOv5s model using DeepSparse in a batch size 1 scenario. It specifies the optimized model from the DeepSparse zoo, runs in synchronous mode ('sync') with a batch size of 1, and uses 1 stream. This demonstrates DeepSparse's advantage in low-latency conditions. ```bash deepsparse.benchmark zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned65_quant-none -s sync -b 1 -nstreams 1 # Original Model Path: zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned65_quant-none # Batch Size: 1 # Scenario: sync # Throughput (items/sec): 134.9468 ``` -------------------------------- ### Configure Comet Optimizer Sweep Source: https://docs.ultralytics.com/yolov5/tutorials/comet_logging_integration Set up a hyperparameter search using Comet's Optimizer. Configuration is done via a JSON file, and the sweep is initiated using the `hpo.py` script, which accepts the same arguments as `train.py` along with the optimizer config path. ```bash python utils/loggers/comet/hpo.py \ --comet_optimizer_config "utils/loggers/comet/optimizer_config.json" ``` -------------------------------- ### Train YOLOv5 with Custom Project and Task Names Source: https://docs.ultralytics.com/yolov5/tutorials/clearml_logging_integration Runs the YOLOv5 training script with custom project and task names for ClearML experiment tracking. This allows for better organization and filtering of your training runs within the ClearML UI. ```python python train.py --project my_project --name my_training --img 640 --batch 16 --epochs 3 --data coco8.yaml --weights yolov5s.pt --cache ``` -------------------------------- ### Train, Validate, Detect, and Export YOLOv5 Models on GCP VM Source: https://docs.ultralytics.com/yolov5/environments/google_cloud_quickstart_tutorial These commands illustrate the core functionalities of YOLOv5 on a GCP VM. They cover training a model, validating its performance, running inference on images or videos, and exporting the trained model to various formats like ONNX, CoreML, and TFLite for deployment. ```bash # Train a YOLOv5 model on your dataset (e.g., yolov5s) python train.py --data coco128.yaml --weights yolov5s.pt --img 640 # Validate the trained model to check Precision, Recall, and mAP python val.py --weights yolov5s.pt --data coco128.yaml # Run inference using the trained model on images or videos python detect.py --weights yolov5s.pt --source path/to/your/images_or_videos # Export the trained model to various formats like ONNX, CoreML, TFLite for deployment python export.py --weights yolov5s.pt --include onnx coreml tflite ``` -------------------------------- ### Train YOLOv5 Model Source: https://docs.ultralytics.com/yolov5/tutorials/train_custom_data Execute the training script for YOLOv5 models. Key arguments control image size, batch size, epochs, dataset configuration, and initial weights. Pretrained weights are recommended for faster convergence. ```shell python train.py --img 640 --batch 16 --epochs 3 --data coco128.yaml --weights yolov5s.pt ``` -------------------------------- ### Train YOLOv5 with Default ClearML Tracking Source: https://docs.ultralytics.com/yolov5/tutorials/clearml_logging_integration Executes the YOLOv5 training script with ClearML integration enabled by default. This command captures all training details like source code, hyperparameters, and performance metrics for experiment tracking. ```python python train.py --img 640 --batch 16 --epochs 3 --data coco8.yaml --weights yolov5s.pt --cache ``` -------------------------------- ### Perform HTTP Inference with YOLOv5 (Python) Source: https://docs.ultralytics.com/yolov5/tutorials/neural_magic_pruning_quantization This snippet demonstrates how to send image files for inference to a DeepSparse endpoint using an HTTP POST request. It specifies the URL, prepares the image files in the required format, sends the request, and parses the JSON response containing annotations, bounding boxes, and labels. Dependencies include the 'requests' and 'json' libraries. ```python import requests import json # list of images for inference (local files on client side) path = ["basilica.jpg"] files = [("request", open(img, "rb")) for img in path] # send request over HTTP to /predict/from_files endpoint url = "http://0.0.0.0:5543/predict/from_files" resp = requests.post(url=url, files=files) # response is returned in JSON annotations = json.loads(resp.text) # dictionary of annotation results bounding_boxes = annotations["boxes"] labels = annotations["labels"] ``` -------------------------------- ### Benchmark DeepSparse Dense YOLOv5 Performance (Shell) Source: https://docs.ultralytics.com/yolov5/tutorials/neural_magic_pruning_quantization This command benchmarks the performance of the standard dense YOLOv5s model using DeepSparse. It specifies the model from the DeepSparse zoo, runs in synchronous mode ('sync') with a batch size of 32, and uses 1 stream. This showcases DeepSparse's performance on non-sparse models. ```bash deepsparse.benchmark zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/base-none -s sync -b 32 -nstreams 1 # Original Model Path: zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/base-none # Batch Size: 32 # Scenario: sync # Throughput (items/sec): 69.5546 ``` -------------------------------- ### Directory Structure for YOLOv5 Datasets Source: https://docs.ultralytics.com/yolov5/tutorials/train_custom_data Illustrates the recommended directory layout for organizing image and label files for YOLOv5 training. The structure places images and their corresponding labels in separate subdirectories. ```directory ../datasets/ └── coco128/ # Dataset root ├── images/ │ ├── train2017/ # Training images │ │ ├── 000000000009.jpg │ │ └── ... │ └── val2017/ # Validation images (optional if using same set for train/val) │ └── ... └── labels/ ├── train2017/ # Training labels │ ├── 000000000009.txt │ └── ... └── val2017/ # Validation labels (optional if using same set for train/val) └── ... ``` -------------------------------- ### Configure Comet Credentials via Environment Variables Source: https://docs.ultralytics.com/yolov5/tutorials/comet_logging_integration Sets Comet API key and project name using environment variables. Ensure these variables are set before running any Comet-integrated scripts. The project name defaults to 'yolov5' if not specified. ```shell export COMET_API_KEY=YOUR_API_KEY export COMET_PROJECT_NAME=YOUR_COMET_PROJECT_NAME # This will default to 'yolov5' ``` -------------------------------- ### Evolve YOLOv5 Hyperparameters (Single-GPU) Source: https://docs.ultralytics.com/yolov5/tutorials/hyperparameter_evolution Command to initiate hyperparameter evolution for YOLOv5 on a single GPU. This command is appended with '--evolve' to the base training command. ```shell python train.py --epochs 10 --data coco128.yaml --weights yolov5s.pt --cache --evolve ``` -------------------------------- ### Train YOLOv5 with Frozen Backbone Source: https://docs.ultralytics.com/yolov5/tutorials/transfer_learning_with_frozen_layers This command shows how to initiate YOLOv5 training while freezing the entire backbone (layers 0-9). This is achieved using the `--freeze 10` argument, which is beneficial for adapting models to new classes while retaining general feature extraction. ```bash python train.py --weights yolov5m.pt --data your_dataset.yaml --freeze 10 ```