【Diffusers】Inpaintモデルのファインチューニング

これでなにができる?

左の画像を右のように修正できるようになります。



こちらをみて頂ければ効果が分かってもらえると思います。

環境構築

Ubuntu 22.04 on WSL (Windows 11)
CUDA 11.7.1
Python 3.10.6
pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 --extra-index-url https://download.pytorch.org/whl/cu117
pip install git+https://github.com/huggingface/diffusers.git
pip install transformers==4.26.0
pip install accelerate==0.16.0 scipy==1.10.0 datasets==2.8.0 ftfy==6.1.1 tensorboard==2.11.2
pip install xformers==0.0.17.dev443
pip install bitsandbytes-cuda117==0.26.0.post2

実行

スクリプトはこちらのものを使わせて頂きました。

accelerate launch train_dreambooth_inpaint.py \
  --pretrained_model_name_or_path="stable-diffusion-inpainting"  \
  --instance_data_dir="RoboData44" \
  --output_dir="dreamboothinpaint1000" \
  --instance_prompt="sks robo" \
  --resolution=512 \
  --train_batch_size=1 \
  --sample_batch_size=1 \
  --gradient_accumulation_steps=1 \
  --gradient_checkpointing \
  --learning_rate=1e-6 \
  --lr_scheduler="constant" \
  --lr_warmup_steps=0 \
  --use_8bit_adam \
  --with_prior_preservation --prior_loss_weight=1.0 \
  --class_data_dir="robo-class-images800" \
  --class_prompt="robo" \
  --train_text_encoder \
  --checkpointing_steps=500 \
  --max_train_steps=1000

はまりポイント

クローンした「stable-diffusion-inpainting」ファルダ直下に「config.json」というファイルが存在していると以下のエラーが出ます。

cross_attention_dim must be specified for CrossAttnDownBlock2D

「config.json」を削除するとエラーは消失します。
github.com

その他

16GBのVRAMでギリギリ実行可能でした。