테스트 시간 증강 (TTA)
📚 이 가이드에서는 YOLOv5 🚀를 사용하여 mAP 및 재현율을 개선하기 위해 테스트 및 추론 중에 TTA(Test Time Augmentation)를 사용하는 방법을 설명합니다.
시작하기 전에
requirements.txt를 복제하고 Python>=3.8.0 환경에 설치합니다. 여기에는 PyTorch>=1.8이 포함됩니다. 모델 및 데이터 세트는 최신 YOLOv5 릴리스에서 자동으로 다운로드됩니다.
git clone https://github.com/ultralytics/yolov5 # clone
cd yolov5
pip install -r requirements.txt # install
정상적으로 테스트
TTA를 시도하기 전에 비교할 기준 성능을 설정하려고 합니다. 이 명령은 640픽셀 이미지 크기에서 COCO val2017에 대해 YOLOv5x를 테스트합니다. yolov5x.pt
은(는) 사용 가능한 모델 중 가장 크고 정확도가 높습니다. 다른 옵션으로는 yolov5s.pt
, yolov5m.pt
및 yolov5l.pt
, 또는 사용자 정의 데이터 세트를 학습하여 얻은 자체 체크포인트 ./weights/best.pt
입니다. 사용 가능한 모든 모델에 대한 자세한 내용은 다음을 참조하십시오. YOLOv5 설명서.
python val.py --weights yolov5x.pt --data coco.yaml --img 640 --half
출력:
val: data=./data/coco.yaml, weights=['yolov5x.pt'], batch_size=32, imgsz=640, conf_thres=0.001, iou_thres=0.65, task=val, device=, single_cls=False, augment=False, verbose=False, save_txt=False, save_conf=False, save_json=True, project=runs/val, name=exp, exist_ok=False, half=True
YOLOv5 🚀 v5.0-267-g6a3ee7c torch 1.9.0+cu102 CUDA:0 (Tesla P100-PCIE-16GB, 16280.875MB)
Fusing layers...
Model Summary: 476 layers, 87730285 parameters, 0 gradients
val: Scanning '../datasets/coco/val2017' images and labels...4952 found, 48 missing, 0 empty, 0 corrupted: 100% 5000/5000 [00:01<00:00, 2846.03it/s]
val: New cache created: ../datasets/coco/val2017.cache
Class Images Labels P R mAP@.5 mAP@.5:.95: 100% 157/157 [02:30<00:00, 1.05it/s]
all 5000 36335 0.746 0.626 0.68 0.49
Speed: 0.1ms pre-process, 22.4ms inference, 1.4ms NMS per image at shape (32, 3, 640, 640) # <--- baseline speed
Evaluating pycocotools mAP... saving runs/val/exp/yolov5x_predictions.json...
...
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.504 # <--- baseline mAP
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.688
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.546
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.351
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.551
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.644
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.382
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.628
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.681 # <--- baseline mAR
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.524
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.735
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.826
TTA로 테스트
추가 --augment
기존의 모든 val.py
TTA를 활성화하고 이미지 크기를 약 30% 늘려 결과를 개선하는 명령어입니다. TTA를 활성화한 추론은 이미지가 좌우로 뒤집히고 3가지 해상도로 처리되며, 출력이 병합되기 전에 일반 추론보다 일반적으로 약 2-3배의 시간이 걸립니다. NMS속도 감소의 일부는 단순히 더 커진 이미지 크기(832 vs 640) 때문이며, 다른 일부는 실제 TTA 연산 때문입니다.
python val.py --weights yolov5x.pt --data coco.yaml --img 832 --augment --half
출력:
val: data=./data/coco.yaml, weights=['yolov5x.pt'], batch_size=32, imgsz=832, conf_thres=0.001, iou_thres=0.6, task=val, device=, single_cls=False, augment=True, verbose=False, save_txt=False, save_conf=False, save_json=True, project=runs/val, name=exp, exist_ok=False, half=True
YOLOv5 🚀 v5.0-267-g6a3ee7c torch 1.9.0+cu102 CUDA:0 (Tesla P100-PCIE-16GB, 16280.875MB)
Fusing layers...
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at /pytorch/c10/core/TensorImpl.h:1156.)
return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
Model Summary: 476 layers, 87730285 parameters, 0 gradients
val: Scanning '../datasets/coco/val2017' images and labels...4952 found, 48 missing, 0 empty, 0 corrupted: 100% 5000/5000 [00:01<00:00, 2885.61it/s]
val: New cache created: ../datasets/coco/val2017.cache
Class Images Labels P R mAP@.5 mAP@.5:.95: 100% 157/157 [07:29<00:00, 2.86s/it]
all 5000 36335 0.718 0.656 0.695 0.503
Speed: 0.2ms pre-process, 80.6ms inference, 2.7ms NMS per image at shape (32, 3, 832, 832) # <--- TTA speed
Evaluating pycocotools mAP... saving runs/val/exp2/yolov5x_predictions.json...
...
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.516 # <--- TTA mAP
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.701
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.562
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.361
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.564
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.656
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.388
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 10 ] = 0.640
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.696 # <--- TTA mAR
Average Recall (AR) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.553
Average Recall (AR) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.744
Average Recall (AR) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.833
TTA를 사용한 추론
detect.py
TTA 추론은 다음과 동일하게 작동합니다. val.py
TTA: 간단히 추가 --augment
기존의 모든 detect.py
명령:
python detect.py --weights yolov5s.pt --img 832 --source data/images --augment
출력:
YOLOv5 🚀 v5.0-267-g6a3ee7c torch 1.9.0+cu102 CUDA:0 (Tesla P100-PCIE-16GB, 16280.875MB)
Downloading https://github.com/ultralytics/yolov5/releases/download/v5.0/yolov5s.pt to yolov5s.pt...
100% 14.1M/14.1M [00:00<00:00, 81.9MB/s]
Fusing layers...
Model Summary: 224 layers, 7266973 parameters, 0 gradients
image 1/2 /content/yolov5/data/images/bus.jpg: 832x640 4 persons, 1 bus, 1 fire hydrant, Done. (0.029s)
image 2/2 /content/yolov5/data/images/zidane.jpg: 480x832 3 persons, 3 ties, Done. (0.024s)
Results saved to runs/detect/exp
Done. (0.156s)
PyTorch Hub TTA
TTA는 모든 항목에 자동으로 통합됩니다. YOLOv5 PyTorch Hub 모델이며, 다음을 전달하여 액세스할 수 있습니다. augment=True
추론 시.
import torch
# Model
model = torch.hub.load("ultralytics/yolov5", "yolov5s") # or yolov5m, yolov5x, custom
# Images
img = "https://ultralytics.com/images/zidane.jpg" # or file, PIL, OpenCV, numpy, multiple
# Inference
results = model(img, augment=True) # <--- TTA inference
# Results
results.print() # or .show(), .save(), .crop(), .pandas(), etc.
맞춤 설정
다음에 적용되는 TTA 작업을 사용자 정의할 수 있습니다. YOLOv5 forward_augment()
메서드.
테스트 시간 증강의 장점
테스트 시간 증강은 객체 탐지 작업에 여러 가지 주요 이점을 제공합니다.
- 향상된 정확도: 위의 결과에서 볼 수 있듯이 TTA는 mAP를 0.504에서 0.516으로, mAR을 0.681에서 0.696으로 증가시킵니다.
- 더 나은 작은 객체 감지: TTA는 특히 작은 객체 감지를 향상시키며, 작은 영역 AP가 0.351에서 0.361로 향상됩니다.
- 향상된 견고성: 각 이미지의 여러 변형을 테스트함으로써 TTA는 시야각, 조명 및 기타 환경 요인의 영향을 줄입니다.
- 간단한 구현: 다음을 추가하기만 하면 됩니다.
--augment
플래그를 기존 명령어에 추가합니다.
단점은 추론 시간이 늘어난다는 점이며, 따라서 TTA는 속도보다 정확성이 우선시되는 애플리케이션에 더 적합합니다.
지원되는 환경
Ultralytics는 CUDA, CUDNN, Python 및 PyTorch와 같은 필수 종속성이 미리 설치된 다양한 즉시 사용 가능한 환경을 제공하여 프로젝트를 시작할 수 있도록 합니다.
- 무료 GPU 노트북:
- Google Cloud: GCP 빠른 시작 가이드
- Amazon: AWS 빠른 시작 가이드
- Azure: AzureML 빠른 시작 가이드
- Docker: Docker 빠른 시작 가이드
프로젝트 상태
이 배지는 모든 YOLOv5 GitHub Actions 지속적 통합(CI) 테스트가 성공적으로 통과되었음을 나타냅니다. 이러한 CI 테스트는 학습, 유효성 검사, 추론, 내보내기 및 벤치마크와 같은 다양한 주요 측면에서 YOLOv5의 기능과 성능을 엄격하게 검사합니다. macOS, Windows 및 Ubuntu에서 일관되고 안정적인 작동을 보장하며, 테스트는 24시간마다 그리고 새로운 커밋마다 수행됩니다.