简体   繁体   中英

Keras Model hdf5 failing to convert Tflite

I ran this piece of code in Google Colab to convert my Keras hdf5 file to a Tflite file:

import tensorflow as tf
keras_model = tf.keras.models.load_model("/content/best_model_11class.hdf5")
converter = tf.lite.TFLiteConverter.from_keras_model(keras_model)
tfmodel = converter.convert()
open ('model.tflite' , "wb") .write(tfmodel)

I get this error when I run the code:

ValueError                                Traceback (most recent call last)
<ipython-input-2-0804f3b57a48> in <module>()
      2 keras_model = tf.keras.models.load_model("/content/best_model_11class.hdf5")
      3 converter = tf.lite.TFLiteConverter.from_keras_model(keras_model)
----> 4 tfmodel = converter.convert()
      5 open ('model.tflite' , "wb") .write(tfmodel)

/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/lite.py in convert(self)
    481               "None is only supported in the 1st dimension. Tensor '{0}' has "
    482               "invalid shape '{1}'.".format(
--> 483                   _get_tensor_name(tensor), shape_list))
    484         elif shape_list and shape_list[0] is None:
    485           # Set the batch size to 1 if undefined.

ValueError: None is only supported in the 1st dimension. Tensor 'input_1' has invalid shape '[None, None, None, 3]'.

Can someone tell me how do I fix this?

Use this Google Colab notebook to convert. Upload the.hdf5 file and it will convert it.tflite file.

Link to notebook https://colab.research.google.com/drive/1E0dqzDYszsG2BfyzVWUOdKLElqvMn4oo?usp=sharing

After the final code executes, go to files( the leftmost side ) then you will find the.tflite model there

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM