ML Kit in Actions
ML Kit in Actions
• Custom Models
• Custom TensorFlow build
• General feedbacks
The building blocks of ML Kit
Google Cloud
Mobile Vision API + = ML Kit Vision APIs
Vision API
On-device Cloud
FirebaseVisionTextDetector
OUTPUT FirebaseVisionText
MobileThings: ML Kit in action
Face tracking
Landmark
Classification
Face Orientation
• Euler X
• Euler Y
• Euler Z
Landmarks
• A landmark is a point of interest within a face. The left
eye, right eye, and nose base are all examples of
landmarks
Classification
• 2 classifications are supported: Eye open (left & right eye) & Smiling
• Inspiration: Android Things photo booth
Face Detection Options
FirebaseVisionFaceDetectorOptions options =
new FirebaseVisionFaceDetectorOptions.Builder()
.setModeType(FirebaseVisionFaceDetectorOptions.ACCURATE_MODE)
.setLandmarkType(FirebaseVisionFaceDetectorOptions.ALL_LANDMARKS)
.setClassificationType(FirebaseVisionFaceDetectorOptions.ALL_CLASSIFICATIONS)
.setMinFaceSize(0.15f)
.setTrackingEnabled(true)
.build();
INPUT FirebaseVisionImage
FirebaseVisionFaceDetector
OUTPUT List<FirebaseVisionFace>
!
➡ boundingBox: Rect
➡ trackingId: Int
➡ headEulerAngleY: Float
➡ headEulerAngleZ: Float
➡ smilingProbability: Float
➡ leftEyeOpenProbability: Float
➡ rightEyeOpenProbability: Float
Feedback
Pricing Free Free for first 1000 uses of this feature per month
400+ labels that cover the most 10,000+ labels in many categories. See below.
Label coverage commonly-found concepts in Also, try the Cloud Vision API demo to see what
photos. See below. labels can be found for an image you provide.
Knowledge
Graph entity ID
support
INPUT FirebaseVisionImage
FirebaseVisionLabelDetector
OUTPUT List<FirebaseVisionLabel>
🎒 ➡ label: String
➡ confidence: Float
➡ entityId: String
Landmark Recognition
Camera
1 Image (Bitmap) 2 input_tensor
Preview
Classifier TensorFlow
Implementation JNI wrapper
Trained Model
Overlay
Classifications + Confidence 4 top_results 3
Display
Ref: https://jalammar.github.io/Supercharging-android-apps-using-tensorflow/
Magritte
Ceci n’est pas une pomme.
Android Makers Paris, April 2017
Model Size
All weights are stored as they are (64-bit floats) => 80MB
!46
Weights Quantization
6.372638493746383 => 6.4
!47
Source: https://www.tensorflow.org/performance/quantization
Model Inception V3
Optimized & Quantized
Google I/O, May 2017
Google AI Blog, June 2017
MobileNet
Mobile-first computer vision models for TensorFlow
!52
Image credit : https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet_v1.md
~80Mb => ~20Mb => ~1-5Mb
Source: https://research.googleblog.com/2017/06/mobilenets-open-source-models-for.html
DevFest Nantes, October 2017
Model MobileNets_0.25_224
Google I/O, May 2018
Custom Model: Key capabilities
TOCO
(TensorFlow Lite
Optimizing
Converter)
How to train your dragon model?
Train your model
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
Source: https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/
TF Lite conversion for retrained quantized model is currently unavailable.
Firebase quickstart ML Kit sample only aimes quantized model.
Convert to tflite format
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
Source: https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/
Do you need custom bob models?
Use custom models if
FirebaseModelInterpreter
OUTPUT FirebaseModelOutputs
// input & output options for non-quantized model
ByteBuffer
🍎
🍌
🥝
🍊
OUTPUT FirebaseModelOutputs
🍓
🍇
🍉
🍋
🍍
Performance Benchmarks
Model MobileNets_1.0_224
Model MobileNets_1.0_224
• No callback or other feedback for model downloading
• Model downloading seems to be blocking => do not use on main
thread