在Google Colab python中使用TFLiteConverter.from_keras_model("model.h5")将Keras的 "model.h5 "转成 "model.h5 "时遇到麻烦。

问题描述 投票:0回答:1

在谷歌Colab python中转Keras时遇到了麻烦。model.h5 使用 TFLiteConverter.from_keras_model("model.h5")

我使用的是TensorFlow 2.2.0。

第一次尝试脚本

import tensorflow as tf 

converter = tf.lite.TFLiteConverter.from_keras_model("model.h5")

tflite_model = converter.convert()

open("Robot_Eyes.tflite", "wb").write(tflite_model)

和错误得到了

AttributeError                            Traceback (most recent call last)

<ipython-input-22-e8e8d3a7c79e> in <module>()

13 #new_model= tf.keras.models.load_model(filepath="model.h5")

14 

--->15 converter = tf.lite.TFLiteConverter.from_keras_model("model.h5")

16 

17 tflite_model = converter.convert()

/usr/local/lib/python3.6/dist-packages/tensorflow/lite/python/lite.py in from_keras_model(cls, model)

426     # to None.

427     # Once we have better support for dynamic shapes, we can remove this.

-->428     if not isinstance(model.call, _def_function.Function):

429       # Pass `keep_original_batch_size=True` will ensure that we get an input

430       # signature including the batch dimension specified by the user.

AttributeError: 'str' object has no attribute 'call'

第二次尝试

import tensorflow as tf 

new_model= tf.keras.models.load_model(filepath="model.h5")

converter = tf.lite.TFLiteConverter.from_keras_model(new_model)

tflite_model = converter.convert()

open("model.tflite", "wb").write(tflite_model)

而第二次错误得到了

OSError                                   Traceback (most recent call last)

<ipython-input-23-2344f7b515a1> in <module>()

11 import tensorflow as tf

12 

--->13 new_model= tf.keras.models.load_model(filepath="model.h5")

14 

15 converter = tf.lite.TFLiteConverter.from_keras_model(new_model)

3 frames

/usr/local/lib/python3.6/dist-packages/h5py/_hl/files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)

171         if swmr and swmr_support:

172             flags |= h5f.ACC_SWMR_READ

-->173         fid = h5f.open(name, flags, fapl=fapl)

174     elif mode == 'r+':

175         fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper()

h5py/h5f.pyx in h5py.h5f.open()

OSError: Unable to open file (truncated file: eof = 40894464, sblock->base_addr = 0, stored_eof = 159686248)

不知道是哪里出了问题,我一直在尝试解决。谢谢大家的帮助。

python-3.x keras model tensorflow2.0 tensorflow-lite
1个回答
0
投票

得到了它的工作

!pip install tensorflow==1.15.0

import tensorflow as tf

print(tf.__version__)

converter = tf.lite.TFLiteConverter.from_keras_model_file("robot_eyes2.h5")

tflite_model = converter.convert()

open("Robot_Eyes.tflite", "wb").write(tflite_model)
© www.soinside.com 2019 - 2024. All rights reserved.