frugally-deep
A lightweight header-only library for using Keras (TensorFlow) models in C++.
Science Score: 54.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
✓Committers with academic emails
2 of 35 committers (5.7%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (11.6%) to scientific vocabulary
Keywords
Keywords from Contributors
Repository
A lightweight header-only library for using Keras (TensorFlow) models in C++.
Basic Info
Statistics
- Stars: 1,110
- Watchers: 49
- Forks: 238
- Open Issues: 7
- Releases: 97
Topics
Metadata Files
README.md

frugally-deep
Use Keras models in C++ with ease
Table of contents
Introduction
Would 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.
frugally-deep
- is a small header-only library written in modern and pure C++.
- is very easy to integrate and use.
- depends only on FunctionalPlus, Eigen and json - also header-only libraries.
- supports inference (
model.predict) not only for sequential models but also for computational graphs with a more complex topology, created with the functional API. - re-implements a (small) subset of TensorFlow, i.e., the operations needed to support prediction.
- results in a much smaller binary size than linking against TensorFlow.
- works out-of-the-box also when compiled into a 32-bit executable. (Of course, 64 bit is fine too.)
- avoids temporarily allocating (potentially large chunks of) additional RAM during convolutions (by not materializing the im2col input matrix).
- utterly ignores even the most powerful GPU in your system and uses only one CPU core per prediction. ;-)
- 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.
Supported layer types
Add,Concatenate,Subtract,Multiply,Average,Maximum,Minimum,DotAveragePooling1D/2D/3D,GlobalAveragePooling1D/2D/3DTimeDistributedConv1D/2D,SeparableConv2D,DepthwiseConv2DConv1DTranspose,Conv2DTransposeCropping1D/2D/3D,ZeroPadding1D/2D/3D,CenterCropBatchNormalization,Dense,Flatten,NormalizationDropout,AlphaDropout,GaussianDropout,GaussianNoiseSpatialDropout1D,SpatialDropout2D,SpatialDropout3DActivityRegularization,LayerNormalization,UnitNormalizationRandomContrast,RandomFlip,RandomHeightRandomRotation,RandomTranslation,RandomWidth,RandomZoomMaxPooling1D/2D/3D,GlobalMaxPooling1D/2D/3DUpSampling1D/2D,Resizing,RescalingReshape,Permute,RepeatVectorEmbedding,CategoryEncodingAttention,AdditiveAttention,MultiHeadAttention
Also supported
- different activiations (
celu,elu,exponential,gelu,hard_shrink,hard_sigmoid,hard_tanh,leaky_relu,leakyrelu,linear,log_sigmoid,log_softmax,prelu,relu,relu6,selu,shared_activation,sigmoid,silu,soft_shrink,softmax,softplus,softsign,sparse_plus,squareplus,swish,tanh,tanh_shrink,threshold) - multiple inputs and outputs
- nested models
- residual connections
- shared layers
- variable input shapes
- arbitrary complex model architectures / computational graphs
- custom layers (by passing custom factory functions to
load_model)
Currently not supported are the following:
Lambda (why),
Conv3D, ConvLSTM1D, ConvLSTM2D, Discretization,
GRUCell, Hashing,
IntegerLookup,
LocallyConnected1D, LocallyConnected2D,
LSTMCell, Masking,
RepeatVector, RNN, SimpleRNN,
SimpleRNNCell, StackedRNNCells, StringLookup, TextVectorization,
Bidirectional, GRU, LSTM, CuDNNGRU, CuDNNLSTM,
ThresholdedReLU, Upsampling3D, temporal models
Usage
1) 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.
2) Now convert it to the frugally-deep file format with keras_export/convert_model.py
3) Finally load it in C++ (fdeep::load_model(...)) and use model.predict(...) to invoke a forward pass with your data.
The following minimal example shows the full workflow:
```python
create_model.py
import numpy as np from keras.layers import Input, Dense from keras.models import Model
inputs = Input(shape=(4,)) x = Dense(5, activation='relu')(inputs) predictions = Dense(3, activation='softmax')(x) model = Model(inputs=inputs, outputs=predictions) model.compile(loss='categorical_crossentropy', optimizer='nadam')
model.fit( np.asarray([[1, 2, 3, 4], [2, 3, 4, 5]]), np.asarray([[1, 0, 0], [0, 0, 1]]), epochs=10)
model.save('keras_model.keras') ```
bash
python3 keras_export/convert_model.py keras_model.keras fdeep_model.json
```cpp // main.cpp
include
int main()
{
const auto model = fdeep::loadmodel("fdeepmodel.json");
const auto result = model.predict(
{fdeep::tensor(fdeep::tensorshape(staticcaststd::size_t(4)),
std::vector
When 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.
For more integration examples please have a look at the FAQ.
Requirements and Installation
- 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
- Python 3.9 or higher
- TensorFlow 2.18.0
- Keras 3.8.0
(These are the tested versions, but somewhat older ones might work too.)
Guides for different ways to install frugally-deep can be found in INSTALL.md.
FAQ
See FAQ.md
Disclaimer
The 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. Of course, contributions are also very welcome.
License
Distributed under the MIT License.
(See accompanying file LICENSE or at
https://opensource.org/licenses/MIT)
Owner
- Name: Tobias Hermann
- Login: Dobiasd
- Kind: user
- Location: Germany
- Website: https://www.linkedin.com/in/t-hermann/
- Repositories: 29
- Profile: https://github.com/Dobiasd
Loving functional programming, machine learning, and neat software architecture.
Citation (CITATION.cff)
cff-version: 1.2.0
title: "frugally-deep"
url: "https://github.com/Dobiasd/frugally-deep"
authors:
- family-names: "Hermann"
given-names: "Tobias"
orcid: "https://orcid.org/0009-0007-4792-4904"
GitHub Events
Total
- Create event: 14
- Release event: 7
- Issues event: 30
- Watch event: 41
- Issue comment event: 72
- Push event: 79
- Pull request event: 18
- Fork event: 3
Last Year
- Create event: 14
- Release event: 7
- Issues event: 30
- Watch event: 41
- Issue comment event: 72
- Push event: 79
- Pull request event: 18
- Fork event: 3
Committers
Last synced: 9 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Dobiasd | e****m@g****m | 1,273 |
| Keith Chugg | c****g@u****u | 76 |
| Dobiasd | h****y@d****e | 72 |
| n-Guard | t****t@g****e | 22 |
| PhilippKopp | p****p@k****e | 17 |
| Levente Hunyadi | l****e@h****u | 11 |
| Patrik Huber | p****r@g****m | 11 |
| Levente Hunyadi | l****i@i****m | 11 |
| David Hirvonen | d****n@e****m | 5 |
| Till Engert | t****t@T****x | 4 |
| Keith Chugg | k****h@t****m | 4 |
| Henry Schreiner | H****I@g****m | 3 |
| alesapin | a****n@g****m | 3 |
| Yibo Guo | y****o@i****m | 2 |
| Chammika Mannakkara | c****a@b****p | 2 |
| Giulio Romualdi | g****i@g****m | 2 |
| danimtb | d****e@g****m | 2 |
| CrikeeIP | i****f@m****g | 1 |
| Fadi-B | 6****B | 1 |
| Ivanov Valeriy | w****9@g****m | 1 |
| Kevin Mader | k****r | 1 |
| Marco Bonelli | m****b@g****m | 1 |
| Tom Rix | t****x@r****m | 1 |
| Boris Kuznetsov | b****v@n****e | 1 |
| Mathis Logemann | m****e@g****m | 1 |
| Noe Casas | n****s@g****m | 1 |
| SiriusWilliam | 6****m | 1 |
| Vesa Norilo | v****o@g****m | 1 |
| jehanyang | j****n@b****u | 1 |
| mpariente | p****l@g****m | 1 |
| and 5 more... | ||
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 98
- Total pull requests: 79
- Average time to close issues: about 1 month
- Average time to close pull requests: 5 days
- Total issue authors: 71
- Total pull request authors: 13
- Average comments per issue: 5.95
- Average comments per pull request: 0.35
- Merged pull requests: 75
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 16
- Pull requests: 19
- Average time to close issues: 14 days
- Average time to close pull requests: about 11 hours
- Issue authors: 8
- Pull request authors: 2
- Average comments per issue: 2.5
- Average comments per pull request: 0.0
- Merged pull requests: 16
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- rep-stosw (6)
- Dobiasd (4)
- SwatikSahoo (3)
- Alex555github (3)
- raphael10-collab (3)
- SEEYOU20 (2)
- cemox35 (2)
- Hongpeng1992 (2)
- XiujianLiu (2)
- TrueWodzu (2)
- Gabit07 (2)
- dasmysh (2)
- latinusveridas (2)
- Guemann-ui (2)
- AngryLoki (2)
Pull Request Authors
- Dobiasd (71)
- GiulioRomualdi (2)
- Fadi-B (1)
- sirius-william (1)
- mathisloge (1)
- patrikhuber (1)
- trixirt (1)
- mebeim (1)
- jehanyang (1)
- AngryLoki (1)
- noe (1)
- tjansse2 (1)
- slibay (1)
Top Labels
Issue Labels
Pull Request Labels
Packages
- Total packages: 4
- Total downloads: unknown
-
Total dependent packages: 0
(may contain duplicates) -
Total dependent repositories: 0
(may contain duplicates) - Total versions: 200
proxy.golang.org: github.com/Dobiasd/frugally-deep
- Documentation: https://pkg.go.dev/github.com/Dobiasd/frugally-deep#section-documentation
- License: mit
-
Latest release: v0.18.2
published 9 months ago
Rankings
proxy.golang.org: github.com/dobiasd/frugally-deep
- Documentation: https://pkg.go.dev/github.com/dobiasd/frugally-deep#section-documentation
- License: mit
-
Latest release: v0.18.2
published 9 months ago
Rankings
conda-forge.org: frugally-deep
- Homepage: https://github.com/Dobiasd/frugally-deep
- License: MIT
-
Latest release: 0.15.19
published over 3 years ago
Rankings
spack.io: frugally-deep
A lightweight header-only library for using Keras (TensorFlow) models in C++.
- Homepage: https://github.com/Dobiasd/frugally-deep
- License: []
-
Latest release: 0.18.2
published 9 months ago
Rankings
Dependencies
- actions/checkout main composite
- ubuntu 20.04 build