YOLOV5测试代码test.py注释与解析

YOLOv5代码注释版更新啦,注释的是最近的2021.07.14的版本,且注释更全
github: https://github.com/Laughing-q/yolov5_annotations

YOLOV5测试代码test.py注释与解析

本文主要对ultralytics\yolov5的测试代码test.py的解析,由于yolov5还在开发当中,平常多多少少都会修复一些bug或者有一些代码和功能的更新,但基本上不会有很大的改动,故以下注释与解析都是适用的;当然如果有大改动,笔者也会更新注释。

yolov5其他代码解析

测试参数以及main函数解析

if __name__ == '__main__':
    """
    opt参数详解
    weights:测试的模型权重文件
    data:数据集配置文件,数据集路径,类名等
    batch-size:前向传播时的批次, 默认32
    img-size:输入图片分辨率大小, 默认640
    conf-thres:筛选框的时候的置信度阈值, 默认0.001
    iou-thres:进行NMS的时候的IOU阈值, 默认0.65
    save-json:是否按照coco的json格式保存预测框,并且使用cocoapi做评估(需要同样coco的json格式的标签), 默认False
    task:设置测试形式, 默认val, 具体可看下面代码解析注释
    device:测试的设备,cpu;0(表示一个gpu设备cuda:0);0,1,2,3(多个gpu设备)
    single-cls:数据集是否只有一个类别,默认False
    augment:测试时是否使用TTA(test time augmentation), 默认False
    merge:在进行NMS时,是否通过合并方式获得预测框, 默认False
    verbose:是否打印出每个类别的mAP, 默认False
    save-txt:是否以txt文件的形式保存模型预测的框坐标, 默认False
    """
    parser = argparse.ArgumentParser(prog='test.py')
    parser.add_argument('--weights', nargs='+', type=str, default='runs/exp0/weights/last.pt', help='model.pt path(s)')
    parser.add_argument('--data', type=str, default='data/mask.yaml', help='*.data path')
    parser.add_argument('--batch-size', type=int, default=32, help='size of each image batch')
    parser.add_argument('--img-size', type=int, default=416, help='inference size (pixels)')
    parser.add_argument('--conf-thres', type=float, default=0.001, help='object confidence threshold')
    parser.add_argument('--iou-thres', type=float, default=0.60, help='IOU threshold for NMS')
    parser.add_argument('--save-json', action='store_true', help='save a cocoapi-compatible JSON results file')
    parser.add_argument('--task', default='val', help="'val', 'test', 'study'")
    parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
    parser.add_argument('--single-cls', action='store_true', help='treat as single-class dataset')
    parser.add_argument('--augment', action='store_true', help='augmented inference')
    parser.add_argument('--merge', action='store_true', help='use Merge NMS')
    parser.add_argument('--verbose', action='store_true', help='report mAP by class')
    parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
    opt = parser.parse_args()
    # 设置参数save_json
    opt.save_json = opt.save_json or opt.data.endswith('coco.yaml')
    # check_file检查文件是否存在
    opt.data = check_file(opt.data)  # check file
    print(opt)

    # task = 'val', 'test', 'study'
    # task = ['val', 'test']时就正常测试验证集、测试集
    if opt.task in ['val', 'test']:  # (default) run normally
        test(opt.data,
             opt.weights,
             opt.batch_size,
             opt.img_size,
             opt.conf_thres,
             opt.iou_thres,
             opt.save_json,
             opt.single_cls,
             opt.augment,
             opt.verbose)

    # task == 'study'时,就评估yolov5系列和yolov3-spp各个模型在各个尺度下的指标并可视化
    elif opt.task == 'study':  # run over a range of settings and save/plot
        for weights in ['yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt', 'yolov3-spp.pt']:
            f = 'study_%s_%s.txt' % (Path(opt.data).stem, Path(weights).stem)  # filename to save to
            x = list(range(352, 832, 64))  # x axis
            y = []  # y axis
            for i in x:  # img-size
                print('\nRunning %s point %s...' % (f, i))
                r, _, t = test(opt.data, weights, opt.batch_size, i, opt.conf_thres, opt.iou_thres, opt.save_json)
                y.append(r + t)  # results and times
            np.savetxt(f, y, fmt='%10.4g')  # save
        os.system('zip -r study.zip study_*.txt')
        plot_study_txt(f, x)  # plot

test函数解析

import argparse
import json

from models.experimental import *
  • 61
    点赞
  • 345
    收藏
    觉得还不错? 一键收藏
  • 192
    评论
要运行YOLOv7的测试代码test.py,您需要按照以下步骤进行操作: 1. 首先,确保您的计算机已经安装了所需的类库。您可以根据\[1\]中提供的链接下载所需的依赖类库。 2. 接下来,您需要下载YOLOv7的源码。您可以按照\[2\]中的说明下载源码。 3. 然后,您还需要下载预训练模型和数据集。您可以根据\[2\]中的说明下载所需的预训练模型和数据集。 4. 在下载完源码、预训练模型和数据集后,您可以使用VSCode打开项目文件夹。 5. 打开test.py文件,并按下F5键运行代码。请确保您的Python环境中已经安装了所需的类库。 6. 运行结果将保存在runs\test\exp目录下。 如果在运行过程中遇到以下问题,您可以尝试解决: 5.1 若提示页面文件太小,无法完成操作,则需要调高虚拟内存大小。您可以按照\[3\]中的说明打开系统高级设置,并调整虚拟内存大小。 5.2 若提示错误:BrokenPipeError,您可以将utils/datasets.py文件中的第87行的num_workers设置为0,然后重新运行代码。 希望以上信息对您有所帮助!如果您还有其他问题,请随时提问。 #### 引用[.reference_title] - *1* *2* *3* [YOLO7环境搭建、代码测试](https://blog.csdn.net/maerdym/article/details/125841657)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 192
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值