Counting 3,663 Big Data & Machine Learning Frameworks, Toolsets, and Examples...
Suggestion? Feedback? Tweet @stkim1

Last Commit
Jan. 14, 2019
Oct. 4, 2018


This mini-project contains code for building Binary, Ternary and N-bit Quantized Convolutional Neural Networks with Keras or Tensorflow.


Low Precision Networks have recently gained popularity due to their applications in devices with low-compute capabilities. During the forward pass, QNNs drastically reduce memory size and accesses, and replace most arithmetic operations with bit-wise operations.

Various Binarization, Ternarization and Quantization schemes are published for weights and activations.

Image Source: Minimum Energy Quantized Neural Networks

Binarization function used in the experiment is deterministic binary-tanh which is placed in

Setup Dependencies

The recommended version for running the experiments is Python3.

  1. Follow the installation guide on Tensorflow Homepage for installing Tensorflow-GPU or Tensorflow-CPU.
  2. Follow instructions outlined on Keras Homepage for installing Keras.

Project Structure

The skeletal overview of the project is as follows:

├── binarize/
│   ├──  # Custom binary layers are defined in Keras 
│   └──     # Binarization functions for weights and activations
├── ternarize/
│   ├──  # Custom ternarized layers are defined in Keras
│   └──     # Ternarization functions for weights and activations
├── quantize/
│   ├──  # Custom quantized layers are defined in Keras
│   └──     # Quantization functions for weights and activations
├──           # Stores generic operations              
├──         # Implementation of Binarized Neural Networks
├──        # Implementation of Ternarized Neural Networks
└──      # Implementation of Quantized Neural Networks


In the root directory, to run the examples use:

python3 {example}  # for binary and ternary net

python3 -nb N  # For quantized net, replace N with the number of bits you want to quantize the weights and activations to. (default value of N =4)

Also, you can import the layers directly in your own Keras or Tensorflow code. Read this blog to know how to use Keras layers in Tensorflow

Thanks to

This work wouldn't have been possible without the help from the following repos: