Android+tensorflow Lite+python building process

Keywords: Android Python Mobile Google

background

Artificial intelligence is on fire, so is tensorflow. Google has launched TensorFlow Lite for mobile, which should be familiar with as an android development. Today's goal is to be able to deploy the deep learning framework on the mobile side. Since Android can also run tensorflow, why don't you try it? This is a common problem of programmers. It's over.

This development environment is TensorFlow 2.1+python 3.7+Android studio 3.6.1

Build TensorFlow under Windows 10

I just want to say that today's goal is not to build an environment, but to deploy TensorFlow on Android. You can download the anaconda version of python directly. You can install it with the command. There are pits. Don't use the pip command. There are two reasons,

  • First, it's too slow. I've been downloading too many times. It's not easy to turn over the wall;
  • Second, error reporting (no appropriate solution was found, the unified method is to downgrade to 1.x, which is a problem in TensorFlow2.0).

Solution: use the command of conda install python. How do you really have no idea about the configuration environment? You can refer to TensorFlow official website , or a lot of Baidu to teach you how to build the environment.

Write python code

Today, I used a very simple example, to fit a function with TensorFlow, y = a x + b, give x, y, and calculate a, b through TensorFlow; let's see my fitting effect

In fact, the code is as follows:

import tensorflow as tf

# Create a simple Keras model.
x = [-1, 0, 1, 2, 3, 4]
y = [-3, -1, 1, 3, 5, 7]

model = tf.keras.models.Sequential(
    [tf.keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
model.fit(x, y, epochs=500)
print(model.predict([1, 3, 7]))

Remember the results ([1, 3, 7]), we will run this set of data on Android to see if the results are the same.

Model transformation

The above is an example written with Keras. Since python TensorFlow has been written, how to use it on Android needs to use conversion to convert python code to Android code.

export_dir = 'saved_model/test'
tf.saved_model.save(model, export_dir)
#Transform the model.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
tflite_model_file = pathlib.Path('saved_model/model.tflite')
tflite_model_file.write_bytes(tflite_model)

This tf.saved_model.save(model, export_dir) may report an error and cannot create a directory. You can create a directory manually.

In the corresponding directory, find a. tflite file. This file is what we want to call on Android.

Create a new Android project

What dependencies does the Android environment need?
Where is the TensorFlow lite file?
How to call?
How to input data?
Where is the result?

I'll explain them all.

1. dependence

  • It is necessary to rely on implementation 'org. Tensorflow: tensorflow Lite: 0.0.0-nightly' in build.gradle.

  • Add arm support

defaultConfig {
        ......
        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a'
        }
    }
  • Prevent aapt from compressing the model, otherwise an error will be reported when reading the model.
android{
	......
 aaptOptions {
        noCompress "tflite"  //Represents a file suffix that does not allow aapt to compress
    }
}

2. How to read files and use

  • Put the * *. tflite * * file under the assets file
  • Know where the files are, and then learn how to get the model and use it in Android.
public class TFLiteLoader {
    private static Context mContext;
    Interpreter mInterpreter;
    private static TFLiteLoader instance;

    public static TFLiteLoader newInstance(Context context) {
        mContext = context;
        if (instance == null) {
            instance = new TFLiteLoader();
        }
        return instance;
    }


    Interpreter get() {
        try {
            if (Objects.isNull(mInterpreter))
                mInterpreter = new Interpreter(loadModelFile(mContext));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return mInterpreter;
    }


    // get files
    private MappedByteBuffer loadModelFile(Context context) throws IOException {
        AssetFileDescriptor fileDescriptor = context.getAssets().openFd("model.tflite");
        FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
        FileChannel fileChannel = inputStream.getChannel();
        long startOffset = fileDescriptor.getStartOffset();
        long declaredLength = fileDescriptor.getDeclaredLength();
        return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
    }
}

Input input and output:

float[][] input = new float[][]{{1, 3, 7}};
float[][] output = new float[3][1];
TFLiteLoader.newInstance(getApplicationContext()).get().run(input, output);
for (int i = 0; i < 3; i++) {
	for (int j = 0; j < 1; j++) {
		Log.i(TAG, output[i][j] + "");
		}
	}

The operation results are as follows:

This result is the same as that of python version. We have successfully deployed TensorFlow lite on Android.

Published 35 original articles, won praise 7, visited 10000+
Private letter follow

Posted by GroundZeroStudios on Tue, 10 Mar 2020 05:28:19 -0700