Skip to content

Commit

Permalink
Update eigenfaces.py
Browse files Browse the repository at this point in the history
made changes in this file shogun-toolbox#4990
  • Loading branch information
mohitmajithia committed May 4, 2020
1 parent 9b5793b commit d9dfdca
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions examples/undocumented/python/graphical/eigenfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@

import numpy as np
from numpy import random

from shogun import RealFeatures
from shogun import PCA
from shogun import EuclideanDistance
from shogun import sg
import math
import os
import pylab as pl
Expand All @@ -69,9 +66,9 @@ def train(self, images, labels):
self._labels = labels;

#transform the numpe vector to shogun structure
features = RealFeatures(images)
features = sg.RealFeatures(images)
#PCA
self.pca = PCA()
self.pca = sg.PCA()
#set dimension
self.pca.set_target_dim(self._num_components);
#compute PCA
Expand All @@ -92,18 +89,18 @@ def predict(self, image):
imageAsRow = np.asarray(image.reshape(image.shape[0]*image.shape[1],1),
np.float64);
#project inthe subspace
p = self.pca.apply_to_feature_vector(RealFeatures(imageAsRow).get_feature_vector(0));
p = self.pca.apply_to_feature_vector(sg.RealFeatures(imageAsRow).get_feature_vector(0));

#min value to find the face
minDist =1e100;
#class
minClass = -1;
#search which face is the best match
for sampleIdx in range(len(self._projections)):
test = RealFeatures(np.asmatrix(p,np.float64).T)
projection = RealFeatures(np.asmatrix(self._projections[sampleIdx],
test = sg.RealFeatures(np.asmatrix(p,np.float64).T)
projection = sg.RealFeatures(np.asmatrix(self._projections[sampleIdx],
np.float64).T)
dist = EuclideanDistance( test, projection).distance(0,0)
dist = sg.EuclideanDistance( test, projection).distance(0,0)

if(dist < minDist ):
minDist = dist;
Expand Down Expand Up @@ -229,17 +226,17 @@ def plot_gallery(images, titles, h, w, n_row=3, n_col=4):

print "Reconstruct with " + str(i) + " eigenvectors"

pca = PCA()
pca = sg.PCA()
#set dimension
pca.set_target_dim(i);
#compute PCA
pca.init(RealFeatures(images))
pca.init(sg.RealFeatures(images))

pca.apply_to_feature_vector(RealFeatures(imageAsRow)
pca.apply_to_feature_vector(sg.RealFeatures(imageAsRow)
.get_feature_vector(0));

#reconstruct
projection = pca.apply_to_feature_vector(RealFeatures(imageAsRow)
projection = pca.apply_to_feature_vector(sg.RealFeatures(imageAsRow)
.get_feature_vector(0));

reconstruction = np.asmatrix( np.asarray(projection, np.float64))* \
Expand Down

0 comments on commit d9dfdca

Please sign in to comment.