Giter Club home page Giter Club logo

kerasify's Introduction

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

kerasify's People

Contributors

moof2k avatar

Watchers

 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.