09.05.2023 Views

pdfcoffee

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

TensorFlow for Mobile and IoT and TensorFlow.js

Once the model is converted it can be copied onto the specific device. Of course,

this step is different for each different device. Then the model can run by using

the language you prefer. For instance, in Java the invocation happens with the

following code snippet:

try (Interpreter interpreter = new Interpreter(tensorflow_lite_model_

file)) {

interpreter.run(input, output);

}

Again, pretty simple! What is very useful is that the same steps can be followed for

a heterogeneous collection of Mobile and IoT devices.

Using GPUs and accelerators

Modern phones frequently have accelerators on board that allow floating-point

matrix operations to be performed faster. In this case, the interpreter can use the

concept of Delegate, and specifically GpuDelegate(), to use GPUs. Let's look at

an example in Java:

GpuDelegate delegate = new GpuDelegate();

Interpreter.Options options = (new Interpreter.Options()).

addDelegate(delegate);

Interpreter interpreter = new Interpreter(tensorflow_lite_model_file,

options);

try {

interpreter.run(input, output);

}

Again, the code is self-commenting. A new GpuDelegate() is created and then it is

used by the Interpreter to run the model on a GPU.

An example of application

In this section, we are going to use TensorFlow Lite for building an example

application that is later deployed on Android. We will use Android Studio

(https://developer.android.com/studio/) to compile the code. The first step is

to clone the repo with:

git clone https://github.com/tensorflow/examples

Then we open an existing project (see Figure 3) with the path examples/lite/

examples/image_classification/android.

[ 466 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!