构建 TensorFlow Lite 后运行 label_image 时出错:tensorflow_src/tensorf low/lite/util.cc BytesRequired 元素数量溢出

问题描述 投票:0回答:1

我按照以下方式构建了 TensorFlow Lite:https://www.tensorflow.org/lite/guide/build_cmake 在 Ubuntu 22.4 中的 x86_64 上

编译器版本是

$ cc --version
cc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0

我尝试着奔跑

tflite_build/examples/label_image/label_image \
  --tflite_model=mobilenet_v1_1.0_224_quant.tflite \
  --labels=labels_mobilenet_quant_v1_224.txt \
  --image=label_image_test.jpg

tflite 文件获取自 https://www.tensorflow.org/lite/examples/image_classification/overview。直接链接是https://storage.googleapis.com/download.tensorflow.org/models/tflite/mobilenet_v1_1.0_224_quant_and_labels.zip

输出:

INFO: Loaded model mobilenet_v1_1.0_224_quant.tflite
INFO: resolved reporter
INFO: Initialized TensorFlow Lite runtime.
INFO: Applying 1 TensorFlow Lite delegate(s) lazily.
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
VERBOSE: Replacing 29 out of 31 node(s) with delegate (TfLiteXNNPackDelegate) no
de, yielding 4 partitions for the whole graph.
INFO: Successfully applied the default TensorFlow Lite delegate indexed at 0.
 *NOTE*: because a delegate has been applied, the precision of computations shou
ld be unchanged, but the exact output tensor values may have changed. If such ou
tput values are checked in your code, like in your tests etc., please consider i
ncreasing error tolerance for the check.
ERROR: xxx/tensorflow_src/tensorflow/lite/util.cc BytesRequired number of elements overflowed.

最终,程序出现段错误,这并不奇怪。

我可以在下面运行它而不会出现错误:

tflite_build/tools/benchmark/benchmark_model --graph=mobilenet_v1_1.0_224_quant.tflite

输出:

INFO: STARTING!
WARN: Duplicate flags: num_threads
INFO: Log parameter values verbosely: [0]
INFO: Graph: [mobilenet_v1_1.0_224_quant.tflite]
INFO: Loaded model mobilenet_v1_1.0_224_quant.tflite
INFO: Initialized TensorFlow Lite runtime.
INFO: Applying 1 TensorFlow Lite delegate(s) lazily.
INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
VERBOSE: Replacing 29 out of 31 node(s) with delegate (TfLiteXNNPackDelegate) node, yielding 4 partitions for the whole graph.
INFO: Successfully applied the default TensorFlow Lite delegate indexed at 0.
 *NOTE*: because a delegate has been applied, the precision of computations should be unchanged, but the exact output tensor values may have changed. If such output values are checked in your code, like in your tests etc., please consider increasing error tolerance for the check.
INFO: The input model file size (MB): 4.27635
INFO: Initialized session in 39.149ms.
INFO: Running benchmark for at least 1 iterations and at least 0.5 seconds but terminate if exceeding 150 seconds.
INFO: count=3 first=217309 curr=196488 min=196488 max=217309 avg=203436 std=9809

INFO: Running benchmark for at least 50 iterations and at least 1 seconds but terminate if exceeding 150 seconds.
INFO: count=50 first=198013 curr=192898 min=191722 max=200007 avg=195195 std=2153

INFO: Inference timings in us: Init: 39149, First inference: 217309, Warmup (avg): 203436, Inference (avg): 195195
INFO: Note: as the benchmark tool itself affects memory footprint, the following is only APPROXIMATE to the actual memory footprint of the model at runtime. Take the information at your discretion.
INFO: Memory footprint delta from the start of the tool (MB): init=11 overall=14.125

我可以使用官方 python 模块成功使用相同的 tflite 文件和 jpg 文件。

怎么了?

我确实注意到了这个可疑的设置:

struct Settings {
  bool verbose = false;
  bool accel = false;
  TfLiteType input_type = kTfLiteFloat32;

我相信该模型是 uint8 格式的。我将其修改为使用 kTfLiteUint8。没有观察到差异。没有命令行参数可以更改此设置。不重要吗?

我也没有看到任何明显的分支可以完全禁用 xnnpack。这似乎是纯 CPU 引擎的唯一实现。

tensorflow tensorflow-lite
1个回答
0
投票

原来图像文件输入只接受位图文件。此外,该格式必须是一种非常简单的格式,因为我之前尝试使用工具将 .jpg 转换为 .bmp,但在通道大小上产生了其他错误。阅读tensorflow/lite/examples/label_image/bitmap_helpers.cc中的文件解析代码后,文件格式的简单性显而易见。

我发现存储库中提供了默认的 bmp 测试文件:tensorflow/lite/examples/label_image/testdata/grace_hopper.bmp。使用该文件会产生成功的输出。

<cut>
Node  29 Operator Builtin Code  22 RESHAPE (delegated by node 32)
  2 Input Tensors:[1,5] -> 0B (0.00MB)
  1 Output Tensors:[4] -> 0B (0.00MB)
Node  30 Operator Builtin Code  25 SOFTMAX (not delegated)
  1 Input Tensors:[4] -> 1001B (0.00MB)
  1 Output Tensors:[87] -> 1001B (0.00MB)
Node  31 Operator Custom Name TfLiteXNNPackDelegate 
  55 Input Tensors:[6,8,10-12,14,16-18,20,22-24,26,28-30,32,34-36,38,40-42,44,46-48,50,52-54,56,58-60,62,64-66,68,70-72,74,76-78,80,82-84,86,88] -> 3379392B (3.22MB)
  1 Output Tensors:[31] -> 50176B (0.05MB)
Node  32 Operator Custom Name TfLiteXNNPackDelegate 
  4 Input Tensors:[0,2,3,5] -> 1030060B (0.98MB)
  1 Output Tensors:[4] -> 1001B (0.00MB)

Execution plan as the list of 4 nodes invoked in-order: [31,27,32,30]
Among these nodes in the execution plan:
  Node 31 is a TfLiteXNNPackDelegate node (0x55fbce0959a0), which has delegated 27 nodes: [0-26]
  Node 32 is a TfLiteXNNPackDelegate node (0x55fbce0959a0), which has delegated 2 nodes: [28,29]
<cut>

完整输出存储在此处以供参考:https://docs.google.com/document/d/1VzUpTtTr4FHqUrrZY0diSDn7wFjp8GaPmlLjbDJ-EMY/edit?usp=sharing

© www.soinside.com 2019 - 2024. All rights reserved.