6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pth(pytorch)をonnxに変換するのは簡単か? OpenVINOのIR(Intermediate Representation)に(も)関連した記事。

Last updated at Posted at 2020-05-10

目的

この記事は、下名のような、いくつかわかってないことがある人向けです。非常に詳しい人は、読む意味がないと思います。

人それぞれ目的は、違うと思いますが、ワタシは、OpenVINOのIRファイルを作りたくて、かつ、そのもともとの情報が、Pytorchであるケースがありました。
そのため、その変換に挑戦しました。

OpenVINOによると、Pytorchは、直接は扱えず、ONNXを経由する必要があることのこと。

よって、

pthをonnxに変換する

ことを検討しました。
例えば、
AAA_BBB.pthというファイルをどこかからもってきて
Pythonを使うとして、期待するのは、以下のような感じでしょうか。

python convert.py -i AAA_BBB.pth -o AAA_BBB.onnx

こんな感じではできない気がしています、今。

説明

簡単じゃない説明

そもそも、
AAA_BBB.pthというファイルをどこかからもってきて
という前提の場合は、
下記では、情報が足りないんではないでしょうか?
たぶん、pthだけでは、足らないのでは?(<--このあたりが極端に不勉強ですが。)

python convert.py -i AAA_BBB.pth -o AAA_BBB.onnx

では、どうすればいいのか。1

以下など参考になるのではないでしょうか?
モデルの情報がある感じで、
かつ、torchの環境で
**torch.onnx.export()**等で出力する。
これだとできるような気がします。
⇒すみません、これは、自分では試していません。

import torch
import torch.onnx

# A model class instance (class not shown)
model = MyModelClass()

# Load the weights from a file (.pth usually)
state_dict = torch.load(weights_path)

# Load the weights now into a model net architecture defined by our class
model.load_state_dict(state_dict)

# Create the right input shape (e.g. for an image)
dummy_input = torch.randn(sample_batch_size, channel, height, width)

torch.onnx.export(model, dummy_input, "onnx_model_name.onnx")

では、どうすればいいのか。2

https://qiita.com/PINTO/items/e1bf5ef24ed6ad6f6a05
も、参考になると思います。

ただ、これの場合は、human-pose-estimation-3d-0001.pthの情報だけで、変換しているわけではない、ハズです。
(いろいろ、モデルの情報がある前提。たぶん、このあたりは、非常に当たり前のことなんだと思いますが。)

⇒実際にやってみました。できました。(というかいきなりIRファイルまでできましたので、、、んんん。)

まとめ

「pth(pytorch)をonnxに変換するのは簡単か?」は、あんまり、状況がよくわかってない人には、簡単でないという記事でした。ワタシのようなレベル、完全な素人ではないが、あまり、しっかり全体を理解していない人には、の前提付きです。

⇒OpenVINOの説明がよくわからないので、記事にしたという面もあります。

コメントなどあれば、お願いします。

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?