Giter Site home page Giter Site logo

arquolo / kerasify Goto Github PK

View Code? Open in Web Editor NEW

This project forked from moof2k/kerasify

0.0 0.0 2.0 179 KB

Small library for running Keras 2.x models from a C++ application (TensorFlow backend)

License: MIT License

C++ 66.27% Python 27.37% CMake 5.20% Makefile 1.16%
c-plus-plus c-plus-plus-17 python python-3 keras keras-tensorflow tensorflow machine-learning deep-learning neural-networks

kerasify's Introduction

Kerasify pipeline status last commit license tag

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

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 (up to 2.3.x) using TensorFlow (up to 1.15.x) as backend.
  • CPU only, no GPU
  • No external dependencies, standard library, C++17 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.
  • Unit testable, rigorous unit tests.

Currently implemented Keras layers:

  • Embedding, Flatten
  • Dense, Conv1D, Conv2D, LocallyConnected1D
  • LSTM
  • BatchNormalization, MaxPooling
  • Activation: ELU, HardSigmoid, Linear, Relu, Sigmoid, SoftMax, SoftPlus, SoftSign, Tanh

Looking for more Keras/C++ libraries? Check out https://github.com/pplonski/keras2cpp/

Example

make_model.py:

import numpy as np
from keras 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([
    Dense(1, input_dim=10)
])

model.compile(loss='mse', optimizer='adam')
model.fit(test_x, test_y, epochs=1, verbose=False)

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

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

test.cpp:

#include "keras/model.h"

using keras::Model;
using keras::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;
}

Unit tests

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

$ python3 make_tests.py
...
$ mkdir build && cd build && cmake .. && cmake --build . && cd
...
$ ./build/kerasify
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

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.