def compute(self, X, y): [D, self.W, self.mu] = fisherfaces(asRowMatrix(X),y, self.num_components) # store labels self.y = y # store projections for xi in X: self.projections.append(project(self.W, xi.reshape(1,-1), self.mu))
def compute(self, X, y): [D, self.W, self.mu] = pca(asRowMatrix(X), y, self.num_components) # store labels self.y = y # store projections for xi in X: self.projections.append(project(self.W, xi.reshape(1, -1), self.mu))
def fisherfaces(X,y,num_components=0): y = np.asarray(y) [n,d] = X.shape c = len(np.unique(y)) [eigenvalues_pca, eigenvectors_pca, mu_pca] = pca(X, y, (n-c)) [eigenvalues_lda, eigenvectors_lda] = lda(project(eigenvectors_pca, X, mu_pca), y, num_components) eigenvectors = np.dot(eigenvectors_pca,eigenvectors_lda) return [eigenvalues_lda, eigenvectors, mu_pca]
def predict(self, X): minDist = np.finfo('float').max minClass = -1 Q = project(self.W, X.reshape(1, -1), self.mu) for i in range(len(self.projections)): dist = self.dist_metric(self.projections[i], Q) if dist < minDist: minDist = dist minClass = self.y[i] return minClass
def predict(self, X): minDist = np.finfo('float').max minClass = -1 Q = project(self.W, X.reshape(1,-1), self.mu) for i in xrange(len(self.projections)): dist = self.dist_metric(self.projections[i], Q) if dist < minDist: minDist = dist minClass = self.y[i] return minClass
def fisherfaces(X, y, num_components=0): y = np.asarray(y) [n, d] = X.shape c = len(np.unique(y)) [eigenvalues_pca, eigenvectors_pca, mu_pca] = pca(X, y, (n - c)) [eigenvalues_lda, eigenvectors_lda] = lda(project(eigenvectors_pca, X, mu_pca), y, num_components) eigenvectors = np.dot(eigenvectors_pca, eigenvectors_lda) return [eigenvalues_lda, eigenvectors, mu_pca]
# plot them and store the plot to "python_eigenfaces.pdf" subplot(title="Eigenfaces AT&T Facedatabase", images=E, rows=4, cols=4, sptitle="Eigenface", colormap=cm.jet, filename="python_pca_eigenfaces.png") from tinyfacerec.subspace import project, reconstruct # reconstruction steps steps = [i for i in xrange(10, min(len(X), 320), 20)] E = [] for i in xrange(min(len(steps), 16)): numEvs = steps[i] P = project(W[:, 0:numEvs], X[0].reshape(1, -1), mu) R = reconstruct(W[:, 0:numEvs], P, mu) # reshape and append to plots R = R.reshape(X[0].shape) E.append(normalize(R, 0, 255)) # plot them and store the plot to "python_reconstruction.pdf" subplot(title="Reconstruction AT&T Facedatabase", images=E, rows=4, cols=4, sptitle="Eigenvectors", sptitles=steps, colormap=cm.gray, filename="python_pca_reconstruction.png")
# images (note: eigenvectors are stored by column!) E = [] for i in xrange(min(len(X), 16)): e = W[:,i].reshape(X[0].shape) E.append(normalize(e,0,255)) # plot them and store the plot to "python_eigenfaces.pdf" subplot(title="Eigenfaces AT&T Facedatabase", images=E, rows=4, cols=4, sptitle="Eigenface", colormap=cm.jet, filename="python_pca_eigenfaces.png") from tinyfacerec.subspace import project, reconstruct # reconstruction steps steps=[i for i in xrange(10, min(len(X), 320), 20)] E = [] for i in xrange(min(len(steps), 16)): numEvs = steps[i] P = project(W[:,0:numEvs], X[0].reshape(1,-1), mu) R = reconstruct(W[:,0:numEvs], P, mu) # reshape and append to plots R = R.reshape(X[0].shape) E.append(normalize(R,0,255)) # plot them and store the plot to "python_reconstruction.pdf" subplot(title="Reconstruction AT&T Facedatabase", images=E, rows=4, cols=4, sptitle="Eigenvectors", sptitles=steps, colormap=cm.gray, filename="python_pca_reconstruction.png") ########NEW FILE######## __FILENAME__ = example_fisherfaces import sys # append tinyfacerec to module search path sys.path.append("..") # import numpy and matplotlib colormaps import numpy as np # import tinyfacerec modules
print "USAGE: example_fisherfaces.py </path/to/images>" sys.exit() # read images [X,y] = read_images(sys.argv[1]) # perform a full pca [D, W, mu] = fisherfaces(asRowMatrix(X), y) #import colormaps import matplotlib.cm as cm # turn the first (at most) 16 eigenvectors into grayscale # images (note: eigenvectors are stored by column!) E = [] for i in xrange(min(W.shape[1], 16)): e = W[:,i].reshape(X[0].shape) E.append(normalize(e,0,255)) # plot them and store the plot to "python_fisherfaces_fisherfaces.pdf" subplot(title="Fisherfaces AT&T Facedatabase", images=E, rows=4, cols=4, sptitle="Fisherface", colormap=cm.jet, filename="python_fisherfaces_fisherfaces.png") from tinyfacerec.subspace import project, reconstruct E = [] for i in xrange(min(W.shape[1], 16)): e = W[:,i].reshape(-1,1) P = project(e, X[0].reshape(1,-1), mu) R = reconstruct(e, P, mu) # reshape and append to plots R = R.reshape(X[0].shape) E.append(normalize(R,0,255)) # plot them and store the plot to "python_reconstruction.pdf" subplot(title="Fisherfaces Reconstruction Yale FDB", images=E, rows=4, cols=4, sptitle="Fisherface", colormap=cm.gray, filename="python_fisherfaces_reconstruction.png")
for i in xrange(min(W.shape[1], 16)): e = W[:, i].reshape(X[0].shape) E.append(normalize(e, 0, 255)) # plot them and store the plot to "python_fisherfaces_fisherfaces.pdf" subplot(title="Fisherfaces AT&T Facedatabase", images=E, rows=4, cols=4, sptitle="Fisherface", colormap=cm.jet, filename="python_fisherfaces_fisherfaces.png") from tinyfacerec.subspace import project, reconstruct E = [] for i in xrange(min(W.shape[1], 16)): e = W[:, i].reshape(-1, 1) P = project(e, X[0].reshape(1, -1), mu) R = reconstruct(e, P, mu) # reshape and append to plots R = R.reshape(X[0].shape) E.append(normalize(R, 0, 255)) # plot them and store the plot to "python_reconstruction.pdf" subplot(title="Fisherfaces Reconstruction Yale FDB", images=E, rows=4, cols=4, sptitle="Fisherface", colormap=cm.gray, filename="python_fisherfaces_reconstruction.png")