Main reference

Official Integration Guide

The official demo

Deep learning tool: Application of TensorFlow in intelligent terminal

Machine learning: Integrate TensorFlow in Android

Implementation method and problem handling of dynamic loading so library

Integration is briefly

Integration of the code is based on AndroidTensorFlowMachineLearningExample modification.

The easiest steps to integrate TensorFlow on Android are:

1 complile introduced

compile 'org. Tensorflow: tensorflow - android: 1.4.0'
Copy the code

2. Obtain the PB file that has been trained and compressed by AI algorithm developers

3. Start writing code according to its Java API

Generally, after compile is introduced, the results of so library will be automatically entered,pb is put in the implies folder, but this will increase the apK package volume. Generally, the ABI of SO library is 10-15m, and the PB file is 20-100M. Therefore, dynamic loading is required: put on the server, download to the local, and read directly from the local when necessary.

Load pb files

AndroidTensorFlowMachineLearningExample API and don’t load flow pb file API, and 1.4.0 in Java API provides the following construction method, can be loaded from a file. Just download it in advance.

new TensorFlowInferenceInterface(inputStream);
Copy the code

Dynamic loading of so library

It’s a bit cumbersome, first you need to identify the abi you support and then download the corresponding SO file, and second you need to comment out the static code blocks in your Java code that load the so library

The dependency importing method is no longer the above line of code compile, but copy the Tensorflow – Android :1.4.0 JAR package and jNI package from gradle cache, and upload the so file in the JNI package to the server.

Comment on the code in the static method that loads the so library:

The jar needs to comment out the static code block that loads the so library: the jar calls tensorFlow.init () at several points, so just comment out the inside of the init.

Static void in the TensorFlow classinit() { //NativeLibrary.load(); } static {init(); }Copy the code

Operation method:

Create a new TensorFlow class with the same package name, copy the code in the JAR package here, comment out that line of code, compile with Java7 (not Java8), open the JAR package with WinRAR, and drag the compiled class file into the JAR package to replace it. Then add the JAR package to the project libs directory as a dependency.

The so library is loaded dynamically

Get the preferred ABI of the mobile system, then go to the corresponding URL, download to the app directory, and load.

Get the preferred ABI:

private static String getFirstSupportedAbi() {
        String abi1 = "";
        if (Build.VERSION.SDK_INT >= 21) {
            String[] abis = Build.SUPPORTED_ABIS;
            if(abis ! = null) { String abistr =""; // The first is native support, and the second is compatibility mode. Abi1 = abis[0]; abi1 = abis[0];for (String abi : abis) {
                    abistr = abistr + abi+",";
                }
                if(showLog)
                Log.e(TAG, "[copySo] supported api:"+ abistr); }}else {
            if(showLog)
            Log.e(TAG, "[copySo] supported api:" + Build.CPU_ABI + "-" + Build.CPU_ABI2);
            if(! TextUtils.isEmpty(Build.CPU_ABI)) { abi1 = Build.CPU_ABI; }else if (!TextUtils.isEmpty(Build.CPU_ABI2)) {
                abi1 = Build.CPU_ABI2;
            }
        }
        return abi1;
    }

Copy the code

The downloaded SO file should be placed in the app directory, not on the SD card:

String abi = getFirstSupportedAbi();
File dir = context.getDir("jnilibs", Context.MODE_PRIVATE);
File subDir = new File(dir, abi);
Copy the code

Load so:

            try {
                    System.load(filePath);
                }catch (Throwable e){
                    e.printStackTrace();
                }

Copy the code

File download

These are large files, so choose a reliable library that can be used for breakpoint continuation :FileDownloader

Also note:

  • The file is large, so download it only on wifi to avoid consuming traffic
  • The check MD5 is downloaded.

code

TensorFlowAndroidDynamic