fbasatemur / KerasToCpp

You can test Keras training weights in CPP environment with CPU or GPU

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

KerasToCpp

You may want to test a model you created using Keras in a C++ environment. This place was created to solve this problem. You can save the training weights of the model you created using Keras and then use it in the C++ environment. All layer and activation functions are designed to be run on CPU or GPU. You can choose the platform you want.

You can access the sample project created for the CPU here : Surface_Detection

You can access the sample project created for the GPU here : Image_Quality_Assessment_with_ANN

Requirements for GPU

You will do your testing in parallel using the GPU. Therefore, you will need the following device and driver requirements.

Design goals for CPU

  • Compatibility with networks generated by Keras using TensorFlow backend.
  • CPU only.
  • No external dependencies, standard library, C++17.

How to use Keras model weights in the C environment ?

Keras weights are in hdf5 file format. I assume you got the model record as .json and .h5. You can create your model training weights as follows:

# keras library import  for Saving and loading model and weights

from keras.models import model_from_json
from keras.models import load_model

# serialize model to JSON
#  the keras model which is trained is defined as 'model' in this example
model_json = model.to_json()

with open("model_save_json.json", "w") as json_file:
    json_file.write(model_json)

# serialize weights to HDF5
model.save_weights("model_save_weight.h5")

It is converted to a text file for use with the C environment. You can do it as follows:

python h5_to_txt.py model_save_weight.h5

Each layer in the model will be saved in a folder and their weight in it. The text files will then be loaded into the model layers.

About

You can test Keras training weights in CPP environment with CPU or GPU

License:MIT License


Languages

Language:C++ 80.1%Language:Cuda 11.0%Language:Python 7.4%Language:C 1.4%