首页 > 解决方案 > 从 C++ 调用 Tensorflow Lite .tflite CNN 模型时的非法指令

问题描述

我正在Illegal Instruction使用以下代码行来调用 Tensorflow Lite .tflite 模型。

该平台是在 BeagleBone Black 上运行的 Raspian Stretch。

if (interpreter->Invoke() != kTfLiteOk) {
      std::cout << "Failed to invoke tflite!\n";
    }

我已经成功地使用相同的代码来使用转换后的纯人工神经网络模型。然而,在使用 CNN 类型模型时,我遇到了这个问题。

附件是一个 gdb backtrace()。

我还尝试调用其他几个 Tensorflow TFLITE 托管模型:mobilenet 和squeezenet,我也遇到了同样的问题。转换后的模型的结构也显示在回溯上方。

回溯是:

input(0) name: images
0: ArgMax, 8, 4, 0, 0
1: ArgMax/dimension, 4, 2, 0, 0
2: ConvNet/Reshape, 45120, 1, 0, 0
3: ConvNet/Reshape/shape, 16, 2, 0, 0
4: ConvNet/conv2d/Conv2D_bias, 64, 1, 0, 0
5: ConvNet/conv2d/Relu, 674880, 1, 0, 0
6: ConvNet/conv2d/kernel, 1024, 1, 0, 0
7: ConvNet/conv2d_1/Conv2D_bias, 128, 1, 0, 0
8: ConvNet/conv2d_1/Relu, 299520, 1, 0, 0
9: ConvNet/conv2d_1/kernel, 18432, 1, 0, 0
10: ConvNet/dense/BiasAdd, 1024, 1, 0, 0
11: ConvNet/dense/MatMul_bias, 1024, 1, 0, 0
12: ConvNet/dense/kernel/transpose, 19169280, 1, 0, 0
13: ConvNet/dense_1/BiasAdd, 8, 1, 0, 0
14: ConvNet/dense_1/MatMul_bias, 8, 1, 0, 0
15: ConvNet/dense_1/kernel/transpose, 2048, 1, 0, 0
16: ConvNet/max_pooling2d/MaxPool, 164864, 1, 0, 0
17: ConvNet/max_pooling2d_1/MaxPool, 74880, 1, 0, 0
18: images, 45120, 1, 0, 0
input: 18
About to memcpy
About to invoke mod!

Thread 1 "minimal" received signal SIGILL, Illegal instruction.
0x0007de64 in EigenForTFLite::TensorCostModel<EigenForTFLite::Threanst&, int) ()
(gdb) bt
#0  0x0007de64 in EigenForTFLite::TensorCostModel<EigenForTFLite::Tt const&, int) ()
#1  0x000901aa in void EigenForTFLite::TensorEvaluator<EigenForTFLi>, 1u> const, EigenForTFLite::TensorReshapingOp<EigenForTFLite::DSigenForTFLite::TensorMap<EigenForTFLite::Tensor<float const, 4, 1, iForTFLite::TensorReshapingOp<EigenForTFLite::DSizes<int, 2> const,  1, int>, 16, EigenForTFLite::MakePointer> const> const, EigenForTFevice>::evalProduct<0>(float*) const ()
#2  0x00090bae in tflite::multithreaded_ops::EigenTensorConvFunctorat const*, float*, int, int, int, int, float const*, int, int, int,
#3  0x00091200 in void tflite::ops::builtin::conv::EvalFloat<(tflit, TfLiteConvParams*, tflite::ops::builtin::conv::OpData*, TfLiteTen, TfLiteTensor*) ()
#4  0x0009134e in TfLiteStatus tflite::ops::builtin::conv::Eval<(tfde*) ()
#5  0x00047c2e in tflite::Subgraph::Invoke() ()
#6  0x00013b70 in tflite::Interpreter::Invoke() ()
#7  0x00012fc4 in main ()
(gdb)

最初我以为我包含了一些 tensorflow lite 不支持的 TensorFlow 操作,但现在其他模型似乎也没有调用,我不确定。

TensorFlow Git 标签/版本是 1.13.1。

使用以下命令从源代码树中编译演示:

CC_PREFIX=arm-linux-gnueabihf- make -j 3 -f -g tensorflow/lite/tools/make/Makefile TARGET=rpi TARGET_ARCH=armv7l minimal

其中 minimum 是在

/tensorflow/tensorflow/lite/tools/make/Makefile

更多从他们的最小和 label_image tflite 演示修改的代码:

std::unique_ptr<tflite::FlatBufferModel> model =
tflite::FlatBufferModel::BuildFromFile(filename);
TFLITE_MINIMAL_CHECK(model != nullptr);

// Build the interpreter
tflite::ops::builtin::BuiltinOpResolver resolver;
InterpreterBuilder builder(*model, resolver);
std::unique_ptr<Interpreter> interpreter;
builder(&interpreter);
TFLITE_MINIMAL_CHECK(interpreter != nullptr);

// Allocate tensor buffers.
TFLITE_MINIMAL_CHECK(interpreter->AllocateTensors() == kTfLiteOk);
printf("=== Pre-invoke Interpreter State ===\n");
tflite::PrintInterpreterState(interpreter.get());

  int input = interpreter->inputs()[0];
  LOG(INFO) << "input: " << input << "\n";

std::cout << "About to memcpy\n";

float* input_ptr = interpreter->typed_tensor<float>(input);
memcpy(input_ptr,float_buf,tf_input_size*sizeof(float));

if (interpreter->Invoke() != kTfLiteOk) {
      std::cout << "Failed to invoke tflite!\n";
    }

任何方向表示赞赏。

::编辑::

哇。在树莓派上运行完全相同的可执行文件和带有数据的 .tflite 可以 100% 工作。

标签: c++tensorflowtensorflow-lite

解决方案


我正在为错误的 FPU 进行编译,它仅在调用 CNN 而不是 ANN 时出现。

将 rpi_makefile.inc -mfpu 更改为目标 neon (v3)

-mfpu=neon \

Tflite 现在似乎在 beaglebone black 上工作得更好。


推荐阅读