yolov4-pytorch icon indicating copy to clipboard operation
yolov4-pytorch copied to clipboard

kmeans聚类问题?

Open ChenMaolong opened this issue 2 years ago • 2 comments

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)]

iter = 0
while True:
    #-------------------------------------------------------------#
    #   计算当前框和先验框的宽高比例
    #-------------------------------------------------------------#
    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
    if iter % 5 == 0:
        print('iter: {:d}. avg_iou:{:.2f}'.format(iter, avg_iou(box, cluster)))
    iter += 1

return cluster, near

大佬我按照 9个聚类中心的话,你这里说 # 随机选5个当聚类中心代码中if iter % 5 == 0:是不是要把5改为9,还是这个5是迭代聚类的次数?

ChenMaolong avatar Feb 19 '23 09:02 ChenMaolong

这只是print用的,和几个点没关

bubbliiiing avatar Feb 23 '23 16:02 bubbliiiing

那这里如果要随机选择2个当聚类中心,是要修改这部分哪里的代码呢?求大佬指点

yusiyiyusiyi avatar Apr 23 '23 13:04 yusiyiyusiyi