1.Kmeans介绍
kmeans聚类属于无监督学习算法,目的是将一组数据分成k组,称之为k个簇,计算出这k组中每组的中心。
从百度图片上找了一张图片,大概就是这样,分成下图中的三组,并且找出每一组的中心。
算法思想:
(1)从数据集中随机选取k个点,作为初始化的簇中心
(2)计算每个点到簇中心的距离,并将该点分配到最近的簇中(与那一簇中心的距离最近)
(3)对于2步中重新分配好的簇,重新计算这个簇的中心(大概就是求横纵坐标的均值做为新的中心)
(4)重复2, 3步,直到新计算的簇中心不再变化为止
2.kmeans应用于anchors box计算
anchors box用于预测bounding box,当anchors box更接近真实的宽高时,模型的性能也就越好。
kmeans应用在anchors box的计算就是为了计算出更接近真实宽高的k对值。
与上边的kmeans不同的是不能用欧式几何距离进行分簇,而是采用IOU交并比来作为衡量每队值应该划分进那一簇。
不知道IOU的看这篇:https://blog.csdn.net/weixin_39025871/article/details/105209726
IOU可以很好的表示出两对宽高的接近情况,IOU取值为[0,1]之间,IOU越大就表示这两对宽高比越贴近,反之越小表示这两对宽高比差别越远。(放进上边介绍中,就代表,IOU越大,就是两个点离得越近,越小就是两个点离得越远)
在kmeans里,"距离" 就用 1 - IOU表示,划分时选择值较小的,也就代表了介绍里的距离近。
算法流程和介绍里的基本类似,就是将距离换成 1-IOU
3.kmeans求解anchors代码:
代码来源:GitHub - qqwweee/keras-yolo3: A Keras implementation of YOLOv3 (Tensorflow backend) 我加上了一些注释
import numpy as np
class YOLO_Kmeans:
def __init__(self, cluster_number, filename):
self.cluster_number = cluster_number
self.filename = filename
def iou(self, boxes, clusters): # 1 box -> k clusters 计算IOU
n = boxes.shape[0]
k = self.cluster_number
box_area = boxes[:, 0] * boxes[:, 1]
box_area = box_area.repeat(k)
box_area = np.reshape(box_area, (n, k))
cluster_area = clusters[:, 0] * clusters[:, 1]
cluster_area = np.tile(cluster_area, [1, n])
cluster_area = np.reshape(cluster_area, (n, k))
box_w_matrix = np.reshape(boxes[:, 0].repeat(k), (n, k))
cluster_w_matrix = np.reshape(np.tile(clusters[:, 0], (1, n)), (n, k))
min_w_matrix = np.minimum(cluster_w_matrix, box_w_matrix)
box_h_matrix = np.reshape(boxes[:, 1].repeat(k), (n, k))
cluster_h_matrix = np.reshape(np.tile(clusters[:, 1], (1, n)), (n, k))
min_h_matrix = np.minimum(cluster_h_matrix, box_h_matrix)
inter_area = np.multiply(min_w_matrix, min_h_matrix) # 交集
result = inter_area / (box_area + cluster_area - inter_area) #交并比IOU
return result
def avg_iou(self, boxes, clusters): #计算 求完anchors后的准确率
accuracy = np.mean([np.max(self.iou(boxes, clusters), axis=1)])
return accuracy
def kmeans(self, boxes, k, dist=np.median):
box_number = boxes.shape[0]
distances = np.empty((box_number, k))
last_nearest = np.zeros((box_number,))
np.random.seed()
clusters = boxes[np.random.choice( #初始化簇中心,随机选取k个宽高作为簇中心
box_number, k, replace=False)] # init k clusters
while True:
distances = 1 - self.iou(boxes, clusters) #距离 用 1-IOU
current_nearest = np.argmin(distances, axis=1)
if (last_nearest == current_nearest).all(): #比较本次与上一次的k个簇中心是否变化
break # clusters won't change #没有变化则停止迭代更新
for cluster in range(k):
clusters[cluster] = dist( # update clusters
boxes[current_nearest == cluster], axis=0)
last_nearest = current_nearest
return clusters
def result2txt(self, data): #将计算结果写进yolo_anchors.txt
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()
def txt2boxes(self): #从训练集文件中读取并计算实际的宽 高
f = open(self.filename, 'r')
dataSet = []
for line in f:
infos = line.split(" ")
length = len(infos)
for i in range(1, length):
width = int(infos[i].split(",")[2]) - \
int(infos[i].split(",")[0])
height = int(infos[i].split(",")[3]) - \
int(infos[i].split(",")[1])
dataSet.append([width, height])
result = np.array(dataSet)
f.close()
return result
def txt2clusters(self): # 计算 anchors
all_boxes = self.txt2boxes() # 加载实际数据 宽高
result = self.kmeans(all_boxes, k=self.cluster_number)
result = result[np.lexsort(result.T[0, None])]
self.result2txt(result)
print("K anchors:\n {}".format(result))
print("Accuracy: {:.2f}%".format(
self.avg_iou(all_boxes, result) * 100))
if __name__ == "__main__":
cluster_number = 9 # 分成k组,根据需要自己设定
filename = "2012_train.txt" #训练集文件,格式为: 路径 [左上 右下 类别](5个值) ...
kmeans = YOLO_Kmeans(cluster_number, filename)
kmeans.txt2clusters()