-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathkmeans.py
More file actions
34 lines (31 loc) · 1.01 KB
/
kmeans.py
File metadata and controls
34 lines (31 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import torch
import random
import numpy as np
from sklearn.cluster import KMeans
def seg2points(X, K):
'''Pick K centroids with K-means++ algorithm.
Args:
X: (tensor) data, sized [N,D].
K: (int) number of clusters.
ReturnsL
(tensor) picked centroids, sized [K,D].
'''
C,H,W = X.size()
M = X.permute(1,2,0).view(-1, C).cpu().numpy()
cls = KMeans(n_clusters = K)
kmeans = cls.fit(M)
labels = kmeans.labels_
labels = labels.reshape((H,W))
idx, count = np.unique(labels,return_counts = True)
dtype = [('idx',np.int32),('count',np.int32)]
arr = list(zip(list(idx),list(count)))
arr = np.array(arr,dtype = dtype)
arr = np.sort(arr,order='count')
arr = arr[:-1]
ret = {}
ret["Lanes"] = [[]]*len(arr)
for rowid,row in enumerate(labels):
for idx,count in arr:
x_ = np.mean(np.where(row == idx)[0])
ret["Lanes"][idx].append({"y":float(rowid),"x":x_})
return ret