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 checkKnownPeople(config, color=blue_color):
    from sklearn.model_selection import train_test_split
    import numpy as np
    #model = DatasetModel([output100lbp, output100lbp_test_strict])
    model = DatasetModel(config[0])
    model = model.labeledByFolderOfFiles(config[2])
    m, n = model.getDim()
    X, y = model.exportedAsClassicDataset()

    X_train, X_test, y_train, y_test = train_test_split(
        X,
        y,
        test_size=0.2,  # 0.2
        random_state=0,
        stratify=y)

    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))

    # 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")
    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 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
Пример #4
0
    print(
        classification_report(y_test,
                              y_predict,
                              target_names=faces95_own_classes))
    if doPlot:
        plotConfusionMatrix(y_test, y_predict, labels=faces95_own_classes)

    # ---------------------- OTHER ALGORITHMS -----------------------------
    from sklearn.neural_network import MLPClassifier
    X_train_pca = eigenfaceModel.exportTrainPca()

    X_test_pca = []

    for X_i in X_test:
        X_test_pca.append(eigenfaceModel.projectOntoFacespace(X_i))

    X_test_pca = array(X_test_pca)
    # train a neural network
    print("Fitting the classifier to the training set")
    clf = MLPClassifier(hidden_layer_sizes=(
        1024,
        1024,
    ),
                        batch_size='auto',
                        verbose=True,
                        early_stopping=True).fit(X_train_pca, y_train)
    print(clf)
    y_pred = clf.predict(X_test_pca)
    print("#--------------------------------------------------------------")
    print("#-                      NEURAL NETWORKS                       -")