mieki256's diary



2023/04/16() [n年前の日記]

#1 [cg_tools] Rembgでエラーが出て少しハマった

画像生成AI、Stabel Diffusion web UI に、背景を消去してくれるらしい拡張機能、Rembg をインストールして使ってみようとしたのだけど。

_GitHub - AUTOMATIC1111/stable-diffusion-webui-rembg: Removes backgrounds from pictures. Extension for webui.
_簡単に背景を除去して透過できる!Rembgの使い方(WebUI AUTOMATIC1111 拡張機能) | note_lilish
_画像の背景を削除する拡張機能「Rembg」の使い方【Stable Diffusion web UI】 | くろくまそふと

妙なエラーが出て少しハマってしまったので、解決すべくジタバタした流れをメモ。

一応結論を先に書くと、Python の rembg と onnxruntime-gpu をアンインストールしてから再インストールすると改善する時もあるっぽい。

環境は、環境は、Windows10 x64 22H2 + Pyhton 3.10.6。AMD Ryzen 5 5600X + GeForce GTX 1060 6GB。

Stable Diffusion web UI (以下、SDwebUIと記述) は、D:\aiwork\stable-diffusion-webui\ にインストールしてある。

Rembg のインストール作業自体は前述の解説ページを参考にして作業。SDwebUI のページ、http://127.0.0.1:7860/ をWebブラウザで開いて、拡張機能 Extensions タブを開けば、そこからインストールができる。

Extras タブを開いて、下のほうに背景削除関連の項目が増えているから、そのあたりを指定していけば処理できるはずなのだけど、以下のエラーが出てしまった。

Error completing request
Arguments: (0, <PIL.Image.Image image mode=RGB size=192x128 at 0x1C2B77937F0>, None, '', '', True, 0, 4, 512, 512, True, 'None', 'None', 0, 0, 0, 0, False, False, 4, 'u2net', False, True, 240, 10, 10) {}
Traceback (most recent call last):
  File "D:\aiwork\stable-diffusion-webui\modules\call_queue.py", line 56, in f
    res = list(func(*args, **kwargs))
  File "D:\aiwork\stable-diffusion-webui\modules\call_queue.py", line 37, in f
    res = func(*args, **kwargs)
  File "D:\aiwork\stable-diffusion-webui\modules\postprocessing.py", line 56, in run_postprocessing
    scripts.scripts_postproc.run(pp, args)
  File "D:\aiwork\stable-diffusion-webui\modules\scripts_postprocessing.py", line 130, in run
    script.process(pp, **process_args)
  File "D:\aiwork\stable-diffusion-webui\extensions\stable-diffusion-webui-rembg\scripts\postprocessing_rembg.py", line 53, in process
    session=rembg.new_session(model),
  File "D:\aiwork\stable-diffusion-webui\venv\lib\site-packages\rembg\session_factory.py", line 71, in new_session
    ort.InferenceSession(
  File "D:\aiwork\stable-diffusion-webui\venv\lib\site-packages\onnxruntime\capi\onnxruntime_inference_collection.py", line 360, in __init__
    self._create_inference_session(providers, provider_options, disabled_optimizers)
  File "D:\aiwork\stable-diffusion-webui\venv\lib\site-packages\onnxruntime\capi\onnxruntime_inference_collection.py", line 408, in _create_inference_session
    sess.initialize_session(providers, provider_options, disabled_optimizers)
RuntimeError: D:\a\_work\1\s\onnxruntime\core\session\provider_bridge_ort.cc:1106 onnxruntime::ProviderLibrary::Get [ONNXRuntimeError] : 1 : FAIL : LoadLibrary failed with error 126 "" when trying to load "D:\aiwork\stable-diffusion-webui\venv\lib\site-packages\onnxruntime\capi\onnxruntime_providers_tensorrt.dll"

どうもよく分からんが、onnxruntime_providers_tensorrt.dll とやらでエラーが出てるんだろうか?

ファイル自体は、D:\aiwork\stable-diffusion-webui\venv\Lib\site-packages\onnxruntime\capi\ の中に、onnxruntime_providers_tensorrt.dll がちゃんとあった。

エラーメッセージ内容でググったら、以下のやり取りに遭遇。

_[BUG] ...RuntimeError: ...\onnxruntime\capi\onnxruntime_providers_tensorrt.dll" - Issue #312 - danielgatis/rembg - GitHub

rembg と onnxruntime をアンインストールしてから再インストールしたら解決した、という報告があるな…。ちなみに、SDwebUIのインストールフォルダに、Python 環境が入ってるはずなので、(SDwebUIインストールフォルダ)\venv\Scripts\ をカレントディレクトリにしてから pip を使って作業する必要がある。
cd /d D:\aiwork\stable-diffusion-webui\venv\Scripts
pip list

pip で、各モジュールのアンインストールと再インストール。
pip uninstall rembg
pip uninstall onnxruntime
pip install rembg[gpu] onnxruntime-gpu

しかし、pip が以下のようなエラーメッセージを出してきた。
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed.
This behaviour is the source of the following dependency conflicts.
open-clip-torch 2.7.0 requires protobuf==3.20.0,
but you have protobuf 3.20.3 which is incompatible.

open-clip-torch 2.7.0 が protobuf 3.20.0 を要求してるけど、protobuf 3.20.3 がインストールされちゃってるぞ、ということかな。

なら、protobuf 3.20.0 をインストールすればいいのだろうか。
pip install protobuf==3.20.0
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed.
This behaviour is the source of the following dependency conflicts.
onnx 1.13.1 requires protobuf<4,>=3.20.2,
but you have protobuf 3.20.0 which is incompatible.

onnx 1.13.1 が protobuf 3.20.2 以上を要求してるけど、protobuf 3.20.0 がインストールされちゃってるぞ、と怒られた。どうしろと。

onnx 1.13.0 をインストールしてみたらどうなるだろう。
pip install onnx==1.13.0
やはり怒られるな…。

とりあえずダメ元で、目に入ったモジュール名について、pip に -U をつけて再インストールしてみた。-U は、そのモジュールの新版がWeb上に存在するならアップデートするように指示するオプション。
pip install rembg[gpu] onnxruntime-gpu -U

こんな感じでアレコレを再インストールしていたら、最初にメモしたエラーが出ない状態になって、Rembg がようやく動いてくれた。助かった。

ちなみに、現時点では、各モジュールのバージョンは以下のようになってる。
> pip list | grep -E "rembg|onnx|proto|open-clip-torch"
onnx                    1.13.0
onnxruntime             1.14.1
onnxruntime-gpu         1.14.1
open-clip-torch         2.16.0
protobuf                3.20.3
rembg                   2.0.32

pipが警告を出す :

余談。pip list を打ったら、なんだかやたらと警告が出てきた。

以下の事例らしい。

_pip listのWARNINGを消す - Qiita

D:\aiwork\stable-diffusion-webui\venv\Lib\site-packages\ 内に、最初の文字が 「~」になってるディレクトリがいくつかあった。それらのディレクトリを別の場所に移動してみたところ、pip list を打っても警告が出てこなくなった。

以上、1 日分です。

過去ログ表示

Prev - 2023/04 - Next
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

カテゴリで表示

検索機能は Namazu for hns で提供されています。(詳細指定/ヘルプ


注意: 現在使用の日記自動生成システムは Version 2.19.6 です。
公開されている日記自動生成システムは Version 2.19.5 です。

Powered by hns-2.19.6, HyperNikkiSystem Project