Daniel Vanum
Daniel Vanum

Reputation: 74

How to link tensorflow lite C library with android NDK

I am trying to use tensorflow using android NDK. I have setup everything as mentioned in this answer (Link)

Answer: I use Native TFL with C-API in the following way:

SETUP:

  1. Download the latest version of TensorFlow Lite AAR file
  2. Change the file type of downloaded .arr file to .zip and unzip the file to get the shared library (.so file)
  3. Download all header files from the c directory in the TFL repository
  4. Create an Android C++ app in Android Studio
  5. Create a jni directory (New -> Folder -> JNI Folder) in app/src/main and also create architecture sub-directories in it (arm64-v8a or x86_64 for example)
  6. Put all header files in the jni directory (next to the architecture directories), and put the shared library inside the architecture directory/ies
  7. Open the CMakeLists.txt file and include an add_library stanza for the TFL library, the path to the shared library in a set_target_properties stanza and the headers in include_directories stanza (see below, in NOTES section)
  8. Sync Gradle

Here is my project structure

app/
|-- src/
|   |-- main/
|       |-- cpp/
|       |   |-- UpscalingShader.cpp
|       |   |-- UpscalingShader.h
|       |   |-- RgbAToRgb.cpp
|       |   |-- RgbAToRgb.h
|       |   |-- GlUtil.cpp
|       |   |-- GlUtil.h
|       |   |-- DebugText.cpp
|       |   |-- DebugText.h
|       |   |-- CMakeLists.txt
|       |-- jniLibs/
|       |   |-- arm64-v8a\
|       |   |   |-- // all the header files in (https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/c) and other files required for the project
|       |   |   |-- libtensorflowlite.so (Which the answer expects me to place this '.so' file here)
|       |   |   |...
|-- build.gradle
|-- ...

This is my CMakeLists.txt file:

# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)

# Declares the project name.
project("my_project")

# Set the JNI directory where the TensorFlow Lite library and headers are located
set(JNI_DIR ${CMAKE_SOURCE_DIR}/src/main/cpp/jniLibs)

# Define the TensorFlow Lite library as an imported library
add_library(tflite-lib SHARED IMPORTED)

# Set the location of the TensorFlow Lite library
set_target_properties(
        tflite-lib
        PROPERTIES IMPORTED_LOCATION
        /path/to/app/src/main/jniLibs/arm64-v8a/libtensorflowlite_jni.so
)

# Include the TensorFlow Lite headers
include_directories(${JNI_DIR}/include)

# Create the main library
add_library(${CMAKE_PROJECT_NAME} SHARED
        my_file.cpp
        UpscalingShader.cpp
        UpscalingEffect.cpp
        RgbAToRgb.cpp
        GlUtil.cpp
        DebugText.cpp
)

# Link libraries to the main library
target_link_libraries(${CMAKE_PROJECT_NAME}
        android
        log
        tflite-lib
)

I did everything accordingly, but I am getting the following error while trying to run my app.

In file included from /path/to/app/src/main/cpp/UpscalingEffect.cpp:5:
In file included from /path/to/app/src/main/cpp/UpscalingEffect.h:9:
In file included from /path/to/app/src/main/cpp/UpscalingShader.h:10:
/path/to/app/src/main/cpp/./tensorflow/tensorflow/lite/c/builtin_op_data.h:21:10: fatal error: 'tensorflow/lite/core/c/builtin_op_data.h' file not found
   21 | #include "tensorflow/lite/core/c/builtin_op_data.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is the builtin_op_data.h (Which I copied from tensorflow/tensorflow/lite/c to here according to the answer)

/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_
#define TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_

/// For documentation, see
/// third_party/tensorflow/lite/core/c/builtin_op_data.h

#include "tensorflow/lite/core/c/builtin_op_data.h"

#endif  // TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_
  1. How to add prefix to every .cpp and .h files so that it would run perfectly
  2. I am unable to run my app. Is there any standard procedure to be followed to integrate tensorflow C++ libraries with android NDK?

Upvotes: 1

Views: 36

Answers (0)

Related Questions