5 from sklearn.cluster
import MiniBatchKMeans
6 from sklearn.neighbors
import NearestNeighbors
15 """Fit features and extract bag of features""" 17 km = MiniBatchKMeans(n_clusters=k, init_size=3*k, max_iter=300)
19 nn = NearestNeighbors(n_neighbors=1)
20 nn.fit(km.cluster_centers_)
24 return np.vstack([self.
make_hist(xi.reshape((-1, 128)))
for xi
in X])
27 """Make histogram for one image""" 30 raise ValueError(
'must fit features before making histogram')
31 indices = nn.kneighbors(descriptors, return_distance=
False)
33 for idx
in np.unique(indices):
35 histogram[idx] = mask.sum()
36 indices = indices[mask ==
False]
41 skip_zero_label=
False):
42 descriptors = descriptors.reshape((-1, 128))
43 positions = positions.reshape((-1, 2))
44 assert descriptors.shape[0] == positions.shape[0]
47 positions = np.round(positions).astype(int)
48 labels = label_img[positions[:, 1], positions[:, 0]]
49 for label
in np.unique(labels):
50 if skip_zero_label
and (label == 0):
52 decomposed[label] = descriptors[labels == label].reshape(-1)
def make_hist(self, descriptors)
def decompose_descriptors_with_label(descriptors, positions, label_img, skip_zero_label=False)
def __init__(self, hist_size=500)