Slide 1

Slide 1 text

ML Kit's on-device APIs Google Developers Expert for Android あんざいゆき / yanzm

Slide 2

Slide 2 text

ML Kit のリブランディング

Slide 3

Slide 3 text

今まで MLKit on-device API cloud API

Slide 4

Slide 4 text

今まで これから MLKit on-device API cloud API MLKit on-device API cloud API

Slide 5

Slide 5 text

新しい ML Kit

Slide 6

Slide 6 text

ML Kit • 機械学習を利⽤する機能をアプリに簡単に組み込むためのモバイル SDK • iOS と Android で使える • Firebase 不要 • 無料 • 現在はβリリース • https://developers.google.com/ml-kit/

Slide 7

Slide 7 text

ML Kit • on-device API のみ • 速い • Offline でも動く • live動画のリアルタイム処理が可能

Slide 8

Slide 8 text

Vision Natural Language Barcode scanning バーコードスキャン Face detection 顔検出 Image labeling 画像のラベル付け Object detection & tracking 物体検出 Text recognition テキスト認識 Language identification ⾔語識別 Translation 翻訳 Smart Reply スマートリプライ

Slide 9

Slide 9 text

新しい ML Kit への移⾏

Slide 10

Slide 10 text

新しい ML Kit への移⾏ • 既存の ML Kit for Firebase SDK • on-device based API → deprecated → ML Kit に移⾏ • cloud based API → 変更の必要なし(Firebase ML)

Slide 11

Slide 11 text

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 : 物体検出" 向けの場合のみ)

Slide 12

Slide 12 text

Bundled vs Thin

Slide 13

Slide 13 text

Bundled vs Thin • Bundled model : アプリにモデルを含める • アプリサイズが⼤きくなる • アプリインストール直後から機能が使える • Thin model : Google Play Services を通してモデルをダウンロード • アプリサイズが⼩さくてすむ • ダウンロードが完了するまで機能が使えない

Slide 14

Slide 14 text

Vision APIs API Bundled Thin Barcode scanning : バーコードスキャン ◯ ◯ Face detection : 顔検出 ◯ ◯ Image labeling : 画像のラベル付け ◯ なし Object detection & tracking : 物体検出 ◯ なし Text recognition : テキスト認識 なし ◯

Slide 15

Slide 15 text

Natural Language APIs API Bundled Thin Language identification : ⾔語識別 ◯ なし Translation : 翻訳 ◯ なし Smart Reply : スマートリプライ ◯ なし

Slide 16

Slide 16 text

Android Jetpack Lifecycle support

Slide 17

Slide 17 text

Lifecycle Support • 推論を実⾏する detector/scanner/labeler/translator などはすべ て Closable かつ LifecycleObserver になった • 不要になったタイミングで close() を呼ぶ必要がある • lifecycle.addObserver(detector/scanner/labeler/translator ) で きるようになった

Slide 18

Slide 18 text

Barcode scanning バーコードスキャン

Slide 19

Slide 19 text

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情報など)の取り出し • バーコードの向きによらず検出可能

Slide 20

Slide 20 text

Artifact • Bundled • com.google.mlkit:barcode-scanning:16.0.1 • Thin • com.google.android.gms:play-services-mlkit-barcode- scanning:16.1.0 • Bundled と Thin で振る舞いに違いあり

Slide 21

Slide 21 text

Bundled vs Thin • Bundled • アプリサイズが約 2.2MB 増える • V2 model (V1 より速くて正確) • Thin • アプリサイズは変わらない • V1 model

Slide 22

Slide 22 text

インストール時ダウンロードオプション • Thin model の場合 ...

Slide 23

Slide 23 text

val options = BarcodeScannerOptions.Builder() .setBarcodeFormats( Barcode.FORMAT_EAN_8, Barcode.FORMAT_EAN_13 ) .build() val scanner = BarcodeScanning.getClient(options)

Slide 24

Slide 24 text

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 // ... } }

Slide 25

Slide 25 text

Face detection 顔検出

Slide 26

Slide 26 text

Face detection : 顔検出 • 顔の領域、ランドマーク(⽬・頬・⿐・⽿・⼝)、輪郭情報(顔の外郭・ ⽬・眉・⿐・⼝)の位置認識 • 顔の表情(⽬の開閉度合い、笑顔の度合い)の認識 • 動画のフレーム間で同じ顔をトラック可能 • Euler X (前後の傾き)が取れるようになった

Slide 27

Slide 27 text

Artifact • Bundled • com.google.mlkit:face-detection:16.0.1 • Thin • com.google.android.gms:play-services-mlkit-face- detection:16.1.0 • Bundled と Thin で振る舞いは同じ

Slide 28

Slide 28 text

Bundled vs Thin • Bundled • アプリサイズが約 16MB 増える • Thin • アプリサイズは変わらない

Slide 29

Slide 29 text

インストール時ダウンロードオプション • Thin model の場合 ...

Slide 30

Slide 30 text

// 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)

Slide 31

Slide 31 text

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 // ... } }

Slide 32

Slide 32 text

Image labeling 画像のラベル付け

Slide 33

Slide 33 text

Image labeling : 画像のラベル付け • 画像の内容を解析し、認識したもののラベルをつける : ⼈、物、 場所、活動など • general-purpose base classifier • 400+ labels をサポート • Custom model と組み合わせて独⾃のラベル付けが可能 • Custom TensorFlow Lite models • Custom AutoML Vision Edge models

Slide 34

Slide 34 text

Artifact (Base Models) • Bundled • com.google.mlkit:image-labeling:16.1.0

Slide 35

Slide 35 text

val labeler = ImageLabeling.getClient(ImageLabelerOptions.DEFAULT_OPTIONS) // or val options = ImageLabelerOptions.Builder() .setConfidenceThreshold(0.9f) .build() val labeler = ImageLabeling.getClient(options)

Slide 36

Slide 36 text

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 // ... } }

Slide 37

Slide 37 text

TensorFlow Lite model の利⽤ • TensorFlow Hub からダウンロードしたモデルや、TensorFlow Lite Model Maker や TensorFlow を使って⾃分で学習したモデルを利⽤で きる • model はアプリにバンドルされている必要がある

Slide 38

Slide 38 text

Artifact (Custom Models) • com.google.mlkit:image-labeling-custom:16.1.0

Slide 39

Slide 39 text

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" } }

Slide 40

Slide 40 text

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)

Slide 41

Slide 41 text

AutoML Vision Edge model の利⽤ • AutoML Vision Edge を使ってトレーニングしたモデルを利⽤できる • model のバンドルオプション • アプリにバンドル • Firebase プロジェクトが不要 • Firebase ML に置いて実⾏時にダウンロード • model の更新や A/B テストが容易にできる

Slide 42

Slide 42 text

Artifact (AutoML Models) • com.google.mlkit:image-labeling-automl:16.1.0 • Firebase ML の Model deployment service を使う場合は以下 も必要 • com.google.mlkit:linkfirebase:16.0.0

Slide 43

Slide 43 text

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" } }

Slide 44

Slide 44 text

val localModel = AutoMLImageLabelerLocalModel.Builder() .setAssetFilePath("flower/manifest.json") // or .setAbsoluteFilePath(absolute file path to manifest file) .build() バンドルする場合

Slide 45

Slide 45 text

val options = AutoMLImageLabelerOptions.Builder(localModel) .setConfidenceThreshold(0.5f) .build() val labeler = ImageLabeling.getClient(options) バンドルする場合

Slide 46

Slide 46 text

val remoteModel = AutoMLImageLabelerRemoteModel.Builder("Flowers_20191213202519") .build() Firebase ML に置く場合

Slide 47

Slide 47 text

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 に置く場合

Slide 48

Slide 48 text

Object detection & tracking 物体認識

Slide 49

Slide 49 text

Object detection & tracking: 物体認識 • 画像からオブジェクトを検出 • デフォルト • 粗いカテゴリー分類(5種類) • Custom model と組み合わせて独⾃の分類が可能 • Custom TensorFlow Lite models

Slide 50

Slide 50 text

Artifact (Base Models) • Bundled • com.google.mlkit:object-detection:16.1.0

Slide 51

Slide 51 text

// 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)

Slide 52

Slide 52 text

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 // ... } }

Slide 53

Slide 53 text

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 -> { ... } } ... } } } ...

Slide 54

Slide 54 text

TensorFlow Lite model の利⽤ • TensorFlow Hub からダウンロードしたモデルや、TensorFlow Lite Model Maker や TensorFlow を使って⾃分で学習したモデルを利⽤で きる • model はアプリにバンドルされている必要がある

Slide 55

Slide 55 text

Artifact (Custom Models) • com.google.mlkit:object-detection-custom:16.1.0

Slide 56

Slide 56 text

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" } }

Slide 57

Slide 57 text

val localModel = LocalModel.Builder() .setAssetFilePath("lite-model_aiy_vision_classifier_food_V1_1.tflite") // or .setAbsoluteFilePath(absolute file path to tflite model) .build()

Slide 58

Slide 58 text

// 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)

Slide 59

Slide 59 text

Text recognition テキスト認識

Slide 60

Slide 60 text

Text recognition : テキスト認識 • 画像からテキストを認識 • 看板などの短いテキスト向け • 全てのラテン⽂字を認識

Slide 61

Slide 61 text

Artifact • Thin • com.google.android.gms:play-services-mlkit-text- recognition:16.1.0

Slide 62

Slide 62 text

インストール時ダウンロードオプション ...

Slide 63

Slide 63 text

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 // ... } }

Slide 64

Slide 64 text

Language Identification ⾔語ID

Slide 65

Slide 65 text

Language Identification : ⾔語ID • ⽂字列からその⾔語を識別(BCP-47 language code) • 100以上の⾔語に対応 • https://developers.google.com/ml-kit/language/ identification/langid-support • 最も可能性の⾼い⾔語を1つ識別(Confidence 情報な し) or 可能性のある複数の⾔語を識別(Confidence 情報あり)

Slide 66

Slide 66 text

Artifact • Bundled • com.google.mlkit:language-id:16.1.0

Slide 67

Slide 67 text

val languageIdentifier = LanguageIdentification.getClient() languageIdentifier.identifyLanguage(text) .addOnSuccessListener { languageCode -> when(languageCode) { "und" -> { // Can't identify language. } else -> { // Success } } } .addOnFailureListener { // Error }

Slide 68

Slide 68 text

Translation 翻訳

Slide 69

Slide 69 text

Translation : 翻訳 • 50 以上の⾔語間の翻訳が可能 • https://developers.google.com/ml-kit/language/ translation/translation-language-support • Google Translate の offline mode と同じモデル • 帰属の表⽰要件、制限事項などのガイドラインあり

Slide 70

Slide 70 text

Artifact • Bundled • com.google.mlkit:translate:16.1.0

Slide 71

Slide 71 text

val options = TranslatorOptions.Builder() .setSourceLanguage(TranslateLanguage.ENGLISH) .setTargetLanguage(TranslateLanguage.JAPANESE) .build() val translator = Translation.getClient(options) ... lifecycle.addObserver(translator)

Slide 72

Slide 72 text

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. }

Slide 73

Slide 73 text

translator.translate(text) .addOnSuccessListener { translatedText -> // Translation successful. } .addOnFailureListener { exception -> // Error. }

Slide 74

Slide 74 text

Smart Reply スマートリプライ

Slide 75

Slide 75 text

Smart Reply : スマートリプライ • 会話に関連する返事を⽣成し提案 • 単⼀のメッセージではなく会話のフルコンテキストから⽣成 • 現在は English のみサポート • 他⾔語と判定された場合提案なしになる • sensitive topics があると判定された場合提案なしになる • 返事の候補は最⼤3つ提案される

Slide 76

Slide 76 text

Artifact • Bundled • com.google.mlkit:smart-reply:16.1.0

Slide 77

Slide 77 text

val conversation = mutableListOf() ... conversation.add( TextMessage.createForLocalUser( localUserMessage.text, localUserMessage.time ) ) conversation.add( TextMessage.createForRemoteUser( remoteUserMessage.text, remoteUserMessage.time, remoteUserMessage.userId ) )

Slide 78

Slide 78 text

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 }

Slide 79

Slide 79 text

Custom Models with ML Kit

Slide 80

Slide 80 text

Custom model を使う利点は? • ByteBuffer など low-level の⼊出⼒処理をおまかせできる • 推論結果とラベルのマッピングをおまかせできる

Slide 81

Slide 81 text

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 : クラスの数)

Slide 82

Slide 82 text

既存の model で使えるものは? • TensorFlow Hub : ML Kit compatible models • https://tfhub.dev/ml-kit/collections/image-classification/1

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

⾃分で 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

Slide 87

Slide 87 text

AutoML で花の分類モデルを作る • たんぽぽ、デイジー、チューリップ、ひまわり、バラ の分類モデルを作る • https://codelabs.developers.google.com/ codelabs/automl-vision-edge-in-mlkit • 各花ごと200枚の画像 • 無料プラン(Spark)でも試せる

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

Early access program

Slide 93

Slide 93 text

Early access program • Entity extraction : エンティティの抽出 • Pose detection : ポーズ検出

Slide 94

Slide 94 text

Entity extraction • ⽣の⽂字列からURL、メールアドレス、電話番号、住所、クレジットカー ド番号、⾦額(通貨込み)、荷物の追跡番号、⽇付・時間などを識別して 抜き出す機能 • https://developers.google.com/ml-kit/early-access/entity- extraction

Slide 95

Slide 95 text

Pose detection • 対象の物理的な動作(= ポーズ)をトラッキング する機能 • ⼿や⾜を含む 33 ポイントをトラッキング • https://developers.google.com/ml-kit/early- access/pose-detection

Slide 96

Slide 96 text

Samples

Slide 97

Slide 97 text

https://developers.google.com/ ml-kit/samples

Slide 98

Slide 98 text

Codelabs

Slide 99

Slide 99 text

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

Slide 100

Slide 100 text

Firebase Machine Learning

Slide 101

Slide 101 text

Firebase ML • Vision • Text recognition : テキスト認識 • Image labeling : 画像のラベル付け • Landmark recognition : ランドマーク認識 • AutoML Vision Edge • Deploy & manage custom models https://firebase.google.com/products/ml

Slide 102

Slide 102 text

Thank you! Google Developers Expert for Android あんざいゆき / yanzm