A numpy based CNN implementation for classifying images.
Follow the steps listed below for using this repository after cloning it.
For examples, you can look at the code in fully_connected_network.py and cnn.py.
Data : https://www.kaggle.com/c/painter-by-numbers/data
The directory structure looks as follows
- root
-
data\
-
layers\
-
loss\
-
utilities\
-
cnn.py
-
fully_connected_network.py
-
- Import the required layer classes from layers folder, for example
from layers.fully_connected import FullyConnected from layers.convolution import Convolution from layers.flatten import Flatten
- Import the activations and losses in a similar way, for example
from layers.activation import Elu, Softmax from loss.losses import CategoricalCrossEntropy
- Import the model class from utilities folder
from utilities.model import Model
- Create a model using Model and layer classes
model = Model( Convolution(filters=5, padding='same'), Relu(), Convolution(filters=5, padding='same'), Relu(), Convolution(filters=5, padding='same'), Relu(), Convolution(filters=5, padding='same'), Relu(), Convolution(filters=5, padding='same'), Relu(), Pooling(mode='max', kernel_shape=(2, 2), stride=2), Flatten(), FullyConnected(units=4), FullyConnected(units=4), FullyConnected(units=4), Softmax(), name='cnn5' )
- Set model loss
model.set_loss(CategoricalCrossEntropy)
- Train the model using
model.train(data, labels)
- set load_and_continue = True for loading trained weights and continue training
- By default the model uses AdamOptimization with AMSgrad
- It also saves the weights after each epoch to a models folder within the project
- For prediction, use
prediction = model.predict(data)
- For calculating accuracy, the model class provides its own function
accuracy = model.evaluate(data, labels)
- To load model in a different place with the trained weights, follow till step 5 and then
Note: You will have to have similar directory structure.
model.load_weights()
The CNN implemented here is based on Andrej Karpathy's notes