# YOLOV3中k-means聚类获得anchor boxes过程详解

6 篇文章 3 订阅
4 篇文章 0 订阅

## YOLOV3中k-means聚类获得anchor boxes过程详解

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

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面积


d=1-IOU

import numpy as np
import xml.etree.ElementTree as ET
import glob
import random

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

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'))
# 对于每一个目标都获得它的宽高
for obj in tree.iter('object'):
xmin = int(float(obj.findtext('bndbox/xmin'))) / width
ymin = int(float(obj.findtext('bndbox/ymin'))) / height
xmax = int(float(obj.findtext('bndbox/xmax'))) / width
ymax = int(float(obj.findtext('bndbox/ymax'))) / height

xmin = np.float64(xmin)
ymin = np.float64(ymin)
xmax = np.float64(xmax)
ymax = np.float64(ymax)
# 得到宽高
data.append([xmax-xmin,ymax-ymin])
return np.array(data)

if __name__ == '__main__':
# 运行该程序会计算'./VOCdevkit/VOC2007/Annotations'的xml
# 会生成yolo_anchors.txt
SIZE = 416
anchors_num = 6
# 载入数据集，可以使用VOC的xml
path = r'./VOCdevkit/VOC2007/Annotations'

# 载入所有的xml
# 存储格式为转化为比例后的width,height

# 使用k聚类算法
out = kmeans(data,anchors_num)
out = out[np.argsort(out[:,0])]
print('acc:{:.2f}%'.format(avg_iou(data,out) * 100))
print(out*SIZE)
data = out*SIZE
f = open("yolo_anchors.txt", 'w')
row = np.shape(data)[0]
for i in range(row):
if i == 0:
x_y = "%d,%d" % (data[i][0], data[i][1])
else:
x_y = ", %d,%d" % (data[i][0], data[i][1])
f.write(x_y)
f.close()


• 58
点赞
• 277
收藏
觉得还不错? 一键收藏
• 45
评论
09-11 4902
02-21 1195
09-06 5387
03-28 6933
10-18 2万+
08-28
05-28 1135
01-12 1547
07-28 860
05-22 192

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

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