私的AI研究会 > AI_Program
生成 AI プログラミング †
これまで検証してきた結果をもとに、Python で生成 AI プログラムを書く
▼ 目 次
▲ 目 次
※ 最終更新:2025/08/09
diffusersではじめめる Stable Diffusion (準備編) †
環境構築 †
- 最初に用意するフレームワークとライブラリ
ライブラリ名 | 概要 |
PyTorch | 深層学習向けの機械学習フレームワーク |
Transformers | 自然言語処理の Transformer 系モデルの学習と推論用のライブラリ |
Diffusers | 画像生成などに使われる拡散モデルのライブラリ |
Accelerrate | PyTorch で分散学習や高速化を簡単にするためのライブラリ |
SciPy | 数値計算用のライブラリ |
- 「Anaconda」の動作する環境を構築しておく
→ Anaconda 環境構築
- 「Python」バージョンを指定して仮想環境『sd_test』を構築する
#ref(): File not found: "ai_prog_001_m.jpg" at page "AI_Program"
・Python 3.11 で作成する
(base) PS > conda create -n sd_test python=3.11 -y
- 仮想環境を有効にする
(base) PS > conda activate sd_test
(sd_test) PS >
- 環境に合わせた「PyTorch」をインストール
・オフィシャルサイト https://pytorch.org/ を開いてインストールコマンドを取得する →
(sd_test) PS > pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu128
- その他のパッケージをインストールする
(sd_test) PS > pip install transformers diffusers accelerate scipy
- 以後、必要になるパッケージはその都度インストールしていく
導入Step | インストールコマンド | 備 考 |
Step 8 | pip install matplotlib | |
Step 12 | pip install translate | 自動翻訳パッケージ |
Step 23 | pip install -U peft | |
Step 48 | pip install controlnet_aux | |
pip install opencv-contrib-python | |
Step 50 | conda install dlib=20.0 -c conda-forge | pip ではインストールできない |
pip install face_recognition | |
Step 51 | pip install asdff | |
Step 82 | pip install opencv-python ffmpeg-python imageio scikit-image pyaudio | |
※ Step 50 パッケージインストール後に起きるエラー対応 → OMP: Error #15:
前提条件 †
- プロジェクトで使用するソースコード
・project_sd_test.zip (105MB) <sd_test> ※2025/08/08更新
ソースコード一式とテスト画像がダウンロードできる
・解凍してできる「project_sd_test/」フォルダ内を次のフォルダの下に上書きコピーする
Windows の場合 →「anaconda_win/」 Linux の場合 → 「~/」
概 要 †
- このプロジェクトで作成するプログラム一覧と実行速度の目安
・ 単位 (時:)分:秒
× CPU では動作不可
※ SDXL モデルでの動作
忘備録 †
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized. †
- エラー内容
:
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://www.intel.com/software/products/support/.
- 対処方法
1. C:\Users\<USER>\anaconda3\envs\sd_test で「libiomp5md.dll」を検索
"C:\Users\<USER>\anaconda3\envs\sd_test\Lib\site-packages\torch\lib\libiomp5md.dll ← 削除"
"C:\Users\<USER>\anaconda3\envs\sd_test\Library\bin\libiomp5md.dll"
2. 1つ目の「libiomp5md.dll」を削除
3. 再度実行する
- コメント
「libiomp5md.dll」が複数存在する場合に起こるらしい
ValueError: PEFT backend is required for this method. †
error: (-2:Unspecified error) The function is not implemented. †
- エラー内容:cv2.error: OpenCV(4.12.0)
:
cv2.error: OpenCV(4.12.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:1284: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvNamedWindow'
AttributeError: module 'cv2' has no attribute 'INTER_AREA' †
- エラー内容:cv2.error: OpenCV(4.12.0)
:
File "C:\Users\XXXX\anaconda3\envs\sd_test\Lib\site-packages\controlnet_aux\midas\midas\transforms.py", line 6, in <module>
def apply_min_size(sample, size, image_interpolation_method=cv2.INTER_AREA):
^^^^^^^^^^^^^^
AttributeError: module 'cv2' has no attribute 'INTER_AREA'
- OpenCV パッケージの種類
パッケージ名 | 特徴 | 注意事項 |
opencv-python-headless | GUI を持たない OpenCV で GUI環境が不要な場面で使用 | GUI関連の関数は使用できなくなる |
opencv-python | Pythonで画像処理やコンピュータビジョンを扱うためのライブラリ | OpenCVの Pythonバインディング |
opencv-contrib-python | opencv-python パッケージに含まれていない追加のモジュールや機能 | 非商用ライセンスのアルゴリズムあり |
更新履歴 †
- 2025/06/12 初版
- 2025/08/09 ページ再構成
参考資料 †
- Image-to-Image/ControlNet/IP-Adapter