Keras model to tflite

I've got a keras model in hdf5 file and wanted to convert it into tflite, but I've got an issue like this

model = 'model.h5'
converter = tf.lite.TFLiteConverter.from_keras_model_file(model)
tflite_model = converter.convert()
open("converted/model.tflite", "wb").write(tflite_model)

ValueError: Unknown loss function:custom_loss

I know that I should load my custom loss as custom_object, but I can't find a way to it with tensorflow function. What is the solution for this?

tensorflowkerash5tflitetensorflow-lite
3 votesLP185.00
1 Answers
JW229.00
1

Currently tensorflow (until v1.13.*) does not let you to pass custom_objects through their api. If you take a look at the code, you will see

...
_keras.backend.clear_session()
_keras.backend.set_learning_phase(False)
keras_model = _keras.models.load_model(model_file)

The issue related to this is already opened and currently is in process of review.

https://github.com/tensorflow/tensorflow/pull/24394

Reply
Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?