Introduction to frugally-deep
frugally-deep is a powerful and efficient tool that allows developers to use Keras models in C++ with ease. This tool specifically caters to those who want the benefits of advanced machine learning models without the overhead of integrating TensorFlow into their C++ applications. Below is a comprehensive overview of frugally-deep, how it works, and how you can use it.
What is frugally-deep?
frugally-deep is a small, header-only library crafted in modern C++. It allows the use of Keras models, which are typically developed and trained in Python, within a C++ environment for predictions or forward passes. One of its key advantages is that it avoids the need to link C++ applications with TensorFlow, substantially simplifying the integration process and reducing binary sizes.
Why Use frugally-deep?
- Interface Simplicity: frugally-deep is easy to integrate and implements a small subset of TensorFlow necessary for model predictions.
- Lightweight: It avoids additional RAM usage during computations and does not require GPU resources—running efficiently on a single CPU core.
- Smooth Integration: It seamlessly supports various layer types and complex model architectures exported from Keras.
Supported Features
- Wide Layer Support: The library supports an extensive list of layer types such as
Conv1D/2D
,Dense
,BatchNormalization
,Dropout
, andAttention
, among others. - Complex Models: It can handle multiple inputs and outputs, nested models, residual connections, and shared layers. This versatility allows developers to employ a broad range of model architectures.
- Custom Layers: Developers can incorporate custom layers by passing custom factory functions during model loading.
Features Not Supported
While frugally-deep is robust, it does have limitations. It does not currently support certain layers like Conv2DTranspose
, Lambda
, and various RNN-related layers. These exclusions are documented in the FAQ section for clarity.
Usage Guide
The typical workflow with frugally-deep involves these steps:
-
Model Creation and Training: Use Keras in Python to build, train, and test your machine learning model. Once ready, save the model in Keras format with
model.save('your_model.keras')
. -
Model Conversion: Convert the saved Keras model to frugally-deep’s file format using
keras_export/convert_model.py
. This process prepares the model for use in C++. -
Use in C++: Load the converted model in your C++ application with
fdeep::load_model(...)
. You can perform predictions on your data by invokingmodel.predict(...)
.
Example
Here’s a simple illustration of how to transition from Python to C++ with frugally-deep:
-
Python (Model Creation and Training):
from tensorflow.keras.layers import Input, Dense from tensorflow.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([[1, 2, 3, 4], [2, 3, 4, 5]], [[1, 0, 0], [0, 0, 1]], epochs=10) model.save('keras_model.keras')
-
Convert Model:
python3 keras_export/convert_model.py keras_model.keras fdeep_model.json
-
C++ (Loading and Predicting):
#include <fdeep/fdeep.hpp> int main() { const auto model = fdeep::load_model("fdeep_model.json"); const auto result = model.predict( {fdeep::tensor(fdeep::tensor_shape(static_cast<std::size_t>(4)), std::vector<float>{1, 2, 3, 4})}); std::cout << fdeep::show_tensors(result) << std::endl; }
Requirements and Installation
To use frugally-deep, the following are necessary:
- A C++14 compatible compiler (such as GCC 4.9 or Clang 3.7).
- Python 3.7 or later.
- TensorFlow 2.17 (older versions might be compatible).
For installation details, refer to the provided INSTALL.md
guide.
Frequently Asked Questions
The FAQ.md
document offers answers to common questions and additional guidance on using the library.
Future Scope and License
frugally-deep continues to evolve, with potential updates to its API. It welcomes feedback and contributions from the developer community. The library is distributed under the MIT License, encouraging broad use and modification.
frugally-deep is an ideal solution for developers looking to leverage Python-trained models in a C++ environment, offering simplicity and efficiency without the dependencies and complexities associated with TensorFlow.