【玩转yolov5】请看代码之自动anchor计算

      早在yolov2时就了解到不同于faster-rcnn中手动设置的anchor,yolov2中的 anchor是通过k-means聚类算法得的,这样更贴合实际的训练数据。这次借学习yolov5的机会把其中关于自动anchor计算的逻辑再梳理一遍,重点就是分析一下utils/autoanchor.py文件的相关函数。除非显示地设置noautoanchor参数为True,否则训练过程中默认会使用自动 anchor 计算,即调用check_anchors函数。

....
if not opt.noautoanchor:
    check_anchors(dataset, model=model, thr=hyp['anchor_t'], imgsz=imgsz)
...

参数dataset代表的是训练集,hyp['anchor_t']这个超参数是一个界定anchor与label匹配程度的阈值,imgsz自然就是网络输入尺寸,后面的讲解中按默认的640来推演。

def check_anchors(dataset, model, thr=4.0, imgsz=640):
    # Check anchor fit to data, recompute if necessary
    print('\nAnalyzing anchors... ', end='')
    m = model.module.model[-1] if hasattr(model, 'module') else model.model[-1]  # Detect()
    shapes = imgsz * dataset.shapes / dataset.shapes.max(1, keepdims=True)
    #0.1-1.1
    scale = np.random.uniform(0.9, 1.1, size=(shapes.shape[0], 1))  # augment scale
    wh = torch.tensor(np.concatenate([l[:, 3:5] * s for s, l in zip(shapes * scale, dataset.labels)])).float()  # wh

    def metric(k):  # compute metric
        r = wh[:, None] / k[None]
        x = torch.min(r, 1. / r).min(2)[0]  # ratio metric
        best = x.max(1)[0]  # best_x
       
        aat = (x > 1. / thr).float().sum(1).mean()  # anchors above threshold
        bpr = (best > 1. / thr).float().mean()  # best possible recall
        return bpr, aat
    
    bpr, aat = metric(m.anchor_grid.clone().cpu().view(-1, 2))
    print('anchors/target = %.2f, Best Possible Recall (BPR) = %.4f' % (aat, bpr), end='')
    #只有当由默认设定的anchor计算出来的bpr这一指标少于设定的阈值时才会通过聚类算法重新计算anchor
    if bpr < 0.98:  # threshold to recompute
        print('. Attempting to improve anchors, please wait...')
        na = m.anchor_grid.numel() // 2  # number of anchors
        new_anchors = kmean_anchors(dataset, n=na, img_size=imgsz, thr=thr, gen=1000, verbose=False)
        new_bpr = metric(new_anchors.reshape(-1, 2))[0]
        if new_bpr > bpr:  # replace anchors
            new_anchors = torch.tensor(new_anchors, device=m.anchors.device).type_as(m.anchors)
            m.anchor_grid[:] = new_anchors.clone().view_as(m.anchor_grid)  # for inference
            m.anchors[:] = new_anchors.clone().view_as(m.anchors) / m.stride.to(m.anchors.device).view(-1, 1, 1)  # loss
            check_anchor_order(m)
            print('New anchors saved to model. Update model *.yaml to use these anchors in the future.')
        else:
            print('Original anchors better than new anchors. Proceeding with original anchors.')
    print('')  # newline

yolov5的网络结构配置文件(如:models/yolov5s.yaml)已经给出了默认的一组anchor,只有当bpr小于0.98时才会重新计算anchor,所以这里首先需要搞清楚bpr是什么,它又是怎么计算的?我们知道在训练过程中任何一个gt框它归根结底是要落到特征图中的某个网格的。在yolov5中默认设置了9种anchor,在具体计算bpr(best possible recall)的时候,会考虑这9类anchor的宽高和gt框的宽高之间的差距。上述代码中变量wh用来存储训练数据中所有gt框的宽高,是一个shape为(N,2)的tensor,这里的2自然就是表示的宽和高,N为gt框的总的个数。metric根据默认anchor和wh来具体计算bpr,aat(anchors above threshold)两个指标。

def metric(k):  # compute metric
    r = wh[:, None] / k[None]
    x = torch.min(r, 1. / r).min(2)[0]  # ratio metric
    best = x.max(1)[0]  # best_x
       
    aat = (x > 1. / thr).float().sum(1).mean()  # anchors above threshold
    bpr = (best > 1. / thr).float().mean()  # best possible recall
    return bpr, aat

metric这个函数初看一脸懵逼,再看拍案叫绝。输入参数k存储anchors,调用时被reshape后的尺寸为(9,2)。接下来要计算每个gt框的宽高和所有这9个anchor的宽高的比例值,得到的r其shape为(N,9,2)。x=torch.min(r,1./r).min(2)[0],怎么理解这句代码呢?w_gt/w_anchor或者h_gt/h_anchor这个比例值可能大于1也可能小于1,通过torch.min(r,1./r)的方式统一到<=1的情形,然后再从中选取较小的这个值。得到的x其shape为(n,9),x.max(1)[0]为每个gt框选择匹配宽高比例值最好的那一个值。这样就可以计算aat和bpr了。

def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=640, thr=4.0, gen=1000, verbose=True):
    """ Creates kmeans-evolved anchors from training dataset

        Arguments:
            path: path to dataset *.yaml, or a loaded dataset
            n: number of anchors
            img_size: image size used for training
            thr: anchor-label wh ratio threshold hyperparameter hyp['anchor_t'] used for training, default=4.0
            gen: generations to evolve anchors using genetic algorithm
            verbose: print all results

        Return:
            k: kmeans evolved anchors

        Usage:
            from utils.autoanchor import *; _ = kmean_anchors()
    """
    #import pdb 
    #pdb.set_trace()

    thr = 1. / thr

    def metric(k, wh):  # compute metrics
        #计算数据集中的gt框与anchor对应宽和高的比例即:gt_w/k_w,gt_h/k_h
        r = wh[:, None] / k[None] 
        x = torch.min(r, 1. / r).min(2)[0]  # ratio metric
        # x = wh_iou(wh, torch.tensor(k))  # iou metric
        return x, x.max(1)[0]  # x, best_x

    def anchor_fitness(k):  # mutation fitness
        _, best = metric(torch.tensor(k, dtype=torch.float32), wh)
        return (best * (best > thr).float()).mean()  # fitness

    def print_results(k):
        k = k[np.argsort(k.prod(1))]  #计算每一行的乘积(w*h),然后排序得到排序后的k
        x, best = metric(k, wh0)
        bpr, aat = (best > thr).float().mean(), (x > thr).float().mean() * n  # best possible recall, anch > thr
        print('thr=%.2f: %.4f best possible recall, %.2f anchors past thr' % (thr, bpr, aat))
        print('n=%g, img_size=%s, metric_all=%.3f/%.3f-mean/best, past_thr=%.3f-mean: ' %
              (n, img_size, x.mean(), best.mean(), x[x > thr].mean()), end='')
        for i, x in enumerate(k):
            print('%i,%i' % (round(x[0]), round(x[1])), end=',  ' if i < len(k) - 1 else '\n')  # use in *.cfg
        return k

    if isinstance(path, str):  # *.yaml file
        with open(path) as f:
            data_dict = yaml.load(f, Loader=yaml.FullLoader)  # model dict
        from utils.datasets import LoadImagesAndLabels
        dataset = LoadImagesAndLabels(data_dict['train'], augment=True, rect=True)
    else:
        dataset = path  # dataset

    # Get label wh
    shapes = img_size * dataset.shapes / dataset.shapes.max(1, keepdims=True)
    wh0 = np.concatenate([l[:, 3:5] * s for s, l in zip(shapes, dataset.labels)])  #数据集中gt框的wh

    # Filter,表示宽或者高小于3个像素,目标太小
    i = (wh0 < 3.0).any(1).sum()
    if i:
        print('WARNING: Extremely small objects found. '
              '%g of %g labels are < 3 pixels in width or height.' % (i, len(wh0)))
    #label大于2个像素的框拿来聚类,[...]内的相当于一个筛选器,为True的留下
    wh = wh0[(wh0 >= 2.0).any(1)]  # filter > 2 pixels
    # wh = wh * (np.random.rand(wh.shape[0], 1) * 0.9 + 0.1)  # multiply by random scale 0-1

    # Kmeans calculation
    print('Running kmeans for %g anchors on %g points...' % (n, len(wh)))
    #计算宽和高的标准差->[w_std,h_std]
    s = wh.std(0)  # sigmas for whitening
    #开始聚类,仍然是聚成n类,返回聚类后的anchors k
    k, dist = kmeans(wh / s, n, iter=30)  # points, mean distance
    k *= s
    wh = torch.tensor(wh, dtype=torch.float32)  # filtered
    wh0 = torch.tensor(wh0, dtype=torch.float32)  # unfiltered
    k = print_results(k)


    # Evolve
    npr = np.random
    f, sh, mp, s = anchor_fitness(k), k.shape, 0.9, 0.1  # fitness, generations, mutation prob, sigma
    pbar = tqdm(range(gen), desc='Evolving anchors with Genetic Algorithm')  # progress bar
    for _ in pbar:
        v = np.ones(sh)
        while (v == 1).all():  # mutate until a change occurs (prevent duplicates)
            v = ((npr.random(sh) < mp) * npr.random() * npr.randn(*sh) * s + 1).clip(0.3, 3.0)
        kg = (k.copy() * v).clip(min=2.0)
        fg = anchor_fitness(kg)
        if fg > f:
            f, k = fg, kg.copy()
            pbar.desc = 'Evolving anchors with Genetic Algorithm: fitness = %.4f' % f
            if verbose:
                print_results(k)

    return print_results(k)

  • 28
    点赞
  • 134
    收藏
    觉得还不错? 一键收藏
  • 27
    评论
YOLO系列是基于深度学习的端到端实时目标检测方法。 PyTorch版的YOLOv5轻量而高性能,更加灵活和易用,当前非常流行。 本课程将手把手地教大家使用labelImg标注和使用YOLOv5训练自己的数据集。课程实战分为两个项目:单目标检测(足球目标检测)和多目标检测(足球和梅西同时检测)。  本课程的YOLOv5使用ultralytics/yolov5,在Windows和Ubuntu系统上分别做项目演示。包括:安装YOLOv5、标注自己的数据集、准备自己的数据集(自动划分训练集和验证集)、修改配置文件、使用wandb训练可视化工具、训练自己的数据集、测试训练出的网络模型和性能统计。 除本课程《YOLOv5实战训练自己的数据集(Windows和Ubuntu演示)》外,本人推出了有关YOLOv5目标检测的系列课程。持续关注该系列的其它视频课程,包括:《YOLOv5(PyTorch)目标检测:原理与源码解析》课程链接:https://edu.csdn.net/course/detail/31428《YOLOv5目标检测实战:Flask Web部署》课程链接:https://edu.csdn.net/course/detail/31087《YOLOv5(PyTorch)目标检测实战:TensorRT加速部署》课程链接:https://edu.csdn.net/course/detail/32303《YOLOv5目标检测实战:Jetson Nano部署》课程链接:https://edu.csdn.net/course/detail/32451《YOLOv5+DeepSORT多目标跟踪与计数精讲》课程链接:https://edu.csdn.net/course/detail/32669《YOLOv5实战口罩佩戴检测》课程链接:https://edu.csdn.net/course/detail/32744《YOLOv5实战中国交通标志识别》课程链接:https://edu.csdn.net/course/detail/35209 《YOLOv5实战垃圾分类目标检测》课程链接:https://edu.csdn.net/course/detail/35284  
YOLOv5 中,自适应锚框(Adaptive Anchors)的计算是在 `yolo.py` 文件中的 `create_grids` 函数中实现的。下面是相关代码的简要解释: ```python def create_grids(self, img_size=640, stride=self.stride): # 计算特征图大小 grid_sizes = [img_size // s for s in stride] # [80, 40, 20] # 计算锚框比例 anchor_ratios = [(1.0, 1.0), (1.4, 0.7), (0.7, 1.4)] # 计算锚框的尺寸 anchors = [] for r in anchor_ratios: for s in stride: anchors.append((int(r[0] * s), int(r[1] * s))) # 不同尺度下的锚框尺寸 # 计算每个特征图上的锚框数量 num_anchors = len(anchor_ratios) * len(stride) # 每个特征图的锚框数量 self.num_anchors = num_anchors self.anchor_grid = torch.tensor(anchors).float() / torch.tensor(stride).float().view(-1, 1, 1) self.anchor_wh = self.anchor_grid.clone().view(len(stride), -1, 2) ``` 该函数首先计算了三个特征图的大小(在输入图像大小为 640 的情况下),即 `[80, 40, 20]`。然后,它定义了三种不同的锚框比例(即高宽比),分别为 `(1.0, 1.0)`、`(1.4, 0.7)` 和 `(0.7, 1.4)`。接下来,对于每个比例和每个特征图尺寸,计算了对应的锚框尺寸,将它们存储在一个列表中。然后,计算了每个特征图上的锚框数量,并使用 `torch.tensor` 将锚框列表转换为张量。最后,将锚框张量除以步幅张量,得到每个锚框在特征图上的位置,存储在 `anchor_grid` 中。同时,将 `anchor_grid` 重新排列成 `(len(stride), num_anchors, 2)` 的张量,存储在 `anchor_wh` 中。这些计算结果将用于后续的预测和损失计算

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值