Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Machine Learning On Mobile

Machine Learning On Mobile

Presented at Women In Tech Summit Mid-Atlantic, 2019

Avatar for Tatyana Casino

Tatyana Casino

March 29, 2019

More Decks by Tatyana Casino

Other Decks in Technology

Transcript

  1. Charlottesville, VA / Durham, NC willowtreeapps.com | [email protected] | +1-888-329-9875

    Machine Learning On Mobile WillowTree, LLC @willowtreeapps Tatyana Casino Platform Software Engineer
  2. 2 Intro To Machine Learning Machine learning is the area

    of computational science that focuses on analyzing and interpreting patterns and structures in data. Machine learning is about learning from data, adapting to it, and getting better at predicting results overtime. Machine learning algorithms are computational algorithms which iteratively “learn” an approximation to some function that describes patterns in datasets. A machine learning model is a result of training of a ML algorithm on a dataset. It has specific parameters and can be used to make predictions from inputs.
  3. 3 Machine Learning Components ML = Representation + Evaluation +

    Optimization Representation A certain algorithm A set of classifiers/features that describe the data - the space of allowed models. Transformation of inputs to a form which can be more easily interpreted Evaluation How you select or prefer one model vs. another Described by loss function (scoring / fitness function) Optimization How you search the space of represented models to find the best ones Adjusting hyperparameters (not dependent on data) in order to improve your evaluation metric
  4. 5 Types of ML algorithms Supervised learning - you have

    input and output data and you use an algorithm to learn the mapping function from the input to the output. Unsupervised learning - you only have input data and no corresponding output variables. The goal is to model the underlying structure or distribution in the data. Semi-supervised learning - trained on a combination of labeled and unlabeled data. Reinforcement learning - uses a system of reward and penalty. It is about selecting action policies (sets of rules) to maximize reward in a particular situation.
  5. 6 Deep Learning Neural networks are models that imitate a

    brain They are made of interconnected elements, which process information: an input layer, arbitrary amount of hidden layers, output layer You can think of neural networks as systems of equations A set of weights and biases between each layer determine the strength of predictions Training is finding the best set of weights and biases based on input data that minimizes the loss function NN playground: https://playground.tensorflow.org
  6. 7 Can my task be solved with ML? It’s all

    dependent on data (garbage in - garbage out) Also beware of bias in training sets and not having enough data ML methods cannot access any knowledge outside of the data you provide Think about edge cases. Do thorough user research Overfitting: model performs well on training data, but much worse on unseen data. If it is working with a simple example, it doesn’t mean that it will work just as well with a more sophisticated one
  7. 8 Bad use cases for ML Extrapolation with new or

    unknown factors coming in (e.g. next year’s sales profits based on past data, when an important new competitor just entered the market) Serving the user segments who use the service as a one-off (e.g. an app that allows users to book doctors’ appointments suggests when to schedule your next visit)
  8. 9 Can a human do this task successfully? App that

    detects the amount of calories / carbs / possible allergens from images of food With Gluten Gluten Free
  9. 10 How much math do I need to know? To

    implement ML in your apps and use ML frameworks - not much, really… But it certainly helps if you want to create your own model, gather and analyze your data, the outcomes, investigate issues without the help of a data scientist!
  10. 11 Machine Learning Process 1. Gathering data 2. Preparing and

    cleaning the data 3. Creating a new model or choose an existing one 4. Train the model on your dataset (setting aside a part of it for testing) 5. Evaluation (against data that was excluded the from training set) 6. Tuning (optimizing hyperparameters that are not dependent on data) 7. Prediction (Inference)
  11. 12 Inference Inference is the process of predicting (inferring) results

    from new, previously unseen data after the training of the model. Cloud Services: the model is not directly accessible and the client makes network calls passing their data to the server On-device: the model is stored locally, all the operations are performed strictly on the device, no network calls Custom: set up your own server and upload your custom model
  12. 13 Cloud ML Services AWS ML IBM Watson Microsoft Azure

    Cognitive Services Google Cloud AutoML (beta): Translation, Natural Language, and Vision Many others
  13. 14 Cloud ML: Pros and Cons Pros Cons Simplicity (standard

    network request / response handling) Pricing No need to host or serve the model to user devices Limited or no configuration Cannot be used for unique use cases or uncommon data types, no custom models No need to train, retrain, optimize the models, they are constantly being improved by the provider Cannot work without internet connection No need to know machine learning in depth Latency, not suitable for real-time processing No need to worry about platform-specific issues Privacy concerns, risks that come with passing sensitive data to servers
  14. 15 On-device ML: A big trend on mobile Increasing push

    towards on-device machine learning is one thing that is consistent across all tech giants, including Apple, Google, Amazon, and Microsoft. Android 9.0 Pie (released 2018) has machine learning integrated into the entire operating system. It’s used to predict which apps you may use at a particular time, analyze usage statistics to make the OS adapt to you, and more. Other examples: Face ID, Google Lens Suggestions, Google Call Screening
  15. 17 Mobile Hardware: Designed for AI In late 2017, major

    chip makers like HiSilicon, Qualcomm, and Apple started making their GPUs more powerful. They also started integrating NPUs dedicated to on-device machine learning. A Neural Processing Unit is a microprocessor that specializes in the acceleration of machine learning algorithms. Example: Apple’s Neural Engine is an AI accelerator core within Apple- designed processors.
  16. 18 According to Apple, the new octa-core Neural Engine on

    the A12 Bionic chipset can handle 5 trillion operations per second. Core ML performance can be up to 9 times faster than it was on the A11 bionic. A fully programmable Image, Vision and AI processor for mobile devices. First featured in the Google Pixel 2 released in October, 2017. Google’s Pixel Visual Core Apple’s A12 Bionic (and A13 coming out soon) Mobile Hardware: Examples
  17. 19 On-device inference: Advantages Low Latency It’s best for doing

    anything in real-time, like manipulating live video, applying filters or detecting face landmarks to anchor AR masks. Low / No Connectivity Your ML feature would be available in rural and other settings where access to Internet is bad or not available at all, or data costs are expensive. Example: InstaSaber
  18. 20 Privacy With on-device machine learning, the data never leaves

    user’s device. Users would not be comfortable with certain kinds of personal data (face scans, health data) sent to the servers. Lower costs Running models on-device means you don’t need to pay for hosting your models in the cloud or maintaining a backend. ML workloads often demand GPUs and lots of memory, even just for inference. This can get expensive really fast. On-device inference: Advantages
  19. 21 On-device inference: Drawbacks System utilization Inference is a heavy

    computational process, so it may increase battery power usage. Consider monitoring the battery health, especially for long-running computations. Trade-off between performance and accuracy Smaller models tend to be faster and more energy efficient, but less accurate.
  20. 22 On-device inference: Drawbacks Resource management and application size Models

    may take up multiple megabytes of memory if bundled with the app. Consider downloading the models after app installation or using smaller models. Model updating Developers need to devise a strategy for updating models and delivering them to users. Model security Your app can be reverse engineered and your machine learning model may be stolen.
  21. 24 TensorFlow Lite TensorFlow Lite is a lightweight version of

    TensorFlow for mobile and embedded devices. Supports Android, iOS Enables on-device machine learning inference Supports hardware acceleration with the Android Neural Networks API Supports a set of core operators, both quantized and float, which have been tuned for mobile platforms Supports using custom operations in models
  22. 25 Android Neural Networks API NNAPI distributes the computation workload

    between dedicated neural network hardware, GPUs, and DSPs ML frameworks use it to perform hardware-accelerated inference operations Available on all devices running Android 8.1+ (API level 27+)
  23. 26 Implementation Steps 1. Choose a pre-trained or custom model

    2. Convert the model to a TensorFlow Lite format, optimize if necessary 3. Integrate the model in your app and add the ability to perform inference on device: (1) Load the .tflite model into memory (2) Preprocess data: Transform data to match the input data format expected by the model (e.g. you may need to resize an image or change the image format) (3) Building the interpreter (4) Running inference (5) Retrieve inference results and interpret the tensors in a meaningful way for the app
  24. 27 TensorFlow Lite Converter The TensorFlow Lite converter generates a

    TensorFlow Lite FlatBuffer file (.tflite) from a TensorFlow model (in protocol buffer format). Can be used from a command line or with a Python API (recommended) The TensorFlow Lite Converter takes a TensorFlow graph file and creates a graph file used by the TensorFlow Lite interpreter. https://www.tensorflow.org/lite/convert
  25. 28 TensorFlow Lite Converter: Example IMAGE_SIZE=224 toco \ --graph_def_file=tf_files/retrained_graph.pb \

    --output_file=tf_files/optimized_graph.lite \ --output_format=TFLITE \ --input_shape=1,${IMAGE_SIZE},${IMAGE_SIZE},3 \ --input_array=input \ --output_array=final_result \ --inference_type=FLOAT \ --inference_input_type=FLOAT This will create an optimized_graph.lite file in your tf_files directory.
  26. 29 Optimizing ML models for mobile Post-training quantization reduces model

    size, providing up to 3x lower latency with little degradation in model accuracy. It quantizes weights from floating point to 8-bits of precision. At inference, weights are converted from 8-bits of precision to floating point and computed using floating-point kernels. See https://www.tensorflow.org/lite/performance/best_practices
  27. 30 TensorFlow Lite on Android Example: Image classifier model The

    model should be added to app’s resources (into the assets folder) Add a TensorFlow Lite dependency in Gradle build file (you can replace + with the desired build number)
  28. 31 TensorFlow Lite on Android Call the run method on

    the Interpreter, passing it the image data and the labels array. TensorFlow Lite will then execute the model and write the outputs. See https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android An Interpreter loads a model and allows you to run it, by providing it with a set of inputs. Create an Interpreter and load your model:
  29. 32 CreateML: Build and train your own ML models Is

    a GPU-accelerated tool for native AI model training on Mac computers. Uses Swift and macOS playgrounds Easy to use: add your text or image dataset, write a few lines of code, and run the playground Supports: Image and text classification Regression
  30. 33 Should I use CreateML? Pros Cons Easy to use

    - small amount of code, especially for proof of concept apps The computing power is restricted to Mac laptop and desktop machines, so processing very large datasets may still take a long time Easy to learn and understand, no knowledge of extra ML tools required No data preprocessing tools Very few ways to tune and debug the final model, since model structure and algorithms are hidden from the developers iOS/macOS only
  31. 34 CoreML 2 Runs strictly on-device Core ML supports custom

    models along with these frameworks: Vision for image analysis (face and landmark detection, text detection, barcode recognition,…) Natural Language (language identification, tokenization, and detecting names) GameplayKit (evaluating learned decision trees, pathfinding, agent behavior)
  32. 35 CoreML 2 vs Core ML 1 Core ML 2

    is more efficient Core ML 2 is 30% faster thanks to batch prediction, and quantization can shrink the size of models by up to 75%. Supports 16-bit Floating Point and all levels of quantization Weights can be encoded using any number of bits, even down to 1 bit. This can greatly reduce model size
  33. 36 CoreML: Implementation steps Add the model to your Xcode

    project by dragging the model into the project navigator. Xcode will generate custom interfaces to the model so you can interact with the model in your code. These interfaces represent the model, its inputs and outputs. Use the generated model initializer to create the model Get input values to pass to the model Make a prediction by using a generated prediction() method on the model class Retrieve results from generated model output interface
  34. 37 Example: Vision Core ML generates a Swift class for

    your ML model Create and run the Vision Classification Request let model = try VNCoreMLModel(for: MobileNet().model) let request = VNCoreMLRequest(model: model, completionHandler: { [weak self] request, error in self?.processClassifications(for: request, error: error) }) request.imageCropAndScaleOption = .centerCrop return request DispatchQueue.global(qos: .userInitiated).async { let handler = VNImageRequestHandler(ciImage: ciImage, orientation: orientation) do { try handler.perform([self.classificationRequest]) } catch { print("Failed to perform classification.\n\(error.localizedDescription)") } }
  35. 38 Example: Vision Handle Image Classification Results func processClassifications(for request:

    VNRequest, error: Error?) { DispatchQueue.main.async { guard let results = request.results else { self.classificationLabel.text = "Unable to classify image.\n\(error!.localizedDescription)" return } // The `results` will always be `VNClassificationObservation` s let classifications = results as! [VNClassificationObservation]
  36. 39 CoreML models Core ML requires the Core ML model

    format (models with a .mlmodel file extension) Apple also provides popular, open source models that are already in the Core ML format. https://github.com/likedan/Awesome-CoreML-Models MobileNet models - Well-known computer vision models for mobile Caffe Model Zoo - Big list of models in Caffe format MXNet Model Zoo - Collection of MXNet models
  37. 40 coremltools A Python framework that converts ML models from

    popular frameworks like Caffe, Caffe 2, Keras, sci-kit-learn, XGBoost, LibSVM and TensorFlow Lite. Has a utility for quantization pip3 install coremltools==2.0 Conversion usage example:
  38. 41 MLKit: Overview Production-ready out-of-the-box solutions for Android and iOS

    On-device or cloud ML (may vary by the API) Firebase can host and serve custom TensorFlow Lite models to your app
  39. 42 Feature On-device Cloud Pricing (Cloud) Text recognition ✓ ✓

    Free for first 1000 uses per month Face detection ✓ Barcode scanning ✓ Image labeling ✓ ✓ Landmark recognition ✓ Custom model inference ✓ Smart Reply (coming soon) MLKit: Features
  40. 43 MLKit Base APIs: Implementation Enable the Cloud-based APIs (Optional

    / If Applicable) Integrate the SDK into your app Include the SDK using Gradle (for Android) or CocoaPods (for iOS) Prepare input data (e.g. capture an image from the camera) Pass your data to the ML model
  41. 44 Identify language of text: Android Set up Firebase: https://firebase.google.com/docs/android/setup

    Create a Firebase project, register your app, add configuration file and Firebase SDKs Or use an assistant: in Android Studio, select Tools > Firebase Click Connect to Firebase to register your app with an existing or new Firebase project Add MLKit Gradle dependencies:
  42. 46 Using custom models with MLKit Host your models using

    Firebase Reduce your app's binary size and make sure your app is always using the most recent version available of your model On-device ML inference Perform inference in the app by using the ML Kit SDK. The model can be bundled with the app, hosted in the cloud, or both Automatic model fallback Specify multiple model sources; use a locally- stored model when the cloud-hosted model is unavailable Automatic model updates Configure the conditions under which your app automatically downloads new versions of your model: (device is idle, is charging, is on Wi-Fi)
  43. 47 Custom Model: Implementation Steps Build and train your TensorFlow

    model Convert the model to TensorFlow Lite format Host your model with Firebase Use your model for inference in your app
  44. 48 Custom TensorFlow Lite Models on Android Add Gradle dependencies

    If hosting on Firebase: Upload your model to Firebase Add permissions to Android Manifest Convert your model to TensorFlow Lite format
  45. 49 Custom TensorFlow Lite Models on Android To ensure Gradle

    doesn’t compress the models when building the app: The model file will be included in the app package and available to ML Kit as a raw asset.
  46. 51 Custom TensorFlow Lite Models on Android A TFLite model

    takes as input and produces as output one or more multidimensional arrays. Use the TensorFlow Lite Python interpreter to inspect your model.
  47. 53 Other SDKs: fritz.ai Supports: Android, iOS Ready-to-use features: Identify

    objects within an image or live video Style Transfer Image Labeling Image Segmentation: recognize objects, identify which pixels belong to each object. Pose Estimation: identify and track a person's body position. Analytics: capture real-world data with user opt-in Custom model hosting, push model updates over-the-air Performance Monitoring Free usage includes: Use a basic version of ready- to-use features Measure, manage, and deploy up to 10 models Up to 1 million monthly active devices
  48. 54 What about Cross-Platform SDKs? Xamarin Programming Language: C# Supported

    by Microsoft Since 2013 Kotlin/Native Programming Language: Kotlin Supported by JetBrains Since 2018 Flutter Programming Language: Dart Supported by Google Since 2017 React Native Programming Language: JavaScript Supported by Facebook Since 2013
  49. 55 Cross-Platform Machine Learning Xamarin https://github.com/migueldeicaza/ TensorFlowSharp MLKit bindings are

    a work in progress Kotlin Native (Multiplatform) https://github.com/JetBrains/kotlin-native/ tree/master/samples/tensorflow Flutter tflite 1.0.1 - A plugin for accessing TensorFlow Lite API. Supports Classification and Object Detection on both iOS and Android. https://pub.dartlang.org/packages/ firebase_ml_vision https://pub.dartlang.org/packages/tflite React Native https://github.com/reneweb/react-native-tensorflow
  50. 56 Which framework should I choose? Platforms Model Updates Pricing

    (Cloud) CoreML 2 iOS Yes, No Hosting CloudKit Hosting TensorFlow Lite iOS, Android No - MLKit iOS, Android Yes, With Hosting Free until 1000 usages a month fritz.ai iOS, Android Yes, With Hosting Free if < 1 mln active devices
  51. 57 Summary On-device inference has become increasingly popular and has

    a lot of advantages. In order to be used strictly on a mobile device, ML models require optimization, careful resource management, updating strategy. Adding the model to the app and interacting with it is not the hardest part. The real challenge lies (as always) in preparing the dataset and optimizing the model.