Nowadays, mobile devices have enough computing power to run pre-trained models on them. This results in an optimal use of the hardware and an increase in speed, because the data are not sent over the Internet, which also means more privacy. In my presentation I will show different ways to integrate machine learning models into an iOS and Android application.
The participants will learn how to integrate a pre-training model into an iOS and Android application with CoreML and Tensorflow Lite, and how to re-train a model for own pictures and use it instead of the pre-trained model. All examples are shown in a small demo.
https://www.mcubed.london/sessions/machine-learning-models-mobile-devices/
10. TensorFlow Lite
.tflite fileAndroid App
Java API
C++ API
Interpreter
Android Neural Network API
Operators
.tflite fileiOS App
C++ API
Interpreter
Operators
20. Inception V3 and TF Lite
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/models.md
21. Inception V3 and TF Lite
String tffile = “inception_v3.tflite”;
protected void onCreate(Bundle savedInstanceState) {
tflite = new Interpreter(loadModelFile(tffile));
button1.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
predict(context, R.drawable.image1);
Interpreter
setOnClickListener
predict
22. Inception V3 and TF Lite
private void predict(Context context, int imgId) {
Bitmap bitmap = BitmapFactory.decodeResource(...,
imgId, ...);
image.setImageBitmap(bitmap);
ByteBuffer imgBuf = convertBitmapToByteBuffer(bitmap);
float[][] labelProb = new float[1][labels.size()];
tflite.run(imgBuf, labelProb);
>>>
predict
imgBuf
imgBuf
labelProb
labelProbtflite.run
23. Inception V3 and TF Lite
String label = "";
Float percent = -1.0f;
for(int index = 0; index < labels.size(); index++) {
if (labelProb[0][index] > percent) {
percent = labelProb[0][index];
label = labels.get(index);
}
labelProb
labelProb
index
index
index
index
labels
labels
36. XOR and TF Lite
private String prediction(int a, int b) {
float[][] in = new float[][]{{a, b}};
float[][] out = new float[][]{{0}};
tflite.run(in, out);
return String.valueOf(out[0][0]);
}
prediction
in
in
out
out
out[0][0]
tflite.run(in, out)