Skip to content

IshitaTakeshi/PCANet

Repository files navigation

PCANet

PCANet is a deep learning network for image classification.

As the name suggests, weights in the network are calculated by PCA. Because of this characteristics, training of PCANet is extremely fast. Furthermore, class labels are not required in training of PCANet itself.
Details are described in the original paper.

Installation

Just running python3 setup.py install.
If you prefer pip, pip3 install . in the PCANet root directory.

If you want to run on GPU, see the installation guide of Chainer.

Usage

import pcanet as net

# Arguments are basically passed as tuple in the form (height, width) but int is also allowed. 
# If int is given, the parameter will be converted into (size, size) implicitly.
pcanet = net.PCANet(
    image_shape=28,
    filter_shape_l1=2, step_shape_l1=1, n_l1_output=3,  # parameters for the 1st layer
    filter_shape_l2=2, step_shape_l2=1, n_l2_output=3,  # parameters for the 2nd layer
    filter_shape_pooling=2, step_shape_pooling=2        # parameters for the pooling layer
)

# Check whether all pixels can be considered. Raise ValueError if the structure is not valid.
# Calling this function is optional. PCANet works without this line.
pcanet.validate_structure()

pcanet.fit(images_train)  # Train PCANet

# Trained PCANet behaves as a transformer from images into features.
# `images` is a 3d array in the form (n_images, height, width), who are transformed into feature vectors.
X_train = pcanet.transform(images_train)
X_test = pcanet.transform(images_test)

# Fit any models you like
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

See run_mnist.py for more details.

Example

CPU is used if you specify a negative value for the GPU ID

Train

python3 run_mnist.py --gpu <GPU ID> train --out <output directory (default='result')>

Test

python3 run_mnist.py --gpu <GPU ID> test --pretrained-model <path to dir (default='result')>

Changes from the original PCANet

This implementation uses IncrementalPCA instead of the ordinary PCA because the ordinary one consumes huge memory space. So it is not possible to train the model on a large dataset in a limited memory.

Documentation

Documentation can be generated by running make html in the docs directory.

Citation

Chan, Tsung-Han, et al. "PCANet: A simple deep learning baseline for image classification?." IEEE Transactions on Image Processing 24.12 (2015): 5017-5032.