将保存的tensorflow模型转换为tensorflow Lite的正确方法是什么

将保存的tensorflow模型转换为tensorflow Lite的正确方法是什么,tensorflow,tensorflow2.0,tensorflow-lite,Tensorflow,Tensorflow2.0,Tensorflow Lite,我有一个保存的tensorflow模型,与中的所有模型相同 我想将其转换为tesorflow lite,我从tensorflow github找到以下方法(我的tensorflw版本是2): 但是转换模型的输出和输入形状与原始模型不匹配,请检查以下内容: 原始模型输入和输出形状 转换后的模型输入和输出形状 所以这里有个问题!输入/输出形状应与原始模型匹配! 有什么想法吗?模型输入和输出的形状应与下图相同 如果模型已采用保存的模型格式,请输入以下代码 # if you are usin

我有一个保存的tensorflow模型,与中的所有模型相同

我想将其转换为tesorflow lite,我从tensorflow github找到以下方法(我的tensorflw版本是2):

但是转换模型的输出和输入形状与原始模型不匹配,请检查以下内容:

  • 原始模型输入和输出形状

  • 转换后的模型输入和输出形状

所以这里有个问题!输入/输出形状应与原始模型匹配!
有什么想法吗?

模型输入和输出的形状应与下图相同

如果模型已采用保存的模型格式,请输入以下代码

# if you are using same model
export_dir = 'ssd_mobilenet_v2_320x320_coco17_tpu-8/saved_model'
converter = tf.lite.TFLiteConverter.from_saved_model(export_dir)
如果您的模型采用Keras格式,请使用以下格式

# if it's a keras model 
model = tf.keras.applications.MobileNetV2(weights="imagenet", input_shape= (224, 224, 3))
converter = tf.lite.TFLiteConverter.from_keras_model(model)
在这两种情况下,目的都是获得转换器

我没有保存的_模型,因此我将使用keras模型并将其转换为保存的_模型格式—仅以keras模型格式为例

import pathlib #to use path
model = tf.keras.applications.MobileNetV2(weights="imagenet", input_shape= (224, 224, 3))
export_dir = 'imagenet/saved_model'
tf.saved_model.save(model, export_dir) #convert keras to saved model

converter = tf.lite.TFLiteConverter.from_saved_model(export_dir)
converter.optimizations = [tf.lite.Optimize.DEFAULT]  #you can also optimize for size or latency OPTIMIZE_FOR_SIZE, OPTIMIZE_FOR_LATENCY
tflite_model = converter.convert()

#save the model
tflite_model_file = pathlib.Path('m.tflite')
tflite_model_file.write_bytes(tflite_model)

tflite_interpreter = tf.lite.Interpreter(model_path= 'm.tflite') #you can load the content with model_content=tflite_model

# get shape of tflite input and output
input_details = tflite_interpreter.get_input_details()
output_details = tflite_interpreter.get_output_details()
print("Input: {}".format( input_details[0]['shape']))
print("Output:{}".format(output_details[0]['shape']))

# get shape of the origin model
print("Input:  {}".format( model.input.shape))
print("Output: {}".format(model.output.shape))
对于tflite:我有这个

对于原始型号,我有这个


您将看到
tflite
keras
模型的形状是相同的

从Tensorflow github问题中,我使用了他们的答案来解决我的问题。

他们的做法:

!pip install tf-nightly
import tensorflow as tf

## TFLite Conversion
model = tf.saved_model.load("saved_model")
concrete_func = model.signatures[tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY]
concrete_func.inputs[0].set_shape([1, 300, 300, 3])
tf.saved_model.save(model, "saved_model_updated", signatures={"serving_default":concrete_func})
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir='saved_model_updated', signature_keys=['serving_default'])

converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()

## TFLite Interpreter to check input shape
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test the model on random input data.
input_shape = input_details[0]['shape']
print(input_shape)
[13003]


谢谢

只需重新调整输入张量即可

您可以使用
resize\u tensor\u input
功能,如下所示:

interpreter.resize_tensor_input(input_index=0, tensor_size=[1, 640, 640, 3])

现在您输入的形状将是:
[1,640,640,3]

我的模型已保存\u model.pb而不是model.h5,因此它不是keras model编辑我的问题并添加模型链接,请确认您是否获得匹配的输入输出@H.H。保存的\u模型似乎有问题。我得到这个
只有在第一维中才支持。张量“input_Tensor”的形状“[1,None,None,3]”无效。
我尝试了另一个测试保存的模型,效果良好,因此我更新为代码,将Keras模型转换为保存的_模型,并使用保存的_模型版本。它仍然像预期的那样工作。因此,您使用的下载保存的模型格式有问题。您可以尝试使用此模型吗调用
解释器.invoke()
时会发生什么情况?我得到
运行时错误:每个步骤的容器\u0不存在。(找不到资源:_per_step_0/_tensor_arraysTensorArrayV3_0)(通过Eager执行'TensorArrayScatterV3'时)237号节点(TfLiteFlexDelegate)调用失败tflite
模型时,在
解释器上调用
invoke()
后进行编码。尽管已经提供了
tflite
models(from),但不要抛出此错误。我已经转换了一个基于定制数据培训的
ssd\u mobilenet\u v2\u coco\u 2018\u 03\u 29
模型。
!pip install tf-nightly
import tensorflow as tf

## TFLite Conversion
model = tf.saved_model.load("saved_model")
concrete_func = model.signatures[tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY]
concrete_func.inputs[0].set_shape([1, 300, 300, 3])
tf.saved_model.save(model, "saved_model_updated", signatures={"serving_default":concrete_func})
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir='saved_model_updated', signature_keys=['serving_default'])

converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()

## TFLite Interpreter to check input shape
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test the model on random input data.
input_shape = input_details[0]['shape']
print(input_shape)
interpreter.resize_tensor_input(input_index=0, tensor_size=[1, 640, 640, 3])