Keras模型hdf5无法转换Tflite

时间:2020-07-12 17:57:27

标签: python-3.x keras tensorflow2.0 tf-lite

我在Google Colab中运行了这段代码,将我的Keras hdf5文件转换为Tflite文件:

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)

运行代码时出现此错误:

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]'.

有人可以告诉我如何解决此问题吗?

1 个答案:

答案 0 :(得分:0)

使用此Google Colab笔记本进行转换。上载.hdf5文件,它将转换为.tflite文件。

链接到笔记本 https://colab.research.google.com/drive/1E0dqzDYszsG2BfyzVWUOdKLElqvMn4oo?usp=sharing

执行完最终代码后,转到文件(最左侧),然后在其中找到.tflite模型

相关问题