Ejemplo n.º 1
0
def run_test(**kwargs):
    b = fetch_sw_orl()
    tic = time.time()

    # split the data in
    X_train, X_test, y_train, y_true = train_test_split(b.data,
                                                        b.target,
                                                        test_size=0.2,
                                                        stratify=b.target)

    hog_train = []
    for img_array in X_train:
        fd, _ = hog(img_array.reshape(b.shape),
                    orientations=8,
                    pixels_per_cell=(PPC, PPC),
                    cells_per_block=(1, 1),
                    visualize=True,
                    multichannel=False)
        hog_train.append(fd)

    clf = OutputCodeClassifier(LinearSVC(random_state=0), code_size=2)
    clf.fit(hog_train, y_train)
    tok = time.time()

    hog_test = []
    for img_arry in X_test:
        fd, _ = hog(img_arry.reshape(b.shape),
                    orientations=8,
                    pixels_per_cell=(PPC, PPC),
                    cells_per_block=(1, 1),
                    visualize=True,
                    multichannel=False)
        hog_test.append(fd)
    y_pred = clf.predict(hog_test)
    return tok - tic, accuracy_score(y_true, y_pred)
Ejemplo n.º 2
0
def run_test(**kwargs):
    """
    runs a single test
    :param kwargs:
    :return: time, accuracy result
    """
    ppc = kwargs['hog']
    b = fetch_sw_orl()
    tic = time.time()

    # split the data in
    X_train, X_test, y_train, y_true = train_test_split(b.data,
                                                        b.target,
                                                        test_size=0.2,
                                                        stratify=b.target)

    hog_train = []
    for img_array in X_train:
        fd, _ = hog(img_array.reshape(b.shape),
                    orientations=8,
                    pixels_per_cell=(ppc, ppc),
                    cells_per_block=(1, 1),
                    visualize=True,
                    multichannel=False)
        hog_train.append(fd)

    if 'clf' not in kwargs:
        clf = MLPClassifier(hidden_layer_sizes=(100, ),
                            max_iter=30,
                            alpha=1e-4,
                            solver='sgd',
                            learning_rate_init=.1)
    else:
        clf = kwargs['clf']

    # this example won't converge because of CI's time constraints, so we catch the
    # warning and are ignore it here
    with warnings.catch_warnings():
        warnings.filterwarnings("ignore",
                                category=ConvergenceWarning,
                                module="sklearn")
        clf.fit(hog_train, y_train)

    tok = time.time()

    hog_test = []
    for img_arry in X_test:
        fd, _ = hog(img_arry.reshape(b.shape),
                    orientations=8,
                    pixels_per_cell=(ppc, ppc),
                    cells_per_block=(1, 1),
                    visualize=True,
                    multichannel=False)
        hog_test.append(fd)
    y_pred = clf.predict(hog_test)
    return tok - tic, accuracy_score(y_true, y_pred)
import random
import numpy as np
import os
import matplotlib.pyplot as plt
from skimage.feature import hog

from orl_face_dataset_examples.read_pgm_file import fetch_sw_orl
from sw_path import WORK_ROOT
from sw_utils.functions import show_class_images

control = [False, False, True]

ORL_PATH = os.path.join(WORK_ROOT, 'RES', 'ORL')

b = fetch_sw_orl()

if control[0]:
    print(f'the data set is {b.DESCR}')
    print(f'the data is located at {b.path}')

    data_shape = b.shape
    data_size = b.data.size
    print(
        f'The shape of the data is {data_shape}, that is there are {data_size} number of images'
    )

if control[1]:
    img = np.array(b.data[random.randint(0, 400 - 1)].reshape(data_shape))
    plt.imshow(img, cmap='gray')
    plt.show()
    fd, hog_img = hog(img,
Ejemplo n.º 4
0
    tic = time.time()
    clf = c.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    toc = time.time()

    result = accuracy_score(y_true, y_pred, normalize=True)
    report = classification_report(y_true, y_pred, zero_division=0.0)

    return result, report, toc - tic


def print_info(args):
    for r in args:
        print(r)


def print_acur(args):
    print(f'{args[0]}%, in {args[2]}')


# load data
b_orl = fetch_sw_orl()

# silly
silly_classifier = SillyClassifier()
print_acur(run_test(silly_classifier, b_orl))

# mean
mean_classifier = MeanClassifier()
print_acur(run_test(mean_classifier, b_orl))
Ejemplo n.º 5
0
def main():
    b_orl = fetch_sw_orl()

    #run_test_on_means(b_orl)
    run_silly(b_orl)