Google Colab でずんだもん*1をimg2img*2 してみました。
やることは前回記事と同様です。
taogya.hatenablog.com
環境構築は、StreamDiffusion公式を参考にします。
github.com
では環境構築から始めます。
Googleドライブを開き、右クリックでGoogle Colaboratoryを選択します。

ファイル名を変えて保存しておきます。

必要パッケージをインストールします。
%pip install torch==2.1.0 torchvision==0.16.0 xformers --index-url https://download.pytorch.org/whl/cu121
%pip install git+https://github.com/cumulo-autumn/StreamDiffusion.git@main#egg=streamdiffusion[tensorrt]
以下のようなエラー出るかもですが一旦無視します。
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.
torchaudio 2.5.1+cu124 requires torch==2.5.1, but you have torch 2.1.0+cu121 which is incompatible.
:
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.
torchvision 0.20.1+cu124 requires torch==2.5.1, but you have torch 2.1.0 which is incompatible.
セッション再起動の指示が出るので、再起動して先ほどのセルをもう一度実行します。


使用する画像をドラッグ&ドロップします。

警告出ますがセッション終了時にこのファイルは削除されてしまうので、それが嫌な方はGoogleドライブをマウントしてください。
今回はマウントしません。

TensorRT extensionをインストールします。
%pip install huggingface_hub==0.25.0
!python -m streamdiffusion.tools.install-tensorrt
huggingface_hubのバージョン指定は前回記事と同じエラー対策です。
wrapper.pyをダウンロードして、ドラッグ&ドロップします。
StreamDiffusion/utils/wrapper.py at main · cumulo-autumn/StreamDiffusion · GitHub

ではメインコード。以下のコードを参考にします。
StreamDiffusion/examples/img2img/single.py at main · cumulo-autumn/StreamDiffusion · GitHub
# Googleドライブをマウントした場合。今回不要。
# from google.colab import drive
# drive.mount('/content/drive')
import os
import sys
from typing import Literal, Dict, Optional
import fire
# __file__は使えないので修正する。
sys.path.append(os.getcwd())
# パスをGoogle Colab用に修正する。
from wrapper import StreamDiffusionWrapper
# __file__は使えないので修正する。
CURRENT_DIR = os.getcwd()
def main(
# パスをGoogle Colab用に修正する。
input: str = os.path.join(CURRENT_DIR, "input.png"),
output: str = os.path.join(CURRENT_DIR, "output.png"),
model_id_or_path: str = "KBlueLeaf/kohaku-v2.1",
lora_dict: Optional[Dict[str, float]] = None,
prompt: str = "1girl with brown dog hair, thick glasses, smiling",
negative_prompt: str = "low quality, bad quality, blurry, low resolution",
width: int = 512,
height: int = 512,
acceleration: Literal["none", "xformers", "tensorrt"] = "xformers",
use_denoising_batch: bool = True,
guidance_scale: float = 1.2,
cfg_type: Literal["none", "full", "self", "initialize"] = "self",
seed: int = 2,
delta: float = 0.5,
):
if guidance_scale <= 1.0:
cfg_type = "none"
stream = StreamDiffusionWrapper(
model_id_or_path=model_id_or_path,
lora_dict=lora_dict,
t_index_list=[22, 32, 45],
frame_buffer_size=1,
width=width,
height=height,
warmup=10,
acceleration=acceleration,
mode="img2img",
use_denoising_batch=use_denoising_batch,
cfg_type=cfg_type,
seed=seed,
)
stream.prepare(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=50,
guidance_scale=guidance_scale,
delta=delta,
)
image_tensor = stream.preprocess_image(input)
for _ in range(stream.batch_size - 1):
stream(image=image_tensor)
output_image = stream(image=image_tensor)
output_image.save(output)
if __name__ == "__main__":
fire.Fire(main)
エラーです。
RuntimeError: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx
Model load has failed. Doesn't exist.
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
/content/utils/wrapper.py in _load_model(self, model_id_or_path, t_index_list, lora_dict, lcm_lora_id, vae_id, acceleration, warmup, do_add_noise, use_lcm_lora, use_tiny_vae, cfg_type, seed,
engine_dir)
418 model_id_or_path,
--> 419 ).to(device=self.device, dtype=self.dtype)
420
12 frames
RuntimeError: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx
During handling of the above exception, another exception occurred:
NameError Traceback (most recent call last)
/content/utils/wrapper.py in _load_model(self, model_id_or_path, t_index_list, lora_dict, lcm_lora_id, vae_id, acceleration, warmup, do_add_noise, use_lcm_lora, use_tiny_vae, cfg_type, seed,
engine_dir)
426 traceback.print_exc()
427 print("Model load has failed. Doesn't exist.")
--> 428 exit()
429
430 stream = StreamDiffusion(
NameError: name 'exit' is not defined
ランタイムのタイプを変更する必要があるようです。


一度セッション終了するので、環境全て消えてしまった。。
最初からやり直します。
30秒くらいで終了!速い!

ということで、M1 Macの方はGoogle ColabとかのWebサービス使ったほうがいいかもですね。
安いし、Google Colab Proも今度使ってみようかな。
colab.research.google.com