Share
Embed
ML Kit's on-device APIs
Android11 Meetups - 機械学習 #gdgtokyo
https://gdg-tokyo.connpass.com/event/181266/
Yuki Anzai
July 07, 2020
Transcript
102 slides
-
ML Kit • 機械学習を利⽤する機能をアプリに簡単に組み込むためのモバイル SDK • iOS と Android で使える
• Firebase 不要 • 無料 • 現在はβリリース • https://developers.google.com/ml-kit/ -
ML Kit • on-device API のみ • 速い • Offline
でも動く • live動画のリアルタイム処理が可能 -
Vision Natural Language Barcode scanning バーコードスキャン Face detection 顔検出 Image
labeling 画像のラベル付け Object detection & tracking 物体検出 Text recognition テキスト認識 Language identification ⾔語識別 Translation 翻訳 Smart Reply スマートリプライ -
新しい ML Kit への移⾏ • 既存の ML Kit for Firebase
SDK • on-device based API → deprecated → ML Kit に移⾏ • cloud based API → 変更の必要なし(Firebase ML) -
Custom model の移⾏ • Custom model の管理と配布の機能は引き続き Firebase ML SDK
で提供 • 既存の ML Kit for Firebase SDK の Custom model の"推論機能"は deprecated • → TensorFlow Lite runtime を直接使う • → ML Kit に組み込んで使う("Image labeling : 画像のラベル付け" と "Object detection & tracking : 物体検出" 向けの場合のみ) -
Bundled vs Thin • Bundled model : アプリにモデルを含める • アプリサイズが⼤きくなる
• アプリインストール直後から機能が使える • Thin model : Google Play Services を通してモデルをダウンロード • アプリサイズが⼩さくてすむ • ダウンロードが完了するまで機能が使えない -
Vision APIs API Bundled Thin Barcode scanning : バーコードスキャン ◯
◯ Face detection : 顔検出 ◯ ◯ Image labeling : 画像のラベル付け ◯ なし Object detection & tracking : 物体検出 ◯ なし Text recognition : テキスト認識 なし ◯ -
Natural Language APIs API Bundled Thin Language identification : ⾔語識別
◯ なし Translation : 翻訳 ◯ なし Smart Reply : スマートリプライ ◯ なし -
Lifecycle Support • 推論を実⾏する detector/scanner/labeler/translator などはすべ て Closable かつ LifecycleObserver
になった • 不要になったタイミングで close() を呼ぶ必要がある • lifecycle.addObserver(detector/scanner/labeler/translator ) で きるようになった -
Barcode scanning : バーコードスキャン • ほとんどの標準フォーマットをサポート • 1次元 : Codabar,
Code 39, Code 93, Code 128, EAN-8, EAN-13, ITF, UPC-A, UPC-E • 2次元 : Aztec, Data Matrix, PDF417, QR Code • ⾃動フォーマット検出 • structured data (WiFi情報など)の取り出し • バーコードの向きによらず検出可能 -
Bundled vs Thin • Bundled • アプリサイズが約 2.2MB 増える •
V2 model (V1 より速くて正確) • Thin • アプリサイズは変わらない • V1 model -
インストール時ダウンロードオプション • Thin model の場合 <application ...> ... <meta-data android:name="com.google.mlkit.vision.DEPENDENCIES"
android:value="barcode" /> <!-- To use multiple models: android:value="barcode,model2,model3" --> </application> -
val options = BarcodeScannerOptions.Builder() .setBarcodeFormats( Barcode.FORMAT_EAN_8, Barcode.FORMAT_EAN_13 ) .build() val
scanner = BarcodeScanning.getClient(options) -
val mediaImage = imageProxy.image if (mediaImage != null) { val
image = InputImage.fromMediaImage( mediaImage, imageProxy.imageInfo.rotationDegrees ) scanner.process(image) .addOnSuccessListener { barcodes -> // Task completed successfully // ... } .addOnFailureListener { e -> // Task failed with an exception // ... } } -
Face detection : 顔検出 • 顔の領域、ランドマーク(⽬・頬・⿐・⽿・⼝)、輪郭情報(顔の外郭・ ⽬・眉・⿐・⼝)の位置認識 • 顔の表情(⽬の開閉度合い、笑顔の度合い)の認識 •
動画のフレーム間で同じ顔をトラック可能 • Euler X (前後の傾き)が取れるようになった -
Bundled vs Thin • Bundled • アプリサイズが約 16MB 増える •
Thin • アプリサイズは変わらない -
インストール時ダウンロードオプション • Thin model の場合 <application ...> ... <meta-data android:name="com.google.mlkit.vision.DEPENDENCIES"
android:value="face" /> <!-- To use multiple models: android:value="face,model2,model3" --> </application> -
// High-accuracy landmark detection and face classification val options =
FaceDetectorOptions.Builder() .setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_ACCURATE) .setLandmarkMode(FaceDetectorOptions.LANDMARK_MODE_ALL) .setClassificationMode(FaceDetectorOptions.CLASSIFICATION_MODE_ALL) .build() // or // Real-time contour detection val options = FaceDetectorOptions.Builder() .setContourMode(FaceDetectorOptions.CONTOUR_MODE_ALL) .build() val detector = FaceDetection.getClient(options) -
val mediaImage = imageProxy.image if (mediaImage != null) { val
image = InputImage.fromMediaImage( mediaImage, imageProxy.imageInfo.rotationDegrees ) detector.process(image) .addOnSuccessListener { faces -> // Task completed successfully // ... } .addOnFailureListener { e -> // Task failed with an exception // ... } } -
Image labeling : 画像のラベル付け • 画像の内容を解析し、認識したもののラベルをつける : ⼈、物、 場所、活動など •
general-purpose base classifier • 400+ labels をサポート • Custom model と組み合わせて独⾃のラベル付けが可能 • Custom TensorFlow Lite models • Custom AutoML Vision Edge models -
val labeler = ImageLabeling.getClient(ImageLabelerOptions.DEFAULT_OPTIONS) // or val options = ImageLabelerOptions.Builder()
.setConfidenceThreshold(0.9f) .build() val labeler = ImageLabeling.getClient(options) -
val mediaImage = imageProxy.image if (mediaImage != null) { val
image = InputImage.fromMediaImage( mediaImage, imageProxy.imageInfo.rotationDegrees ) labeler.process(image) .addOnSuccessListener { labels -> // Task completed successfully // ... } .addOnFailureListener { e -> // Task failed with an exception // ... } } -
TensorFlow Lite model の利⽤ • TensorFlow Hub からダウンロードしたモデルや、TensorFlow Lite Model
Maker や TensorFlow を使って⾃分で学習したモデルを利⽤で きる • model はアプリにバンドルされている必要がある -
Model file • assets/ に .tflite や .lite などの model
file を置く • 以下の noCompress 設定を build.gradle に書く(Android Gradle plugin 4.1 以上で .tflite の場合は不要) android { ... aaptOptions { // Your model's file extension: "tflite", "lite", etc. noCompress "tflite" } } -
val localModel = LocalModel.Builder() .setAssetFilePath("lite-model_aiy_vision_classifier_food_V1_1.tflite") // or .setAbsoluteFilePath(absolute file path
to tflite model) .build() val options = CustomImageLabelerOptions.Builder(localModel) .setConfidenceThreshold(0.5f) .setMaxResultCount(5) .build() val labeler = ImageLabeling.getClient(options) -
AutoML Vision Edge model の利⽤ • AutoML Vision Edge を使ってトレーニングしたモデルを利⽤できる
• model のバンドルオプション • アプリにバンドル • Firebase プロジェクトが不要 • Firebase ML に置いて実⾏時にダウンロード • model の更新や A/B テストが容易にできる -
Artifact (AutoML Models) • com.google.mlkit:image-labeling-automl:16.1.0 • Firebase ML の Model
deployment service を使う場合は以下 も必要 • com.google.mlkit:linkfirebase:16.0.0 -
Model file(バンドルする場合) • assets/ に Auto ML Vision Edge で作成した
model.tflite、dict.txt、 manifest.json を置く • 以下の noCompress 設定を build.gradle に書く(Android Gradle plugin 4.1 以上で .tflite の場合は不要) android { ... aaptOptions { // Your model's file extension: "tflite", "lite", etc. noCompress "tflite" } } -
val localModel = AutoMLImageLabelerLocalModel.Builder() .setAssetFilePath("flower/manifest.json") // or .setAbsoluteFilePath(absolute file path
to manifest file) .build() バンドルする場合 -
val downloadConditions = DownloadConditions.Builder() .requireWifi() .build() RemoteModelManager.getInstance() .download(remoteModel, downloadConditions) .addOnSuccessListener
{ val options = AutoMLImageLabelerOptions.Builder(remoteModel) .setConfidenceThreshold(0.5f) .build() labeler = ImageLabeling.getClient(options) } Firebase ML に置く場合 -
Object detection & tracking: 物体認識 • 画像からオブジェクトを検出 • デフォルト •
粗いカテゴリー分類(5種類) • Custom model と組み合わせて独⾃の分類が可能 • Custom TensorFlow Lite models -
// Live detection and tracking val options = ObjectDetectorOptions.Builder() .setDetectorMode(ObjectDetectorOptions.STREAM_MODE)
.enableClassification() // Optional .build() // or // Multiple object detection in static images val options = ObjectDetectorOptions.Builder() .setDetectorMode(ObjectDetectorOptions.SINGLE_IMAGE_MODE) .enableMultipleObjects() .enableClassification() // Optional .build() val objectDetector = ObjectDetection.getClient(options) -
val mediaImage = imageProxy.image if (mediaImage != null) { val
image = InputImage.fromMediaImage( mediaImage, imageProxy.imageInfo.rotationDegrees ) objectDetector.process(image) .addOnSuccessListener { detectedObjects -> // Task completed successfully // ... } .addOnFailureListener { e -> // Task failed with an exception // ... } } -
objectDetector.process(image) .addOnSuccessListener { detectedObjects -> for (detectedObject in detectedObjects) {
for (label in detectedObject.labels) { val name = label.text val confidence: Int = label.confidence.times(100).toInt() when (label.index) { PredefinedCategory.HOME_GOOD_INDEX -> { ... } } ... } } } ... -
TensorFlow Lite model の利⽤ • TensorFlow Hub からダウンロードしたモデルや、TensorFlow Lite Model
Maker や TensorFlow を使って⾃分で学習したモデルを利⽤で きる • model はアプリにバンドルされている必要がある -
Model file • assets/ に .tflite や .lite などの model
file を置く • 以下の noCompress 設定を build.gradle に書く(Android Gradle plugin 4.1 以上で .tflite の場合は不要) android { ... aaptOptions { // Your model's file extension: "tflite", "lite", etc. noCompress "tflite" } } -
// Live detection and tracking val customObjectDetectorOptions = CustomObjectDetectorOptions .Builder(localModel)
.setDetectorMode(CustomObjectDetectorOptions.STREAM_MODE) .enableClassification() .setClassificationConfidenceThreshold(0.5f) // Optional .setMaxPerObjectLabelCount(3) // Optional .build() val objectDetector = ObjectDetection.getClient(customObjectDetectorOptions) -
インストール時ダウンロードオプション <application ...> ... <meta-data android:name="com.google.mlkit.vision.DEPENDENCIES" android:value="ocr" /> <!-- To
use multiple models: android:value="ocr,model2,model3" --> </application> -
val recognizer = TextRecognition.getClient() ... val mediaImage = imageProxy.image if
(mediaImage != null) { val image = InputImage.fromMediaImage( mediaImage, imageProxy.imageInfo.rotationDegrees ) recognizer.process(image) .addOnSuccessListener { visionText -> // Task completed successfully // ... } .addOnFailureListener { e -> // Task failed with an exception // ... } } -
Language Identification : ⾔語ID • ⽂字列からその⾔語を識別(BCP-47 language code) • 100以上の⾔語に対応
• https://developers.google.com/ml-kit/language/ identification/langid-support • 最も可能性の⾼い⾔語を1つ識別(Confidence 情報な し) or 可能性のある複数の⾔語を識別(Confidence 情報あり) -
val languageIdentifier = LanguageIdentification.getClient() languageIdentifier.identifyLanguage(text) .addOnSuccessListener { languageCode -> when(languageCode)
{ "und" -> { // Can't identify language. } else -> { // Success } } } .addOnFailureListener { // Error } -
Translation : 翻訳 • 50 以上の⾔語間の翻訳が可能 • https://developers.google.com/ml-kit/language/ translation/translation-language-support •
Google Translate の offline mode と同じモデル • 帰属の表⽰要件、制限事項などのガイドラインあり -
val options = TranslatorOptions.Builder() .setSourceLanguage(TranslateLanguage.ENGLISH) .setTargetLanguage(TranslateLanguage.JAPANESE) .build() val translator =
Translation.getClient(options) ... lifecycle.addObserver(translator) -
val conditions = DownloadConditions.Builder() .requireWifi() .build() translator.downloadModelIfNeeded(conditions) .addOnSuccessListener { //
Model downloaded successfully. Okay to start translating. } .addOnFailureListener { exception -> // Model couldn’t be downloaded or other internal error. } -
Smart Reply : スマートリプライ • 会話に関連する返事を⽣成し提案 • 単⼀のメッセージではなく会話のフルコンテキストから⽣成 • 現在は
English のみサポート • 他⾔語と判定された場合提案なしになる • sensitive topics があると判定された場合提案なしになる • 返事の候補は最⼤3つ提案される -
val conversation = mutableListOf<TextMessage>() ... conversation.add( TextMessage.createForLocalUser( localUserMessage.text, localUserMessage.time )
) conversation.add( TextMessage.createForRemoteUser( remoteUserMessage.text, remoteUserMessage.time, remoteUserMessage.userId ) ) -
val smartReplyGenerator = SmartReply.getClient() lifecycle.addObserver(smartReplyGenerator) smartReplyGenerator.suggestReplies(conversation) .addOnSuccessListener { when (it.status)
{ SmartReplySuggestionResult.STATUS_NOT_SUPPORTED_LANGUAGE -> { // The conversation's language isn't supported } SmartReplySuggestionResult.STATUS_SUCCESS -> { // Task completed successfully } } } .addOnFailureListener { exception -> // Task failed with an exception } -
Custom model の要件は? • https://developers.google.com/ml-kit/custom-models#model- compatibility • 1つの input tensor
• RGB pixel format, INT8 or FLOAT32 type, dimensions : 1xHxWxC(W : width, H: height, C : channels (必ず 3)) • 1つ以上の output tensor • dimensions: (1xN) または (1x1x1xN) (N : クラスの数) -
既存の model で使えるものは? • TensorFlow Hub : ML Kit compatible
models • https://tfhub.dev/ml-kit/collections/image-classification/1 -
⾃分で model を学習する⽅法は? • AutoML Vision Edge • TensorFlow Lite
Model Maker • TensorFlow neural-network model を転移学習して on-device 向けに変換する処 理を簡単にしてくれるライブラリ • https://www.tensorflow.org/lite/tutorials/model_maker_image_classification • TensorFlow で model を学習して TensorFlow Lite に変換する • https://www.tensorflow.org/lite/convert -
Early access program • Entity extraction : エンティティの抽出 • Pose
detection : ポーズ検出 -
Pose detection • 対象の物理的な動作(= ポーズ)をトラッキング する機能 • ⼿や⾜を含む 33 ポイントをトラッキング
• https://developers.google.com/ml-kit/early- access/pose-detection -
Codelabs • 「Recognize text and facial features with ML Kit:
Android」 • https://codelabs.developers.google.com/codelabs/mlkit-android • 「Recognize, Identify Language and Translate text with ML Kit and CameraX: Android」 • https://codelabs.developers.google.com/codelabs/mlkit-android- translate -
Firebase ML • Vision • Text recognition : テキスト認識 •
Image labeling : 画像のラベル付け • Landmark recognition : ランドマーク認識 • AutoML Vision Edge • Deploy & manage custom models https://firebase.google.com/products/ml