首页 > 解决方案 > 将 tensorflow 2.0 估计器转换为 tensorflow lite

问题描述

导出我的 saved_model.pb 文件后,我想在 TfLite 中转换它。

我和这篇文章有同样的错误。所以我尝试了建议的答案:

import tensorflow as tf

export_dir = './saved/1587630121'

# Convert the model.
saved_model_obj = tf.saved_model.load(export_dir="saved/1587630121/")
concrete_func = saved_model_obj.signatures['serving_default']

#this line produce the error: ValueError: This converter can only convert a single
#ConcreteFunction. Converting multiple functions is under development. :
#converter = tf.lite.TFLiteConverter.from_saved_model(export_dir)

converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func])
print(saved_model_obj.signatures.keys())
# converter.optimizations = [tf.lite.Optimize.DEFAULT]
#converter.experimental_new_converter = True

tflite_model = converter.convert()

但是,我有一个新错误:

打印输出:

keysView(_SignatureMap({'predict': <tensorflow.python.eager.wrap_function.WrappedFunction     object at 0x7f6a9843cc90>, 
'classification':     <tensorflow.python.eager.wrap_function.WrappedFunction object at 0x7f6a981a6190>,
 'regression': <tensorflow.python.eager.wrap_function.WrappedFunction object at 0x7f6a74f78190>, 
'serving_default': <tensorflow.python.eager.wrap_function.WrappedFunction object at 0x7f6a74d020d0>}))

converter.convert() 上的输出错误:

Exception has occurred: ConverterError 
2020-04-23 12:05:52.644550: I tensorflow/lite/toco/import_tensorflow.cc:193] Unsupported data type in placeholder op: 20 
2020-04-23 12:05:52.644632: F tensorflow/lite/toco/import_tensorflow.cc:2690] 
Check failed: status.ok() 
Input_content string_val doesn't have the right dimensions for this string tensor   
 (while processing node 'head/AsString') 
Fatal Python error: Aborted

WARNING:tensorflow:Issue encountered when serializing variables.
Type is unsupported, or the types of the items don't match field type in CollectionDef. 
Note this is a warning and probably safe to ignore.
to_proto not supported in EAGER mode.

以防万一,由于以下代码,我毫无问题地导出了我的 DNNclassifier 估计器:

serving_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(tf.feature_column.make_parse_example_spec(self.feature_columns))
export_path = self.model_trained.export_saved_model(export_dir, serving_input_fn)

我想知道我的错误是否是因为我的 .pb 文件导出错误,这可能吗?否则您有解决此错误的想法吗?

谢谢您的帮助。

标签: tensorflowtensorflow2.0tensorflow-litetensorflow-estimator

解决方案


推荐阅读