frugally-deep

A lightweight header-only library for using Keras (TensorFlow) models in C++.

https://github.com/dobiasd/frugally-deep

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

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

Keywords from Contributors

c-plus-plus-17 composition range stl
Last synced: 6 months ago · JSON representation ·

Repository

A lightweight header-only library for using Keras (TensorFlow) models in C++.

Basic Info
  • Host: GitHub
  • Owner: Dobiasd
  • License: mit
  • Language: C++
  • Default Branch: master
  • Homepage:
  • Size: 3.41 MB
Statistics
  • Stars: 1,110
  • Watchers: 49
  • Forks: 238
  • Open Issues: 7
  • Releases: 97
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
Created over 9 years ago · Last pushed 8 months ago
Metadata Files
Readme Funding License Citation

README.md

logo

CI (License MIT 1.0)

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, Dot
  • AveragePooling1D/2D/3D, GlobalAveragePooling1D/2D/3D
  • TimeDistributed
  • Conv1D/2D, SeparableConv2D, DepthwiseConv2D
  • Conv1DTranspose, Conv2DTranspose
  • Cropping1D/2D/3D, ZeroPadding1D/2D/3D, CenterCrop
  • BatchNormalization, Dense, Flatten, Normalization
  • Dropout, AlphaDropout, GaussianDropout, GaussianNoise
  • SpatialDropout1D, SpatialDropout2D, SpatialDropout3D
  • ActivityRegularization, LayerNormalization, UnitNormalization
  • RandomContrast, RandomFlip, RandomHeight
  • RandomRotation, RandomTranslation, RandomWidth, RandomZoom
  • MaxPooling1D/2D/3D, GlobalMaxPooling1D/2D/3D
  • UpSampling1D/2D, Resizing, Rescaling
  • Reshape, Permute, RepeatVector
  • Embedding, CategoryEncoding
  • Attention, 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{1, 2, 3, 4})}); std::cout << fdeep::show_tensors(result) << std::endl; } ```

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

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

All Time
  • Total Commits: 1,538
  • Total Committers: 35
  • Avg Commits per committer: 43.943
  • Development Distribution Score (DDS): 0.172
Past Year
  • Commits: 20
  • Committers: 1
  • Avg Commits per committer: 20.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email 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...

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
help wanted (3)
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
  • Versions: 95
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 7.0%
Average: 8.2%
Dependent repos count: 9.3%
Last synced: 6 months ago
proxy.golang.org: github.com/dobiasd/frugally-deep
  • Versions: 95
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 7.0%
Average: 8.2%
Dependent repos count: 9.3%
Last synced: 6 months ago
conda-forge.org: frugally-deep
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Forks count: 11.1%
Stargazers count: 12.1%
Average: 27.1%
Dependent repos count: 34.0%
Dependent packages count: 51.2%
Last synced: 6 months ago
spack.io: frugally-deep

A lightweight header-only library for using Keras (TensorFlow) models in C++.

  • Versions: 9
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent repos count: 0.0%
Average: 27.5%
Dependent packages count: 55.0%
Last synced: 6 months ago

Dependencies

.github/workflows/ci.yml actions
  • actions/checkout main composite
test/Dockerfile docker
  • ubuntu 20.04 build