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

ML Kit in Action

jinqian
June 20, 2018

ML Kit in Action

Slides for MobileThings meetup: When Machine Learning meets Augmented Reality (ML Kit / Core ML + ARKit)

Demo repo can be found here: https://github.com/jinqian/MLKit-in-actions

jinqian

June 20, 2018
Tweet

More Decks by jinqian

Other Decks in Technology

Transcript

  1. ML Kit in Action (Android) Mobile Things S02E03 When machine

    learning meets augmented reality Qian JIN | @bonbonking | [email protected] Image Credit: https://becominghuman.ai/part-1-migrate-deep-learning-training-onto-mobile-devices-c28029ffeb30
  2. ML Kit in Action • The building blocks of ML

    Kit • Vision APIs: text recognition, face detection, barcode scanning, image labeling, landmark recognition • Custom Models • Custom TensorFlow build • General feedbacks
  3. Mobile Vision API TensorFlow Lite Neural Network API Google Cloud

    Vision API + ML Kit Vision APIs + ML Kit Custom Models / TF Lite Build = =
  4. Text Recognition On-device Cloud Pricing Free Free for first 1000

    uses of this feature per month Ideal use cases Real-time processing High-accuracy text recognition Document scanning Language support Latin characters A broad range of languages and special characters
  5. MobileThings: ML Kit in action for (FirebaseVisionText.Block block: firebaseVisionText.getBlocks()) {

    Rect boundingBox = block.getBoundingBox(); Point[] cornerPoints = block.getCornerPoints(); String text = block.getText(); for (FirebaseVisionText.Line line: block.getLines()) { // ... for (FirebaseVisionText.Element element: line.getElements()) { // ... } } }
  6. Face Detection: Key Capabilities • Recognise and locate facial features

    • Recognise facial expressions • Track faces across video frames • Process video frames in real time
  7. Landmarks • A landmark is a point of interest within

    a face. The left eye, right eye, and nose base are all examples of landmarks
  8. Classification • 2 classifications are supported: Eye open (left &

    right eye) & Smiling • Inspiration: Android Things photo booth
  9. ➡ boundingBox: Rect ➡ trackingId: Int ➡ headEulerAngleY: Float ➡

    headEulerAngleZ: Float ➡ smilingProbability: Float ➡ leftEyeOpenProbability: Float ➡ rightEyeOpenProbability: Float !
  10. Feedback • Real-time application: pay attention to the image size

    /fr.xebia.mlkitinactions E/pittpatt: online_face_detector.cc:236] inconsistent image dimensions detector.cc:220] inconsistent image dimensions /fr.xebia.mlkitinactions E/NativeFaceDetectorImpl: Native face detection failed java.lang.RuntimeException: Error detecting faces. at com.google.android.gms.vision.face.NativeFaceDetectorImpl.detectFacesJni(Native Method)
  11. Image Labeling On-device Cloud Pricing Free Free for first 1000

    uses of this feature per month Label coverage 400+ labels that cover the most commonly-found concepts in photos. See below. 10,000+ labels in many categories. See below. Also, try the Cloud Vision API demo to see what labels can be found for an image you provide. Knowledge Graph entity ID support
  12. Landmark Recognition • Still in preview, using Cloud Vision API

    instead • Recognizes well-known landmarks • Get Google Knowledge Graph entity IDs • Low-volume use free (first 1000 images)
  13. !40

  14. Android SDK (Java) Android NDK (C++) Classifier Implementation TensorFlow JNI

    wrapper Image (Bitmap) Trained Model top_results Classifications + Confidence input_tensor 1 2 3 4 Camera Preview Ref: https://jalammar.github.io/Supercharging-android-apps-using-tensorflow/ Overlay Display
  15. ~80MB -> ~20MB !47 Weights Quantization 6.372638493746383 => 6.4 Source:

    https://www.tensorflow.org/performance/quantization
  16. MobileNet Mobile-first computer vision models for TensorFlow !52 Image credit

    : https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet_v1.md
  17. Custom Model: Key capabilities • TensorFlow lite model hosting •

    On-device ML inference • Automatic model fallback • Automatic model updates
  18. Convert model to TF Lite (model.lite) Host your TF Lite

    model on Firebase Use the TF Lite model for inference Train your TF model (model.pb) TOCO (TensorFlow Lite Optimizing Converter)
  19. Train your model Source: https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/ python -m tensorflow/examples/image_retraining/retrain.py \ --bottleneck_dir=tf_files/bottlenecks

    \ --how_many_training_steps=500 \ --model_dir=tf_files/models/ \ --summaries_dir=tf_files/training_summaries/ \ --output_graph=tf_files/retrained_graph.pb \ --output_labels=tf_files/retrained_labels.txt \ —architecture=mobilenet_0.50_224 \ --image_dir=tf_files/fruit_photos
  20. TF Lite conversion for retrained quantized model is currently unavailable.

    Firebase quickstart ML Kit sample only aimes quantized model.
  21. Convert to tflite format Source: https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/ bazel run --config=opt \

    //tensorflow/contrib/lite/toco:toco -- \ --input_file=/tmp/magritte_retrained_graph.pb \ --output_file=/tmp/magritte_graph.tflite \ --inference_type=FLOAT \ --input_shape=1,224,224,3 \ --input_array=input \ --output_array=final_result \ --mean_value=128 \ --std_value=128 \ --default_ranges_min=0 \ --default_ranges_max=6
  22. Use custom models if • Specific needs CAN NOT be

    met by general purpose APIs • Need high matching precision • You are an experienced ML developer (or you know Yoann Benoit) Let me train your model!
  23. // input & output options for non-quantized model val inputDims

    = intArrayOf(DIM_BATCH_SIZE, DIM_IMG_SIZE_X, DIM_IMG_SIZE_Y, DIM_PIXEL_SIZE) val outputDims = intArrayOf(1, labelList.size) inputOutputOptions = FirebaseModelInputOutputOptions.Builder() .setInputFormat(0, FirebaseModelDataType.FLOAT32, inputDims) .setOutputFormat(0, FirebaseModelDataType.FLOAT32, outputDims) .build()
  24. // input & output options for non-quantized model val inputDims

    = intArrayOf(DIM_BATCH_SIZE, DIM_IMG_SIZE_X, DIM_IMG_SIZE_Y, DIM_PIXEL_SIZE) val outputDims = intArrayOf(1, labelList.size) inputOutputOptions = FirebaseModelInputOutputOptions.Builder() .setInputFormat(0, FirebaseModelDataType.FLOAT32, inputDims) .setOutputFormat(0, FirebaseModelDataType.FLOAT32, outputDims) .build()
  25. • No callback or other feedback for model downloading •

    Model downloading seems to be blocking => do not use on main thread • Lack of documentations at this point (e.g. how to stop the interpreter?) • Slight performance loss comparing to TensorFlow Lite • A/B test your machine learning model!
  26. HowTo: Face Recognition Model • Trained with Keras + FaceNet

    • Converted to TensorFlow • Then converted to TensorFlow lite • Then we got stuck…
  27. Custom TF Lite build • ML Kit uses a pre-built

    TensorFlow Lite library • Build your own AAR with bazel • Add custom ops for example
  28. ML Kit: State of the art • Lack of high

    quality demos (e.g. firebase mlkit quickstart, bugs, deprecated camera API, deformed camera preview) • Lack of high level guidelines / best practises • Performance issue on old devices
  29. The best is yet to come • Face contours: 100

    data points • Smart Reply: conversation model • Online model compression
  30. References • Talk Magritte for DroidCon London • Medium article:

    Android meets Machine Learning • Github Repo for demo • Joe Birch: Exploring Firebase MLKit on Android: Introducing MLKit (Part one) • Joe Birch: Exploring Firebase MLKit on Android: Face Detection (Part Two) • Merci Sandra ;)