{"id":21360077,"url":"https://github.com/gosha20777/keras2cpp","last_synced_at":"2025-10-18T12:02:49.998Z","repository":{"id":49149263,"uuid":"158204748","full_name":"gosha20777/keras2cpp","owner":"gosha20777","description":"it's a small library for running trained Keras 2 models from a native C++ code. ","archived":false,"fork":false,"pushed_at":"2022-11-21T07:02:03.000Z","size":93,"stargazers_count":180,"open_issues_count":19,"forks_count":54,"subscribers_count":20,"default_branch":"master","last_synced_at":"2023-11-07T17:36:51.310Z","etag":null,"topics":["c-plus-plus","c-plus-plus-17","cpp","keras","keras2cpp","machine-learning","neural-networks","python"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gosha20777.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-11-19T10:43:27.000Z","updated_at":"2023-11-06T06:31:34.000Z","dependencies_parsed_at":"2023-01-23T20:00:36.854Z","dependency_job_id":null,"html_url":"https://github.com/gosha20777/keras2cpp","commit_stats":null,"previous_names":[],"tags_count":6,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gosha20777%2Fkeras2cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gosha20777%2Fkeras2cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gosha20777%2Fkeras2cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gosha20777%2Fkeras2cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gosha20777","download_url":"https://codeload.github.com/gosha20777/keras2cpp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225850208,"owners_count":17534062,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c-plus-plus","c-plus-plus-17","cpp","keras","keras2cpp","machine-learning","neural-networks","python"],"created_at":"2024-11-22T05:32:21.116Z","updated_at":"2025-10-18T12:02:44.965Z","avatar_url":"https://github.com/gosha20777.png","language":"C++","readme":"# Keras2cpp ![release](https://img.shields.io/github/release/gosha20777/keras2cpp.svg?colorB=red) ![lisense](https://img.shields.io/github/license/gosha20777/keras2cpp.svg) [![Build Status](https://travis-ci.org/gosha20777/keras2cpp.svg?branch=master)](https://travis-ci.org/gosha20777/keras2cpp)\n![keras2cpp](docs/img/keras2cpp.png)\n\nKeras2cpp is a small library for running trained Keras models from a C++ application without any dependences. \n\nDesign goals:\n\n- Compatibility with networks generated by Keras using TensorFlow backend.\n- CPU only, no GPU.\n- No external dependencies, standard library, C++17.\n- Model stored on disk in binary format and can be quickly read.\n- Model stored in memory in contiguous block for better cache performance.\n\n*Not not layer and activation types are supported yet. Work in progress*\n\nSupported Keras layers:\n- [x] Dense\n- [x] Convolution1D\n- [x] Convolution2D\n- [ ] Convolution3D\n- [x] Flatten\n- [x] ELU\n- [x] Activation\n- [x] MaxPooling2D\n- [x] Embedding\n- [x] LocallyConnected1D\n- [x] LocallyConnected2D\n- [x] LSTM\n- [ ] GRU\n- [ ] CNN\n- [X] BatchNormalization\n\nSupported activation:\n- [x] linear\n- [x] relu\n- [x] softplus\n- [x] tanh\n- [x] sigmoid\n- [x] hard_sigmoid\n- [x] elu\n- [x] softsign\n- [x] softmax\n\nOther tasks:\n- [x] Create unit tests\n- [x] Create Makefile\n- [x] Code refactoring *(in progress)*\n\nThe project is compatible with Keras 2.x (all versions) and Python 3.x\n\n# Example\n\npython_model.py:\n\n```python\nimport numpy as np\nfrom keras import Sequential\nfrom keras.layers import Dense\n\n#create random data\ntest_x = np.random.rand(10, 10).astype('f')\ntest_y = np.random.rand(10).astype('f')\nmodel = Sequential([\n    Dense(1, input_dim=10)\n])\nmodel.compile(loss='mse', optimizer='adam')\n\n#train model by 1 iteration\nmodel.fit(test_x, test_y, epochs=1, verbose=False)\n\n#predict\ndata = np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])\nprediction = model.predict(data)\nprint(prediction)\n\n#save model\nfrom keras2cpp import export_model\nexport_model(model, 'example.model')\n```\n\ncpp_model.cc:\n\n```c++\n#include \"src/model.h\"\n\nusing keras2cpp::Model;\nusing keras2cpp::Tensor;\n\nint main() {\n    // Initialize model.\n    auto model = Model::load(\"example.model\");\n\n    // Create a 1D Tensor on length 10 for input data.\n    Tensor in{10};\n    in.data_ = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};\n\n    // Run prediction.\n    Tensor out = model(in);\n    out.print();\n    return 0;\n}\n```\n\n# How to build and run\n\n*Tested with Keras 2.2.1, Python 3.6*\n\n```bash\n$ git clone https://github.com/gosha20777/keras2cpp.git\n$ cd keras2cpp\n$ mkdir build \u0026\u0026 cd build\n$ python3 ../python_model.py\n[[-1.85735667]]\n\n$ cmake ..\n$ cmake --build .\n$ ./keras2cpp\n[ -1.857357 ]\n```\n\n# License\n\nMIT\n\n# Similar projects\n\nI found another similar projects on Github:\n- \u003chttps://github.com/pplonski/keras2cpp/\u003e;\n- \u003chttps://github.com/moof2k/kerasify\u003e\n- \u003chttps://github.com/Dobiasd/frugally-deep\u003e\n\nBut It works only with Keras 1 and didn’t work for me. \nThat's why I wrote my own implementation.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgosha20777%2Fkeras2cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgosha20777%2Fkeras2cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgosha20777%2Fkeras2cpp/lists"}