Keras2cpp
it's a small library for running trained Keras 2 models from a native C++ code.
Install / Use
/learn @gosha20777/Keras2cppREADME
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:
- [x] Dense
- [x] Convolution1D
- [x] Convolution2D
- [ ] Convolution3D
- [x] Flatten
- [x] ELU
- [x] Activation
- [x] MaxPooling2D
- [x] Embedding
- [x] LocallyConnected1D
- [x] LocallyConnected2D
- [x] LSTM
- [ ] GRU
- [ ] CNN
- [X] BatchNormalization
Supported activation:
- [x] linear
- [x] relu
- [x] softplus
- [x] tanh
- [x] sigmoid
- [x] hard_sigmoid
- [x] elu
- [x] softsign
- [x] softmax
Other tasks:
- [x] Create unit tests
- [x] Create Makefile
- [x] 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:
- https://github.com/pplonski/keras2cpp/;
- https://github.com/moof2k/kerasify
- https://github.com/Dobiasd/frugally-deep
But It works only with Keras 1 and didn’t work for me. That's why I wrote my own implementation.
Related Skills
claude-opus-4-5-migration
82.1kMigrate prompts and code from Claude Sonnet 4.0, Sonnet 4.5, or Opus 4.1 to Opus 4.5
model-usage
334.1kUse CodexBar CLI local cost usage to summarize per-model usage for Codex or Claude, including the current (most recent) model or a full model breakdown. Trigger when asked for model-level usage/cost data from codexbar, or when you need a scriptable per-model summary from codexbar cost JSON.
mcp-for-beginners
15.6kThis open-source curriculum introduces the fundamentals of Model Context Protocol (MCP) through real-world, cross-language examples in .NET, Java, TypeScript, JavaScript, Rust and Python. Designed for developers, it focuses on practical techniques for building modular, scalable, and secure AI workflows from session setup to service orchestration.
TrendRadar
49.7k⭐AI-driven public opinion & trend monitor with multi-platform aggregation, RSS, and smart alerts.🎯 告别信息过载,你的 AI 舆情监控助手与热点筛选工具!聚合多平台热点 + RSS 订阅,支持关键词精准筛选。AI 智能筛选新闻 + AI 翻译 + AI 分析简报直推手机,也支持接入 MCP 架构,赋能 AI 自然语言对话分析、情感洞察与趋势预测等。支持 Docker ,数据本地/云端自持。集成微信/飞书/钉钉/Telegram/邮件/ntfy/bark/slack 等渠道智能推送。
