Python 使用TF2.0将保存的_模型转换为TFLite模型

Python 使用TF2.0将保存的_模型转换为TFLite模型,python,tensorflow,tensorflow2.0,tensorflow-lite,Python,Tensorflow,Tensorflow2.0,Tensorflow Lite,目前,我正在将使用SSD和inception网络训练的自定义对象检测模型转换为量化TFLite模型。使用Tensorflow 1.4,我可以使用以下代码片段将自定义对象检测模型从冻结图转换为量化TFLite模型: 但是,Tensorflow 2.0无法使用tf.lite.TFLiteConverter.from_freezed_图形类方法。因此,我尝试使用tf.lite.TFLiteConverter.from\u saved\u model类方法转换模型。代码片段如下所示: converter

目前,我正在将使用SSD和inception网络训练的自定义对象检测模型转换为量化TFLite模型。使用Tensorflow 1.4,我可以使用以下代码片段将自定义对象检测模型从冻结图转换为量化TFLite模型:


但是,Tensorflow 2.0无法使用tf.lite.TFLiteConverter.from_freezed_图形类方法。因此,我尝试使用tf.lite.TFLiteConverter.from\u saved\u model类方法转换模型。代码片段如下所示:

converter = tf.lite.TFLiteConverter.from_saved_model("/content/") # Path to saved_model directory
converter.optimizations =  [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()
上述代码段引发以下错误:

ValueError: None is only supported in the 1st dimension. Tensor 'image_tensor' has invalid shape '[None, None, None, 3]'.
TypeError: from_saved_model() got an unexpected keyword argument 'input_shapes'
我尝试将输入形状作为参数传递


converter=tf.lite.TFLiteConverter.from_saved_model/content/,input_shapes={image_tensor:[1300300,3]}

但它会抛出以下错误:

ValueError: None is only supported in the 1st dimension. Tensor 'image_tensor' has invalid shape '[None, None, None, 3]'.
TypeError: from_saved_model() got an unexpected keyword argument 'input_shapes'
我错过什么了吗?请随时纠正我

我使用tf.compat.v1.lite.TFLiteConverter.from_freezed_graph获得了解决方案。此compat.v1将TF1.x的功能引入到TF2.x中。 以下是完整代码:

converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph("/content/tflite_graph.pb",input_shapes = {'normalized_input_image_tensor':[1,300,300,3]},
    input_arrays = ['normalized_input_image_tensor'],output_arrays = ['TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1',
    'TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3'])

converter.allow_custom_ops=True

# Convert the model to quantized TFLite model.
converter.optimizations =  [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()


# Write a model using the following line
open("/content/uno_mobilenetV2.tflite", "wb").write(tflite_model)
我使用tf.compat.v1.lite.TFLiteConverter.from_freezed_graph获得了解决方案。此compat.v1将TF1.x的功能引入到TF2.x中。 以下是完整代码:

converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph("/content/tflite_graph.pb",input_shapes = {'normalized_input_image_tensor':[1,300,300,3]},
    input_arrays = ['normalized_input_image_tensor'],output_arrays = ['TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1',
    'TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3'])

converter.allow_custom_ops=True

# Convert the model to quantized TFLite model.
converter.optimizations =  [tf.lite.Optimize.DEFAULT]
tflite_model = converter.convert()


# Write a model using the following line
open("/content/uno_mobilenetV2.tflite", "wb").write(tflite_model)

TFLiteConverter.from_saved_model不支持参数输入_形状。问题是,您是否使用tensorflow2创建了保存的_模型?您好,我使用TensorFlow1.4创建了模型。我在github上提出了一个问题:我是否必须将tensorflow1模型转换为tensorflow2模型,然后再转换为tflite模型?我认为,如果使用tensorflow2重新加载模型,您可以更改输入形状签名-尽管我还没有测试it@edkeveked我得到了一个解决方案并添加了一些内容以供参考。非常感谢。TFLiteConverter.from_saved_model不支持参数输入_形状。问题是,您是否使用tensorflow2创建了保存的_模型?您好,我使用TensorFlow1.4创建了模型。我在github上提出了一个问题:我是否必须将tensorflow1模型转换为tensorflow2模型,然后再转换为tflite模型?我认为,如果使用tensorflow2重新加载模型,您可以更改输入形状签名-尽管我还没有测试it@edkeveked我得到了一个解决方案并添加了一些内容以供参考。非常感谢。