【精选】【目标检测】K

您所在的位置:网站首页 yolov3中的anchors怎么产生的 【精选】【目标检测】K

【精选】【目标检测】K

2023-11-19 05:24| 来源: 网络整理| 查看: 265

我们都知道yolov3对训练数据使用了k-means聚类的算法 来获得anchor boxes大小,但是具体其计算过程是怎样的呢? 下面我们来详细的分析其具体计算过程:

第一步:首先我们要知道我们需要聚类的是bounding box,所以我们无需考虑其所属类别,第一步我们需要将所有的bounding box坐标提取出来,也许一张图有一个矩形框,也许有多个,但是我们需要无区别的将所有图片的所有矩形框提取出来,放在一起。

第二步:数据处理获得所有训练数据bounding boxes的宽高数据。给的训练数据往往是其bounding box的4个坐标,但是我们后续需要聚类分析的是bounding box的宽高大小,所以我们需要将坐标数据转换为框的宽高大小,计算方法很简单:宽 = 右下角横坐标 - 左上角横坐标、高 = 右下角纵坐标-左上角纵坐标。

第三步:初始化k个anchor box,通过在所有的bounding boxes中随机选取k个值作为k个anchor boxes的初始值。

第四步:计算每个bounding box与每个anchor box的iou值。传统的聚类方法是使用欧氏距离来衡量差异,也就是说如果我们运用传统的k-means聚类算法,可以直接聚类bounding box的宽和高,产生k个宽、高组合的anchor boxes,但是作者发现此方法在box尺寸比较大的时候,其误差也更大,所以作者引入了iou值,可以避免这个问题。iou值计算方法:这里参考下图和计算代码: 在这里插入图片描述

min_w_matrix = np.minimum(cluster_w_matrix, box_w_matrix) #cluster_w_matrix, box_w_matrix分别代表anchor box和bounding box宽大小 min_h_matrix = np.minimum(cluster_h_matrix, box_h_matrix) #cluster_h_matrix, box_h_matrix分别代表anchor box和bounding box高大小 inter_area = np.multiply(min_w_matrix, min_h_matrix) #inter_area表示重叠面积 IOU = inter_area / (box_area + cluster_area - inter_area)#box_area表示bounding box面积 ;cluster_area表示anchor box面积

由于iou值往往越大越好,所以作者定义了一个距离d参数,用来表示其误差:

d=1-IOU

第五步:分类操作。经过前一步的计算可以的到每一个bounding box对于每个anchor box的误差d(n,k),我们通过比较每个bounding box其对于每个anchor box的误差大小{d(i,1),d(i,2),…,d(i,k)},选取最小误差的那个anchor box,将这个bounding box分类给它,对于每个bounding box都做这个操作,最后记录下来每个anchor box有哪些bounding box属于它。

第六步:anchor box更新。经过上一步,我们就知道每一个anchor box都有哪些bounding box属于它,然后对于每个anchor box中的那些bounding box,我们再求这些bounding box的宽高中值大小(这里参照github上作者qqwweee那个yolov3项目,也许也有使用平均值进行更新),将其作为该anchor box新的尺寸。

第七步:重复操作第四步到第六步,直到在第五步中发现对于全部bounding box其所属的anchor box类与之前所属的anchor box类完全一样。(这里表示所有bounding box的分类已经不再更新)

第八步:计算anchor boxes精确度。至第七步,其实已经通过k-means算法计算出anchor box。但是细心的同学可能已经发现,k-means.py还给出其精确度大小,其计算方法如下:使用最后得到的anchor boxes与每个bounding box计算其IOU值,对于每个bounding box选取其最高的那个IOU值(代表其属于某一个anchor box类),然后求所有bounding box该IOU值的平均值也即最后的精确度值。

代码:

import glob import random import xml.etree.ElementTree as ET import numpy as np def cas_iou(box, cluster): x = np.minimum(cluster[:, 0], box[0]) y = np.minimum(cluster[:, 1], box[1]) intersection = x * y area1 = box[0] * box[1] area2 = cluster[:, 0] * cluster[:, 1] iou = intersection / (area1 + area2 - intersection) return iou def avg_iou(box, cluster): return np.mean([np.max(cas_iou(box[i], cluster)) for i in range(box.shape[0])]) def kmeans(box, k): # 取出一共有多少框 row = box.shape[0] # 每个框各个点的位置 distance = np.empty((row, k)) # 最后的聚类位置 last_clu = np.zeros((row,)) np.random.seed() # 随机选5个当聚类中心 cluster = box[np.random.choice(row, k, replace=False)] # cluster = random.sample(row, k) while True: # 计算每一行距离五个点的iou情况。 for i in range(row): distance[i] = 1 - cas_iou(box[i], cluster) # 取出最小点 near = np.argmin(distance, axis=1) if (last_clu == near).all(): break # 求每一个类的中位点 for j in range(k): cluster[j] = np.median( box[near == j], axis=0) last_clu = near return cluster def load_data(path): data = [] # 对于每一个xml都寻找box for xml_file in glob.glob('{}/*xml'.format(path)): tree = ET.parse(xml_file) height = int(tree.findtext('./size/height')) width = int(tree.findtext('./size/width')) if height


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3