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
260
Direct Preference Optimization
zchenry
0
490
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
290
ことのはの力で画像の異常検知 / Anomaly Detection by Language
zchenry
0
770
驚愕の事実!LangChainが抱える問題 / Problems of LangChain
zchenry
0
350
MLOps初心者がMLflowを触る / MLflow Brief Introduction
zchenry
0
230
Other Decks in Programming
See All in Programming
気持ちぃ〜角丸 Squircle
ryu1013
0
210
自サークルのIdPからログアウトできるようになるまで
taiseiue
1
130
Embedded Swiftで作る自作USBデバイス によるiOSデバイスの自動テスト / iOSDC Japan 2026 glassfiber
glassfiber
0
320
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
960
KiroのSpecで「五目並べ」を作ってみる
satoshi256kbyte
1
370
Agents on Rails - Rails at Scale 2026
irinanazarova
0
530
更なる可用性を求めて、5年間運用したKotlinのアプリケーションをGoでリプレイスする話
ken_tunc
0
480
Kubernetes祭り #2 OP/ED
bells17
0
110
APNsからLive Activityを開始する話
yumnumm
0
160
AI主導のプログラミング学習アプリを作ってみた
econanringo
0
150
ソニーのクラウド共通基盤の変遷とAI時代の開発スタイルに合わせた進化 / The Journey of Sony’s Common Cloud Platform and Its Evolution for AI-Native Development
kenjiyoneyama
0
250
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
1.2k
Featured
See All Featured
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
560
Design in an AI World
tapps
1
350
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.5k
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.1k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.6k
Designing for Timeless Needs
cassininazir
1
520
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.5k
Marketing to machines
jonoalderson
1
5.9k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.7k
Ethics towards AI in product and experience design
skipperchong
2
420
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