nam-ngh / modularCNN

A mini-library with modular code to efficiently build and train convolutional neural network architectures. Created from scratch on numpy.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Convolutional Neural Network (CNN) modules from scratch

About modularCNN

This project contains modular code to efficiently build and implement CNN models from scratch, WITHOUT the use of existing deep learning packages such as keras or pytorch. With this library, you can experiment building various neural network architectures for image classification, from a simple one-hidden-layer perceptron to a deep network of multiple Convolutional and Pooling layers, with the appropriate activation functions and weights initialisation strategies. Training performance is relatively efficient for python due to the use of fully vectorised numpy operations throughout all layers, though still a little behind established libraries such as keras.

Instalation and Use Guide

A simple build and train example notebook is provided with the CIFAR10 dataset. Otherwise, to work with this library on your local machine, simply:

  1. Clone the repository,
!git clone https://github.com/nam-ngh/modularCNN.git
  1. Import modules:
from lib import layer, network
  1. Now you can easily build your own neural networks, for example:
  • Defining your model with the Net class:
model = network.Net()
  • adding in layers:
model.add(layer.Convolutional(input_shape=(32,32,3),filters=16,filter_size=3,stride=1,pad=1))
# e.g.: a Convolutional layer with 16 3x3 filters, stride of 1 and padding of 1 pixel
  • get a summary of the architecture:
model.summary()
  • and train the model:
model.train(x_train, y_train, epochs=30, learn_rate=0.00001, val_size=0.1)
# split 10% of training data for validation

Important Notes

  • The current model and layers are only compatible with square images: Input feature sets x_train, x_test must be provided in shape (n,x,x,c) where n = number of sample, x = height and width of the image, and c = number of channels (c=1 for b&w, c=3 for RGB)
  • input_shape must be specified for all layers upon adding to the network, in the form of (x,x,c), with the exception of Activation layer where this is not needed and Dense layer, where the number of neurons in and neurons out are required instead.
  • Ouput size of Convolutional and MaxPooling layer can be determined as follows: o = (x - filter_size + 2*pad)/stride + 1. Please make sure this is a whole number so that convolutions are complete and free of errors
  • It is recommmended that you determine the output shape of the previous layer first before adding the next, to make sure that the shapes don't mismatch. If you are unsure you can run the model summary function each time you add a layer to check the layers added so far
  • Currently for MaxPooling layer, pool_size and stride can only be set as the same number under pool_size, i.e. pooling regions can't overlap

About

A mini-library with modular code to efficiently build and train convolutional neural network architectures. Created from scratch on numpy.

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Jupyter Notebook 85.8%Language:Python 14.2%