def checkAllPeople(config, color="#ab1749"): from sklearn.model_selection import train_test_split import numpy as np model = DatasetModel([config[0], config[1]]) model = model.labeledByFolderOfFiles(config[2]) m, n = model.getDim() X, y = model.exportedAsClassicDataset() pI = model.partitionIndexes X0 = X[:pI[1]] y0 = y[:pI[1]] X_train, X_test1, y_train, y_test1 = train_test_split( X0, y0, test_size=0.2, # 0.2 random_state=0, stratify=y0) X_test2 = X[pI[1]:] y_test2 = y[pI[1]:] print(X_test1.shape, X_test2.shape) X_test = np.concatenate((X_test1, X_test2)) y_test = np.concatenate((y_test1, y_test2)) eigenfaceModel = EigenfaceModel(X_train, y_train, nmin=4, nmax=150, m=m, n=n) epsilons = zeros(len(y_test)) for i, X_i in enumerate(X_test): Phi_i = X_i - eigenfaceModel.mean_face Ω_i = eigenfaceModel.projectOntoFacespace(X_i) epsilons[i] = sqrt(abs(Phi_i.T.dot(Phi_i) - Ω_i.T.dot(Ω_i))) #sqrt(norm(X_i - Ω_i)) # 4 possibilities (from paper) # (1) near face space and near a face class # (2) near face space but not near to a known face class # (3) distant from face space and near a face class # (4) distant from face space and not near a known face class plotHist(epsilons, "Distance between image and face-space", "Distance Hist for faces that should be known", color) print(max(epsilons)) sd = np.std(epsilons, ddof=1, dtype=np.float64) mean = np.mean(epsilons) print("mean:", mean) print("sd:", sd) print("suggested cut threshold (mean+2sd):", mean + 2 * sd) return X_train, y_train, X_test, y_test, eigenfaceModel, model
def showMinDistancesBetweenInVsOutTrainingDataset(config): from sklearn.model_selection import train_test_split import numpy as np model = DatasetModel([config[0], config[1]]) model = model.labeledByFolderOfFiles(config[2]) m, n = model.getDim() X, y = model.exportedAsClassicDataset() pI = model.partitionIndexes X0 = X[:pI[1]] y0 = y[:pI[1]] X_train, X_test1, y_train, y_test1 = train_test_split( X0, y0, test_size=0.2, # 0.2 random_state=0, stratify=y0) X_test2 = X[pI[1]:] y_test2 = y[pI[1]:] eigenfaceModel = EigenfaceModel(X_train, y_train, nmin=4, nmax=150, m=m, n=n) # 4 | 150 # People that should be KNOWN epsilons1 = zeros(len(y_test1)) for i, X_i in enumerate(X_test1): y_pred_i, min_err_i = eigenfaceModel._noFitTest(X_i) epsilons1[i] = min_err_i # People that should NOT be KNOWN epsilons2 = zeros(len(y_test2)) for i, X_i in enumerate(X_test2): y_pred_i, min_err_i = eigenfaceModel._noFitTest(X_i) epsilons2[i] = min_err_i plotHist(epsilons1, "Minimum error vs other samples", "(min) Error Hist for faces that should be known", blue_color) sd, mean = np.std(epsilons1, ddof=1, dtype=np.float64), np.mean(epsilons1) print("mean:", mean, " | sd:", sd, " |max:", max(epsilons1)) print("suggested cut threshold (mean+2sd):", mean + 2 * sd) plotHist(epsilons2, "Minimum error vs other samples", "(min) Error Hist for faces that should NOT be known", pink_color) sd, mean = np.std(epsilons2, ddof=1, dtype=np.float64), np.mean(epsilons2) print("mean:", mean, " | sd:", sd, " |max:", max(epsilons2)) print("suggested cut threshold (mean+2sd):", mean + 2 * sd)
def checkUnknownPeople(config, color=pink_color): model = DatasetModel([config[0], config[1]]) model = model.labeledByFolderOfFiles(config[2]) m, n = model.getDim() X, y = model.exportedAsClassicDataset() pI = model.partitionIndexes X_train = X[:pI[1]] y_train = y[:pI[1]] X_test = X[pI[1]:] y_test = y[pI[1]:] eigenfaceModel = EigenfaceModel(X_train, y_train, nmin=4, nmax=150, m=m, n=n) epsilons = zeros(len(y_test)) for i, X_i in enumerate(X_test): Phi_i = X_i - eigenfaceModel.mean_face Ω_i = eigenfaceModel.projectOntoFacespace(X_i) #Phi_f = zeros(len(Phi_i)) #Phi_f[:len(Ω_i)] = Ω_i #epsilons[i] = norm(Phi_i - Phi_f) epsilons[i] = sqrt(abs(Phi_i.T.dot(Phi_i) - Ω_i.T.dot(Ω_i))) #sqrt(norm(X_i - Ω_i)) #print(argmax(epsilons)) #print(model.files[argmax(epsilons)]) # 4 possibilities (from paper) # (1) near face space and near a face class # (2) near face space but not near to a known face class # (3) distant from face space and near a face class # (4) distant from face space and not near a known face class plotHist(epsilons, "Distance between image and face-space", "Distance Hist for faces that should NOT be known", color) print(max(epsilons)) return X_train, y_train, X_test, y_test, eigenfaceModel, model
# Let's do a second split between just known people (to mix later) X_train, X_test1, y_train, y_test1 = train_test_split( X0, y0, test_size=0.2, # 0.2 random_state=0, stratify=y0) # Unknown people X_test2 = X[pI[1]:] y_test2 = y[pI[1]:] eigenfaceModel = EigenfaceModel(X_train, y_train, nmin=4, nmax=150, m=m, n=n) print(eigenfaceModel) X_test = np.concatenate((X_test1, X_test2)) y_test = np.concatenate((y_test1, y_test2)) # People that should be KNOWN #epsilons1 = zeros(len(y_test1)) #for i, X_i in enumerate(X_test1): # y_pred_i, min_err_i = eigenfaceModel._noFitTest(X_i) # epsilons1[i] = min_err_i # People that should NOT be KNOWN #epsilons2 = zeros(len(y_test2))
model = model.labeledByFolderOfFiles(output100strict_classes) X, y = model.exportedAsClassicDataset() #X_train, X_test, y_train, y_test = train_test_split(X, # y, # test_size=0.1, # 0.2 # random_state=0, # stratify=y) pI = model.partitionIndexes X_train = X[:pI[1]] y_train = y[:pI[1]] X_test = X[pI[1]:] y_test = y[pI[1]:] eigenfaceModel = EigenfaceModel(X_train, y_train, nmin=2, nmax=50) err = zeros(len(y_test)) for i, X_i in enumerate(X_test): y_i, err[i] = eigenfaceModel._noFitTest(X_i) plotHist( err, 'Error', 'Errors Histogram for faces that should be KNOWN from the model') print(err) print(max(err)) if 0: ########################################################################### #2. model = DatasetModel([output100lbp, output100lbp_test_complement])
X, y = model.exportedAsClassicDataset() indices = range(len(y)) X_train, X_test, y_train, y_test, i_train, i_test = train_test_split( X, y, indices, test_size=0.1, # 0.2 random_state=0, stratify=y) #X_test = X_test[y_test < output100_all_classes.index('io')] #y_test = y_test[y_test < output100_all_classes.index('io')] eigenfaceModel = EigenfaceModel( X_train, y_train, nmin=2, nmax=150) # old config 2 | 150 (documentation) print(eigenfaceModel) y_predict2 = zeros(len(y_test)) for i, X_i in enumerate(X_test): y_predict2[i] = eigenfaceModel.noFitTest(X_i, 1) from sklearn.metrics import classification_report import matplotlib.pyplot as plt print(classification_report(y_test, y_predict2, target_names=labels)) plotConfusionMatrix(y_test, y_predict2, labels=labels) # ---------------------- ANGLE ----------------------------------------