Exemple #1
0
def reconstruction_error_classifier(training_image, train_label, face_image):
    df = pd.DataFrame(training_image.T, index=train_label)
    grouped = df.groupby(df.index)

    min_e = float('inf')
    min_label = df.index[0]
    for key, tab in grouped:
        # Compute Principle (eigen) subspace per class
        train_image = tab.values.T
        m, N = train_image.shape
        eigvals, u_i = calc_eig_pca_small(train_image, m, N)
        # Classification
        face_mean = compute_avg_face(train_image)

        phi = face_image - face_mean
        w = np.dot(phi.T, u_i)
        x_n = face_mean + np.dot(u_i, w)

        e = np.linalg.norm(face_image - x_n)
        if e < min_e:
            min_label = key
            min_e = e
    return min_label
def commMachineRandDataXFeat(data, test_img, feature_list, N, m_lda):

    pred = []
    for k in data:
        train_image = k[:, :-1].T
        train_label = k[:, -1].T
        m, N = train_image.shape
        eigvals_pca, eigvecs_pca = calc_eig_pca_small(train_image, m, N)
        face_avg = compute_avg_face(train_image)
        phi_face = train_image - face_avg.reshape(face_avg.shape[0], 1)

        # FLD
        sw = compute_s_w(train_image, train_label, face_avg)
        sb = compute_s_b(train_image, train_label, face_avg)

        for l in feature_list:

            m_eigvecs = eigvecs_pca[:, l]

            eigvals_fld, eigvecs_fld, m_eigvecs = calc_eig_fld(
                k_eigvecs=m_eigvecs, sw=sw, sb=sb)

            ## Variables
            m_eigvecs_fld = eigvecs_fld[:, :m_lda]
            w_opt = np.dot(m_eigvecs_fld.T, m_eigvecs.T).T.real
            a_fld = np.dot(phi_face.T, w_opt)

            out = list(
                map(
                    lambda k: nn_classifier(a_fld, train_label, w_opt,
                                            face_avg, k), test_img.T))
            pred.append(out)

    result_mat = np.array(pred)
    result = stats.mode(result_mat, axis=0)
    return result, result_mat
Exemple #3
0
def calc_eig_fld(train_image=None,
                 train_label=None,
                 k=None,
                 sw=[],
                 sb=[],
                 k_eigvecs=[]):
    if sw == [] or sb == [] or k_eigvecs == []:
        m, N = train_image.shape
        eigvals, eigvecs_pca = calc_eig_pca_small(train_image, m, N)
        k_eigvecs = eigvecs_pca[:, :k]
        face_avg = compute_avg_face(train_image)

        sw = compute_s_w(train_image, train_label, face_avg)
        sb = compute_s_b(train_image, train_label, face_avg)

    rank_sw = np.linalg.matrix_rank(sw)
    rank_sb = np.linalg.matrix_rank(sb)
    #    print("rank of sw: %a" %rank_sw)
    #    print("rank of sb: %a" %rank_sb)
    wsww = np.dot(k_eigvecs.T, np.dot(sw, k_eigvecs))
    wsbw = np.dot(k_eigvecs.T, np.dot(sb, k_eigvecs))
    pca_mat = np.dot(np.linalg.pinv(wsww), wsbw)
    eigvals_fld, eigvecs_fld = np.linalg.eig(pca_mat)
    return eigvals_fld, eigvecs_fld, k_eigvecs
from data import train_image, train_label, test_image, test_label
from pca import calc_eig_pca_small
from fld import calc_eig_fld
from functions import compute_avg_face
from prediction import calc_accuracy, produce_pred_label, produce_reconstruction_pred_label
from ensemble import randFeature, bagging, commMachineRandDataAndFeat, commMachineRandDataXFeat
from sklearn.metrics import accuracy_score
import numpy as np

################################## Calculate accuracy #############################################
m, N = train_image.shape
k = 200

print(set(train_label))
# PCA
eigvals_pca, eigvecs_pca = calc_eig_pca_small(train_image, m, N)

## Variables
k_eigvecs = eigvecs_pca[:, :k]
face_avg = compute_avg_face(train_image)
phi_face = train_image - face_avg.reshape(m, 1)
a = np.dot(phi_face.T, k_eigvecs)

## Calcualte accuracy of pca
pred_label = produce_pred_label(a, train_label, k_eigvecs, face_avg,
                                test_image)
acc_pca = calc_accuracy(pred_label, test_label)
print(acc_pca)

## Calcualte accuracy of pca using reconstruction method
pred_label = produce_reconstruction_pred_label(train_image, train_label,