{"id":13453505,"url":"https://github.com/Dobiasd/frugally-deep","last_synced_at":"2025-03-24T01:31:34.907Z","repository":{"id":37752472,"uuid":"63446519","full_name":"Dobiasd/frugally-deep","owner":"Dobiasd","description":"A lightweight header-only library for using Keras (TensorFlow) models in C++.","archived":false,"fork":false,"pushed_at":"2024-07-13T07:05:15.000Z","size":3467,"stargazers_count":1069,"open_issues_count":7,"forks_count":236,"subscribers_count":52,"default_branch":"master","last_synced_at":"2024-10-29T15:38:07.188Z","etag":null,"topics":["c-plus-plus","c-plus-plus-14","convolutional-neural-networks","cpp","cpp14","deep-learning","edge-computing","header-only","keras","keras-models","library","machine-learning","prediction","python","tensorflow","tinyml"],"latest_commit_sha":null,"homepage":"","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/Dobiasd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["dobiasd"]}},"created_at":"2016-07-15T19:36:26.000Z","updated_at":"2024-10-28T13:05:39.000Z","dependencies_parsed_at":"2023-10-05T07:15:30.006Z","dependency_job_id":"888de4c8-28ff-42f6-be42-4080b77b2ec9","html_url":"https://github.com/Dobiasd/frugally-deep","commit_stats":{"total_commits":1508,"total_committers":36,"mean_commits":"41.888888888888886","dds":"0.17572944297082227","last_synced_commit":"38c52448b1a4996b3e0e435a877d02441098b1dd"},"previous_names":[],"tags_count":90,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dobiasd%2Ffrugally-deep","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dobiasd%2Ffrugally-deep/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dobiasd%2Ffrugally-deep/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dobiasd%2Ffrugally-deep/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Dobiasd","download_url":"https://codeload.github.com/Dobiasd/frugally-deep/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245194244,"owners_count":20575728,"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-14","convolutional-neural-networks","cpp","cpp14","deep-learning","edge-computing","header-only","keras","keras-models","library","machine-learning","prediction","python","tensorflow","tinyml"],"created_at":"2024-07-31T08:00:41.567Z","updated_at":"2025-03-24T01:31:34.900Z","avatar_url":"https://github.com/Dobiasd.png","language":"C++","funding_links":["https://github.com/sponsors/dobiasd"],"categories":["C++","Artificial Intelligence","Data Mining, Machine Learning, and Deep Learning","Machine Learning Frameworks"],"sub_categories":[],"readme":"![logo](logo/fdeep.png)\n\n[![CI](https://github.com/Dobiasd/frugally-deep/workflows/ci/badge.svg)](https://github.com/Dobiasd/frugally-deep/actions)\n[![(License MIT 1.0)](https://img.shields.io/badge/license-MIT%201.0-blue.svg)][license]\n\n[license]: LICENSE\n\nfrugally-deep\n=============\n\n**Use Keras models in C++ with ease**\n\nTable of contents\n-----------------\n\n* [Introduction](#introduction)\n* [Usage](#usage)\n* [Requirements and Installation](#requirements-and-installation)\n* [FAQ](#faq)\n\nIntroduction\n------------\n\nWould you like to build/train a model using Keras/Python? And would you like to run the prediction (forward pass) on your model in C++ without linking your application against TensorFlow? Then frugally-deep is exactly for you.\n\n**frugally-deep**\n\n* **is a small header-only library** written in modern and pure C++.\n* is very easy to integrate and use.\n* depends only on [FunctionalPlus](https://github.com/Dobiasd/FunctionalPlus), [Eigen](http://eigen.tuxfamily.org/) and [json](https://github.com/nlohmann/json) - also header-only libraries.\n* supports inference (`model.predict`) not only for [sequential models](https://keras.io/getting-started/sequential-model-guide/) but also for computational graphs with a more complex topology, created with the [functional API](https://keras.io/getting-started/functional-api-guide/).\n* re-implements a (small) subset of TensorFlow, i.e., the operations needed to support prediction.\n* results in a much smaller binary size than linking against TensorFlow.\n* works out-of-the-box also when compiled into a 32-bit executable. (Of course, 64 bit is fine too.)\n* avoids temporarily allocating (potentially large chunks of) additional RAM during convolutions (by not materializing the im2col input matrix).\n* utterly ignores even the most powerful GPU in your system and uses only one CPU core per prediction. ;-)\n* but is quite fast on one CPU core, and you can run multiple predictions in parallel, thus utilizing as many CPUs as you like to improve the overall prediction throughput of your application/pipeline.\n\n\n### Supported layer types\n\n* `Add`, `Concatenate`, `Subtract`, `Multiply`, `Average`, `Maximum`, `Minimum`, `Dot`\n* `AveragePooling1D/2D/3D`, `GlobalAveragePooling1D/2D/3D`\n* `TimeDistributed`\n* `Conv1D/2D`, `SeparableConv2D`, `DepthwiseConv2D`\n* `Conv1DTranspose`, `Conv2DTranspose`\n* `Cropping1D/2D/3D`, `ZeroPadding1D/2D/3D`, `CenterCrop`\n* `BatchNormalization`, `Dense`, `Flatten`, `Normalization`\n* `Dropout`, `AlphaDropout`, `GaussianDropout`, `GaussianNoise`\n* `SpatialDropout1D`, `SpatialDropout2D`, `SpatialDropout3D`\n* `ActivityRegularization`, `LayerNormalization`, `UnitNormalization`\n* `RandomContrast`, `RandomFlip`, `RandomHeight`\n* `RandomRotation`, `RandomTranslation`, `RandomWidth`, `RandomZoom`\n* `MaxPooling1D/2D/3D`, `GlobalMaxPooling1D/2D/3D`\n* `ELU`, `LeakyReLU`, `ReLU`, `SeLU`, `PReLU`\n* `Sigmoid`, `Softmax`, `Softplus`, `Tanh`\n* `Exponential`, `GELU`, `Softsign`, `Rescaling`\n* `UpSampling1D/2D`, `Resizing`\n* `Reshape`, `Permute`, `RepeatVector`\n* `Embedding`, `CategoryEncoding`\n* `Attention`, `AdditiveAttention`, `MultiHeadAttention`\n\n### Also supported\n\n* multiple inputs and outputs\n* nested models\n* residual connections\n* shared layers\n* variable input shapes\n* arbitrary complex model architectures / computational graphs\n* custom layers (by passing custom factory functions to `load_model`)\n\n### Currently not supported are the following:\n\n`Lambda` ([why](FAQ.md#why-are-lambda-layers-not-supported)),\n`Conv3D`, `ConvLSTM1D`, `ConvLSTM2D`, `Discretization`,\n`GRUCell`, `Hashing`,\n`IntegerLookup`,\n`LocallyConnected1D`, `LocallyConnected2D`,\n`LSTMCell`, `Masking`,\n`RepeatVector`, `RNN`, `SimpleRNN`,\n`SimpleRNNCell`, `StackedRNNCells`, `StringLookup`, `TextVectorization`,\n`Bidirectional`, `GRU`, `LSTM`, `CuDNNGRU`, `CuDNNLSTM`,\n`ThresholdedReLU`, `Upsampling3D`, `temporal` models\n\nUsage\n-----\n\n1) Use Keras/Python to build (`model.compile(...)`), train (`model.fit(...)`) and test (`model.evaluate(...)`) your model as usual. Then save it to a single file using `model.save('....keras')`. The `image_data_format` in your model must be `channels_last`, which is the default when using the TensorFlow backend. Models created with a different `image_data_format` and other backends are not supported.\n\n2) Now convert it to the frugally-deep file format with `keras_export/convert_model.py`\n\n3) Finally load it in C++ (`fdeep::load_model(...)`) and use `model.predict(...)` to invoke a forward pass with your data.\n\nThe following minimal example shows the full workflow:\n\n```python\n# create_model.py\nimport numpy as np\nfrom keras.layers import Input, Dense\nfrom keras.models import Model\n\ninputs = Input(shape=(4,))\nx = Dense(5, activation='relu')(inputs)\npredictions = Dense(3, activation='softmax')(x)\nmodel = Model(inputs=inputs, outputs=predictions)\nmodel.compile(loss='categorical_crossentropy', optimizer='nadam')\n\nmodel.fit(\n    np.asarray([[1, 2, 3, 4], [2, 3, 4, 5]]),\n    np.asarray([[1, 0, 0], [0, 0, 1]]), epochs=10)\n\nmodel.save('keras_model.keras')\n```\n\n```bash\npython3 keras_export/convert_model.py keras_model.keras fdeep_model.json\n```\n\n```cpp\n// main.cpp\n#include \u003cfdeep/fdeep.hpp\u003e\nint main()\n{\n    const auto model = fdeep::load_model(\"fdeep_model.json\");\n    const auto result = model.predict(\n        {fdeep::tensor(fdeep::tensor_shape(static_cast\u003cstd::size_t\u003e(4)),\n        std::vector\u003cfloat\u003e{1, 2, 3, 4})});\n    std::cout \u003c\u003c fdeep::show_tensors(result) \u003c\u003c std::endl;\n}\n```\n\nWhen using `convert_model.py` a test case (input and corresponding output values) is generated automatically and saved along with your model. `fdeep::load_model` runs this test to make sure the results of a forward pass in frugally-deep are the same as in Keras.\n\nFor more integration examples please have a look at the [FAQ](FAQ.md).\n\nRequirements and Installation\n-----------------------------\n\n- A **C++14**-compatible compiler: Compilers from these versions on are fine: GCC 4.9, Clang 3.7 (libc++ 3.7) and Visual C++ 2015\n- Python 3.9 or higher\n- TensorFlow 2.18.0\n- Keras 3.8.0\n\n(These are the tested versions, but somewhat older ones might work too.)\n\nGuides for different ways to install frugally-deep can be found in [`INSTALL.md`](INSTALL.md).\n\nFAQ\n---\n\nSee [`FAQ.md`](FAQ.md)\n\nDisclaimer\n----------\n\nThe API of this library still might change in the future. If you have any suggestions, find errors, or want to give general feedback/criticism, I'd [love to hear from you](issues). Of course, [contributions](pulls) are also very welcome.\n\nLicense\n-------\n\nDistributed under the MIT License.\n(See accompanying file [`LICENSE`](LICENSE) or at\n[https://opensource.org/licenses/MIT](https://opensource.org/licenses/MIT))\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDobiasd%2Ffrugally-deep","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FDobiasd%2Ffrugally-deep","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDobiasd%2Ffrugally-deep/lists"}