Segmentation fault with Efficient net C++ deployment

Question/Issue:
Converted efficient net model to a c++ deployment library. Tried to build and test the model with camera.cpp application provided by GitHub - edgeimpulse/example-standalone-inferencing-linux: Builds and runs an exported impulse locally (Linux). The building process consumes close to 6 gb of RAM before generating binary. Test was done on a X86 laptop with 16 gb RAM. Any idea how to by pass the TFlite model error.

Failed to allocate TFLite arena (0 bytes)
ERR: Failed to run classifier (-6)
Segmentation fault (core dumped)

On using gdb
Failed to allocate TFLite arena (0 bytes)
ERR: Failed to run classifier (-6)
Thread 1 “camera” received signal SIGSEGV, Segmentation fault.
0x00007ffff4dafcce in __GI___libc_free (mem=0x3f800000) at malloc.c:3255
3255 malloc.c: No such file or directory.

@kwasif2020 You should build with hardware-optimization flags enabled (see Hardware acceleration in the README.md). E.g.:

APP_CUSTOM=1 make clean
APP_CUSTOM=1 TARGET_LINUX_X86=1 USE_FULL_TFLITE=1 make -j

I’ve also added this to the README now

The above error comes after building it using below command
APP_CAMERA=1 TARGET_LINUX_X86=1 make -j1

Even after adding the additional arguement of US_FULL_TFLITE it gave an error

APP_CAMERA=1 TARGET_LINUX_X86=1 USE_FULL_TFLITE=1 make -j1

INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
ERROR: Regular TensorFlow ops are not supported by this interpreter. Make sure you apply/link the Flex delegate before inference.
ERROR: Node number 10 (FlexErf) failed to prepare.

ERROR: Failed to apply the default TensorFlow Lite delegate indexed at 0.
AllocateTensors failed
ERR: Failed to run classifier (-3)

I tried to switch to a different model which consumes less memory tiny v1.
I get the following segmentation fault when running the inference

0x00014f90 in fill_result_struct_f32 (debug=, data=, result=, impulse=) at ./edge-impulse-sdk/classifier/ei_fill_result_struct.h:362
362 result->classification[ix].label = impulse->categories[ix];

GDB output

#0 0x00014f90 in fill_result_struct_f32 (debug=, data=, result=, impulse=) at ./edge-impulse-sdk/classifier/ei_fill_result_struct.h:362
#1 fill_result_struct_from_output_tensor_tflite (impulse=0xbefff15c, output=, labels_tensor=, scores_tensor=, result=0xbefff280, debug=false)
at ./edge-impulse-sdk/classifier/inferencing_engines/tflite_helper.h:422
#2 0x00017c14 in inference_tflite_run (config=, tensor_arena=, debug=false, result=0xbefff280, interpreter=0x572070, scores_tensor=0x10d10, labels_tensor=0xb6ff3f40,
output=, ctx_start_us=13762983418814042689, impulse=0xbefff15c) at ./edge-impulse-sdk/classifier/inferencing_engines/tflite_micro.h:200
#3 run_nn_inference (impulse=impulse@entry=0xbefff15c, fmatrix=fmatrix@entry=0xbefff0ec, result=result@entry=0xbefff280, config_ptr=, debug=debug@entry=false)
at ./edge-impulse-sdk/classifier/inferencing_engines/tflite_micro.h:319
#4 0x0001507c in (anonymous namespace)::run_inference (impulse=0xbefff15c, fmatrix=0xbefff0ec, result=0xbefff280, debug=) at ./edge-impulse-sdk/classifier/ei_run_classifier.h:117
#5 0x00015914 in (anonymous namespace)::process_impulse (impulse=0xbefff15c, impulse@entry=0xbefff154, signal=signal@entry=0xbefff1fc, result=result@entry=0xbefff280, debug=debug@entry=false)
at ./edge-impulse-sdk/classifier/ei_run_classifier.h:211
#6 0x000159dc in (anonymous namespace)::run_classifier (signal=0xbefff1fc, signal@entry=0xbefff1f4, result=0xbefff280, result@entry=0xbefff278, debug=debug@entry=false)
at ./edge-impulse-sdk/classifier/ei_run_classifier.h:564

@kwasif2020 Can you send me your model file at jan@edgeimpulse.com ?

FYI, I’ve used GitHub - edgeimpulse/example-custom-ml-block-keras at efficientnet before (trained with transfer learning) which works so interested to see what you have.

@janjongboom
Sure, The project id is 209401

There are 2 aspects to it. When I try to build on Pi4 it fails due to lack of RAM memory.
When i try to build on my laptop it is able to build but gives seg fault. Given below are additional details
After making some changes when I enabled the debug flag I saw the seg fault happening here after it reaches class id 338. Is it something to with limitation?

class 334: 1.861942
class 335: -0.418132
class 336: 2.402938
class 337: -3.687085
class 338: 1.649055

Program received signal SIGSEGV, Segmentation fault.
0x000055555556cd56 in fill_result_struct_f32 (debug=, data=, result=, impulse=) at ./edge-impulse-sdk/classifier/ei_fill_result_struct.h:363
363 result->classification[ix].value = value;

@kwasif2020 The segfault is because instantiating the model failed (and in the example repo I didn’t properly terminate the program when that happens) so we don’t have a properly initialized model (the error here):

INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
ERROR: Regular TensorFlow ops are not supported by this interpreter. Make sure you apply/link the Flex delegate before inference.
ERROR: Node number 10 (FlexErf) failed to prepare.

ERROR: Failed to apply the default TensorFlow Lite delegate indexed at 0.

Right now we don’t support Flex ops in the Linux runtime. This is something that I’ll add to backlog to investigate while we’re upgrading the TFLite version (which will happen shortly, we’ll need it for some new transformer layers as well).

One way around is to grab the Keras version of EfficientNet, that doesn’t have the FlexErf op after conversion from ONNX.

For people finding this thread: I’ve spent my Saturday trying to get flex ops to link but:

  1. We need static libraries
  2. There’s two build toolchains for TensorFlow Lite: 1) CMake, can create static libraries, but does not support flex ops. 2) Bazel, cannot create static libraries, but does support flex ops.

:man_facepalming: So I don’t really have a good easy solution right now.

Hi @kwasif2020 We now have support for flex delegates, see the docs here: Flex delegates - Edge Impulse Documentation

1 Like