【问题标题】:Keras Model hdf5 failing to convert TfliteKeras Model hdf5 无法转换 Tflite
【发布时间】:2020-07-12 17:57:27
【问题描述】:

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

谁能告诉我如何解决这个问题?

【问题讨论】:

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


【解决方案1】:

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

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

最终代码执行完毕后,进入文件(最左边)然后你会在那里找到 .tflite 模型

【讨论】:

    猜你喜欢
    • 2019-09-05
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 2021-12-09
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多