示例#1
0
    def test_histogram(self):
        image = np.array([[0, 1, 1, 3], [3, 1, 2, 2], [2, 0, 1, 2],
                          [2, 0, 1, 2], [1, 3, 0, 1], [2, 2, 2, 3]])
        expected = np.array([2, 2, 1, 1, 0, 2, 3, 1, 1, 1, 3, 1, 1, 2, 2, 1])

        pcanet = PCANet(None,
                        None,
                        None,
                        None,
                        None,
                        None,
                        n_l2_output=2,
                        block_shape=(3, 2))
        # assume that n_l1_output = 2
        assert_array_equal(pcanet.histogram(image), expected)
示例#2
0
    def __init__(self, n_estimators, sampling_ratio, n_jobs=-1,
                 **transformer_params):
        """
        n_estimators: int
            The number of estimators
        sampling_ratio: int
            The number of samples to draw from X to train each base transformer.
        n_jobs: int
            The number of jobs to run in parallel.
            The number of cores is set if -1.
        transformer_params: dict
            Parameters for PCANet.__init__
        """

        self.transformers = \
            [PCANet(**transformer_params) for i in range(n_estimators)]
        # Validate only the first transformer
        # since all of transformers share the same hyperparameters
        self.transformers[0].validate_structure()
        self.estimators = [SVC(C=1e8) for i in range(n_estimators)]

        self.sampling_ratio = sampling_ratio

        self.n_jobs = n_jobs
        if n_jobs == -1:
            self.n_jobs = cpu_count()
示例#3
0
    def test_histogram(self):
        images = xp.array([[[0, 1, 1, 3], [3, 1, 2, 2], [2, 0, 1, 2],
                            [0, 1, 1, 1]],
                           [[2, 0, 1, 2], [1, 3, 0, 1], [2, 2, 2, 3],
                            [1, 3, 3, 1]]])
        expected = xp.array([[1, 2, 0, 1, 0, 1, 2, 1, 2, 1, 1, 0, 0, 3, 1, 0],
                             [1, 1, 1, 1, 1, 2, 1, 0, 0, 1, 2, 1, 0, 1, 1, 2]])
        pcanet = PCANet(None,
                        None,
                        None,
                        None,
                        None,
                        None,
                        n_l2_output=2,
                        filter_shape_pooling=2,
                        step_shape_pooling=2)
        assert_array_equal(pcanet.histogram(images), expected)

        images = xp.array([[[1, 0, 1], [2, 0, 0], [1, 3, 3]],
                           [[2, 0, 0], [1, 1, 1], [3, 0, 1]]])
        expected = xp.array([[2, 1, 1, 0, 3, 1, 0, 0, 1, 1, 1, 1, 2, 0, 0, 2],
                             [1, 2, 1, 0, 2, 2, 0, 0, 1, 2, 0, 1, 1, 3, 0, 0]])
        pcanet = PCANet(None,
                        None,
                        None,
                        None,
                        None,
                        None,
                        n_l2_output=2,
                        filter_shape_pooling=2,
                        step_shape_pooling=1)
        assert_array_equal(pcanet.histogram(images), expected)
示例#4
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
示例#5
0
文件: mnist.py 项目: delldu/PCANet
for i, class_id in itertools.product(range(nexamples), range(nclasses)):
    ax[i, class_id].imshow(train_set_X[img_idx[class_id][i]], cmap='gray')
    ax[i, class_id].get_xaxis().set_visible(False)
    ax[i, class_id].get_yaxis().set_visible(False)

plt.savefig('mnist.png', bbox_inches='tight', facecolor='black', pad_inches=0)
plt.show()

# %%
# Convert to NCHW
train_set_X = train_set_X[:, None, ...]
test_set_X = test_set_X[:, None, ...]

# %%
print(' ====== PCANet Training ======= ')
net = PCANet([8, 8], 7, 7, 0.5)
time_start = time.time()
train_features = net.extract_features(train_set_X)
train_features = scipy.sparse.csr_matrix(train_features)
time_end = time.time()
print('Time cost %.2f s' % (time_end - time_start))
del train_set_X

#%%
#visualize PCA filter banks
fig, ax = plt.subplots(2, 8, figsize=(16, 4), facecolor='black')
for stage, i in itertools.product(range(2), range(8)):
    filter_bank = eval('net.W_' + str(stage + 1))[:, i].reshape(7, 7)
    ax[stage, i].imshow(filter_bank, cmap='gray')
    ax[stage, i].get_xaxis().set_visible(False)
    ax[stage, i].get_yaxis().set_visible(False)
示例#6
0
def main():
    day_str = "{:%B_%d}".format(datetime.now())
    time_str = "{:%H_%M_%S}".format(datetime.now())
    day_dir = "log_data/" + day_str + "/"
    log_path = day_dir + day_str + "_" + time_str + "/"
    writer = tf.summary.FileWriter(log_path)

    # Open text editor to write description of the run and commit it
    if '--temp' not in sys.argv:
        if '-m' in sys.argv:
            m_i = sys.argv.index('-m')
            msg = sys.argv[m_i + 1]
            cmd = ['git', 'commit', '*.py', '-m', msg]
        else:
            cmd = ['git', 'commit', '*.py']

        os.environ['TF_LOG_DIR'] = log_path
        call(cmd)

    # setup the input data pipelines
    train_image_batch, train_label_batch, test_image_batch, test_label_batch, info = load(
        'mnist')
    # train_image_batch, train_label_batch, test_image_batch, test_label_batch, info = load('cifar10')

    sess = tf.Session()
    tf.train.start_queue_runners(sess=sess)

    if '--debug' in sys.argv:
        sess = tf_debug.LocalCLIDebugWrapperSession(sess)
        sess.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan)
        sess.add_tensor_filter("greater_than_or_equal_to", gre_filter)

    init = tf.global_variables_initializer()

    # Hyper-params
    k1 = 7
    k2 = 7
    l1 = 8
    l2 = 8
    block_w = 4
    block_h = 4
    block_overlap = 0
    num_hist_bins = 2**l2
    stride_w = max(int((1 - block_overlap) * block_w), 1)
    stride_h = max(int((1 - block_overlap) * block_h), 1)
    w_steps = range(block_w, info.IMAGE_W + 1, stride_w)
    h_steps = range(block_h, info.IMAGE_H + 1, stride_h)
    print(list(w_steps))
    num_blocks = len(h_steps) * len(w_steps)

    hyperparams = {
        'l1': l1,
        'l2': l2,
        'k1': k1,
        'k2': k2,
        'num_hist_bins': num_hist_bins,
        'block_w': block_w,
        'block_h': block_h,
        'stride_w': stride_w,
        'stride_h': stride_h,
        'num_blocks': num_blocks,
    }

    # check that the blocks in the final step to be even & cover all pixels
    if w_steps[-1] != info.IMAGE_W:
        print("invalid block_overlap or block width for given image width:")
        print("W: %i, Block W: %i, Overlap: %0.2f" %
              (info.IMAGE_W, block_w, block_overlap))
        exit(0)

    if h_steps[-1] != info.IMAGE_H:
        print("invalid block_overlap or block height for given image height")
        print("H: %i, Block H: %i, Overlap: %0.2f" %
              (info.IMAGE_H, block_h, block_overlap))
        exit(0)

    # define the model
    m = PCANet(train_image_batch, hyperparams, info)

    # define placeholders for putting scores on Tensorboard
    train_score_tensor = tf.placeholder(tf.float32,
                                        shape=[],
                                        name='train_score')
    test_score_tensor = tf.placeholder(tf.float32, shape=[], name='test_score')
    tf.summary.scalar("train_score", train_score_tensor, collections=['train'])
    tf.summary.scalar("test_score", test_score_tensor, collections=['test'])

    # run it
    sess.run(init)
    writer.add_graph(sess.graph)
    merged_summary_op = tf.summary.merge_all('summaries')
    train_summary_op = tf.summary.merge_all('train')
    test_summary_op = tf.summary.merge_all('test')

    # extract PCA features from training set
    train_pcanet_features, train_labels, summary = sess.run(
        [m.output_features, train_label_batch, merged_summary_op])
    writer.add_summary(summary, 0)

    # train linear SVM
    svm = LinearSVC(C=1.0, fit_intercept=False)
    svm.fit(train_pcanet_features, train_labels)
    train_score = svm.score(train_pcanet_features, train_labels)

    print("training score:", train_score)
    train_summary = sess.run(train_summary_op,
                             feed_dict={train_score_tensor: train_score})
    writer.add_summary(train_summary, 0)

    # switch to test set, compute PCA filters, and score with learned SVM parameters
    scores = []
    m = PCANet(test_image_batch, hyperparams, info)
    for i in range(4):
        test_pcanet_features, test_labels, merged_summary = sess.run(
            [m.output_features, test_label_batch, merged_summary_op])
        writer.add_summary(merged_summary, i + 1)

        score = svm.score(test_pcanet_features, test_labels)
        scores.append(score)

        print("batch test score:", score)
        test_summary = sess.run(test_summary_op,
                                feed_dict={test_score_tensor: score})
        writer.add_summary(test_summary, i + 1)

    print("Final score on test set: ", sum(scores) / len(scores))
    writer.close()
示例#7
0
def main():
    if not torch.cuda.is_available():
        logging.info('no gpu device available')
        sys.exit(1)

    logging.info('gpu device = %d' % args.gpu)
    logging.info("args = %s", args)

    pcanet = PCANet(args.stages, args.filter_shape, args.stages_channels,
                    args.block_size, args.block_overlap)
    train_queue, valid_queue = load_train_mnist(args)  # load dataset
    logging.info("load training dataset completely")
    total_train_labels = torch.tensor([]).long()

    writer = SummaryWriter(args.save)  # tensorboardX

    # extract feature from images
    with torch.no_grad():
        # first of all, generate eigenvector, and then execute convolution
        stage_save_path = args.save
        save_filename = utils.create_pickle_file_name(stage_save_path, 0)
        for global_step, (train_images,
                          train_labels) in enumerate(train_queue):
            train_images = train_images.cuda()
            total_train_labels = torch.cat((total_train_labels, train_labels))
            utils.save_feature([train_images, train_labels], save_filename)
            pcanet.unrolled_stage(train_images, 0)

            if global_step % args.log_freq == 0:
                logging.info("init training global_step: %d" % global_step)
                # convert a batch of tensor into CHW format
                grid_images = make_grid(train_images,
                                        nrow=16,
                                        padding=5,
                                        pad_value=125)
                writer.add_image("raw_images_in_step_%d" % global_step,
                                 grid_images)

        total_features = torch.tensor([])  # empty tensor
        for stage in range(args.stages):
            logging.info('PCANet stage: %d' % stage)

            # transform eigenvector to convolution kernel
            kernel = pcanet.eigenvector_to_kernel(stage)

            load_filename = utils.create_pickle_file_name(
                stage_save_path, stage)
            if stage + 1 < args.stages:
                save_filename = utils.create_pickle_file_name(
                    stage_save_path, stage + 1)

            load_filename_pointer = 0  # clear file object pointer
            for step in range(global_step + 1):
                train_images, train_labels, load_filename_pointer = \
                    utils.load_feature(load_filename, load_filename_pointer)
                batch_features = pcanet.pca_conv(train_images, kernel)
                if step % args.log_freq == 0:
                    # view the i-th image's feature map in a single batch
                    single_image_feature = utils.exchange_channel(
                        batch_features[5])
                    grid_images = make_grid(single_image_feature,
                                            nrow=8,
                                            padding=5,
                                            pad_value=125)
                    writer.add_image(
                        "feature_image_in_step_%d_in_stage_%d" % (step, stage),
                        grid_images)

                if stage + 1 < args.stages:
                    utils.save_feature([batch_features, train_labels],
                                       save_filename)
                    pcanet.unrolled_stage(batch_features, stage + 1)
                else:
                    decimal_features = pcanet.binary_mapping(
                        batch_features, stage)
                    final_features = pcanet.generate_histogram(
                        decimal_features)
                    final_features = final_features.cpu()
                    total_features = torch.cat(
                        (total_features, final_features), dim=0)

                if step % args.log_freq == 0:
                    logging.info("circulate training step: %d" % step)

            grid_kernels = make_grid(pcanet.kernel[stage],
                                     nrow=args.stages_channels[stage],
                                     padding=5,
                                     pad_value=125)
            writer.add_image("kernel_in_stage_%d" % stage, grid_kernels)

        writer.close()
        logging.info('extract feature completely, start training classifier')

        # train classifier
        classifier = LinearSVC()
        # classifier = SVC()
        # total_features = total_features.cpu()
        classifier.fit(total_features, total_train_labels)
        logging.info('classifier trained completely')

        # save model
        utils.save_model(pcanet, stage_save_path + "/pcanet.pkl")
        utils.save_model(classifier, stage_save_path + "/classifier.pkl")

        train_score = classifier.score(total_features, total_train_labels)
        logging.info("score of training is %s" % train_score)
示例#8
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)
示例#9
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))
示例#10
0
文件: cifar-10.py 项目: delldu/PCANet
    ax[i, class_id].imshow(train_set_X[img_idx[class_id][i]])
    ax[i, class_id].get_xaxis().set_visible(False)
    ax[i, class_id].get_yaxis().set_visible(False)

plt.savefig('cifar10.png', bbox_inches='tight')
plt.show()

#%%
# Convert to NCHW
train_set_X = train_set_X.permute(0, 3, 1, 2)
test_set_X = test_set_X.permute(0, 3, 1, 2)

#%%
print(' ====== PCANet Training ======= ')
# net = PCANet([40, 8], 5, 8, 0.5)
net = PCANet([40, 8], 5, 8, 0.5, [4, 2, 1])
time_start = time.time()
train_features = net.extract_features(train_set_X)
train_features = scipy.sparse.csr_matrix(train_features)
time_end = time.time()
print('Time cost %.2f s' % (time_end - time_start))
del train_set_X

#%%
#visualize PCA filter banks
fig, ax = plt.subplots(5, 8, figsize=(16, 10), facecolor='black')
for r, c in itertools.product(range(5), range(8)):
    # convert to HWC
    filter_bank = net.W_1[:, r * 8 + c].reshape(3, 5, 5).permute(1, 2, 0)
    #scale to [0, 1]
    filter_bank = np.interp(filter_bank,