将 tf_hub 模型转换为 tflite

问题描述

我正在尝试将 tf_hub 模型 (MUSE) 转换为 tflite,但是我收到了一个非常奇怪的 FileNotFoundError,我不知道如何解释错误消息。这是我的代码错误消息:

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-multilingual/3"))
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.target_spec.supported_ops = [
    tf.lite.OpsSet.TFLITE_BUILTINS,# enable TensorFlow Lite ops.
    tf.lite.OpsSet.SELECT_TF_OPS,# enable TensorFlow ops.
]
tflite_model = converter.convert()

我得到的错误

FileNotFoundError: Op type not registered 'SentencepieceOp' in binary running on hongtao-mac. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib,accessing (e.g.) `tf.contrib.resampler` should be done before importing the graph,as contrib ops are lazily registered when the module is first accessed.
 If trying to load on a different device from the computational device,consider using setting the `experimental_io_device` option on tf.saved_model.LoadOptions to the io_device such as '/job:localhost'.

有什么想法吗?

注意:可能相关的问题:Tensorflow hub.load Model to TFLite

解决方法

https://tfhub.dev/google/universal-sentence-encoder-multilingual/3 处的文档指出,您需要在加载模型之前安装 tensorflow-text 并导入它。 https://www.tensorflow.org/lite/convert 表示建议使用 from_saved_model 而不是 from_keras_model。以下内容在新的 Colab 中对我有用:

!pip install -q tensorflow-text
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_text as text

hub.load("https://tfhub.dev/google/universal-sentence-encoder-multilingual/3")  # Caches the model in /tmp/tfhub_modules
converter = tf.lite.TFLiteConverter.from_saved_model("/tmp/tfhub_modules/26c892ffbc8d7b032f5a95f316e2841ed4f1608c")
converter.target_spec.supported_ops = [
    tf.lite.OpsSet.TFLITE_BUILTINS,# enable TensorFlow Lite ops.
    tf.lite.OpsSet.SELECT_TF_OPS,# enable TensorFlow ops.
]
tflite_file = "model.tflite"
with open(tflite_file,'wb') as f:
  f.write(converter.convert())
interpreter = tf.lite.Interpreter(tflite_file)
interpreter.get_signature_list()  # {'serving_default': {'inputs': ['inputs'],'outputs': ['outputs']}}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...