feature.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 import numpy as np
00005 from sklearn.cluster import MiniBatchKMeans
00006 from sklearn.neighbors import NearestNeighbors
00007 
00008 
00009 class BagOfFeatures(object):
00010     def __init__(self, hist_size=500):
00011         self.nn = None
00012         self.hist_size = hist_size
00013 
00014     def fit(self, X):
00015         """Fit features and extract bag of features"""
00016         k = self.hist_size
00017         km = MiniBatchKMeans(n_clusters=k, init_size=3*k, max_iter=300)
00018         km.fit(X)
00019         nn = NearestNeighbors(n_neighbors=1)
00020         nn.fit(km.cluster_centers_)
00021         self.nn = nn
00022 
00023     def transform(self, X):
00024         return np.vstack([self.make_hist(xi.reshape((-1, 128))) for xi in X])
00025 
00026     def make_hist(self, descriptors):
00027         """Make histogram for one image"""
00028         nn = self.nn
00029         if nn is None:
00030             raise ValueError('must fit features before making histogram')
00031         indices = nn.kneighbors(descriptors, return_distance=False)
00032         histogram = np.zeros(self.hist_size)
00033         for idx in np.unique(indices):
00034             mask = indices == idx
00035             histogram[idx] = mask.sum()  # count the idx
00036             indices = indices[mask == False]
00037         return histogram
00038 
00039 
00040 def decompose_descriptors_with_label(descriptors, positions, label_img,
00041                                      skip_zero_label=False):
00042     descriptors = descriptors.reshape((-1, 128))
00043     positions = positions.reshape((-1, 2))
00044     assert descriptors.shape[0] == positions.shape[0]
00045 
00046     decomposed = {}
00047     positions = np.round(positions).astype(int)
00048     labels = label_img[positions[:, 1], positions[:, 0]]
00049     for label in np.unique(labels):
00050         if skip_zero_label and (label == 0):
00051             continue
00052         decomposed[label] = descriptors[labels == label].reshape(-1)
00053 
00054     return decomposed


jsk_recognition_utils
Author(s):
autogenerated on Tue Jul 2 2019 19:40:37