Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
PyTorchモデルの組み込み変換 / Pytorch Model Conversion
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Henry Cui
April 29, 2023
Programming
950
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
260
Direct Preference Optimization
zchenry
0
480
Diffusion Model with Perceptual Loss
zchenry
0
530
レンズの下のLLM / LLM under the Lens
zchenry
0
240
Go with the Prompt Flow
zchenry
0
250
Mojo Dojo
zchenry
0
280
ことのはの力で画像の異常検知 / Anomaly Detection by Language
zchenry
0
750
驚愕の事実!LangChainが抱える問題 / Problems of LangChain
zchenry
0
330
MLOps初心者がMLflowを触る / MLflow Brief Introduction
zchenry
0
230
Other Decks in Programming
See All in Programming
Claude CodeとAgentCore Gatewayを繋ぐ際の認証認可 / Authentication and authorization when connecting Claude Code with AgentCore Gateway
har1101
2
420
Can LLMs Replicate 4 Years of Compose Migration? Exploring the boundaries of automation with 279 XML files from a real product
makun
0
110
まだ間に合う!今年の夏こそSchemeのマクロ展開器を完全理解!
omasanori
0
590
関東Kaggler会_NVIDIA_Nemotron_コンペ_振り返り
rick_ds
0
770
承認済みなのに差戻しできてしまうバグ、型で潰せます
shinchit
0
130
Claude Codeを組織的に動かして月400PRを実現した話
happy_ryo
0
220
AIを上手に使っていこうとしたら越境せざるを得なくなった話 〜実践1年で見えた境界を越えなければならない理由と進め方〜 / Crossing borders with AI
tomoyakitaura
2
910
【QA Test Talk Vol.8】AI-DLC による Whole Team Approach の加速
pkshadeck
PRO
0
270
【DroidKaigi 2026】「アクセシビリティを利用するとき、 アクセシビリティもまたこちらを利用している」 〜マルウェアによる攻撃と防衛について〜
halunoyo
0
130
Cloudflare is Agents
chimame
0
190
GKE アップグレード前に知っておきたい Blue/Green と PDB の関係
stkk
0
120
AI時代に学ぶ 好きなルール 嫌いなルール Linter編
shorty5121
0
700
Featured
See All Featured
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
2
3.8k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.6k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
430
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Statistics for Hackers
jakevdp
799
230k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.4k
How to build a perfect <img>
jonoalderson
1
5.9k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Code Review Best Practice
trishagee
74
20k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.5k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
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