Skip to main content

Convert PyTorch model to Onnx model

PyTorchで作成した重みファイルをONNX形式の重みファイルに変換するCLIアプリケーションです。

サポートしているAIタスクは以下のとおりです。

  • Image Classification
  • Object Detection

動作確認OS

  • Windows 10 Pro
  • Windows 11 Pro

インストール方法

pip install pth2onnx

pth2onnxの実行方法

YOLOXモードの場合

# githubからソースをダウンロードし、YOLOXフォルダ内に.venvを生成しインストールする
pth2onnx -m yolox -c install -f
# Windows環境の場合下記のエラーが出ることがある
# ERROR: Could not install packages due to an OSError: [WinError 206] ファイル名または拡張子が長すぎます。
# これが出たときは「YOLOX/requirements.txt」ファイルの「onnx-simplifier」をコメントアウトして再実行する

# 学習済みモデルのダウンロード先URLを表示
pth2onnx -m yolox -c zoo -f
# see: https://github.com/Megvii-BaseDetection/YOLOX/#benchmark

# pytorchの重みファイルでデモを実行
pth2onnx -m yolox -c demo -f --yolox_model_name <モデル名> --yolox_weight_file <pytorchモデルファイルのパス> --yolox_output_preview
# モデル名は「yolox_nano」「yolox_tiny」「yolox_s」「yolox_m」「yolox_l」「yolox_x」など
# pytorchモデルファイルのパスはYOLOXフォルダ内のパス。「models/yolox_tiny.pth」など

# pytorchの重みファイルをONNXの重みファイルに変換
pth2onnx -m yolox -c convert -f --yolox_model_name <モデル名> --yolox_weight_file <pytorchモデルファイルのパス> --yolox_onnx_file <ONNXモデルファイルのパス>
# ONNXモデルファイルのパスはYOLOXフォルダ内のパス。「models/yolox_tiny.onnx」など

# ONNXの重みファイルで推論を実行
pth2onnx -m yolox -c inference -f --yolox_onnx_file <ONNXモデルファイルのパス> --yolox_model_img_size <モデルのINPUTサイズ> --yolox_output_preview
# モデルのINPUTサイズは「416」など

その他便利なオプション

コマンドラインオプションが多いので、それを保存して再利用できるようにする

# 通常のコマンドに「-u」と「-s」オプションを追加する
pth2onnx -u <オプションを保存するファイル> -s

# 次から使用するときは「-u」を使用する
pth2onnx -u <オプションを保存するファイル>

コマンドの実行結果を見やすくする。

# 通常のコマンドに「-f」オプションを追加する
pth2onnx -f

# 「-f」オプションを外せば、結果はjson形式で取得できる
pth2onnx 

コマンドラインオプションのヘルプ。

pth2onnx -h

pth2onnxコマンドについて

python -m pth2onnxの省略形です。 実体はscriptsディレクトリ内にあります。

データの保存場所

pathlib.Path(HOME_DIR) / '.pth2onnx'

動作確認したモデル

AI Task base Model
Object Detection YOLOX YOLOX-Nano
Object Detection YOLOX YOLOX-Tiny
Object Detection YOLOX YOLOX-s
Object Detection YOLOX YOLOX-m
Object Detection YOLOX YOLOX-l
Object Detection YOLOX YOLOX-x

開発環境構築

git clone https://github.com/hamacom2004jp/pth2onnx.git
cd pth2onnx
python -m venv .venv
.venv\Scripts\activate
python.exe -m pip install --upgrade pip
pip install -r requirements.txt
deactivate

pyplにアップするための準備

python setup.py sdist
python setup.py bdist_wheel
[distutils]
index-servers =
  pypi
  testpypi

[pypi]
repository: https://upload.pypi.org/legacy/
username: __token__
password: 本番環境のAPIトークン

[testpypi]
repository: https://test.pypi.org/legacy/
username: __token__
password: テスト環境のAPIトークン
  • テスト環境にアップロード .pyplrcを作っていない場合はコマンド実行時にusernameとpasswordを要求される 成功するとURLが返ってくる。
twine upload --repository testpypi dist/*
  • pipコマンドのテスト
pip install -i https://test.pypi.org/simple/ pth2onnx
  • 本番環境にアップロード
twine upload --repository pypi dist/*

Lisence

This project is licensed under the MIT License, see the LICENSE.txt file for details

Metadata

Release files for pth2onnx 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pth2onnx 0.0.4
File Size Uploaded Provenance Actions
pth2onnx-0.0.4.tar.gz 14.0 kB Without provenance Details for pth2onnx-0.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pth2onnx 0.0.4
File Interpreter ABI Platform Actions
pth2onnx-0.0.4-py3-none-any.whl Python 3 none any Details for pth2onnx-0.0.4-py3-none-any.whl

Total release size: 27.7 kB

Release files / pth2onnx-0.0.4.tar.gz

Download URL pth2onnx-0.0.4.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
ef3b7162aeb08a9c4fb557914b3bf697a3849e83c5fc47010adb269f2f960d99
BLAKE2b-256 checksum
How to use checksums
7bc92b48ea64dd44ad28e2aa9618b49bd427899fb666c1be532117461c43365f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / pth2onnx-0.0.4-py3-none-any.whl

Download URL pth2onnx-0.0.4-py3-none-any.whl
Size 13.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2b44b26e806cce0a432d0f8663c9f32ce023794d2b4a74c1a2868dc7083a93e7
BLAKE2b-256 checksum
How to use checksums
23a514b88bcd016e49a572232ab2aa194207226825597c772b82895eed6a6d60
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Security for pth2onnx 0.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page