The cnn
package contains a primary cnn.CNN
class as well as convolution, max-pooling, and softmax activation layers at cnn.layers.Conv
, cnn.layers.MaxPool
and cnn.layers.SoftMax
, respectively. These layers can be configured along with the learning rate in order to fine-tune the training of the network. This network currently works with the MNIST handwritten digits dataset, which can be tested by running python run_mnist.py
.
The network supports both grayscale and RGB images.
- clone this repo locally
- have Python 3 and pip installed on your machine
- install dependencies with
pip install -r requirements.txt
- run
python run_mnist.py
# package must exist locally, whether cloned or copied into a project
import cnn
# get training images (RGB or grayscale) and labels, ordered
training_images = get_ordered_images_list()
training_labels = get_ordered_labels_list()
# define list of classes
classes = ['cat', 'dog']
# initialize layer stack
layers = [
cnn.layers.Conv(num_kernels=16, kernel_dimension=5, stride=1),
cnn.layers.MaxPool(kernel_dimension=2, stride=2),
cnn.layers.Conv(num_kernels=16, kernel_dimension=3, stride=1),
cnn.layers.MaxPool(kernel_dimension=2, stride=2),
cnn.layers.SoftMax(num_classes=2),
]
# initialize network object
net = cnn.CNN(layers)
# train
net.train(training_images, training_labels, classes, num_epochs=20, rate=0.001)
# get test image and label
test_image = get_dog_png()
test_label = 'dog'
# test model prediction
prediction_index = net.predict(test_image)
prediction = classes[prediction_index]
correct = prediction == test_label
- To run unit tests, run
python -m pytest
.