yolov5 test.py val.py detec.py 区别在哪里呢?

用户在训练数据的时候必须使用 train.py 来进行 数据训练和验证,但我很难理解 detect.pytest.py 之间的区别。应该在一个数据集中的(看不见的)测试数据上运行这两者中的哪一个?

首先: test.py 最近被重命名为 val.py

这 3 个文件是为不同目的而设计的,并使用具有不同设置的不同数据加载器。

  • train.py 数据加载器旨在兼顾速度和准确性,
  • val.py 旨在获得验证数据集上的最佳 mAP,
  • detect.py 旨在真实世界中获得最佳的推理结果。

每个文件中的几个重要方面包括:

train.py

trainloader: LoadImagesAndLabels():旨在加载训练数据集图像和标签。增强能力可以选择启用。

 # Trainloader 
 dataloader, dataset = create_dataloader(train_path, imgsz, batch_size, gs, opt, 
                                         hyp=hyp, augment=True, cache=opt.cache_images, rect=opt.rect, rank=rank, 
                                         world_size=opt.world_size, workers=opt.workers, 
                                         image_weights=opt.image_weights, quad=opt.quad, prefix=colorstr('train: ')) 

testloader: LoadImagesAndLabels():旨在加载 val 数据集图像和标签。增强能力但被禁用。

 testloader = create_dataloader(test_path, imgsz_test, batch_size * 2, gs, opt,  # testloader 
                                hyp=hyp, cache=opt.cache_images and not opt.notest, rect=True, rank=-1, 
                                world_size=opt.world_size, workers=opt.workers, 
                                pad=0.5, prefix=colorstr('val: '))[0] 
  • image size: 640
  • rectangular inference: False
  • confidence threshold: 0.001
  • iou threshold: 0.6
  • multi-label: True
  • padding: None

val.py

dataloader: LoadImagesAndLabels():设计用于加载训练、验证、测试数据集图像和标签。增强能力但被禁用。

 dataloader = create_dataloader(data[task], imgsz, batch_size, gs, single_cls, pad=0.5, rect=True, 
                                prefix=colorstr(f'{task}: '))[0] 
  • image size: 640
  • rectangular inference: True
  • confidence threshold: 0.001
  • iou threshold: 0.6
  • multi-label: True
  • padding: 0.5 * maximum stride

detect.py

dataloaders(多个):设计用于加载多种类型的媒体(images, videos, globs, directories, streams)。

 # Set Dataloader 
 vid_path, vid_writer = None, None 
 if webcam: 
     view_img = check_imshow() 
     cudnn.benchmark = True  # set True to speed up constant image size inference 
     dataset = LoadStreams(source, img_size=imgsz, stride=stride) 
 else: 
     dataset = LoadImages(source, img_size=imgsz, stride=stride) 
  • image size: 640
  • rectangular inference: True
  • confidence threshold: 0.25
  • iou threshold: 0.45
  • multi-label: False
  • padding: None

YOLOv5 PyTorch Hub Inference

models.autoShape() 类用于图像加载、预处理、推理和 NMS。有关更多信息,请参阅 YOLOv5 PyTorch Hub 教程

 class autoShape(nn.Module): 
     # input-robust model wrapper for passing cv2/np/PIL/torch inputs. Includes preprocessing, inference and NMS 
     conf = 0.25  # NMS confidence threshold 
     iou = 0.45  # NMS IoU threshold 
     classes = None  # (optional list) filter by class 
  
     def __init__(self, model): 
         super(autoShape, self).__init__() 
         self.model = model.eval() 
  
     def autoshape(self): 
  • image size: 640
  • rectangular inference: True
  • confidence threshold: 0.25
  • iou threshold: 0.45
  • multi-label: False
  • padding: None
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐