pth2onnx


Namepth2onnx JSON
Version 0.0.4 PyPI version JSON
download
home_pagehttps://github.com/hamacom2004jp/pth2onnx
Summarypth2onnx: Convert PyTorch model to Onnx model.
upload_time2023-11-25 10:28:31
maintainerhamacom2004jp
docs_urlNone
authorhamacom2004jp
requires_python
licenseMIT
keywords pytorch onnx convert image ai model
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Convert PyTorch model to Onnx model

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

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

## 動作確認OS
- `Windows 10 Pro`
- `Windows 11 Pro`

## インストール方法

``` cmd or bash
pip install pth2onnx
```

## pth2onnxの実行方法
### YOLOXモードの場合
``` cmd or bash
# 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」など
```


## その他便利なオプション
コマンドラインオプションが多いので、それを保存して再利用できるようにする
``` cmd or bash
# 通常のコマンドに「-u」と「-s」オプションを追加する
pth2onnx -u <オプションを保存するファイル> -s

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

コマンドの実行結果を見やすくする。
``` cmd or bash
# 通常のコマンドに「-f」オプションを追加する
pth2onnx -f

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

コマンドラインオプションのヘルプ。
``` cmd or bash
pth2onnx -h
```

## pth2onnxコマンドについて
```python -m pth2onnx```の省略形です。
実体は```scripts```ディレクトリ内にあります。

### データの保存場所
```
pathlib.Path(HOME_DIR) / '.pth2onnx'
```

## 動作確認したモデル
|AI Task|base|Model|
|------|------|------|
|Object Detection|[YOLOX](https://github.com/Megvii-BaseDetection/YOLOX/#benchmark)|YOLOX-Nano|
|Object Detection|[YOLOX](https://github.com/Megvii-BaseDetection/YOLOX/#benchmark)|YOLOX-Tiny|
|Object Detection|[YOLOX](https://github.com/Megvii-BaseDetection/YOLOX/#benchmark)|YOLOX-s|
|Object Detection|[YOLOX](https://github.com/Megvii-BaseDetection/YOLOX/#benchmark)|YOLOX-m|
|Object Detection|[YOLOX](https://github.com/Megvii-BaseDetection/YOLOX/#benchmark)|YOLOX-l|
|Object Detection|[YOLOX](https://github.com/Megvii-BaseDetection/YOLOX/#benchmark)|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にアップするための準備

``` cmd or bash
python setup.py sdist
python setup.py bdist_wheel
```

- pyplのユーザー登録【本番】
  https://pypi.org/account/register/

- pyplのユーザー登録【テスト】
  https://test.pypi.org/account/register/

- それぞれ2要素認証とAPIトークンを登録

- ホームディレクトリに```.pypirc```を作成
``` .pypirc
[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が返ってくる。
``` cmd or bash
twine upload --repository testpypi dist/*
```
- pipコマンドのテスト
``` cmd or bash
pip install -i https://test.pypi.org/simple/ pth2onnx
```

- 本番環境にアップロード
``` cmd or bash
twine upload --repository pypi dist/*
```

## Lisence

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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hamacom2004jp/pth2onnx",
    "name": "pth2onnx",
    "maintainer": "hamacom2004jp",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "hamacom2004jp@gmail.com",
    "keywords": "pytorch onnx convert image ai model",
    "author": "hamacom2004jp",
    "author_email": "hamacom2004jp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/7b/c9/2b48ea64dd44ad28e2aa9618b49bd427899fb666c1be532117461c43365f/pth2onnx-0.0.4.tar.gz",
    "platform": null,
    "description": "# Convert PyTorch model to Onnx model\r\n\r\nPyTorch\u3067\u4f5c\u6210\u3057\u305f\u91cd\u307f\u30d5\u30a1\u30a4\u30eb\u3092ONNX\u5f62\u5f0f\u306e\u91cd\u307f\u30d5\u30a1\u30a4\u30eb\u306b\u5909\u63db\u3059\u308bCLI\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u3059\u3002\r\n\r\n\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u308bAI\u30bf\u30b9\u30af\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002\r\n- Image Classification\r\n- Object Detection\r\n\r\n## \u52d5\u4f5c\u78ba\u8a8dOS\r\n- `Windows 10 Pro`\r\n- `Windows 11 Pro`\r\n\r\n## \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u65b9\u6cd5\r\n\r\n``` cmd or bash\r\npip install pth2onnx\r\n```\r\n\r\n## pth2onnx\u306e\u5b9f\u884c\u65b9\u6cd5\r\n### YOLOX\u30e2\u30fc\u30c9\u306e\u5834\u5408\r\n``` cmd or bash\r\n# github\u304b\u3089\u30bd\u30fc\u30b9\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u3001YOLOX\u30d5\u30a9\u30eb\u30c0\u5185\u306b.venv\u3092\u751f\u6210\u3057\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\r\npth2onnx -m yolox -c install -f\r\n# Windows\u74b0\u5883\u306e\u5834\u5408\u4e0b\u8a18\u306e\u30a8\u30e9\u30fc\u304c\u51fa\u308b\u3053\u3068\u304c\u3042\u308b\r\n# ERROR: Could not install packages due to an OSError: [WinError 206] \u30d5\u30a1\u30a4\u30eb\u540d\u307e\u305f\u306f\u62e1\u5f35\u5b50\u304c\u9577\u3059\u304e\u307e\u3059\u3002\r\n# \u3053\u308c\u304c\u51fa\u305f\u3068\u304d\u306f\u300cYOLOX/requirements.txt\u300d\u30d5\u30a1\u30a4\u30eb\u306e\u300connx-simplifier\u300d\u3092\u30b3\u30e1\u30f3\u30c8\u30a2\u30a6\u30c8\u3057\u3066\u518d\u5b9f\u884c\u3059\u308b\r\n\r\n# \u5b66\u7fd2\u6e08\u307f\u30e2\u30c7\u30eb\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u5148URL\u3092\u8868\u793a\r\npth2onnx -m yolox -c zoo -f\r\n# see: https://github.com/Megvii-BaseDetection/YOLOX/#benchmark\r\n\r\n# pytorch\u306e\u91cd\u307f\u30d5\u30a1\u30a4\u30eb\u3067\u30c7\u30e2\u3092\u5b9f\u884c\r\npth2onnx -m yolox -c demo -f --yolox_model_name <\u30e2\u30c7\u30eb\u540d> --yolox_weight_file <pytorch\u30e2\u30c7\u30eb\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9> --yolox_output_preview\r\n# \u30e2\u30c7\u30eb\u540d\u306f\u300cyolox_nano\u300d\u300cyolox_tiny\u300d\u300cyolox_s\u300d\u300cyolox_m\u300d\u300cyolox_l\u300d\u300cyolox_x\u300d\u306a\u3069\r\n# pytorch\u30e2\u30c7\u30eb\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u306fYOLOX\u30d5\u30a9\u30eb\u30c0\u5185\u306e\u30d1\u30b9\u3002\u300cmodels/yolox_tiny.pth\u300d\u306a\u3069\r\n\r\n# pytorch\u306e\u91cd\u307f\u30d5\u30a1\u30a4\u30eb\u3092ONNX\u306e\u91cd\u307f\u30d5\u30a1\u30a4\u30eb\u306b\u5909\u63db\r\npth2onnx -m yolox -c convert -f --yolox_model_name <\u30e2\u30c7\u30eb\u540d> --yolox_weight_file <pytorch\u30e2\u30c7\u30eb\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9> --yolox_onnx_file <ONNX\u30e2\u30c7\u30eb\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9>\r\n# ONNX\u30e2\u30c7\u30eb\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9\u306fYOLOX\u30d5\u30a9\u30eb\u30c0\u5185\u306e\u30d1\u30b9\u3002\u300cmodels/yolox_tiny.onnx\u300d\u306a\u3069\r\n\r\n# ONNX\u306e\u91cd\u307f\u30d5\u30a1\u30a4\u30eb\u3067\u63a8\u8ad6\u3092\u5b9f\u884c\r\npth2onnx -m yolox -c inference -f --yolox_onnx_file <ONNX\u30e2\u30c7\u30eb\u30d5\u30a1\u30a4\u30eb\u306e\u30d1\u30b9> --yolox_model_img_size <\u30e2\u30c7\u30eb\u306eINPUT\u30b5\u30a4\u30ba> --yolox_output_preview\r\n# \u30e2\u30c7\u30eb\u306eINPUT\u30b5\u30a4\u30ba\u306f\u300c416\u300d\u306a\u3069\r\n```\r\n\r\n\r\n## \u305d\u306e\u4ed6\u4fbf\u5229\u306a\u30aa\u30d7\u30b7\u30e7\u30f3\r\n\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u591a\u3044\u306e\u3067\u3001\u305d\u308c\u3092\u4fdd\u5b58\u3057\u3066\u518d\u5229\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\r\n``` cmd or bash\r\n# \u901a\u5e38\u306e\u30b3\u30de\u30f3\u30c9\u306b\u300c-u\u300d\u3068\u300c-s\u300d\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u8ffd\u52a0\u3059\u308b\r\npth2onnx -u <\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4fdd\u5b58\u3059\u308b\u30d5\u30a1\u30a4\u30eb> -s\r\n\r\n# \u6b21\u304b\u3089\u4f7f\u7528\u3059\u308b\u3068\u304d\u306f\u300c-u\u300d\u3092\u4f7f\u7528\u3059\u308b\r\npth2onnx -u <\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u4fdd\u5b58\u3059\u308b\u30d5\u30a1\u30a4\u30eb>\r\n```\r\n\r\n\u30b3\u30de\u30f3\u30c9\u306e\u5b9f\u884c\u7d50\u679c\u3092\u898b\u3084\u3059\u304f\u3059\u308b\u3002\r\n``` cmd or bash\r\n# \u901a\u5e38\u306e\u30b3\u30de\u30f3\u30c9\u306b\u300c-f\u300d\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u8ffd\u52a0\u3059\u308b\r\npth2onnx -f\r\n\r\n# \u300c-f\u300d\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u5916\u305b\u3070\u3001\u7d50\u679c\u306fjson\u5f62\u5f0f\u3067\u53d6\u5f97\u3067\u304d\u308b\r\npth2onnx \r\n```\r\n\r\n\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u30aa\u30d7\u30b7\u30e7\u30f3\u306e\u30d8\u30eb\u30d7\u3002\r\n``` cmd or bash\r\npth2onnx -h\r\n```\r\n\r\n## pth2onnx\u30b3\u30de\u30f3\u30c9\u306b\u3064\u3044\u3066\r\n```python -m pth2onnx```\u306e\u7701\u7565\u5f62\u3067\u3059\u3002\r\n\u5b9f\u4f53\u306f```scripts```\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u5185\u306b\u3042\u308a\u307e\u3059\u3002\r\n\r\n### \u30c7\u30fc\u30bf\u306e\u4fdd\u5b58\u5834\u6240\r\n```\r\npathlib.Path(HOME_DIR) / '.pth2onnx'\r\n```\r\n\r\n## \u52d5\u4f5c\u78ba\u8a8d\u3057\u305f\u30e2\u30c7\u30eb\r\n|AI Task|base|Model|\r\n|------|------|------|\r\n|Object Detection|[YOLOX](https://github.com/Megvii-BaseDetection/YOLOX/#benchmark)|YOLOX-Nano|\r\n|Object Detection|[YOLOX](https://github.com/Megvii-BaseDetection/YOLOX/#benchmark)|YOLOX-Tiny|\r\n|Object Detection|[YOLOX](https://github.com/Megvii-BaseDetection/YOLOX/#benchmark)|YOLOX-s|\r\n|Object Detection|[YOLOX](https://github.com/Megvii-BaseDetection/YOLOX/#benchmark)|YOLOX-m|\r\n|Object Detection|[YOLOX](https://github.com/Megvii-BaseDetection/YOLOX/#benchmark)|YOLOX-l|\r\n|Object Detection|[YOLOX](https://github.com/Megvii-BaseDetection/YOLOX/#benchmark)|YOLOX-x|\r\n\r\n\r\n## \u958b\u767a\u74b0\u5883\u69cb\u7bc9\r\n```\r\ngit clone https://github.com/hamacom2004jp/pth2onnx.git\r\ncd pth2onnx\r\npython -m venv .venv\r\n.venv\\Scripts\\activate\r\npython.exe -m pip install --upgrade pip\r\npip install -r requirements.txt\r\ndeactivate\r\n```\r\n\r\n## pypl\u306b\u30a2\u30c3\u30d7\u3059\u308b\u305f\u3081\u306e\u6e96\u5099\r\n\r\n``` cmd or bash\r\npython setup.py sdist\r\npython setup.py bdist_wheel\r\n```\r\n\r\n- pypl\u306e\u30e6\u30fc\u30b6\u30fc\u767b\u9332\u3010\u672c\u756a\u3011\r\n  https://pypi.org/account/register/\r\n\r\n- pypl\u306e\u30e6\u30fc\u30b6\u30fc\u767b\u9332\u3010\u30c6\u30b9\u30c8\u3011\r\n  https://test.pypi.org/account/register/\r\n\r\n- \u305d\u308c\u305e\u308c2\u8981\u7d20\u8a8d\u8a3c\u3068API\u30c8\u30fc\u30af\u30f3\u3092\u767b\u9332\r\n\r\n- \u30db\u30fc\u30e0\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b```.pypirc```\u3092\u4f5c\u6210\r\n``` .pypirc\r\n[distutils]\r\nindex-servers =\r\n  pypi\r\n  testpypi\r\n\r\n[pypi]\r\nrepository: https://upload.pypi.org/legacy/\r\nusername: __token__\r\npassword: \u672c\u756a\u74b0\u5883\u306eAPI\u30c8\u30fc\u30af\u30f3\r\n\r\n[testpypi]\r\nrepository: https://test.pypi.org/legacy/\r\nusername: __token__\r\npassword: \u30c6\u30b9\u30c8\u74b0\u5883\u306eAPI\u30c8\u30fc\u30af\u30f3\r\n```\r\n\r\n- \u30c6\u30b9\u30c8\u74b0\u5883\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\r\n  ```.pyplrc```\u3092\u4f5c\u3063\u3066\u3044\u306a\u3044\u5834\u5408\u306f\u30b3\u30de\u30f3\u30c9\u5b9f\u884c\u6642\u306busername\u3068password\u3092\u8981\u6c42\u3055\u308c\u308b\r\n  \u6210\u529f\u3059\u308b\u3068URL\u304c\u8fd4\u3063\u3066\u304f\u308b\u3002\r\n``` cmd or bash\r\ntwine upload --repository testpypi dist/*\r\n```\r\n- pip\u30b3\u30de\u30f3\u30c9\u306e\u30c6\u30b9\u30c8\r\n``` cmd or bash\r\npip install -i https://test.pypi.org/simple/ pth2onnx\r\n```\r\n\r\n- \u672c\u756a\u74b0\u5883\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\r\n``` cmd or bash\r\ntwine upload --repository pypi dist/*\r\n```\r\n\r\n## Lisence\r\n\r\nThis project is licensed under the MIT License, see the LICENSE.txt file for details\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "pth2onnx: Convert PyTorch model to Onnx model.",
    "version": "0.0.4",
    "project_urls": {
        "Download": "https://github.com/hamacom2004jp/pth2onnx",
        "Homepage": "https://github.com/hamacom2004jp/pth2onnx"
    },
    "split_keywords": [
        "pytorch",
        "onnx",
        "convert",
        "image",
        "ai",
        "model"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "23a514b88bcd016e49a572232ab2aa194207226825597c772b82895eed6a6d60",
                "md5": "0326d54c539aaaa169542cdef6fc249f",
                "sha256": "2b44b26e806cce0a432d0f8663c9f32ce023794d2b4a74c1a2868dc7083a93e7"
            },
            "downloads": -1,
            "filename": "pth2onnx-0.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0326d54c539aaaa169542cdef6fc249f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 13664,
            "upload_time": "2023-11-25T10:28:30",
            "upload_time_iso_8601": "2023-11-25T10:28:30.238963Z",
            "url": "https://files.pythonhosted.org/packages/23/a5/14b88bcd016e49a572232ab2aa194207226825597c772b82895eed6a6d60/pth2onnx-0.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7bc92b48ea64dd44ad28e2aa9618b49bd427899fb666c1be532117461c43365f",
                "md5": "666ea2ec87fda09720abd5dd66087356",
                "sha256": "ef3b7162aeb08a9c4fb557914b3bf697a3849e83c5fc47010adb269f2f960d99"
            },
            "downloads": -1,
            "filename": "pth2onnx-0.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "666ea2ec87fda09720abd5dd66087356",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 14020,
            "upload_time": "2023-11-25T10:28:31",
            "upload_time_iso_8601": "2023-11-25T10:28:31.674208Z",
            "url": "https://files.pythonhosted.org/packages/7b/c9/2b48ea64dd44ad28e2aa9618b49bd427899fb666c1be532117461c43365f/pth2onnx-0.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-25 10:28:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "hamacom2004jp",
    "github_project": "pth2onnx",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "pth2onnx"
}
        
Elapsed time: 0.14313s