Code Monkey home page Code Monkey logo

low-level-cnn's Introduction

Low Level Convolutional Neural Network with Parallelism

Author: Brandon

Introduction

This is a very basic convolutional neural network "from scratch", using lower level code such as the C programming language, and custom Cuda kernels.

Motivations:
After discovering Keras in the previous fall term, I decided to deepen my understanding of deep learning by implementing a basic convolutional neural network from scratch. One thing I was dying to figure out was how to perform backpropagation without depending on some higher-level API. This project accomplishes that. Another reason for undertaking this project was to create an opportunity to practice Cuda programming, and to motivate the need for performance optimization. Last but not least, my professor mentioned the massive speedup that could be achieved by using a GPU in deep learning tasks.

Disclaimer:
I'm still learning about CNNs, and I have not taken any courses on deep learning. This project doesn't necessarily follow best practices; it is only a best-effort attempt.

Current Results

  • The sequential implementation (main branch) was compared with the parallel implementation (parallel branch) using the following neural network architecture:

    • input layer: receives 64 * 64 grayscale images that were normalized
      • Normalization was not per pixel, just per image
      • 64(width) x 64(height) x 1(channels) inputs, no batches
    • hidden layer 1: convolutional 2D
      • 16 filters/activation maps
      • 5 x 5 kernels, stride of 3 in each axis
      • ReLU activation
    • hidden layer 2: dense layer (based on my implementation, no need to flatten)
      • 128 output nodes
      • Sigmoid activation
    • output layer: dense layer
      • 15 output nodes (for the 15 classes of chinese characters)
      • This layer doesn't apply an activation, this is because I am using the softmax classifier (softmax activation + categorical cross entropy loss)
      • I can skip calculating the entire Jacobian matrix for the softmax classifier, and just calculate the partial derivative of loss with respect to this layer's outputs in one go if I handle the activation function separately.
  • By a not-very-strict, naive benchmark, noted a speedup of over 18x compared to sequential implementation.

  • The current weights uploaded to the repo are for a bigger network of 32 filters in hidden layer 1, and 512 output nodes in hidden layer 2.

    • This larger network yielded up to 98% accuracy.
    • (But this accuracy is from the model predicting on training data. No train/test split, yet.)

Dependencies and shoutouts

  • This project is written in C and Cuda, with some c++ dependencies.

    • C++ 14
    • gcc/g++ v7.5.0
    • Cuda compilation tools V10.2.89
    • Test hardware: GeForce GTX TITAN X (Compute Capability 5.2)
  • The data set used for this project is the Chinese MNIST numbers dataset. This is similar to the classic MNIST numbers, but was slightly smaller and had a higher usability score.

  • This project uses the wonderfully convenient stb_image.h single-header image library:

  • Unit tests were a lifesaver. I decided to use Catch2, an elegant and handy unit-testing (mainly) framework.

Misc

Possible plans

  • Work needs to be done on convolutional layers.
  • Pooling layers need to be implemented.
  • Trainable biases need to be implemented.
  • Revisit Cuda kernels and take benchmarks. Optimize as needed.
  • Add training optimizer, or change from stochastic gradient descent to batched or something.

low-level-cnn's People

Contributors

brandonxue avatar github-classroom[bot] avatar

Watchers

Doina Bein avatar Kostas Georgiou avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.