TensorFlowLiteTensor形状没有意义(输入:[1,1,1,3])

t8e9dugd  于 6个月前  发布在  其他
关注(0)|答案(1)|浏览(93)

我用这个step-by-step tutorial训练了一个对象检测模型,它检测一个类:person。它也很好用,找到各种形状和大小的人,不再把我的画架标记为person。与我在某个地方找到的一些普通的python tf推理运行器配合得很好。
现在我想在我的手机上使用它。所以我使用默认的model converter tutorial来做这件事,只是为了意识到显然在我的模型中有一个叫做“StridedSlice”的操作符不能转换为tflite,所以我使用select tensorflow model operators tutorial来转换它,包括这个操作符,并将所需的库添加到应用程序中,这基本上是默认的示例应用程序,带有检测.tflite:

implementation 'org.tensorflow:tensorflow-lite-select-tf-ops:2.8.0'

字符串
我尝试了detect.tflite,以确保这不会破坏任何东西,它仍然像一个魅力。
问题是:在tflite看来,我的模型现在需要一个[1,1,1,3]Tensor作为输入,这一点得到了Netron的证实(这要归功于Alex K.的answer):


的数据
因此,似乎确实由于某种原因,我的模型接受了一个[1,1,1,3]输入Tensor,并提供了几个输出Tensor,这些Tensor的形状我不理解,遗憾的是没有任何关于输出含义的描述。此外,我不能强制它做任何事情,相反,它崩溃了,正如正确的静态类型语言所期望的那样:

Cannot copy to a TensorFlowLite tensor (serving_default_input_tensor:0) with 3 bytes from a Java Buffer with 1228800 bytes.


因为当然我的缓冲区的大小不是(1 * 1 * 1 * 3),因为这没有任何意义。相反,我的缓冲区使用640的输入大小,但不是因为我确定这是正确的大小,而是因为我猜到了,而且我的pipeline.config也有这个。

image_resizer {
  fixed_shape_resizer {
    height: 640
    width: 640
  }
}


但是由于某些原因,当使用普通的python tf推理运行器时,我可以把任何图像都扔进去(同样的脚本在我笔记本电脑的集成摄像头和realsense 3d cam上都能工作,当然笔记本电脑的分辨率很差)。
我哪里做错了?转换到tflite,它显然丢失了有关输入的信息?在应用程序中的使用,也许我必须告诉它不要听所谓的[1,1,1,3]输入形状?完全在其他地方?

cld4siwp

cld4siwp1#

我也是在遵循分步教程的过程中遇到这个问题的。经过调查,我设法将输入改为[1,320,320,4]。
要解决此问题,在分步教程的导出步骤中,您必须将代码从以下位置更改为:

python .\exporter_main_v2.py --input_type image_tensor --pipeline_config_path .\models\my_ssd_resnet50_v1_fpn\pipeline.config --trained_checkpoint_dir .\models\my_ssd_resnet50_v1_fpn\ --output_directory .\exported-models\my_model

字符串
致:

python .\export_tflite_graph_tf2.py --pipeline_config_path .\models\my_ssd_resnet50_v1_fpn\pipeline.config --trained_checkpoint_dir .\models\my_ssd_resnet50_v1_fpn\ --output_directory .\exported-models\my_model


注意:* export_tflite_graph_tf2位于tensorflow\models\research\object_detection中 *
之后,您可以导出此保存的模型。
你可以使用下面的python脚本来验证保存的模型输入:

import tensorflow as tf

interpreter = tf.lite.Interpreter([Your tflite Model])
print(interpreter.get_input_details())

"""expected result: [{'name': 'serving_default_input:0', 'index': 0, 'shape': array([  1, 320, 320,   3]), 'shape_signature': array([  1, 320, 320,   3]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]"""

相关问题