[PYTHON] A addictive story when using tensorflow on Android

Overview

I wanted to use the tensorflow model made with Python on Android, so I tried various things, but I stumbled quite a bit, so I will record the circumstances. In the end, I was able to run tensorflow on Android safely, but the cause is not fixed.

PC:mac Development environment: Android Studio tensorflow program: There is only model.pb that stores parameters and LSTM.java that contains Tensor.create (), session.runner (), etc.

Initial action

I didn't want to use Bazel, so I proceeded with reference to the following. Then, record the part where the problem occurred and stumbled below. https://qiita.com/icchi_h/items/a1df9f27569714edfc5e

https://qiita.com/tchkwkzk/items/aa481db14126b6abdaa1

problem

I had some problems, so I dealt with them individually. Problem 1: I can't read model.pb Problem 2: tensorflow cannot be imported Problem 3: The app quits with the following error No implementation found for native Lorg/tensorflow/TensorFlow;.version:()Ljava/lang/String; Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lorg/tensorflow/TensorFlow; FATAL EXCEPTION: main java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: linux, architecture: i686.

Problem 1: model.pb cannot be read

This is quite inconvenient if you usually develop iOS apps, but Android Studio now reads local files from the assets folder.

1: Add assets folder to app / src / main (note how to add) How to add: Right-click main → "New" → "Folder" → "Assets Folder"

2: Place the model.pb file in the created assets folder

3: Call the following code in onCreate of MainActivity.java   byte[] graphDef = readByteFile(String.valueOf("model.pb"))

readFileToByte is a self-made function and is described as follows.

   byte[] readByteFile(String filePath) {
        try {
            byte[] b = new byte[1];
            InputStream fis = this.getAssets().open(filePath);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while (fis.read(b) > 0) {
                baos.write(b);
            }
            baos.close();
            fis.close();
            b = baos.toByteArray();

            return b;
        }catch(Exception ex){
            System.out.println(ex.toString());
            return null;
        }
    }

Problem 2: tensorflow cannot be imported

The following import was described in LSTM.java used this time.

import org.tensorflow.Graph;
import org.tensorflow.Session;
import org.tensorflow.Tensor;

To read this, I added the following description to "build.gradle" in the app. By the way, it seems that there are "1.2.0" and "1.2.0-preview", and "copyTo" only supports preview, so I wrote preview. After that, press the Sync button of Android Studio.

dependencies {
    
    ...
 
    compile 'org.tensorflow:tensorflow-android:1.2.0-preview'
    //compile 'org.tensorflow:tensorflow:1.2.0'

   ...
   
}

Problem 3: Throw an error and kill

No implementation found for native Lorg/tensorflow/TensorFlow;.version:()Ljava/lang/String; Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lorg/tensorflow/TensorFlow; FATAL EXCEPTION: main java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: linux, architecture: i686.

However, I was in trouble because a surprising solution for this did not come out. (Although my study is insufficient)

I've done some things, so I'll describe them below, but there may be some operations that I don't need.

1. Add the so file

From the link below, download the file by clicking "Latest successful build artifacts-> out-> native-> (zip all files)". http://ci.tensorflow.org/view/Nightly/job/nightly-android/

Add the libs folder directly under app in Android Studio. In the libs folder Add all the folders in "libtensorflow_inference.so" in the native folder downloaded earlier to the libs folder. The directory structure should look like this: -app --libs ---arm64-v8a ----libtensorflow_inference.so ---armeabi-v7a ----libtensorflow_inference.so ---x86 ----libtensorflow_inference.so ---x86_64 ----libtensorflow_inference.so

2. Add loadLibrary to MainAcitivity

public class MainActivity extends AppCompatActivity {

    ....

    static {
        System.loadLibrary("tensorflow_inference");
    }

    ....

}

3. Change the sourceSets of build.gradle in the app

android {
    
    ...

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }

    ...

}

For the time being, it worked. I don't know what the correct answer is, but I'll leave it as a memorandum.

Recommended Posts

A addictive story when using tensorflow on Android
SoC FPGA: A small story when using on Linux
A swampy story when using firebase on AWS lamda
Error, warning when using TensorFlow on Mac
A story about simple machine learning using TensorFlow
Troublesome story when using Python3 with VScode on ubuntu
Addictive points when downloading files using boto on AWS Lambda
A story that stumbled when using pip in a proxy environment
I made a VGG16 model using TensorFlow (on the way)
A memorandum when using beautiful soup
Make a face recognizer using TensorFlow
Using a serial console on Ubuntu 20.04
Run TensorFlow2 on a VPS server
A story about using Python's reduce
The story that a hash error came out when using Pipenv
A note on using tab completion when running Python interactively on Windows
Set up a node to do MNIST on ROS using Tensorflow
What I stumbled upon when using CodeIgniter on a Linux server
Knowledge when making a bot using discord.py
Run TensorFlow on a GPU instance on AWS
Try using Bash on Windows 10 2 (TensorFlow installation)
intel Atom (notebook) Frequent petit freezes when using linux on a PC.
Try to infer using a linear regression model on android [PyTorch Mobile]
I tried playing a ○ ✕ game using TensorFlow
A struggle when installing pyenv on Cygwin
A story about deploying a Twitter-linked app created using Flask + gunicorn on Heroku
I built a TensorFlow environment on windows10
A story about a 503 error on Heroku open
CPU usage is 100% when using pygame on Ubuntu 16.04
Try Tensorflow with a GPU instance on AWS
Try using a QR code on a Raspberry Pi
A story about displaying article-linked ads on Jubatus
A story about running Python on PHP on Heroku
Notes for using TensorFlow on Bash on Ubuntu on Windows
TensorFlow: Run data learned in Python on Android
I tried to make a ○ ✕ game using TensorFlow
A story when a Python user passes a JSON file
Create a GUI on the terminal using curses
A story that was convenient when I tried using the python ip address module
Build a machine learning environment using PyCharm on Ubuntu environment (TensorFlow will also be introduced!)
A story that got stuck when trying to upgrade the Python version on GCE
The story when I was using IntelliJ on Linux and could not input Japanese
A little trick to know when writing a Twilio application using Python on AWS Lambda
A story about an error when loading a TensorFlow model created with Google Colab locally
[Grasshopper] When creating a data tree on Python script
Calculate on multiple GPUs using Tensorflow 2's Mirrored Strategy
A story about using Resona's software token with 1Password
Build a Django development environment using pyenv-virtualenv on Mac
Precautions when using sqlite3 on macOS Sierra (10.12) with multiprocessing
Set up a file server on Ubuntu 20.04 using Samba
What I did when I stumbled on a Django tutorial
I got a UnicodeDecodeError when pip install on ubuntu
Building a TensorFlow environment that uses GPU on Windows 10
Check types_map when using mimetypes on AWS Lambda (Python)
Error around GUI when using PyQt5 on Ubuntu (WSL2)
Solution if you crash when using selenium on heroku
Build a Python environment on your Mac using pyenv
Build a Python development environment using pyenv on MacOS
[kotlin] Create a real-time image recognition app on android
A memo when Django was released on VPS (preparation)
A story about a Linux beginner putting Linux on a Windows tablet