Stable Diffusion の img2img を試す
ローカル環境で StableDiffusion を試してみたところ
txt2img が成功したので、続けて img2img も試してみた。
以下サイトを参考にして、無事成功した !🙏
https://self-development.info/%E3%80%90stable-diffusion%E3%80%91img2img%E3%81%AE%E3%82%A8%E3%83%A9%E3%83%BC%E8%A7%A3%E6%B1%BA%E6%96%B9%E6%B3%95/
また、Pythonでコードをカスタマイズして、
・生成される画像ファイル名の末尾の連番をインクリメントセーブする
・promptなどのパラメータをテキストで保存する
というのも実装できた!
※一応Code貼っておきます(py3.9)
## repair_img2img_error_StableDiffusion_Custom.py | |
## | |
## <内容> | |
## img2imgで画像生成 | |
## ファイル名末尾の番号を自動でインクリメントセーブ | |
## promptフォルダにParameterの設定ログを同時保存 | |
## | |
from PIL import Image | |
import torch | |
from torch import autocast | |
from diffusers import ( | |
DDIMScheduler, | |
StableDiffusionImg2ImgPipeline | |
) | |
import re #正規表現 | |
import os | |
#Paremeters | |
MODEL_ID = "CompVis/stable-diffusion-v1-4" | |
DEVICE = "cuda" | |
YOUR_TOKEN = "{あなたのトークン}" | |
BASE_IMG = "input.jpg" | |
PROMPT = "a photo of,Fine sashimi boatloads,cinematics" | |
SEED = 1024 | |
STEP = 100 | |
SCALE = 7.5 | |
STRENGTH = 0.9 | |
WIDTH = 512 | |
HEIGHT = 256 | |
#Custom Code =========== | |
def IncrementNumber(OriginalString): #引数の拡張子手前の数値をインクリメントして返す | |
renbanRe = re.match(".*[^\d](\d+)\..*$" ,OriginalString) # 正規表現で数字を抽出 | |
renban = renbanRe.groups()[0] # 拡張子直前の文字列だけ抽出 renban.groups() ・・・末尾からドットをはさんだ最初の数字群 | |
extRe = re.match(".*\.(.+)$",OriginalString) | |
ext = extRe.groups()[0] #拡張子 | |
#ここで番号を更新 | |
newRenban = str(int(renban)+1) | |
while len(newRenban) < len(renban): #桁数を合わせる | |
newRenban = "0" + newRenban | |
NewString = OriginalString[:(len(OriginalString)-len(renban)-len(ext)-1)] + newRenban + "." + ext | |
return NewString | |
#========================== | |
FileName = "img2img_test001.png" | |
scheduler = DDIMScheduler(beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear", clip_sample=False, | |
set_alpha_to_one=False) | |
pipe = StableDiffusionImg2ImgPipeline.from_pretrained( | |
MODEL_ID, | |
scheduler=scheduler, | |
revision="fp16", | |
torch_dtype=torch.float16, | |
use_auth_token=YOUR_TOKEN | |
).to(DEVICE) | |
init_img = Image.open(BASE_IMG) | |
init_img = init_img.resize((WIDTH, HEIGHT)) | |
generator = torch.Generator(device=DEVICE).manual_seed(SEED) | |
with autocast(DEVICE): | |
image = pipe(prompt=PROMPT, init_image=init_img, strength=STRENGTH, guidance_scale=SCALE, | |
generator=generator, num_inference_steps=STEP)["sample"][0] | |
#Increment FileName | |
currentPath = "C:/StableDiffusion/images/" | |
saveName = FileName | |
while os.path.isfile(currentPath+saveName): #存在しない番号までインクリメントする | |
saveName = IncrementNumber(saveName) | |
#Save File | |
image.save(currentPath+saveName) | |
#Save Prompt | |
if not(os.path.exists(currentPath+'prompt/')): | |
os.mkdir(currentPath+'prompt/') | |
f = open(currentPath+'prompt/'+'prompt_'+saveName.rsplit(".",1)[0]+'.txt', 'w') | |
doc = "Paremeters"+"\n"\ | |
"MODEL_ID=["+MODEL_ID+"]"+"\n"\ | |
"BASE_IMG="+BASE_IMG+"\n"\ | |
"PROMPT=["+PROMPT+"]"+"\n"\ | |
"SEED="+str(SEED)+"\n"\ | |
"STEP="+str(STEP)+"\n"\ | |
"SCALE="+str(SCALE)+"\n"\ | |
"STRENGTH="+str(STRENGTH)+"\n"\ | |
"WIDTH="+str(WIDTH)+"\n"\ | |
"HEIGHT="+str(HEIGHT) | |
f.write(doc) # promptのログ | |
f.close() |
・・・けれど、
地味に時間かかるし、使い勝手悪いので、ちょっと先行き怪しい、、、
とのことで、ちまたで騒がしい既存の超便利なWebUIを試してみる!
色々ありすぎるが、自分の用途が達成できそうな
-----------------------------------
「AUTOMATIC1111版Stable Diffusion web UI」が良さそう。
全部入り機能だが、特に気になった機能4選!!!
・Inpainting:特定の部分の描き直し ・Stable Diffusion upscale:512x512で作成後、高解像度化機能 ・Loopback:作成した画像をinputにして繰り返すことで、ブラッシュアップする ・Textual Inversion:自分で用意した学習データを使う
-----------------------------------
ということで、次回は、、、
■Stable Diffusion web UI(AUTOMATIC1111版)のインストール
https://gigazine.net/news/20220907-automatic1111-stable-diffusion-webui/
■基本の「tex2img」
https://gigazine.net/news/20220909-automatic1111-stable-diffusion-webui-how-to-use/
■クリエイティブを加速せよ「img2img」
https://gigazine.net/news/20220913-automatic1111-stable-diffusion-webui-img2img/
を試す!
ひとまず、ローカルのVRAMが12GBあるので ローカル環境でためそう。
どっかのタイミングで、Google Colabを使ってみたい。
0 コメント:
コメントを投稿