簡體   English   中英

將 tensorflow-hub model 轉換為 tensorflow lite(tflite)

[英]Convert tensorflow-hub model to tensorflow lite(tflite)

我試圖使用以下代碼將 tensorflow-hub(.pb) 中的 BigGAN model 轉換為 TensorFlow Lite 文件 (.tflite):

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)

返回信息:

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.

拋出錯誤:

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什么意思以及如何修復錯誤?

使用TF 2.xTF 1.x model 轉換為 TensorFlow Lite 文件非常棘手。 我建議在 Google Colab 上運行您的代碼示例並切換到TF 1.x

%tensorflow_version 1.x

它似乎工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM