本指南解释了如何在测试和推断改进的mAP和Recall过程中使用YOLOv5模型集成。2022年1月25日更新。
集成建模是一个过程,通过使用许多不同的建模算法或使用不同的训练数据集,创建多个不同的模型来预测结果。然后,集合模型将每个基本模型的预测聚合起来,并对未见数据产生一次最终预测。使用集成模型的动机是为了减少预测的泛化误差。当采用集成方法时,只要基本模型是不同的和独立的,模型的预测误差就会减小。这种方法在预测中寻求群体的智慧。尽管集成模型在模型中有多个基本模型,但它作为单个模型进行操作和执行。
1、Ensemble Test
python val.py --weights yolov5x.pt yolov5l6.pt --data coco.yaml --img 640 --half
2、Ensemble Inference
python detect.py --weights model1.pt model2.pt --augment
3、TTA和模型集成测试
集合和TTA不是相互排斥的。你可以TTA一个模型,你也可以集成一组有或没有TTA的模型:
python detect.py --weights yolov5x.pt yolov5l6.pt --img 640 --source data/images
ensembling runs multiple models, while TTA tests a single model at with different augmentations. Typically I've seen the best result when merging output grids directly, (i.e. ensembling YOLOv5l and YOLOv5x), rather than simply appending boxes from multiple models for NMS to sort out. This is not always possible however, for example Ensembling an EfficientDet model with YOLOv5x, you can not merge grids, you must use NMS or WBF (or Merge NMS) to get a final result.
$ python val.py --weights yolov5x.pt --data coco.yaml --img 832 --augment --half
$ python detect.py --weights yolov5s.pt --img 832 --source data/images --augment
ou can customize the TTA ops applied in the YOLOv5 forward_augment()
method here:
def forward_augment(self, x):
img_size = x.shape[-2:] # height, width
s = [1, 0.83, 0.67] # scales
f = [None, 3, None] # flips (2-ud, 3-lr)
y = [] # outputs
for si, fi in zip(s, f):
xi = scale_img(x.flip(fi) if fi else x, si, gs=int(self.stride.max()))
yi = self.forward_once(xi)[0] # forward
# cv2.imwrite(f'img_{si}.jpg', 255 * xi[0].cpu().numpy().transpose((1, 2, 0))[:, :, ::-1]) # save
yi = self._descale_pred(yi, fi, si, img_size)
y.append(yi)
return torch.cat(y, 1), None # augmented inference, train
WBF:优化目标检测,融合过滤预测框
使用YoloV5高级功能提升目标检测性能
https://towardsdatascience.com/advanced-yolov5-tutorial-enhancing-yolov5-with-weighted-boxes-fusion-3bead5b71688