serizba / cppflow

Run TensorFlow models in C++ without installation and without Bazel
https://serizba.github.io/cppflow/
MIT License
787 stars 179 forks source link

Save the model in the `saved_model` format. #265

Closed alex-thk closed 3 weeks ago

alex-thk commented 3 weeks ago
          Save the model in the `saved_model` format.

This can be done by doing model.save("model_name"). Tensorflow will save the model in the saved_model format if you don't put .h5 in "model_name".

Originally posted by @yochananscharf in https://github.com/serizba/cppflow/issues/219#issuecomment-1290196967

Sorry, I am having a similar problem. When I try to save this dummy model:

import tensorflow as tf

input = tf.keras.Input(shape=(5,))

output = tf.keras.layers.Dense(5, activation=tf.nn.relu)(input)
output = tf.keras.layers.Dense(1, activation=tf.nn.sigmoid)(output)
model = tf.keras.Model(inputs=input, outputs=output)

model.compile()

model.save('model')

I get ths error:

ValueError: Invalid filepath extension for saving. Please add either a `.keras` extension for the native Keras format (recommended) or a `.h5` extension. Use `model.export(filepath)` if you want to export a SavedModel for use with TFLite/TFServing/etc. Received: filepath=model.

If I actually save it with a .h5 or a .keras extension at the end I get an error at runtime in the cpp script that is like:

2024-11-02 17:35:43.198951: I tensorflow/cc/saved_model/reader.cc:83] Reading SavedModel from: complex_model.keras
2024-11-02 17:35:43.198995: I tensorflow/cc/saved_model/loader.cc:466] SavedModel load for tags { serve }; Status: fail: NOT_FOUND: Could not find SavedModel .pb or .pbtxt at supplied export directory path: complex_model.keras. Check that the directory exists and that you have the right permissions for accessing it.. Took 49 microseconds.
terminate called after throwing an instance of 'std::runtime_error'
  what():  Could not find SavedModel .pb or .pbtxt at supplied export directory path: complex_model.keras. Check that the directory exists and that you have the right permissions for accessing it.
Aborted (core dumped)
alex-thk commented 3 weeks ago

OK, solved it by using

model .export('model') 

in python and passing the model path via argv[] as an argument to main in cpp program.