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.
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
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.
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;
}
}
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'
...
}
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.
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
public class MainActivity extends AppCompatActivity {
....
static {
System.loadLibrary("tensorflow_inference");
}
....
}
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