This code is to port Keras neural network model to C++. Neural networks architecture is saved in a JSON file and weights are stored in HDF5 format. The saved model is then loaded and dumped to .dat file, which will be used in cpp file. As of now the it supports Dense and Activation layers only. Also you have to add activation as a new layer instead of passing it as a parameter to Dense layer. Major activation functions like relu, softmax, sigmoid, linear, softplus, softsign, etc
has been implemented. You can add implementation to any other activation function or any other layer as you wish in future.
Also the code works for binary classification only as it was my only requirement at that time. But it can be easily extended for multiclass classification.
It is working with the Tensorflow backend.
- Save your network weights and architecture using
save_model.py
script. - Dump network structure to plain text file with
dump_to_cpp.py
script. - Use network with code from
predict.h
andpredict.cc
files - seetest_run.sh
.
- Run
save_model.py
script. It will produce files with architecturearch.json
and weights in HDF5 formatweights.h5
. - Dump network to dat file
python dump_to_cpp.py -a arch.json -w weights.h5 -o dumped_nn.dat
. - Run
test_keras.py -a arch.json -w weights.h5 -o input.dat
. It will output the predict and actual class based on data ininput.dat
file. - Compile example
g++ test_main.cpp predict.cpp
- see code intest_main.cpp
. - Run binary
./a.out dumped_nn.dat input.dat
- you should get the same output as in step one from Keras.
If you want to test dumping for your network, please use test_run.sh
script. Please provide there your network architecture and weights. The script do the following job:
- Dump network into text file.
- Generate random sample and save in inout.dat. First line contains number of features in input, next line contains features and last line contains actual class.
- Compute predictions from keras and cpp on generated sample.
- Compare predictions.