ホーム 暮らし Using safetensors models in Diffusers & Upscaling [ControlNet Tile]

Using safetensors models in Diffusers & Upscaling [ControlNet Tile]

藤崎 健人 藤崎 健人 • 更新: • 公開: • 読了約 14 分
目次
nb8e1c3be0893

Happy New Year, everyone.
I'm hoping to post more AI illustrations and such on X (formerly Twitter) this year, but I found that generating a large number of high-resolution illustrations for free all at once is surprisingly difficult, so these are the results of my trial and error.

The general workflow is:
1. Generate images as usual using a safetensors model in Diffusers.
2. Upscale the images generated in step 1 to high resolution using ControlNet Tile in Diffusers.
That's it.

Since safetensors models seem to be more common (?) than Diffusers models these days, I will be using safetensors. I will be using the free version of Colab for the work environment.
Ultimately, I think it's best to save images to Google Drive and separate the notebooks for image generation and upscaling. If you only want to know that part, please skip to the summary.

Using a safetensors model in Diffusers

That said, the process is simple.
This time, I will use the anime-style model aamAnyloraAnimeMixAnime_v1.safetensors.

Here is the sample code.

各種インストール
!apt -y install -qq aria2
!pip install safetensors diffusers transformers omegaconf
モデルのダウンロード
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://civitai.com/api/download/models/89927 -d /content/ -o aamAnyloraAnimeMixAnime_v1.safetensors
パイプラインの設定(nsfwフィルターは外す(これしないと高確率で真っ黒になる))
from diffusers import StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_single_file("aamAnyloraAnimeMixAnime_v1.safetensors",safety_checker=None).to("cuda")
各種パラメータ設定
prompt = "plain white t-shirt,short cut hair,red hair,blue eyes,jeans,inside the room,smile,girl,solo, in the forest"
n_prompt = "nsfw, worst quality, low quality, medium quality, deleted, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digits, fewer digits, cropped, jpeg artifacts, signature, watermark, username, blurry"
height = 512
width = 512
生成
image = pipe(prompt, negative_prompt=n_prompt,height=height, width=width, num_inference_steps=30).images[0]
image.save("test.png")

With this, you can generate images for now. This is what I was able to generate as a test using the above.

Upscaling using ControlNet Tile in Diffusers

I will use ControlNet's Tile on the file output earlier.
Some of these have already been installed and downloaded from before, but I will write them all out just in case. Here is the sample code.

#各種インストール
!pip install diffusers transformers omegaconf
!pip install accelerate
!pip install xformers
!apt -y install -qq aria2
モデルのダウンロード
!aria2c --console-log-level=error -c -x 16 -s 16 -k 1M https://civitai.com/api/download/models/89927 -d /content/ -o aamAnyloraAnimeMixAnime_v1.safetensors
ControlNetとパイプラインの設定
import torch
from PIL import Image
from diffusers import ControlNetModel, StableDiffusionControlNetPipeline
from diffusers.utils import load_image
import xformers

controlnet = ControlNetModel.from_pretrained('lllyasviel/control_v11f1e_sd15_tile')

pipe = StableDiffusionControlNetPipeline.from_single_file(
    "aamAnyloraAnimeMixAnime_v1.safetensors",
    safety_checker=None,
    custom_pipeline="stable_diffusion_controlnet_img2img",
    controlnet=controlnet).to('cuda')
pipe.enable_xformers_memory_efficient_attention()
画像の読み込み
image_path = "/content/test.png"
init_img = Image.open(image_path).convert("RGB")
2倍に拡大
new_size = (init_img.width * 2, init_img.height * 2)
resized_img = init_img.resize(new_size)
プロンプト
prompt = "plain white t-shirt,short cut hair,red hair,blue eyes,jeans,inside the room,smile,girl,solo, in the forest"
n_prompt = "nsfw, worst quality, low quality, medium quality, deleted, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digits, fewer digits, cropped, jpeg artifacts, signature, watermark, username, blurry"
生成
image = pipe(prompt=prompt,
    negative_prompt=n_prompt,
    image=resized_img,
    controlnet_conditioning_image=resized_img,
    width=resized_img.size[0],
    height=resized_img.size[1],
    strength=1.0,
    generator=torch.manual_seed(0),
    num_inference_steps=32,
    ).images[0]
画像を保存
image.save("test2.png")

This is what was created with the above.


Since I doubled the 512x512 image, it became 1024x1024.
If you run out of memory along the way, it's not really recommended, but

[_ for _ in range(10000000000)]

you can use up the memory to force a crash and then re-run it, or save test.png, "disconnect and delete the runtime," and then place it again when you re-run.

Summary

How was it? The art style changes a bit, but I think I was able to output relatively large images quite cleanly. I'm a beginner at Python, so I apologize if there's anything strange.
Also, since the sample code above only outputs one image at a time, I created Colab notebooks for batch output for both image generation and upscaling. Please take a look if you'd like.
Oh, and please don't forget to set the runtime type to GPU, and be aware that if you run the same code a second time, the images will be overwritten.

safetensors image output

ControlNet Tile upscaling


Lastly... I've started an Amazon Associates account, so I would appreciate it if you could click on the product links, even if you don't end up buying anything.
(I want to check if it's working properly...)

Well then, thank you for reading this far!

同じカテゴリの記事

藤崎 健人

芸能取材歴10年のデスク。所属事務所の公式リリース、番組公式サイト、配信サービスの告知を一次情報として採用し、裏付けの取れない内容は記事にしていません。

おすすめの関連記事