Git Product home page Git Product logo

keras2cpp's Introduction

Keras2cpp release lisense Build Status

keras2cpp

Keras2cpp is a small library for running trained Keras models from a C++ application without any dependences.

Design goals:

  • Compatibility with networks generated by Keras using TensorFlow backend.
  • CPU only, no GPU.
  • No external dependencies, standard library, C++17.
  • Model stored on disk in binary format and can be quickly read.
  • Model stored in memory in contiguous block for better cache performance.

Not not layer and activation types are supported yet. Work in progress

Supported Keras layers:

  • Dense
  • Convolution1D
  • Convolution2D
  • Convolution3D
  • Flatten
  • ELU
  • Activation
  • MaxPooling2D
  • Embedding
  • LocallyConnected1D
  • LocallyConnected2D
  • LSTM
  • GRU
  • CNN
  • BatchNormalization

Supported activation:

  • linear
  • relu
  • softplus
  • tanh
  • sigmoid
  • hard_sigmoid
  • elu
  • softsign
  • softmax

Other tasks:

  • Create unit tests
  • Create Makefile
  • Code refactoring (in progress)

The project is compatible with Keras 2.x (all versions) and Python 3.x

Example

python_model.py:

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

#create random data
test_x = np.random.rand(10, 10).astype('f')
test_y = np.random.rand(10).astype('f')
model = Sequential([
    Dense(1, input_dim=10)
])
model.compile(loss='mse', optimizer='adam')

#train model by 1 iteration
model.fit(test_x, test_y, epochs=1, verbose=False)

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

#save model
from keras2cpp import export_model
export_model(model, 'example.model')

cpp_model.cc:

#include "src/model.h"

using keras2cpp::Model;
using keras2cpp::Tensor;

int main() {
    // Initialize model.
    auto model = Model::load("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(in);
    out.print();
    return 0;
}

How to build and run

Tested with Keras 2.2.1, Python 3.6

$ git clone https://github.com/gosha20777/keras2cpp.git
$ cd keras2cpp
$ mkdir build && cd build
$ python3 ../python_model.py
[[-1.85735667]]

$ cmake ..
$ cmake --build .
$ ./keras2cpp
[ -1.857357 ]

License

MIT

Similar projects

I found another similar projects on Github:

But It works only with Keras 1 and didn’t work for me. That's why I wrote my own implementation.

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.