Tensorflow hub.load 模型到 TFLite [英] Tensorflow hub.load Model to TFLite
问题描述
我正在尝试将加载了 hub.load 的模型转换为 TFLite.有问题的模型是在 https://tfhub 找到的通用句子编码器 (4).dev/google/universal-sentence-encoder/4我在 Python 中尝试使用 Tensorflow 2.1.0 和 2.2.0 版
I am trying to convert a model loaded with hub.load to TFLite. The model in question is universal-sentence-encoder (4) found at https://tfhub.dev/google/universal-sentence-encoder/4 I tried in Python with Tensorflow version 2.1.0 and 2.2.0
import tensorflow as tf
import tensorflow_hub as hub
model = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")
converter = tf.lite.TFLiteConverter.from_keras_model(model )
converter.experimental_new_converter = True // tried with and without
tflite_model = converter.convert()
我收到以下错误:
converter = tf.lite.TFLiteConverter.from_keras_model(model)
File "...\lib\site-packages\tensorflow_core\lite\python\lite.py", line 394, in from_keras_model
if not isinstance(model.call, _def_function.Function):
AttributeError: '_UserObject' object has no attribute 'call'
根据我的理解,hub.load 返回一个 keras SavedModel,所以不应该立即转换吗?
From my understanding hub.load return a keras SavedModel, so shouldn't be convertible right away?
推荐答案
尝试使用 hub.KerasLayer
将您的模型加载到 tf.keras.Model
中,然后进行转换使用 .from_keras_model
将其转换为 ŧflite
.
Try using hub.KerasLayer
to load your model into a tf.keras.Model
and then convert it to ŧflite
using .from_keras_model
.
没有keras SavedModel"这样的东西.有 SavedModel
,它是 .pb
文件 + assets
文件夹 + variables
文件夹.它就像一种文件格式,一种存储模型的方式.它与内存中的 tf.keras.Model
s 无关.hub.load
不返回 tf.keras.Model
,而是可以保存为 SavedModel
文件格式的最通用的东西",即一个 _UserObject
.这是因为您可以在 SavedModel
s 文件格式中保存除 tf.keras.Models
s 之外的其他内容.
There's no such thing as a "keras SavedModel". There's the SavedModel
, which is .pb
file + assets
folder + variables
folder. It's like a file format, a way to store your model. It has nothing to do with the in memory tf.keras.Model
s. hub.load
does not return a tf.keras.Model
, but rather "the most generic thing" you can save in the SavedModel
file format, namely a _UserObject
. This is because you can save other things than just tf.keras.Models
s in a SavedModel
s file format.
我知道这不是您的问题,但是如果您确实想在加载后取回 tf.keras.Model
,您可以使用 tf.keras.save_model
来保存它.然后它会在使用 tf.saved_model.load
加载后作为 tf.keras.Model
返回(所以它不再是最通用的东西).
I know this was not your question, but if you do want to get your tf.keras.Model
back after loading, you can use tf.keras.save_model
to save it. Then it will come back as a tf.keras.Model
after loading using tf.saved_model.load
(so then it's no longer the most generic thing).
只是代码:
import tensorflow as tf
import tensorflow_hub as hub
model = tf.keras.Sequential()
model.add(tf.keras.layers.InputLayer(dtype=tf.string, input_shape=()))
model.add(hub.KerasLayer("https://tfhub.dev/google/universal-sentence-encoder/4"))
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
哪个有效(它开始转换),但你得到一个:
which works (it starts converting), but you get a:
2020-05-05 10:48:44.927433: I tensorflow/lite/toco/import_tensorflow.cc:659] Converting unsupported operation: StatefulPartitionedCall
所以这是将以SavedModel
格式保存的模型转换为tflite
的代码,但是你得到一个google-universal-句子编码器
特定错误.不知道如何解决这个棘手的问题.
So this is the code to convert models saved in SavedModel
format to tflite
, but you get a google-universal-sentence-encoder
specific error. No idea how to fix that tough.
这篇关于Tensorflow hub.load 模型到 TFLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!