def preprocessor_pca_modular (data): from modshogun import RealFeatures from modshogun import PCA features = RealFeatures(data) preprocessor = PCA() preprocessor.init(features) preprocessor.apply_to_feature_matrix(features) return features
def train(self, images, labels): """ Train eigenfaces """ print "Train..." #copy labels self._labels = labels #transform the numpe vector to shogun structure features = RealFeatures(images) #PCA self.pca = PCA() #set dimension self.pca.set_target_dim(self._num_components) #compute PCA self.pca.init(features) for sampleIdx in range(features.get_num_vectors()): v = features.get_feature_vector(sampleIdx) p = self.pca.apply_to_feature_vector(v) self._projections.insert(sampleIdx, p) print "Train ok!"
#Reconstruction with diferents values of eigenvectos #Read the last image of the file to test Eigenfaces image = cv2.resize(cv2.imread(list_filenames[0], cv2.IMREAD_GRAYSCALE), (IMAGE_HEIGHT, IMAGE_WIDHT)) #image as row imageAsRow = np.asarray(image.reshape(image.shape[0] * image.shape[1], 1), np.float64) #Reconstruct 10 eigen vectors to 300, step 15 for i in range(10, 300, 50): print "Reconstruct with " + str(i) + " eigenvectors" pca = PCA() #set dimension pca.set_target_dim(i) #compute PCA pca.init(RealFeatures(images)) pca.apply_to_feature_vector( RealFeatures(imageAsRow).get_feature_vector(0)) #reconstruct projection = pca.apply_to_feature_vector( RealFeatures(imageAsRow).get_feature_vector(0)) reconstruction = np.asmatrix( np.asarray(projection, np.float64))* \ np.asmatrix( pca.get_transformation_matrix()).T reconstruction = reconstruction + pca.get_mean()