首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将tensorflow-hub模型转换为tensorflow lite(tflite)

将tensorflow-hub模型转换为tensorflow lite(tflite)
EN

Stack Overflow用户
提问于 2022-01-28 09:43:32
回答 1查看 187关注 0票数 1

我试图使用以下代码将tensorflow-hub(.pb)中的.pb模型转换为TensorFlow Lite文件(.tflite):

代码语言:javascript
复制
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import numpy as np
import tensorflow_hub as hub

module_path = 'https://tfhub.dev/deepmind/biggan-deep-256/1'
tf.compat.v1.reset_default_graph()
print('Loading BigGAN module from:', module_path)
module = hub.Module(module_path)
dummy_inputs = {
    "y": tf.compat.v1.placeholder(tf.float32, [1, 1000], 'y'),
    "z": tf.compat.v1.placeholder(tf.float32, [1, 128], 'z'),
    "truncation": tf.compat.v1.placeholder(tf.float32, [], 'truncation'),
}
dummy_output = module(dummy_inputs)
print('dummy_Inputs:\n', '\n'.join(
    '  {}: {}'.format(*kv) for kv in dummy_inputs.items()))
print('dummy_Output:', dummy_output)

initializer = tf.global_variables_initializer()
sess = tf.Session()
sess.run(initializer)

save_path = "./big_gan.tflite"
_input = [dummy_inputs[k] for k in dummy_inputs]
converter = tf.lite.TFLiteConverter.from_session(sess, _input, [dummy_output])
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.experimental_new_converter = True

# errors here
tflite_model = converter.convert()

open(save_path, "wb").write(tflite_model)

返回的信息:

代码语言:javascript
复制
2022-01-28 17:10:30.240145: E tensorflow/core/grappler/grappler_item_builder.cc:670] Init node AssignVariableOp_1003 doesn't exist in graph
2022-01-28 17:10:33.853842: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:363] Ignored output_format.
2022-01-28 17:10:33.853899: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:366] Ignored drop_control_dependency.

抛出错误:

代码语言:javascript
复制
ConverterError: Input 0 of node module_apply_default/cond/AssignVariableOp was passed float from module_apply_default/cond/AssignVariableOp/Switch:0 incompatible with expected resource.

该模块按照“示例使用”的要求工作,可以正常生成图像。

有人能帮助我理解Init node AssignVariableOp_1003 doesn't exist in graph的含义以及如何修复错误吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-28 10:38:31

使用TF 2.xTF 1.x模型转换为TensorFlow Lite文件是很棘手的。我建议在Google上运行您的代码示例并切换到TF 1.x

代码语言:javascript
复制
%tensorflow_version 1.x

好像很管用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70891625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档