提问者:小点点

Tensorflow Lite error undefined reference to 'tflite::D efaultErrorReporter()'


我想在ARMLinux板上运行tflite模型。以下是我遵循的步骤:

1-使用python API将我的Tensorflow 2线性估算模型转换为带有select ops的tflite:

import tensorflow as tf
loaded = tf.saved_model.load(saved_model_path, tags = None)
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir = saved_model_dir, signature_keys=['serving_default'])
converter.experimental_new_converter = True
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
  f.write(tflite_model)

2-将“//tensor flow/lite/delegates/flex:delegate”添加到/tensorflow/lite/Build中tensorflowlite的deps[]中,以获得此处提到的依赖关系。

3-使用以下命令在我的电脑上构建tflite共享库:

Bazel-3.1.0 build --config=elinux_armhf --config=monolithic -c opt --config=v2 //tensorflow/lite/c:libtensorflowlite_c.so

4-将位于bazel-bin/tensor flow/lite/libtensor flow lite _ c . so的输出共享库重命名为libtensorflow-lite.so

5复制model.tflite,demo.cc和libtensorflow-lite.so到我的设备 /opt/demo和demo.cc内容是:

#include <stdio.h>
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/tools/gen_op_registration.h"

int main(){

    std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile("model.tflite");

    if(!model){
        printf("Failed to mmap model\n");
        exit(0);
    }

    tflite::ops::builtin::BuiltinOpResolver resolver;
    std::unique_ptr<tflite::Interpreter> interpreter;
    tflite::InterpreterBuilder(*model.get(), resolver)(&interpreter);

    // Resize input tensors, if desired.
    interpreter->AllocateTensors();

    float* input = interpreter->typed_input_tensor<float>(0);
    // Dummy input for testing
    *input = 2.0;

    interpreter->Invoke();

    float* output = interpreter->typed_output_tensor<float>(0);

    printf("Result is: %f\n", *output);

    return 0;
}

6-将Tensorflow源复制到ARM板的/opt。

7-通过在我的设备上运行以下命令创建了目标文件:

/opt/demo# g++ demo.cc -c -I /opt/tensorflow_src -I /opt/tensorflow_src/flatbuffers/

8-当我运行g-L/opt/demo/-o exec demo. o-tlensorflow-lite命令生成二进制文件时,我得到以下错误:

/usr/lib/gcc/arm-poky-linux-gnueabi/9.3.0/../../../../arm-poky-linux-gnueabi/bin/ld: demo.o: in function `main':
demo.cc:(.text+0xc): undefined reference to `tflite::DefaultErrorReporter()'
/usr/lib/gcc/arm-poky-linux-gnueabi/9.3.0/../../../../arm-poky-linux-gnueabi/bin/ld: demo.cc:(.text+0x24): undefined reference to `tflite::FlatBufferModel::BuildFromFile(char const*, tflite::ErrorReporter*)'
/usr/lib/gcc/arm-poky-linux-gnueabi/9.3.0/../../../../arm-poky-linux-gnueabi/bin/ld: demo.cc:(.text+0x68): undefined reference to `tflite::ops::builtin::BuiltinOpResolver::BuiltinOpResolver()'
/usr/lib/gcc/arm-poky-linux-gnueabi/9.3.0/../../../../arm-poky-linux-gnueabi/bin/ld: demo.cc:(.text+0x90): undefined reference to `tflite::InterpreterBuilder::InterpreterBuilder(tflite::FlatBufferModel const&, tflite::OpResolver const&)'
/usr/lib/gcc/arm-poky-linux-gnueabi/9.3.0/../../../../arm-poky-linux-gnueabi/bin/ld: demo.cc:(.text+0xa4): undefined reference to `tflite::InterpreterBuilder::operator()(std::unique_ptr<tflite::Interpreter, std::default_delete<tflite::Interpreter> >*)'
/usr/lib/gcc/arm-poky-linux-gnueabi/9.3.0/../../../../arm-poky-linux-gnueabi/bin/ld: demo.cc:(.text+0xb0): undefined reference to `tflite::InterpreterBuilder::~InterpreterBuilder()'
/usr/lib/gcc/arm-poky-linux-gnueabi/9.3.0/../../../../arm-poky-linux-gnueabi/bin/ld: demo.cc:(.text+0xc8): undefined reference to `tflite::Interpreter::AllocateTensors()'
/usr/lib/gcc/arm-poky-linux-gnueabi/9.3.0/../../../../arm-poky-linux-gnueabi/bin/ld: demo.cc:(.text+0x110): undefined reference to `tflite::Interpreter::Invoke()'
/usr/lib/gcc/arm-poky-linux-gnueabi/9.3.0/../../../../arm-poky-linux-gnueabi/bin/ld: demo.cc:(.text+0x190): undefined reference to `tflite::InterpreterBuilder::~InterpreterBuilder()'
/usr/lib/gcc/arm-poky-linux-gnueabi/9.3.0/../../../../arm-poky-linux-gnueabi/bin/ld: demo.o: in function `tflite::MutableOpResolver::~MutableOpResolver()':
demo.cc:(.text._ZN6tflite17MutableOpResolverD2Ev[_ZN6tflite17MutableOpResolverD5Ev]+0x6c): undefined reference to `vtable for tflite::MutableOpResolver'
/usr/lib/gcc/arm-poky-linux-gnueabi/9.3.0/../../../../arm-poky-linux-gnueabi/bin/ld: demo.o: in function `tflite::ops::builtin::BuiltinOpResolver::~BuiltinOpResolver()':
demo.cc:(.text._ZN6tflite3ops7builtin17BuiltinOpResolverD2Ev[_ZN6tflite3ops7builtin17BuiltinOpResolverD5Ev]+0x4c): undefined reference to `vtable for tflite::ops::builtin::BuiltinOpResolver'
/usr/lib/gcc/arm-poky-linux-gnueabi/9.3.0/../../../../arm-poky-linux-gnueabi/bin/ld: demo.o: in function `std::default_delete<tflite::FlatBufferModel>::operator()(tflite::FlatBufferModel*) const':
demo.cc:(.text._ZNKSt14default_deleteIN6tflite15FlatBufferModelEEclEPS1_[_ZNKSt14default_deleteIN6tflite15FlatBufferModelEEclEPS1_]+0x24): undefined reference to `tflite::FlatBufferModel::~FlatBufferModel()'
/usr/lib/gcc/arm-poky-linux-gnueabi/9.3.0/../../../../arm-poky-linux-gnueabi/bin/ld: demo.o: in function `std::default_delete<tflite::Interpreter>::operator()(tflite::Interpreter*) const':
demo.cc:(.text._ZNKSt14default_deleteIN6tflite11InterpreterEEclEPS1_[_ZNKSt14default_deleteIN6tflite11InterpreterEEclEPS1_]+0x24): undefined reference to `tflite::Interpreter::~Interpreter()'
collect2: error: ld returned 1 exit status

如何解决这个问题并编译我的解释器二进制文件?


共1个答案

匿名用户

我重新检查了我的步骤,注意到我的bazel build命令中有一个问题。所以用C语言的lib代替C语言。我可以通过使用下面的命令为C语言构建libtensorflow来解决这个问题:

bazel-3.1.0 build --config=elinux_armhf --config=monolithic -c opt //tensorflow/lite:libtensorflowlite.so --local_ram_resources=10240 --config=noaws

起初,我尝试在没有 noaws 标志的情况下构建,但由于在 bazel 的依赖项下载阶段,与 aws 相关的包链接被破坏,构建失败,但是我通过添加 noaws 标志解决了这个问题。

这篇文章对于尝试将 Tensorflow2 线性估计器模型转换为 TensorFlow lite 模型并希望使用 Bazel 为 ARM 架构构建具有精选 TensorFlow ops 的 C 库的人很有用。