Exemple #1
0
def run_pcanet_normal(transformer_params,
                      images_train, images_test, y_train, y_test):
    model = PCANet(**transformer_params)
    model.validate_structure()

    t1 = timeit.default_timer()
    model.fit(images_train)
    t2 = timeit.default_timer()
    train_time = t2 - t1

    t1 = timeit.default_timer()
    X_train = model.transform(images_train)
    t2 = timeit.default_timer()
    transform_time = t2 - t1
    X_test = model.transform(images_test)

    y_test, y_pred = run_classifier(X_train, X_test, y_train, y_test)
    accuracy = accuracy_score(y_test, y_pred)

    return model, accuracy, train_time, transform_time
Exemple #2
0
    def test_validate_structure(self):
        # Check whether filters visit all pixels of input images
        pcanet = PCANet(image_shape=9,
                        filter_shape_l1=3,
                        step_shape_l1=2,
                        n_l1_output=1,
                        filter_shape_l2=3,
                        step_shape_l2=1,
                        n_l2_output=1,
                        filter_shape_pooling=1,
                        step_shape_pooling=1)
        pcanet.validate_structure()

        pcanet = PCANet(image_shape=10,
                        filter_shape_l1=3,
                        step_shape_l1=2,
                        n_l1_output=1,
                        filter_shape_l2=3,
                        step_shape_l2=1,
                        n_l2_output=1,
                        filter_shape_pooling=1,
                        step_shape_pooling=1)
        self.assertRaises(ValueError, pcanet.validate_structure)

        # Check whether filters visit all pixels of L1 output
        # the shape of L1 output is (6, 6)
        pcanet = PCANet(image_shape=13,
                        filter_shape_l1=3,
                        step_shape_l1=2,
                        n_l1_output=1,
                        filter_shape_l2=3,
                        step_shape_l2=1,
                        n_l2_output=1,
                        filter_shape_pooling=1,
                        step_shape_pooling=1)
        pcanet.validate_structure()

        pcanet = PCANet(image_shape=13,
                        filter_shape_l1=3,
                        step_shape_l1=2,
                        n_l1_output=1,
                        filter_shape_l2=3,
                        step_shape_l2=2,
                        n_l2_output=1,
                        filter_shape_pooling=1,
                        step_shape_pooling=1)
        self.assertRaises(ValueError, pcanet.validate_structure)

        # Check whether blocks cover all pixels of L2 output
        # the shape of L1 output is (9, 9)
        # the shape of L2 output is (4, 4)
        pcanet = PCANet(image_shape=19,
                        filter_shape_l1=3,
                        step_shape_l1=2,
                        n_l1_output=1,
                        filter_shape_l2=3,
                        step_shape_l2=2,
                        n_l2_output=1,
                        filter_shape_pooling=2,
                        step_shape_pooling=2)
        pcanet.validate_structure()

        pcanet = PCANet(image_shape=19,
                        filter_shape_l1=3,
                        step_shape_l1=2,
                        n_l1_output=1,
                        filter_shape_l2=3,
                        step_shape_l2=2,
                        n_l2_output=1,
                        filter_shape_pooling=3,
                        step_shape_pooling=1)
        self.assertRaises(ValueError, pcanet.validate_structure)
Exemple #3
0
train_set, valid_set, test_set = load_mnist()

images_train, y_train = train_set
images_test, y_test = test_set

images_train, y_train = shuffle(images_train, y_train, random_state=0)
images_train, y_train = images_train[:n_train], y_train[:n_train]

images_test, y_test = shuffle(images_test, y_test, random_state=0)
images_test, y_test = images_test[:n_test], y_test[:n_test]

pcanet = PCANet(image_shape=28,
                filter_shape_l1=2,
                step_shape_l1=1,
                n_l1_output=4,
                filter_shape_l2=2,
                step_shape_l2=1,
                n_l2_output=4,
                block_shape=2)
pcanet.validate_structure()

pcanet.fit(images_train)
X_train = pcanet.transform(images_train)
X_test = pcanet.transform(images_test)

model = RandomForestClassifier(n_estimators=100, random_state=1234, n_jobs=-1)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("accuracy: " + str(accuracy))