Test-Time Augmentation (TTA) in YOLOv5: How to Enable and Use It
Test-Time Augmentation (TTA) improves YOLOv5 inference accuracy by running the model on horizontally flipped and multi-scale versions of input images, then merging predictions—activated simply by passing the --augment flag.
Test-Time Augmentation (TTA) is a post-training technique that boosts object detection performance without retraining the model. In the ultralytics/yolov5 repository, TTA is implemented as a built-in inference option that applies horizontal flips and scale variations during prediction. This guide explains how TTA works internally and how to activate it via the command line or Python API.
What is Test-Time Augmentation in YOLOv5?
TTA is an inference-time technique that processes multiple augmented views of the same input image and aggregates the results. According to the YOLOv5 source code, enabling TTA triggers three parallel inference passes: the original image, a horizontally flipped version, and two additional scaled versions at 0.5× and 1.5× resolution alongside the standard 1× scale.
After each forward pass, the predictions are de-augmented—transformed back to the original image coordinate system—and concatenated before Non-Maximum Suppression (NMS). This aggregation typically yields a richer set of bounding boxes and modest improvements in mean Average Precision (mAP), particularly for small or partially occluded objects.
How to Enable TTA in YOLOv5
You can activate TTA through either the command-line interface or the Python API by setting the augment parameter to True.
Using the CLI with detect.py and val.py
For single-image inference, video processing, or webcam streams, add the --augment argument when running detect.py:
python detect.py \
--weights yolov5s.pt \
--source data/images/zidane.jpg \
--augment
In detect.py (lines 88–90), the run() function accepts this flag and passes it directly to the model inference call:
preds = model(im, augment=augment)
For validation or test set evaluation, use the same flag with val.py:
python val.py \
--weights yolov5s.pt \
--data data/coco128.yaml \
--img 640 \
--augment
The val.py script (lines 38–40) forwards this parameter to the model() call during the validation loop, ensuring TTA is applied consistently across the dataset.
Using the Python API
When working with the Ultralytics Hub interface or custom scripts, enable TTA by passing augment=True to the prediction method:
from yolov5 import YOLOv5
model = YOLOv5("yolov5s.pt", device="cuda")
results = model.predict("data/images/zidane.jpg", augment=True)
The hub API forwards this flag to the underlying DetectMultiBackend class defined in models/common.py, which handles the actual augmentation logic and result aggregation.
How TTA Works Under the Hood
The implementation resides primarily in models/common.py within the DetectMultiBackend class. When augment=True, the forward method internally generates augmented variants of the input tensor.
Specifically, the pipeline performs:
- Horizontal flip: Mirrors the image horizontally to catch objects that might be detected more easily in reversed orientation.
- Multi-scale resizing: Processes the image at three scales (0.5×, 1.0×, and 1.5×) to improve detection of both small and large objects.
After inference on each variant, the raw predictions undergo geometric transformations to map bounding box coordinates back to the original image space. These de-augmented tensors are concatenated along the detection dimension and fed into the standard NMS routine, which filters overlapping boxes across all augmented views simultaneously.
Summary
- Test-Time Augmentation increases detection accuracy by aggregating predictions from flipped and multi-scale image versions at inference time.
- Enable TTA in YOLOv5 by passing
--augmenttodetect.pyorval.py, or settingaugment=Truein the Python API. - Internal implementation uses
DetectMultiBackendinmodels/common.pyto handle horizontal flips and 0.5×/1.0×/1.5× scaling, followed by de-augmentation and NMS. - Performance impact adds computational overhead (approximately 3× inference time) but improves mAP without requiring model retraining.
Frequently Asked Questions
Does TTA work with all YOLOv5 model sizes?
Yes. The --augment flag functions identically across all YOLOv5 variants (YOLOv5n, YOLOv5s, YOLOv5m, YOLOv5l, YOLOv5x). The augmentation logic in DetectMultiBackend is model-agnostic and operates on the input tensor before it reaches the backbone network.
How much does TTA improve mAP on COCO?
Typical improvements range from 1–2% mAP on the COCO validation set, though results vary by model size and input resolution. The trade-off is increased inference latency, as TTA runs three separate forward passes per image.
Can I use TTA during training or only at inference?
TTA is strictly an inference-time technique. It is activated via the augment parameter in detect.py and val.py, not during the training loop in train.py. For training augmentation, YOLOv5 uses Mosaic and MixUp augmentation instead.
Is TTA compatible with model export formats like ONNX or TensorRT?
No. When you export a YOLOv5 model to ONNX, TensorRT, or other formats using export.py, the dynamic augmentation logic is not included in the exported graph. TTA requires the Python-based DetectMultiBackend wrapper in models/common.py and must be run within the native PyTorch environment.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →