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
PyTorchモデルの組み込み変換 / Pytorch Model Conversion
Search
Henry Cui
April 29, 2023
Programming
970
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
PyTorchモデルの組み込み変換 / Pytorch Model Conversion
Henry Cui
April 29, 2023
More Decks by Henry Cui
See All by Henry Cui
プロダクション言語モデルの情報を盗む攻撃 / Stealing Part of a Production Language Model
zchenry
1
270
Direct Preference Optimization
zchenry
0
500
Diffusion Model with Perceptual Loss
zchenry
0
550
レンズの下のLLM / LLM under the Lens
zchenry
0
250
Go with the Prompt Flow
zchenry
0
260
Mojo Dojo
zchenry
0
300
ことのはの力で画像の異常検知 / Anomaly Detection by Language
zchenry
0
780
驚愕の事実!LangChainが抱える問題 / Problems of LangChain
zchenry
0
350
MLOps初心者がMLflowを触る / MLflow Brief Introduction
zchenry
0
240
Other Decks in Programming
See All in Programming
GraphQL の onError と Nullability のこれから
quramy
0
200
Spec-Driven Development - Why Vaadin Is the Perfect Fit
simas
PRO
0
200
Groovy as a human and AI friendly programming language
paulk
0
150
Crash Auto Pipelines
veronikapj
1
300
全ての永続トークンを、生まれる前に消し去りたい!
sylph01
2
1.6k
Web3Dライブラリを作ってVue Custom Rendererの仕組みを理解しよう / lets learn vue custom renderer by making 3d library
drumath2237
1
110
LLM 101
dalinaum
0
430
[2026-10-14](C)Re:ゼロから始める異世界生活〜始まりの終わりと終わりの始まり〜
tosite
0
230
AI時代のIaC入門 〜なぜインフラをコードで管理したいのか?〜 / Introduction to IaC in the AI Era
akihisaikeda
6
1.3k
個人開発者よ、Honoを使え(おじさんver)
tanahiro2010
2
300
Materialが消えた日 〜真っ白なFlutterキャンバスにRenderObjectで絵を描く〜
umigishiaoi
0
130
Web Framework Benchmark Battle 2026, Any%
a_matsuda
3
3.1k
Featured
See All Featured
A better future with KSS
kneath
240
18k
Six Lessons from altMBA
skipperchong
29
4.6k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.6k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
550
Embracing the Ebb and Flow
colly
88
5.2k
Exploring anti-patterns in Rails
aemeredith
4
550
The agentic SEO stack - context over prompts
schlessera
0
990
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
520
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
2
660
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
450
Transcript
PyTorchモデルの組み込み変換 機械学習の社会実装勉強会第22回 Henry 2023/4/29
Pytorchモデルを変換するモチベーション ▪ モデル開発においては、Pytorchのエコシステムが活発 ▪ モデルデプロイ、特にエッジデバイスにおいては、Tensorflow のエコシステムが活発 ▪ Pytorchでモデルを学習して、Tensorflowモデルに変換しデプ ロイすることが考えられます ▪
今日はこの流れの基本と注意点について紹介 2
Pytorch ⇛ Tensorflow ▪ 一回だけの変換ではできない ▪ エッジではTF Liteモデルが多いので、実は以下のアプローチ になる •
Pytorch ⇛ ONNX ⇛ Tensorflow ⇛ TF Lite ▪ 一つずつ見ていきます 3
Pytorch ⇛ ONNX ▪ Open Neural Network Exchange (ONNX) は共通になろうと
するニューラルネット記述方式 • https://onnx.ai/ ▪ pytorch_modelにモデルをロード • 例えば pytorch_model = timm.create_model('mobilenetv2_100') ▪ torchに内蔵された関数で変換できる • torch.onnx.export(model=pytorch_model, args=torch.ones((1, 3, 224, 224)), f='./model.onnx', input_names=['input'], output_names=['output']) 4
ONNX ⇛ Tensorflow ▪ ONNXライブラリに内蔵された関数で変換できる ▪ モデルロード • onnx_model =
onnx.load('./model.onnx') ▪ モデルチェック • onnx.checker.check_model(onnx_model) ▪ モデル変換 • from onnx_tf.backend import prepare prepare(onnx_model).export_graph('./model.tf') 5
Tensorflow ⇛ TF Lite ▪ Tensorflowに内蔵される関数で変換できる ▪ 変換準備 • converter
= tf.lite.TFLiteConverter.from_saved_model('./model.tf') ▪ 変換実行 • tflite_f32_model = converter.convert() ▪ 変換書き出し • with open('./model.tflite', 'wb') as f: f.write(tflite_f32_model) 6
Netronでモデル構造を可視化 ▪ ニューラルネットの構造と入出力の形を可視化するツール • https://github.com/lutzroeder/netron • パッケージ版とブラウザ版がある ▪ 生成されたTF Liteモデルを渡すと
• 至るところでTranspose! 7
ライブラリのNCHWとNHWC ▪ Pytorchを含むほとんどのライブラリは Batchsize, Channel , Height, WidthのNCHW形式を取る ▪ 残念ながらTensorflowがNHWC形式
▪ 先のやり方では、ONNXをTensorflowへの変換では、NCHW の痕跡はまだ残っているので、計算の前後などでTranspose を入れてデータの軸を調整しないといけない 8
パラメータの軸を変換する ▪ 以下のツールを使えば、NCHW形式のONNXモデルをきれい にNHWC形式のTensorflowモデルに変換できる • https://github.com/PINTO0309/onnx2tf ▪ 名の通り、ONNX ⇛ Tensorlflowなので、全体の変換手順が
Pytorch ⇛ ONNX ⇛ Tensorflow ⇛ TF Liteのままで変わらな い 9
変換結果 ▪ Transpose層がちゃんと消えて、入力もNHWCに ▪ バッチサイズ1の推論スピードも速くなる • 改善前の10回平均:11.19 e-3秒 (±0.28 e-3秒)
• 改善後の10回平均: 7.82 e-3秒 (±0.14 e-3秒) 10
まとめ ▪ モデル変換の基本 ▪ NCHWとNHWCの違いと注意 11