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
940
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
470
Diffusion Model with Perceptual Loss
zchenry
0
520
レンズの下のLLM / LLM under the Lens
zchenry
0
230
Go with the Prompt Flow
zchenry
0
230
Mojo Dojo
zchenry
0
270
ことのはの力で画像の異常検知 / Anomaly Detection by Language
zchenry
0
720
驚愕の事実!LangChainが抱える問題 / Problems of LangChain
zchenry
0
330
MLOps初心者がMLflowを触る / MLflow Brief Introduction
zchenry
0
210
Other Decks in Programming
See All in Programming
セキュリティの専門家じゃなくてもできる。「セキュリティ意識」をアップデートして サプライチェーン攻撃への耐性を高めよう。
tk3fftk
5
980
肥大化するレガシーコードに立ち向かうためのインターフェース分離と依存の逆転 / JJUG CCC 2026 Spring
hirokunimaeta
0
640
The NotImplementedError Problem in Ruby
koic
1
970
Spec Driven Development | AI Summit Lisbon
danielsogl
PRO
0
220
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
4
1.5k
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
610
スマートグラスで並列バイブコーディング
hyshu
0
260
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
4.6k
どこまでゆるくて許されるのか
tk3fftk
0
260
エンジニア向け会社紹介/Findy Company Profile
findyinc
6
350k
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.7k
AI駆動開発を妨げる技術的負債の解消アプローチ / ai-refactoring-approach
minodriven
15
7.6k
Featured
See All Featured
Skip the Path - Find Your Career Trail
mkilby
1
150
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
470
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.9k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.6k
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
850
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
210
The agentic SEO stack - context over prompts
schlessera
0
830
HDC tutorial
michielstock
2
720
The untapped power of vector embeddings
frankvandijk
2
1.8k
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