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
960
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
540
レンズの下のLLM / LLM under the Lens
zchenry
0
250
Go with the Prompt Flow
zchenry
0
260
Mojo Dojo
zchenry
0
280
ことのはの力で画像の異常検知 / Anomaly Detection by Language
zchenry
0
760
驚愕の事実!LangChainが抱える問題 / Problems of LangChain
zchenry
0
340
MLOps初心者がMLflowを触る / MLflow Brief Introduction
zchenry
0
230
Other Decks in Programming
See All in Programming
Jetpack Compose メカニズム
skydoves
0
460
速く作れる。その次は、速く確かめられる開発へ 〜AIネイティブ開発を支える、Shift Down〜 / Can build fast. Next, moving to development where we can verify fast.
rkaga
5
3.8k
AWS DevOps Agentで インシデント対応をAIに任せたい
honmarkhunt
7
3.1k
TiDB Cloudのカスタムコントローラーによるオートスケール対応
takaidohigasi
0
130
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
500
Go × SIMDで高速化するベクトル検索 ~ルーフラインモデルでSIMDが効く境界を探れ! ~
po3rin
1
2.5k
Verilogで学ぶCPU自作入門.pdf
uyuki234
1
310
更なる可用性を求めて、5年間運用したKotlinのアプリケーションをGoでリプレイスする話
ken_tunc
0
330
Kiroで創り、AgentCoreで繋ぐ!AWSで実践する「AI-DLC」から「AIエージェント統合」までの最新地図
licux
4
730
選挙速報を多くのユーザーへ 届ける Live Activities 設計
hamayokokuririn
0
150
Omarchy Tokyo やると聞いて UMPC 買ってセットアップしてきた
mtsmfm
0
150
技術的負債の返済は、AI時代の複利で効く投資 — 経営としての意思決定とその遂行
curekoshimizu
0
1.7k
Featured
See All Featured
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
5
710
ラッコキーワード サービス紹介資料
rakko
1
5M
Skip the Path - Find Your Career Trail
mkilby
1
240
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
Agile that works and the tools we love
rasmusluckow
331
22k
Statistics for Hackers
jakevdp
799
230k
A Modern Web Designer's Workflow
chriscoyier
699
190k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
340
[SF Ruby Conf 2025] Rails X
palkan
3
1.4k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
760
It's Worth the Effort
3n
188
29k
YesSQL, Process and Tooling at Scale
rocio
174
15k
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