项目作者: ElefHead

项目描述 :
A numpy based CNN implementation for classifying images
高级语言: Python
项目地址: git://github.com/ElefHead/numpy-cnn.git
创建时间: 2018-06-06T05:53:56Z
项目社区:https://github.com/ElefHead/numpy-cnn

开源协议:

下载


Numpy CNN

A numpy based CNN implementation for classifying images.

status: archived

Usage

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.
I placed the data inside a folder called data within the project root folder (this code works by default with cifar10, for other datasets, the filereader in utilities can’t be used).

After placing data, the directory structure looks as follows

  • root
    • data\
      • data_batch_1
      • data_batch_2
      • ..
    • layers\
    • loss\
    • utilities\
    • cnn.py
    • fully_connected_network.py

1) Import the required layer classes from layers folder, for example

  1. ```python
  2. from layers.fully_connected import FullyConnected
  3. from layers.convolution import Convolution
  4. from layers.flatten import Flatten
  5. ```

2) Import the activations and losses in a similar way, for example

  1. ```python
  2. from layers.activation import Elu, Softmax
  3. from loss.losses import CategoricalCrossEntropy
  4. ```

3) Import the model class from utilities folder

  1. ```python
  2. from utilities.model import Model
  3. ```

4) Create a model using Model and layer classes

  1. ```python
  2. model = Model(
  3. Convolution(filters=5, padding='same'),
  4. Elu(),
  5. Pooling(mode='max', kernel_shape=(2, 2), stride=2),
  6. Flatten(),
  7. FullyConnected(units=10),
  8. Softmax(),
  9. name='cnn-model'
  10. )
  11. ```

5) Set model loss

  1. ```python
  2. model.set_loss(CategoricalCrossEntropy)
  3. ```

6) Train the model using

  1. ```python
  2. model.train(data, labels)
  3. ```
  4. * set load_and_continue = True for loading trained weights and continue training
  5. * By default the model uses AdamOptimization with AMSgrad
  6. * It also saves the weights after each epoch to a models folder within the project

7) For prediction, use

  1. ```python
  2. prediction = model.predict(data)
  3. ```

8) For calculating accuracy, the model class provides its own function

  1. ```python
  2. accuracy = model.evaluate(data, labels)
  3. ```

9) To load model in a different place with the trained weights, follow till step 5 and then

  1. ```python
  2. model.load_weights()
  3. ```
  4. Note: You will have to have similar directory structure.

This was a fun project that started out as me trying to implement a CNN by myself for classifying cifar10 images. In process, I was able to implement a reusable (numpy based)
library-ish code for creating CNNs with adam optimization.

Anyone wanting to understand how backpropagation works in CNNs is welcome to try out this code, but for all practical usage there are better frameworks
with performances that this code cannot even come close to replicating.

The CNN implemented here is based on Andrej Karpathy’s notes