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

Last Commit
May. 22, 2019
Oct. 25, 2016


Kerasify is a small library for running trained Keras models from a C++ application.

Design goals:

  • Compatibility with image processing Sequential networks generated by Keras using Theano backend.
  • CPU only, no GPU
  • No external dependencies, standard library, C++11 features OK.
  • Model stored on disk in binary format that can be quickly read.
  • Model stored in memory in contiguous block for better cache performance.
  • Doesn't throw exceptions, returns only bool on error.
  • Unit testable, rigorous unit tests.

Looking for more Keras/C++ libraries? Check out


import numpy as np
from keras.models import Sequential
from keras.layers import Dense

test_x = np.random.rand(10, 10).astype('f')
test_y = np.random.rand(10).astype('f')

model = Sequential()
model.add(Dense(1, input_dim=10))

model.compile(loss='mean_squared_error', optimizer='adamax'), test_y, nb_epoch=1, verbose=False)

print model.predict(np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]))

from kerasify import export_model
export_model(model, 'example.model')

#include "keras_model.h"

int main() {
    // Initialize model.
    KerasModel model;

    // Create a 1D Tensor on length 10 for input data.
    Tensor in(10);
    in.data_ = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};

    // Run prediction.
    Tensor out;
    model.Apply(&in, &out);
    return 0;

To test:

$ python

$ g++ --std=c++11 -Wall -O3
$ ./a.out 
[ -1.857357 ]

Unit tests

To run the unit tests, generate the unit test models and then run keras_model_test:

$ python

$ make
cppcheck --error-exitcode=1
Checking DEBUG...
g++ --std=c++11 -I. -Wall -Werror -MMD -O3 -mtune=core2 -o keras_model.o -c
cppcheck --error-exitcode=1
Checking DEBUG...
g++ --std=c++11 -I. -Wall -Werror -MMD -O3 -mtune=core2 -o keras_model_test.o -c
g++ -o keras_model_test keras_model_test.o keras_model.o

$ ./keras_model_test
TEST dense_1x1
TEST dense_10x1
TEST dense_2x2
TEST dense_10x10
TEST dense_10x10x10
TEST conv_2x2
TEST conv_3x3
TEST conv_3x3x3
TEST elu_10
TEST benchmark
TEST benchmark
TEST benchmark
TEST benchmark
TEST benchmark
Benchmark network loads in 0.022415s
Benchmark network runs in 0.022597s