ImaginAIry のインストールと動作確認(画像合成,画像編集,Prompt Based Masking,Image to Image)(Stable Diffusion,InstructPix2Pix,Stable Diffusion 2 Depth,Python を使用)(Windows 上)

ImaginAIry は,Python から stable diffusion 画像の生成を扱う機能を持ったソフトウエア.

目次

  1. 前準備
  2. ImaginAIry のインストール(Python,pip を使用)(Windows 上)
  3. 動作確認のため動かしてみる(Windows 上)

サイト内の関連ページ

Ubuntu について: 別ページ »で説明

関連する外部ページ

ImaginAIry の GitHub のページ: https://github.com/brycedrennan/imaginAIry

前準備

Build Tools for Visual Studio 2022 (ビルドツール for Visual Studio 2022)または Visual Studio 2022 のインストール(Windows 上)

インストールの判断Build Tools for Visual Studio は,開発ツールセットである. Visual Studio は統合開発環境であり,いくつかの種類があり,Build Tools for Visual Studioの機能を含むか連携して使用するものである.インストールは以下の基準で判断してください:

不明な点がある場合は,Visual Studio 全体をインストール を行う方が良い.

Build Tools for Visual Studio 2022 のインストール(Windows 上)

  1. Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)

    次のコマンドを実行

    次のコマンドは,Build Tools for Visual Studio 2022と VC2015 再配布可能パッケージをインストールするものである.

    winget install --scope machine Microsoft.VisualStudio.2022.BuildTools 
    winget install --scope machine Microsoft.VCRedist.2015+.x64
    
  2. Build Tools for Visual Studio 2022 での C++ によるデスクトップ開発,CLI,ATL,MFC のインストール(Windows 上)
    1. Visual Studio Installer の起動

      起動方法: スタートメニューの「Visual Studio Installer」を選ぶ.

    2. Visual Studio Build Tools 2022 で「変更」を選ぶ.
    3. C++ によるデスクトップ開発」をクリック.そして,画面右側の「インストール」の詳細で「v143 ビルドツール用 C++/CLI サポート(最新)」,「ATL」,「MFC」をチェックする.その後,「変更」をクリック.

Visual Studio のインストール(Windows 上)

  1. Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)

    次のコマンドを実行

    1. コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
    2. インストールコマンドの実行
      winget install Microsoft.VisualStudio.2022.Community --scope machine --override "--add Microsoft.VisualStudio.Workload.NativeDesktop Microsoft.VisualStudio.ComponentGroup.NativeDesktop.Core Microsoft.VisualStudio.Component.VC.CLI.Support Microsoft.VisualStudio.Component.CoreEditor Microsoft.VisualStudio.Component.NuGet Microsoft.VisualStudio.Component.Roslyn.Compiler Microsoft.VisualStudio.Component.TextTemplating Microsoft.VisualStudio.Component.Windows.SDK.Latest Microsoft.VisualStudio.Component.VC.Tools.x86.x64 Microsoft.VisualStudio.Component.VC.ATL Microsoft.VisualStudio.Component.VC.ATLMFC"
      winget install Microsoft.VisualStudio.2022.Community --scope machine Microsoft.VCRedist.2015+.x64
      

      インストールされるコンポーネントの説明:

      • NativeDesktop:C++によるデスクトップアプリケーション開発のためのワークロード一式
      • NativeDesktop.Core:C++デスクトップ開発に必要な基本コンポーネント群
      • VC.CLI.Support:マネージドコードとネイティブコードの統合開発を可能にするC++/CLIサポート
      • CoreEditor:コード編集,デバッグ,検索などの基本機能を提供するVisual Studioのコアエディタ
      • NuGet:.NETライブラリの依存関係を管理するパッケージ管理システム
      • Windows.SDK.Latest:Windows 向けアプリケーション開発用SDK(Software Development Kit)
      • VC.Tools.x86.x64:32ビット及び64ビット向けC++コンパイラとビルドツール
      • VC.ATL:Windowsコンポーネント開発用のActive Template Library
      • VC.ATLMFC:デスクトップアプリケーション開発用のMicrosoft Foundation Class Library

      システム要件と注意事項:

      • 管理者権限でのインストールが必須
      • 必要ディスク容量:10GB以上
      • 推奨メモリ:8GB以上のRAM
      • インストール過程でシステムの再起動が要求される可能性がある
      • 安定したインターネット接続環境が必要

      追加のコンポーネントが必要な場合は,Visual Studio Installerを使用して個別にインストールすることが可能である.

    3. インストール完了の確認
      winget list Microsoft.VisualStudio.2022.Community
      

      トラブルシューティング:

      インストール失敗時は,以下のログファイルを確認:

      %TEMP%\dd_setup_<timestamp>.log
      %TEMP%\dd_bootstrapper_<timestamp>.log
  2. Visual Studio での C++ によるデスクトップ開発,CLI のインストール(Windows 上)
    1. Visual Studio Installer の起動

      起動方法: スタートメニューの「Visual Studio Installer」を選ぶ.

    2. Visual Studio Community 2022 で「変更」を選ぶ.
    3. C++ によるデスクトップ開発」をチェック.そして,画面右側の「インストール」の詳細で「v143 ビルドツール用 C++/CLI サポート(最新)」をチェックする.その後,「インストール」をクリック.

Python 3.10,Git のインストール(Windows 上)

Pythonは,プログラミング言語の1つ. Gitは,分散型のバージョン管理システム.

手順

  1. Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)

    次のコマンドを実行

    次のコマンドは,Python ランチャーとPython 3.10とGitをインストールし,Gitパスを通すものである.

    次のコマンドでインストールされるGitは 「git for Windows」と呼ばれるものであり, Git,MinGW などから構成されている.

    winget install --scope machine Python.Launcher
    winget install --scope machine Python.Python.3.10
    winget install --scope machine Git.Git
    powershell -command "$oldpath = [System.Environment]::GetEnvironmentVariable(\"Path\", \"Machine\"); $oldpath += \";c:\Program Files\Git\cmd\"; [System.Environment]::SetEnvironmentVariable(\"Path\", $oldpath, \"Machine\")"
    

関連する外部ページ

サイト内の関連ページ

関連項目Python, Git バージョン管理システム, Git の利用

Build Tools for Visual Studio 2022,NVIDIA ドライバ,NVIDIA CUDA ツールキット 11.8,NVIDIA cuDNN 8.9.7 のインストール(Windows 上)

サイト内の関連ページNVIDIA グラフィックスボードを搭載しているパソコンの場合には, NVIDIA ドライバNVIDIA CUDA ツールキットNVIDIA cuDNN のインストールを行う.

関連する外部ページ

PyTorch のインストール(Windows 上)

  1. Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
  2. PyTorch のページを確認

    PyTorch の公式ページ: https://pytorch.org/index.html

  3. 次のようなコマンドを実行(実行するコマンドは,PyTorch のページの表示されるコマンドを使う).

    次のコマンドを実行することにより, PyTorch 2.3 (NVIDIA CUDA 11.8 用)がインストールされる. 但し,Anaconda3を使いたい場合には別手順になる.

    事前に NVIDIA CUDA のバージョンを確認しておくこと(ここでは,NVIDIA CUDA ツールキット 11.8 が前もってインストール済みであるとする).

    PyTorch で,GPU が動作している場合には,「torch.cuda.is_available()」により,True が表示される.

    python -m pip install -U --ignore-installed pip
    python -m pip uninstall -y torch torchvision torchaudio torchtext xformers
    python -m pip install -U torch torchvision torchaudio numpy --index-url https://download.pytorch.org/whl/cu118
    
    python -c "import torch; print(torch.__version__, torch.cuda.is_available())" 
    

    Anaconda3を使いたい場合には, Anaconda プロンプト (Anaconda Prompt)管理者として実行し, 次のコマンドを実行する. (PyTorch と NVIDIA CUDA との連携がうまくいかない可能性があるため,Anaconda3を使わないことも検討して欲しい).

    conda install -y pytorch torchvision torchaudio pytorch-cuda=11.8 cudnn -c pytorch -c nvidia
    py -c "import torch; print(torch.__version__, torch.cuda.is_available())" 
    

    サイト内の関連ページ

    関連する外部ページ

ImaginAIry のインストール(Python,pip を使用)(Windows 上)

  1. Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)
  2. インストール
    python -m pip install -U imaginairy
    
  3. 関連ファイルをダウンロード
    cd /d c:%HOMEPATH%
    rmdir /s /q imaginAIry
    git clone --recursive https://github.com/brycedrennan/imaginAIry
    

動作確認のため動かしてみる(Windows 上)

https://github.com/brycedrennan/imaginAIry の手順に従う.

テキストからの画像生成(Stable Diffusion を使用)

Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)

次を実行することにより,画像ファイルが生成される.画像ファイル名が表示されるので確認.

imagine "海沿いを走る"
imagine "a scenic landscape"

テキストからの画像生成,Stable Diffusion のバージョンを 2.1 に指定

Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)

次を実行することにより,画像ファイルが生成される.画像ファイル名が表示されるので確認.

imagine --model SD-2.1 "海沿いを走る"
imagine --model SD-2.1 "a scenic landscape"

画像編集(InstructPix2Pix を使用)

InstructPix2Pix を使用している

InstructPix2Pix の GitHub のページ: https://github.com/timothybrooks/instruct-pix2pix

Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)

cd /d c:%HOMEPATH%
cd imaginAIry
cd assets
aimg edit scenic_landscape_winter.jpg "make a sky sunset"

元画像

画像編集の結果

cd /d c:%HOMEPATH%
cd imaginAIry
cd assets
aimg edit 000032_337692011_PLMS40_PS7.5_a_photo_of_a_dog.jpg "make a dog white"

元画像

画像編集の結果

cd /d c:%HOMEPATH%
cd imaginAIry
cd assets
aimg edit 000056_293284644_PLMS40_PS7.5_photo_of_a_bowl_of_fruit.jpg "make fruits vegetables"

元画像

画像編集の結果

cd /d c:%HOMEPATH%
cd imaginAIry
cd assets
aimg edit spock.jpg "make a man long hair" --prompt-strength 20

元画像

画像編集の結果

cd /d c:%HOMEPATH%
cd imaginAIry
cd assets
aimg edit spock.jpg "add glasses"

元画像

画像編集の結果

cd /d c:%HOMEPATH%
cd imaginAIry
cd assets
aimg edit spock.jpg "make color tropical"

元画像

画像編集の結果

Prompt Based Masking(clipseg を使用)

clipseg を使用している.

clipseg の GitHub のページ: https://github.com/timojl/clipseg

Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)

cd /d c:%HOMEPATH%
cd imaginAIry
cd assets
imagine ^
    --init-image girl_with_a_pearl_earring.jpg ^
    --mask-prompt "face AND NOT (bandana OR hair OR blue fabric){*6}" ^
    --mask-mode keep ^
    --init-image-strength .2 ^
    --fix-faces ^
   "a young girl" "an old lady" "white dressed" "fireman"

元画像

Prompt Based Masking の結果

cd /d c:%HOMEPATH%
cd imaginAIry
cd assets
imagine ^
    --init-image 000056_293284644_PLMS40_PS7.5_photo_of_a_bowl_of_fruit.jpg ^
     --mask-prompt "fruit OR fruit stem{*6}" ^
    --mask-mode replace ^
    --mask-modify-original ^
    --init-image-strength .1 ^
    "ball" "animal doll" "soup" "rice" "candy" "pencil" 

元画像

Prompt Based Masking の結果

Image-to-Image(Stable Diffusion 2 Depth を使用)

Stable Diffusion 2 Depth を使用している.

Windows で,コマンドプロンプト管理者権限で起動する(例:Windowsキーを押し,「cmd」と入力し,「管理者として実行」を選択)

cd /d c:%HOMEPATH%
cd imaginAIry
cd assets
imagine --model SD-2.0-depth --init-image mona-lisa.jpg --init-image-strength 0.05  "professional headshot photo of a woman" -r 4 -w 1024 -h 1024 --steps 50