Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Few steps to make your app smarter with TensorF...
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Volodia Chornenkyi
January 30, 2019
Programming
90
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Few steps to make your app smarter with TensorFlow Lite
Volodia Chornenkyi
January 30, 2019
More Decks by Volodia Chornenkyi
See All by Volodia Chornenkyi
Android Studio 👀 Flamingo 🦩 Giraffe 🦒
chornenky_volodia
1
490
Let's add AR to your Android app
chornenky_volodia
0
160
Android Architecture Components. Do you really need them?
chornenky_volodia
1
140
Other Decks in Programming
See All in Programming
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
170
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
210
すこし踏み込む CancellationToken
htkym
2
1.3k
更なる可用性を求めて、5年間運用したKotlinのアプリケーションをGoでリプレイスする話
ken_tunc
0
390
SONY CISC-NEWS NWS-1750 + NWB-225 フレームバッファの NetBSD/news68k ドライバ実装 / OSC2026Hiroshima
tsutsui
0
150
setup-vp GitLab対応の裏側
naokihaba
0
140
[2026-09-26]空論ジェネリックプロセス~テスト資産とAIで紡ぐ、再現可能なパフォーマンスチューニングの話~
tosite
0
220
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
4.3k
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
re:Inventに行く前に知っておきたい現地参加のノウハウ
nokomoro3
0
230
カツオ、ご期待ください
suneo3476
0
110
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
910
Featured
See All Featured
A Tale of Four Properties
chriscoyier
163
24k
Context Engineering - Making Every Token Count
addyosmani
9
1.2k
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
550
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.6k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
540
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
68
58k
Documentation Writing (for coders)
carmenintech
77
5.5k
Visualization
eitanlees
153
17k
Statistics for Hackers
jakevdp
799
230k
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.2k
Everyday Curiosity
cassininazir
0
330
Transcript
Few steps to make your app smarter with TensorFlow Lite
Volodia Chornenkyi Android Software Engineer @TechMagic @GoodCo
What is TFLite? Framework for running machine learning models on
mobile and embedded devices.
What is TFLite? Framework for running machine learning models on
mobile and embedded devices.
Lite Two brothers
• Lightweight • Limited set of op • Custom op
support • Optimized work with models • Hardware acceleration • No Swift support Lite • Training support • Java&Swift APIs are not up to date • Android NDK • Bazel • Higher accuracy
TensorFlow Mobile?
TensorFlow Mobile? https://www.tensorflow.org/lite/tfmobile/
MLKit vs Tensor Flow https://developers.google.com/ml-kit/
How it works inside Architecture of a CNN. — Source: https://www.mathworks.com/videos/introduction-to-deep-learning-wh at-are-convolutional-neural-networks--1489512765771.html
https://society6.com/product/get-shit-done--get-shit-done_print
Step 1. Model cp tf_files/optimized_graph.lite ios/tflite/data/graph.lite cp tf_files/retrained_labels.txt ios/tflite/data/labels.txt cp
tf_files/optimized_graph.lite android/tflite/app/src/main/ass ets/graph.lite cp tf_files/retrained_labels.txt android/tflite/app/src/main/ass ets/labels.txt iOS Android https://www.tensorflow.org/lite/
Step 2.1. Setup platform :ios, '8.0' inhibit_all_warnings! target 'tflite_photos_example' pod
'TensorFlowLite' repositories { maven { url 'https://google.bintray.com/tensorflo w' } } dependencies { implementation 'org.tensorflow:tensorflow-lite:+' } iOS Android https://www.tensorflow.org/lite/
Step 2.2. Setup android { aaptOptions { noCompress "tflite" noCompress
"lite" } } Android https://www.tensorflow.org/lite/
NSString* graph_path = [[NSBundle mainBundle] pathForResource:name ofType:extension]; std::unique_ptr<tflite::FlatBufferModel> model =
tflite::FlatBufferModel::BuildFromFile([graph_path UTF8String]); iOS Android Step 3. Model AssetFileDescriptor fileDescriptor = activity.getAssets().openFd(MODEL_PATH); FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor()); FileChannel fileChannel = inputStream.getChannel(); long startOffset = fileDescriptor.getStartOffset(); long declaredLength = fileDescriptor.getDeclaredLength(); MappedByteBuffer model = fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength); Android https://www.tensorflow.org/lite/
std::vector<std::string> labels; NSString* labels_path = [[NSBundle mainBundle] pathForResource:name ofType:extension]; std::ifstream
t; t.open([labels_path UTF8String]); std::string line; while (t) { std::getline(t, line); if (line.length()){ labels->push_back(line); } } t.close(); Android Step 4. Labels on iOS https://www.tensorflow.org/lite/
Android Step 4. Labels on Android List<String> labelList = new
ArrayList<String>(); BufferedReader reader = new BufferedReader(new InputStreamReader(activity.getAssets().open(LABEL_PATH))); String line; while ((line = reader.readLine()) != null) { labelList.add(line); } reader.close(); https://www.tensorflow.org/lite/
Step 5. Interpreter std::unique_ptr<tflite::Interpreter> interpreter; tflite::InterpreterBuilder(*model, opResolver)(&interpreter); Interpreter tflite =
new Interpreter(model); iOS Android https://www.tensorflow.org/lite/
float* out = interpreter->typed_input_tensor<float>(0); uint8_t* in = image.data.data(); for (int
y = 0; y < wanted_input_height; ++y) { const int in_y = (y * image.height) / wanted_input_height; uint8_t* in_row = in + (in_y * image.width * image.channels); float* out_row = out + (y * wanted_input_width * wanted_input_channels); for (int x = 0; x < wanted_input_width; ++x) { const int in_x = (x * image.width) / wanted_input_width; uint8_t* in_pixel = in_row + (in_x * image.channels); float* out_pixel = out_row + (x * wanted_input_channels); for (int c = 0; c < wanted_input_channels; ++c) { out_pixel[c] = (in_pixel[c] - input_mean) / input_std; } } } Android Step 6. Convert image on iOS https://www.tensorflow.org/lite/
// ByteBuffer imgData bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
int pixel = 0; for (int i = 0; i < DIM_IMG_SIZE_X; ++i) { for (int j = 0; j < DIM_IMG_SIZE_Y; ++j) { final int val = intValues[pixel++]; imgData.putFloat((((val >> 16) & 0xFF)-IMAGE_MEAN)/IMAGE_STD); imgData.putFloat((((val >> 8) & 0xFF)-IMAGE_MEAN)/IMAGE_STD); imgData.putFloat((((val) & 0xFF)-IMAGE_MEAN)/IMAGE_STD); } } Android Step 6. Convert image on Android https://www.tensorflow.org/lite/
Step 7. Run model interpreter->Invoke() float* output = interpreter-> typed_output_tensor<float>(0)
tflite.run(imgData, labelProbArray) iOS Android https://www.tensorflow.org/lite/
Output. Threshold. Results number Step 8. Grand finale
Special slide for QA • unittest.TestCase • Model visualization •
Believe
Use TF Lite if • Weight is important • Limited
set of op is fine • Speed over accuracy • Hardware acceleration as a bonus
None
None
Links General: https://www.tensorflow.org/lite/ https://codelabs.developers.google.com/codelabs/tensorflow-for-poets Android: https://codelabs.developers.google.com/codelabs/tensorflow-for-poets-2-tflite iOS: https://codelabs.developers.google.com/codelabs/tensorflow-for-poets-2-ios QA: https://www.linkedin.com/pulse/tensorflow-dont-forget-unit-test-david-o-neill-1/
https://www.tensorflow.org/api_guides/python/test