def compute(K, faces):
    [r, c] = faceMatrix.shape
    W, LL, m = FisherFace.myPCA(faceMatrix)
    W1 = W[:, :K]
    facesNew = faces - np.transpose(np.tile(m, (c, 1)))
    X = np.transpose(np.dot(np.transpose(facesNew), W1))
    return X
    def getLDAFeatures(self, faces_train, id_train, r=90, use_pca=True):
        if use_pca == True:
            W_1, b, m = self.getPCAFeatures(faces_train, id_train, r)
            X = np.transpose(b)
            W_f, Centers, classLabels = ff.myLDA(X, id_train)
            # calculate LDA feature for each training image
            q = np.transpose(np.dot(np.transpose(W_f), X))

            return np.dot(np.transpose(W_f), np.transpose(W_1)), q, m
        else:
            m = np.mean(faces_train, 1)
            W_f, Centers, classLabels = ff.myLDA(faces_train, id_train)
            # calculate LDA feature for each training image
            q = np.transpose(np.dot(np.transpose(W_f), faces_train))

            return np.dot(np.transpose(W_f)), q, m
def LDA(faces):
    K1 = 90
    X = compute(K1, faceMatrix)
    Wf, C, classLabels = FisherFace.myLDA(X, label)
    Y = compute(K1, faces)
    Y = np.dot(Wf.transpose(), Y)
    Yf = np.dot(Wf.transpose(), X)
    return C, Yf, Y
    def getPCAFeatures(self, faces_train, id_train, r):
        W, LL, m = ff.myPCA(faces_train)
        W_e = W[:, :r + 1]
        y = np.empty([id_train.shape[0], r])
        print(r, W_e.shape, y.shape, m.shape)
        # calculate PCA feature for each training image
        for i in range(0, id_train.shape[0]):
            f = faces_train[:, i] - m
            y[i] = np.dot(np.transpose(W_e), f)

        return W_e, y, m
Esempio n. 5
0
    def reduceDimension(self, faces_train, id_train, r):
        W, LL, m = ff.myPCA(faces_train)
        W_e = W[:, :r]
        y = np.empty([id_train.shape[0], r])
        print(W_e.shape, y.shape, m.shape)
        #calculate PCA feature for each training image
        for i in range(0, id_train.shape[0]):
            f = faces_train[:, i] - m
            y_e = np.dot(np.transpose(W_e), f)
            y[i] = y_e

        return W_e, y, m
Esempio n. 6
0
def process_lda(faces, W, me, idLabel):

    Xmat = []
    for i in range(len(idLabel)):
        x = faces[:,i:i+1]
        x = np.dot(W.T, (x - me))
        Xmat.extend(x.T)
    Xmat = np.array(Xmat)
    X_Trans = Xmat.transpose()
    
    LDAW,Centers, classLabels = FisherFace.myLDA(X_Trans,idLabel)

    return Centers,LDAW,me
Esempio n. 7
0
def process_pca(faces, num, K, idLabel):
    W, LL, m = FisherFace.myPCA(faces)
    We = W[:, 5:K + 5] # select features
    me = m[:, np.newaxis]
    Mat = np.arange(0).reshape(num, 0)
    Mat = Mat.tolist()

    for i in range(len(idLabel)):
        x = faces[:, i : i + 1]
        y = np.dot(We.T, (x - me))
        y = y.T
        Mat[idLabel[i]].extend(y)

    Z = np.ones((num, y.size))

    for i in range(num):
        mat = np.array(Mat[i])
        Z[i] = np.mean(mat.T, 1)


    Z = Z.T
    return Z, W, me, We
Esempio n. 8
0
import FisherFace
import feature
import test
faces, testLabel = FisherFace.read_faces('test')
z, Ye, Yt = feature.PCA(faces)

print('the accuracy of PCA:')
test.accur(z, Yt)


import FisherFace as FF
import numpy as np
from matplotlib import pyplot as plt

faces_train,idLabel_train = FF.read_faces("./train")
print(">>-1:ReadData: feaces.shape=",faces_train.shape,"\tidLabel.shape=",idLabel_train.shape)
K = 30
K1 = 90
# Task 1,3=========================enroll faces==========
# project all training images into corresponding feature space
W,LL,m = FF.myPCA(faces_train)
We = W[:,:K]
W1 = W[:,:K1]
print(">>0:PCA: We.shape=",We.shape,"\tLL.shape=",LL.shape,"\tm.shape=",m.shape)
print(">>0:LDA: W1.shape=",W1.shape)
proj_pca_train = np.dot(We.T,(faces_train.T-m).T).T
print(">>1:PCA-Projection: train.shape=",proj_pca_train.shape)
reduced_faces_lda =  np.dot(W1.T,(faces_train.T-m).T)
print(">>1:LDA-ReduceDimension: train.shape=",reduced_faces_lda.shape)
W_lda, Centers, classLabels = FF.myLDA(reduced_faces_lda,idLabel_train)
proj_lda_train = np.dot(np.dot(W_lda.T,W1.T),(faces_train.T-m).T)
print(">>1:LDA-Projection: train.shape=",proj_lda_train.shape)
# compute the mean z of all his feature vectors
faces_mean_train = [[] for x in range(10)]
for label,vector in zip(idLabel_train,proj_pca_train):
    faces_mean_train[label].append(vector)
faces_mean_train = [ np.mean(x,0) for x in faces_mean_train]
faces_mean_train = np.asarray(faces_mean_train)
print(">>2:PCA-FacesMean: train.shape=",faces_mean_train.shape)
# store these vectors as columns in numpy-matrix Z for every person
Z = faces_mean_train.T
import FisherFace
import feature
from matplotlib import pyplot
from pylab import *
import test
import numpy as np

test_faces, testLabel = FisherFace.read_faces('test')
train_faces, label = FisherFace.read_faces('train')


def fused(faces, alpha):
    z, Ye, Yt = feature.PCA(faces)
    C, Yf, Y = feature.LDA(faces)

    a = np.dot(alpha, Ye)
    b = np.dot((1 - alpha), Yf)
    y = np.concatenate((a, b))
    return y


def compute(y):
    x = []
    l = y.shape
    z = np.ones((10, 39))
    for i in range(l[1]):
        x.append(y[:, i])
        if (i + 1) % 12 == 0:
            temp = np.array(x).transpose()
            j = i // 12
            z[j] = np.mean(temp, 1)
Esempio n. 11
0
def main():
    K = 30
    K1 = 90
    num = 10
    faces, idLabel = FisherFace.read_faces('./train')

    Z ,W, me, We = process_pca(faces, num, K, idLabel)
    faces_test, idLabel_test = FisherFace.read_faces('./test') 
    N = len(idLabel_test)
    confusion_mat = np.zeros((num, num))
    confusion_mat = np.uint8(confusion_mat)

    print('PCA Confusion Matrix:')
    for i in range( N ) :
        feature = get_PCA_feature(faces_test[:, i : i + 1],We,me)
        label = classifier(Z, feature)
        confusion_mat[idLabel[i]][label] += 1
    print(confusion_mat)
    accuracy = ComputeAccuracy(num, N, confusion_mat)
    print('PCA Accuracy: ',accuracy)

    W1 = W[:, : K1]
    centers, W_lda, me = process_lda(faces, W1, me, idLabel)
    confusion_mat[:][:] = 0 
    print('LDA Confusion Matrix:')
    for i in range( N ) :
        feature = get_LDA_feature(faces_test[:,i:i+1], W_lda, W1, me)
        label = classifier(centers, feature)
        confusion_mat[idLabel[i]][label] += 1
    print(confusion_mat)
    accuracy = ComputeAccuracy(num, N, confusion_mat)
    print('LDA Accuracy: ', accuracy)

    fusion = np.zeros((num, 0))
    fusion = fusion.tolist()
    plt.figure()
    plt.xlabel('alpha')
    plt.ylabel('Accuracy Rate')
    xaxes = []
    yaxes = []
    for alpha in range(1,10):
        alpha = alpha/10
        fusion = process_fusion(Z, centers, num, alpha)
        confusion_mat[:][:] = 0
        print('Fusion Confusion Matrix:')
        for i in range( N ) :
            feature_PCA = get_PCA_feature(faces_test[:, i : i + 1], We, me)
            feature_LDA = get_LDA_feature(faces_test[:, i : i + 1], W_lda, W1, me)
            feature = get_fusion_feature(feature_PCA, feature_LDA, alpha)
            label = classifier(fusion,feature)
            confusion_mat[idLabel[i]][label] += 1
        print(confusion_mat)
    
        accuracy = ComputeAccuracy(num, N, confusion_mat)
        xaxes.append(alpha)
        yaxes.append(accuracy)
        print("Fusion Accuracy: ",accuracy," alpha: ",alpha)
        plt.annotate('(' + str(round(alpha, 2)) + ', ' + str(round(accuracy, 2)) + ')', (alpha, accuracy))
    plt.plot(xaxes, yaxes, 'b-o')
    print(xaxes, yaxes)
        

        
    plt.savefig("figure.png")
    plt.show()
import FisherFace
import numpy as np

faceMatrix, label = FisherFace.read_faces('train')


def compute(K, faces):
    [r, c] = faceMatrix.shape
    W, LL, m = FisherFace.myPCA(faceMatrix)
    W1 = W[:, :K]
    facesNew = faces - np.transpose(np.tile(m, (c, 1)))
    X = np.transpose(np.dot(np.transpose(facesNew), W1))
    return X


#PCA
def PCA(faces):
    K = 30
    Ye = compute(K, faceMatrix)
    [r, c] = Ye.shape
    x = []
    z = np.ones((10, K))
    for i in range(c):
        x.append(Ye[:, i])
        if (i + 1) % 12 == 0:
            temp = np.array(x).transpose()
            j = i // 12
            z[j] = np.mean(temp, 1)
            x = []
    z = z.transpose()
    Yt = compute(K, faces)