• Stars
    star
    179
  • Rank 214,039 (Top 5 %)
  • Language
    C++
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Small library for running Keras models from a C++ application

Kerasify

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 https://github.com/pplonski/keras2cpp/

Example

make_model.py:

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')
model.fit(test_x, 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')

test.cc:

#include "keras_model.h"

int main() {
    // Initialize model.
    KerasModel model;
    model.LoadModel("example.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);
    out.Print();
    return 0;
}

To test:

$ python make_model.py
[[-1.85735667]]

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

Unit tests

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

$ python make_tests.py
...

$ make
cppcheck --error-exitcode=1 keras_model.cc
Checking keras_model.cc...
Checking keras_model.cc: DEBUG...
g++ --std=c++11 -I. -Wall -Werror -MMD -O3 -mtune=core2 -o keras_model.o -c keras_model.cc
cppcheck --error-exitcode=1 keras_model_test.cc
Checking keras_model_test.cc...
Checking keras_model_test.cc: DEBUG...
g++ --std=c++11 -I. -Wall -Werror -MMD -O3 -mtune=core2 -o keras_model_test.o -c keras_model_test.cc
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

License

MIT