At DevFest Vancouver 2018, I gave a talk on the end to end process of how to train a model with TensorFlow high level API tf.Keras, obtain the TensorFlow Lite model and deploy it to an Android app.
AI, ML, deep learning and computer vision ◦ Learning resources of TensorFlow & Keras • Overview of TensorFlow to Android ◦ Your options for getting a model • Train a neural network with tf.Keras ◦ Deep learning tools ◦ Visualize neural networks • Convert to TensorFlow Lite model ◦ Convert from Keras model to tflite ◦ Inspect & test the tflite model • Run tflite on Android 3
NSynth - make music with deep learning 6 Computer Vision • Image classification - is this a cat? • Object detection - self driving car • Generating new images Generating new images using generative adversarial networks (GANs) Is this a cat?
TensorFlow is a deep learning framework for both research & production Write TensorFlow code in C++, Python, Java, R, Go, SWIFT, JavaScript Deploy to CPU, GPU, TPU, Mobile, Android Things, Raspberry Pi tf.* tf.layers tf.keras Custom Estimator Premade Estimator ← Low level ← mid level ← high level ← model in a box ← distributed execution, tf serving 9
Deep learning with Python by Francois Chollet TensorFlow on Youtube TensorFlow on Twitter #AskTensorFlow #TensorFlowMeets Collection of interactive ML examples (blogpost | website) TensorFlow Dev Summit Blog.tensorflow.org 10
remains an independent open-source project, with these as backend • TensorFlow • Theano • CNTK Tf.keras - part of the TensorFlow core APIs Import tensorflow as tf tf.keras... 11
2015 TensorFlow open sourced - 2016 TensorFlow Mobile - 2017 TensorFlow Lite developer preview - 2018 TensorFlow Lite exits developer preview - 2019 TensorFlow Mobile to be deprecated 12
are your options of getting a model, from easy to difficult: • Get a ready-made model through ML Kit • Download a pre-trained model (here): Inception-v3, mobilenet etc. • Use a premade estimator • Use transfer learning with a pre-trained model ◦ Feature extraction or fine tuning on pre-trained model ◦ TensorFlow hub (https://www.tensorflow.org/hub/) • Train your own model from scratch 13
Google’s machine learning expertise to mobile developers in a powerful and easy-to-use package. Powered by TF Lite, with the help of Firebase, ML kits offers: • Dynamic model downloads • A/B testing (via Firebase remote Configuration) • Model compression & conversion (from TensorFlow to TF Lite) Learn more about ML Kit here Image labelling OCR Face detection Barcode scanning Landmark detection Smart reply (coming soon)
learning or training from scratch, you will need a dataset. • Part of the deep learning framework such as Keras: ◦ MNIST, CIFAR10, FASHION_MNIST, IMDB movie reviews etc • Open datasets: ◦ MNIST, MS-COCO, IMAGENet, CelebA etc • Kaggle datasets • Google Dataset search tool: https://toolbox.google.com/datasetsearch 17
Why use a virtual environment? Ease of upgrade/downgrade of tensorflow • Download anaconda here • Create a new virtual environment $ conda create -n [my-env-name] • Activate the virtual environment you created $ conda activate [my-env-name] • Install TensorFlow which also contains tf.keras $ pip install --upgrade tensorflow • Launch Jupyter Notebook server $ jupyter notebook My blog post Anaconda, Jupyter Notebook, TensorFlow, Keras 19
• Jupyter Notebook running on Google’s VM in the cloud • Free GPU and TPU! • TensorFlow is already installed • Save and share from your Drive • Save directly to GitHub Check out my blogpost on Colab, and TensorFlow team’s blog on Colab
use an example of Fashion-MNIST • Direct drop-in replacement for MNIST • 60,000 train set and 10,000 test set • 28x28x1 grayscale images • 10 classes: T-shirt/top, 1 Trouser , 2 Pullover etc... • Popular for computer vision ◦ “hello world” tutorial or ◦ benchmarking ML algorithms 24
Colab Launch code on Colab → here 1. Import data 2. Define a model 3. Train a model 4. Use the model to predict Note: • we need to first “import tensorflow as tf”, then use tf.keras • not “keras import keras” 25
in developer preview • Works with Inception & MobileNet • May not support all operations • The future of TensorFlow for ◦ Mobile: Android & IOS ◦ Android Things ◦ Raspberry Pi 28
from Keras model to a tflite model with the tflite converter There are two options 1. Command line 2. Python API Read the documentation on tflite converter here. 29
code # Create a converter converter = tf.contrib.lite.TocoConverter.from_keras_model_file(model_file) # Set quantize to true converter.post_training_quantize=True # Convert the model tflite_model = converter.convert() # Create the tflite model file tflite_model_name = "mymodel.tflite" open(tflite_model_name, "wb").write(tflite_model) 31
Protip: testing the tflite model in python before putting it in Android - # Load TFLite model and allocate tensors. interpreter = tf.contrib.lite.Interpreter(model_path="converted_model.tflite") interpreter.allocate_tensors() # Get input and output tensors. input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() # Test model on random input data. input_shape = input_details[0]['shape'] input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32) interpreter.set_tensor(input_details[0]['index'], input_data) interpreter.invoke() output_data = interpreter.get_tensor(output_details[0]['index']) print(output_data) 33
Clone tensorflow project from github git clone https://www.github.com/tensorflow/tensorflow Then open the tflite Android demo from Android Studio /tensorflow/contrib/lite/java/demo Note: TensorFlow Lite moved out contrib as of 10/31/2018 35
build.gradle to add dependency of the TensoFlow lite wrapper compile ‘org.tensorflow:tensorflow-lite:+’ Place the .tflite model file under /assets folder Update the Classifier Java class 36