Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kmeans聚类问题? #358

Open
ChenMaolong opened this issue Feb 19, 2023 · 2 comments
Open

kmeans聚类问题? #358

ChenMaolong opened this issue Feb 19, 2023 · 2 comments

Comments

@ChenMaolong
Copy link

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是迭代聚类的次数?

@bubbliiiing
Copy link
Owner

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

@yusiyiyusiyi
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants