
ãStable Diffusion 3 Medium ãã¡ã€ã³ãã¥ãŒãã³ã°ãã¥ãŒããªã¢ã«ãããèªãã Stability AIã®ãæ°ã¢ãã«ã
Stability AIå ¬åŒããã°ã§è峿·±ãèšäºããªãªãŒã¹ãããŸããã
https://ja.stability.ai/blog/stable-diffusion-3-medium-fine-tuning-tutorial
以äžãAICU mediaã«ããAIïŒäººå翻蚳ãååãããªãå§çž®ãã€ã€2äžåè¶ ãã§ãéãããŸãã
ã¯ããã«
ããã«ã¡ã¯ïŒStability AI ã®çæã¡ãã£ã¢ãœãªã¥ãŒã·ã§ã³ãšã³ãžãã¢ïŒããã³ããªãŒã©ã³ã¹ã® 2D/3D ã³ã³ã»ãããã¶ã€ããŒïŒã® Yeo Wang ã§ããYouTube ã§ç§ã®ãããªãèŠãããšããããããããŸããããã³ãã¥ããã£ïŒGithubïŒãéããŠç§ãç¥ã£ãŠãããããããŸãããå人çã«ã¯ãSD3 Medium ããã¬ãŒãã³ã°ãããšãã«ãŸãšããªçµæãåŸãããã®ã§ãå®å šãªãã¡ã€ã³ãã¥ãŒãã³ã°ãš LoRA ãã¬ãŒãã³ã°ã®äž¡æ¹ã«ã€ããŠãããã€ãã®æŽå¯ãšã¯ã€ãã¯ã¹ã¿ãŒãæ§æãå ±æããŸãã
察象èªè : ãã¡ã€ã³ãã¥ãŒãã³ã°ã«é¢ããåºæ¬çãªç¥èãæã€ãšã³ãžãã¢ãŸãã¯æè¡è
ç®ç: SD1.5/SDXL ãš Stable Diffusion 3 Medium (SD3M) ã®ãã¡ã€ã³ãã¥ãŒãã³ã°ã®éããçè§£ããããå€ãã®ãŠãŒã¶ãŒã SD3M ã§ãã¡ã€ã³ãã¥ãŒãã³ã°ã§ããããã«ãã
SD3 ã®ãããªç»åãçæããããšã«èå³ããããŸããïŒ
ïŒ3ã€ã®ããã³ãããšãããŒã¹ã¢ãã«ãšãã¡ã€ã³ãã¥ãŒã³ã¢ãã«ã«ããåºåç»åã®äŸïŒ
詳现ã«ã€ããŠã¯ããã®ãªã³ã¯ããç¶ãããèªã¿ãã ããã
ãŸããããŸããšããŠãè¿æ¥å
¬éäºå®ã®ç»åã¢ãã«ããã£ãã玹ä»ããŸãïœð
Yeo Wangããã®ç±å¿ãªåç»ãšæ å ±å¯åºŠ
以äžã®ããã«éåžžã«è»œãããªã§ç޹ä»ãããçãããã°ã®å ã«ã¯ã Stability AI ã«ããèšå€§ãª notionããŒãžããããŸããã
ãŸã玹ä»ãããŠããKasukanraããã®ãã£ã³ãã«ã«ã¯ Stable Diffusionã®è§£èª¬åç»ã ãã§ãªããFLUX.1[dev]ã«ããLoRAã®è§£èª¬åç»ãªã©ããããŸãã

kasukanraããã®ãã£ã³ãã«ã¯ã€ã©ã¹ãã¬ãŒã·ã§ã³ã顿ã«ãã話é¡ã¯å€ãã§ãããåçšãã£ã©ã¯ã¿ãŒã®æš¡å£ãäºæ¬¡åµäœãå€ãæ±ãããŠããããã§ã¯ãªãããšã«æ³šç®ã§ãããããŠéåžžã«æ å ±éãå€ãïŒåç»ãéåžžã«æ¿å¯ã§ãåéã§é£ã°ããŠã¿ãããšãããããæ¢ããŠã¿ãåç»ããšããå°è±¡ã§ãã
notionã«æžãããæ å ±ãã
ããŠãnotionã«æžãããæ
å ±ãèªã¿è§£ããŠãããŸãã
Ubuntuç°å¢ã«ãããComfyUIã䜿ã£ãSD3ã®ãã¡ã€ã³ãã¥ãŒãã³ã°ã«é¢ããæ
å ±ããæå
¥ããã³ãã³ãããåŠç¿ã¬ãŒããã³ã³ãã£ã°ãã¡ã€ã«ã®èšå®ãŸã§çްããèšèŒãããŠããŸãã
ãã玹ä»ãSDXLæä»£ã®LoRAããã¡ã€ã³ãã¥ãŒãã³ã°ã«é¢ããåºç€ç¥èã«ã€ããŠã¯ãã²SDé»è²æ¬ãç»åçæAI Stable Diffusionã¹ã¿ãŒãã¬ã€ããããåç §ãã ããã
ååã¯Ubuntuã§ã®ç°å¢é¢ãšãSimpleTunerãã®ã€ã³ã¹ããŒã«ã§ããããã«ã¹ã¿ã ã³ã³ãã£ã°ã«æ»ãã以éã®ãSimpleTunerã§ãã£ãã·ã§ã³ã®ããããã¢ãŠãæ©èœã䜿ãã®ãšãã·ã£ããã«ãã·ãã¥ã¬ãŒãããã®ãšã§ã¯ãäœãéããããã®ã§ããããïŒããšãã話é¡ä»¥éãèªã¿ãããããããŸãã
In addition, there is a repeats parameter that you may or may not be familiar with depending on whether or not youâve used other training repositories before.
repeats duplicates your images (and optionally rotates, changes the color, etc.) and captions as well to help generalize the style into the model and prevent overfitting. While SimpleTuner supports caption dropout (randomly dropping captions a specified percentage of the time),it doesnât support shuffling tokens (tokens are kind of like words in the caption) as of this moment, but you can simulate the behavior of kohyaâs sd-scripts where you can shuffle tokens while keeping an n amount of tokens.
If youâd like to replicate that, Iâve provided a script here that will duplicate the images and manipulate the captions:
ããã«ã以åã«ä»ã®ãã¬ãŒãã³ã°ãªããžããªã䜿ã£ãããšããããã©ããã«ãã£ãŠã銎æã¿ããããã©ãããåããããããããªãrepeatsãã©ã¡ãŒã¿ããããŸããrepeatsã¯ãã¢ãã«ã«ã¹ã¿ã€ã«ãäžè¬åãããªãŒããŒãã£ããã£ã³ã°ãé²ãããã«ãç»åïŒãªãã·ã§ã³ã§å転ãè²ã®å€æŽãªã©ïŒãšãã£ãã·ã§ã³ãè€è£œããŸããSimpleTunerã¯ãã£ãã·ã§ã³ã®ããããã¢ãŠãïŒæå®ããå²åã§ã©ã³ãã ã«ãã£ãã·ã§ã³ãããããããïŒããµããŒãããŠããŸãããçŸæç¹ã§ã¯ããŒã¯ã³ïŒããŒã¯ã³ã¯ãã£ãã·ã§ã³ã®åèªã®ãããªãã®ïŒã®ã·ã£ããã«ã¯ãµããŒãããŠããŸããïŒ
ïŒä»¥äžAICU mediaã解説ããªããèªã¿ããã翻蚳ããŸãïŒ
â
以äžããç§ãã¯Yeo Wangãããããã¯notionèè
ã®èšãšãªããŸãã
ããããkohyaã®sd-scriptsã®åäœãã·ãã¥ã¬ãŒãããããšãã§ããŸãããã®å Žåãnåã®ããŒã¯ã³ãä¿æããªãããããŒã¯ã³ãã·ã£ããã«ããããšãã§ããŸãã
ãããããåçŸãããã®ã§ããã°ãç»åãè€è£œããŠãã£ãã·ã§ã³ãæäœããã¹ã¯ãªãããããã«çšæããŸããïŒ
import os
import shutil
import random
from pathlib import Path
import re
def duplicate_and_shuffle_dataset(input_folder, output_folder, dataset_repeats, n_tokens_to_keep):
# Create output folder if it doesn't exist
Path(output_folder).mkdir(parents=True, exist_ok=True)
# Get all image files
image_files = [f for f in os.listdir(input_folder) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
for i in range(dataset_repeats):
for image_file in image_files:
# Get corresponding text file
text_file = os.path.splitext(image_file)[0] + '.txt'
if not os.path.exists(os.path.join(input_folder, text_file)):
print(f"Warning: No corresponding text file found for {image_file}")
continue
# Create new file names
new_image_file = f"{os.path.splitext(image_file)[0]}_{i+1}{os.path.splitext(image_file)[1]}"
new_text_file = f"{os.path.splitext(text_file)[0]}_{i+1}.txt"
# Copy image file
shutil.copy2(os.path.join(input_folder, image_file), os.path.join(output_folder, new_image_file))
# Read, shuffle, and write text file
with open(os.path.join(input_folder, text_file), 'r') as f:
content = f.read().strip()
# Split tokens using comma or period as separator
tokens = re.split(r'[,.]', content)
tokens = [token.strip() for token in tokens if token.strip()] # Remove empty tokens and strip whitespace
tokens_to_keep = tokens[:n_tokens_to_keep]
tokens_to_shuffle = tokens[n_tokens_to_keep:]
random.shuffle(tokens_to_shuffle)
new_content = ', '.join(tokens_to_keep + tokens_to_shuffle)
with open(os.path.join(output_folder, new_text_file), 'w') as f:
f.write(new_content)
print(f"Dataset duplication and shuffling complete. Output saved to {output_folder}")
# Example usage
input_folder = "/weka2/home-yeo/datasets/SDXL/full_dataset_neo"
output_folder = "/weka2/home-yeo/datasets/SDXL/duplicate_shuffle_10_fantasy"
dataset_repeats = 10
n_tokens_to_keep = 2
duplicate_and_shuffle_dataset(input_folder, output_folder, dataset_repeats, n_tokens_to_keep)
ph070, A rainy urban nighttime street scene features two cars parked along the wet pavement. The primary subject is a sleek, modern silver car with streamlined curves and glistening wheels reflecting the rain-soaked road. The background includes another vehicleâa darker sedanâpartially obscured. Illuminated signs with Asian characters suggest a city setting possibly in a bustling nightlife district. The style is distinctly cinematic with a futuristic, neo-noir aesthetic, characterized by moody blue tones and the reflective glow of wet surfaces. The streetlights and the occasional gleam of neon green and orange provide contrast, enhancing the dark ambiance of the city at night. The atmosphere evokes a sense of mystery and quiet anticipation, with the deserted street hinting at hidden stories or events about to unfold.
ã§ã¯ãSimpleTunerã§ãã£ãã·ã§ã³ã®ããããã¢ãŠãæ©èœã䜿ãã®ãšãã·ã£ããã«ãã·ãã¥ã¬ãŒãããã®ãšã§ã¯ãäœãéããããã®ã§ããããïŒ
ããã¡ã³ã¿ãžãŒã»ã¢ãŒããããŒã¿ã»ããã®å Žåãããã£ãã·ã§ã³ã»ããããã¢ãŠããã®æ¹ããã·ã£ãããªã³ã°ã«æ¯ã¹ãŠãããŒã¿ã»ããã®ã¹ã¿ã€ã«ãããä¿ã€ããšãã§ãããšæããããCinematic photoãããŒã¿ã»ããã®å Žåããã£ãã·ã§ã³ã®ããããã¢ãŠããšã·ã£ãããªã³ã°ã®äž¡æ¹ã®æ¹æ³ã¯äºææ§ããããšæãããäž¡è ã«ã€ããŠæç¢ºãªçµè«ãåºãããšã¯ã§ããªãã£ãã
ãšããããã`multibackend.json`ãã¡ã€ã«ã§ `repeats` ã®æ°ã `10` ã«èšå®ãã`config` ã§ãã£ãã·ã§ã³ã®ããããã¢ãŠãã `0.25` ã«èšå®ããããšã§ã`repeats` ã䜿ãïŒãããŠãã£ãã·ã§ã³ãããããã¢ãŠãããïŒã»ãã«åŸããã
â èš³æ³šïŒæ®å¿µãªãããdatasets/SDXL/full_dataset_neoãã«ã€ããŠã®æäŸã¯ãªãããã§ããã貎éãªããŒã¿ã»ããã«ã€ããŠã®æ å ±ããããŸãã
ããã«ãããŒã¿ã»ããã«ã€ããŠããå°ã詳ããè§ŠããŠãããããfantasy art` ããŒã¿ã»ããã®å Žåãè§£å床ã `1024 x 1024` ãã¯ã»ã«ã®æ£æ¹åœ¢ã®ç»åã§æ§æãããŠãããäžæ¹ã `cinematic photo` ããŒã¿ã»ããã«ã¯ããããçš®é¡ã®ã¢ã¹ãã¯ãæ¯/è§£å床ããã£ããç§ã® `multibackend.json` ã§ã¯ããªãã³ã°ãç¡å¹ã«ããŠãããããçš®ã®ãã¥ãŒãªã¹ãã£ãã¯ãªæ¹æ³(äŸãã°ç§ã®detect_utils.py)ã䜿ã£ãŠããªã¯ãããã³ã°ãè¡ãããšã匷ããå§ãããŸããããã§ãªãå Žåã¯ã`multibackend.json`ã®ãªãã·ã§ã³ã®äžã€ã䜿ãå¿ èŠããããŸãïŒ
``jsx "crop_style"ïŒ"random|center|corner", "crop_aspect"ïŒãæ£æ¹åœ¢|ä¿å", ```
ãããã¯ãæ³å®ãããé¢å¿é åã«çŠç¹ãåãããã®ãé£ãããçæ³çã§ã¯ãªãããã ããããã«ãããããã©ã«ãã® `config` ã§ããç§ã®ã«ã¹ã¿ã `config` ã§ããããªãã³ã°ã¯ `false` ã«èšå®ãããŠããã®ã§ãç§ãæ°ã«ããããšã§ã¯ãªãã
ã¹ãããã®èšç®
æå€§åŠç¿ã¹ãããæ°ã¯ç°¡åãªæ°åŒã§èšç®ã§ããŸãïŒåäžã³ã³ã»ããã®å ŽåïŒïŒ

æå€§åŠç¿ã¹ãããæ° = ïŒãµã³ãã«æ° x ç¹°ãè¿ã / ããããµã€ãºïŒx ãšããã¯
ããããµã€ãºïŒ1ã€ãã¬ãŒã·ã§ã³ïŒç¹°ãè¿ãïŒã®ãªãã§åŠçããããµã³ãã«æ°
ãµã³ãã«æ°ïŒããŒã¿ã»ããå ã®ç·ãµã³ãã«æ°
ç¹°ãè¿ãåæ°ïŒäœå1ãšããã¯ã§ããŒã¿ã»ãããç¹°ãè¿ãã
EpochsïŒããŒã¿ã»ããå šäœãåŠçããåæ°
äŸãã°ããã§ãfantasy artãããŒã¿ã»ããã«ã¯476æã®ç»åããããšããŸããultibackend.jsonã®10åã®ç¹°ãè¿ãã«è¿œå ãããtrain_batch_size`ã`6`ã«ãããããããèšå®ã®çç±ã¯2ã€ãããŸãã
(1)ãã®å€ãªããããã°ã¬ã¹ããŒã1ç§ã2ç§ããšã«æŽæ°ãããã®ãèŠãããšãã§ããã 1åã®å埩ã§`6`ã®ãµã³ãã«ãåãããšãã§ãããšããç¹ã§ååãªå€§ããã§ããããã¬ãŒãã³ã°ã®éçšã§ããå€ãã®æ±åãè¡ãããšãã§ããã
ããç§ã30ãšããã¯ãšã欲ãããªããæçµçãªèšç®ã¯ãããªãïŒ

ããã¯å€ããå°ãªããã23,800ã¹ããããã«çžåœããŸãã
æ¬åŒ§ã®äžã®éšåã476 x10 / 6ãã¯1ãšããã¯ãããã®ã¹ãããæ°ã衚ãã793ã§ãã
CHECKPOINTING_STEPS ãš MAX_NUM_STEPS ã¯ãããã 800 ãš 24,000 ã«åãäžããããŸãã
CHECKPOINTING_STEPS ã¯ã¢ãã«ã®ãã§ãã¯ãã€ã³ããä¿åããé »åºŠã衚ããŸããããã800ã«èšå®ãããšã1ãšããã¯ã«ããªãè¿ãã®ã§ãåé¡ãªããšæãããŸããCHECKPOINTING_LIMITã¯ã以åã®ãã§ãã¯ãã€ã³ããäžæžãããåã«ä¿åããããã§ãã¯ãã€ã³ãã®æ°ã§ããç§ã®å Žåããã¹ãŠã®ãã§ãã¯ãã€ã³ããä¿åããŠããããã£ãã®ã§ã`30`ã®ãããªé«ãæ°å€ã«å¶éãèšå®ããŸããã
è€æ°ã®ã³ã³ã»ãã
äžèšã®äŸã§ã¯ãé ã«1ã€ã®çµ±äžçãªããªã¬ãŒã¯ãŒã `k4s4` ãæã€åäžã®æŠå¿µã§åŠç¿ããŠããŸããããããããŒã¿ã»ããã«è€æ°ã®æŠå¿µ/ããªã¬ãŒã¯ãŒããããå Žåãã¹ãããã®èšç®ã¯ä»¥äžã®ããã«ãªããŸãïŒ
2ã³ã³ã»ããã®å Žåãã¹ãããã®èšç®åŒã¯ä»¥äžã®ããã«ãªããŸãã

iã³ã³ã»ããã®å Žå

æåŸã«åŠç¿çã§ããããã以äžé«ããããšåŸé ãççºããŠããŸãã®ã§ã1.5e-5ããšããŠããŸãã

ããã§éèŠãªèšå®ã¯ã»ãšãã©ã«ããŒã§ããã
â
蚳泚ïŒè³ªãé«ããããã«å€æŽã§ãã远å èšå®ãããã€ããããŸãã®ã§ãã«ã¹ã¿ã èšå®ããåç
§ãã ããã
æ€èšŒç»åãçæããé »åºŠã¯ VALIDATION_STEPS ã§èšå®ãããç§ã¯800ïŒç§ã®ããŒã¿ã»ããã®ãšããã¯ã®ã¹ãããæ°ïŒã®1/4ã§ãã200ã«èšå®ãããã€ãŸãã1/4ãšããã¯ããšã«æ€èšŒç»åãçæããããšã«ãªããæ£æ°åºŠããã§ãã¯(sanity check)ããããã«ãå°ãªããšãååã®ãšããã¯ããšã«æ€èšŒç»åãçæããããšããå§ããããããããªããšããšã©ãŒãçŽ æ©ãçºèŠã§ããªããããããªãã
LR_SCHEDULEãšLR_WARMUP_STEPS
æåŸã«`LR_SCHEDULE`ãš`LR_WARMUP_STEPS`ã
ãã®å Žåã¯`cosine`ã¹ã±ãžã¥ãŒã«ã§è¡ããŸãããããããªæãã«ãªããŸãã


SimpleTunerã¯ããã©ã«ãã®ãŠã©ãŒã ã¢ãããå šãã¬ãŒãã³ã°ã¹ãããã®10ïŒ ã«èšå®ããŠãããããã¯å€æŽããªãã
â 蚳泚ïŒCustom SD3 full config.env ã«ã€ããŠãå ¬éãããŠããŸãã
å®éã«ãã¬ãŒãã³ã°ãéå§ãã
ããã§ `config.env` ãš `multibackend.json` ãé©åã«ã»ããã¢ãããããã®ã§ããããããã¬ãŒãã³ã°ãå®è¡ãããããã¯ã«ãŒããã£ã¬ã¯ããªã§
bash train.shã§å®è¡ããŸãã
èšç®èœåã«é¢ããèå¯
äžèšã®å®éšã¯ã¯ã©ãŠãã³ã³ãã¥ãŒãã£ã³ã°ã䜿ã£ãŠè¡ããŸããããç§ãæäŸãã`config.env`ã¯ã24GBã®VRAM GPUïŒ`RTX 4090`ïŒã䜿ã£ãç§ã®å人çãªãã·ã³ã«ãŠïŒãããããŠïŒå¯èœã§ãã


ã¢ãã«ãã©ããããïŒ
ããã©ã«ãã§ã¯ããã¬ãŒãã³ã°ãå®äºãããšã以äžã®ãããªãã©ã«ãæ§é ã«ãªããŸãã

ããã§çè§£ããŠããã¹ãããšãããã€ããããŸãã
datasets/models`å ã®å`checkpoint`ãã£ã¬ã¯ããªã«ãã`diffusion_pytorch_model.safetensors`ãã¡ã€ã«ãå¿ èŠã§ãããã®ãã¡ã€ã«ã®å Žæã¯ãtransformerãã£ã¬ã¯ããªã®äžã§ããèŠãŠã®éããããã¯ããªãè€éã§ããæçµçãªãã¹ã¯æ¬¡ã®ããã«ãªããŸãïŒ weka2/home-yeo/simpletuner_models/ninth_run/datasets/models/checkpoint-2400/transformer/diffusion_pytorch_model.safetensors` .
diffusion_pytorch_model.safetensors`ãã¡ã€ã«ã¯ã`ComfyUI`ã®`Load Checkpoint`ããŒãå ã§äœ¿çšãããšåäœããŸãããUNETããŒããŒ`ã䜿çšããããæ°ããããŒãžã§ã³ã®`ComfyUI`ã§ã¯ã`Load Diffusion Model`ããŒãã䜿çšããå¿ èŠããããŸãããã®ããããããã®ã¢ãã«ã `ComfyUI` ã® models ãã£ã¬ã¯ããªå ã® `unet` ãã£ã¬ã¯ããªã«ã³ããŒããå¿ èŠããããŸãïŒ admin/home-yeo/workspace/ComfyUI/models/unet`ã®ããã«ããŸãã
ãããã®ã¢ãã«ãå¿ èŠãªå Žæã«é 眮ããããã»ã¹ãå¹çåããããã«ã`source`ãš`target`ãã£ã¬ã¯ããªã®äž¡æ¹ãæå®ãããšãç§ãæžããã·ã§ã«ã¹ã¯ãªãããããããã£ãŠãããŸãã
SOURCE_DIR` ãš `TARGET_DIR` ã眮ãæããŠãã¢ãã«ã `unet` ãã©ã«ãã« `symlink` ããŸãã
#!/bin/bash
# Source directory where the models are stored
SOURCE_DIR="/admin/home-yeo/workspace/simpletuner_models/sd3_medium/full_finetune/cinema_photo/03/datasets/models"
# Target directory for symlinks
TARGET_DIR="/admin/home-yeo/workspace/ComfyUI/models/unet/simpletuner_blog_cine_photo_03"
# Iterate over each checkpoint directory
for CHECKPOINT_DIR in $(ls -d ${SOURCE_DIR}/checkpoint-*); do
# Extract the checkpoint number from the directory name
CHECKPOINT_NAME=$(basename ${CHECKPOINT_DIR})
# Define the source file path
SOURCE_FILE="${CHECKPOINT_DIR}/transformer/diffusion_pytorch_model.safetensors"
# Define the symlink name
LINK_NAME="${TARGET_DIR}/${CHECKPOINT_NAME}.safetensors"
# Check if the source file exists
if [ -f "${SOURCE_FILE}" ]; then
# Create a symlink in the target directory
ln -s "${SOURCE_FILE}" "${LINK_NAME}"
echo "Symlink created for ${CHECKPOINT_NAME}"
else
echo "File not found: ${SOURCE_FILE}"
fi
done
echo "Symlinking complete."ãã¹ããã§ãã¯ãã€ã³ãã®æ±ºå®
æé©ãªãã§ãã¯ãã€ã³ããæ±ºå®ããæ¹æ³ã¯ãç¹å®ã®ããã³ããã®ãã§ãã¯ãã€ã³ãçªå·ãX軞ã«ãããããããã€ãŸããããªãããã




ãããè¡ãããã«ãç§ã¯ComfyUIã¯ãŒã¯ãããŒã®APIããŒãžã§ã³ãããŒãããã«ã¹ã¿ã ã¹ã¯ãªããã䜿çšããŠããŸããä¿å(API圢åŒ)ãã¿ã³ãã¯ãªãã¯ããã°ãã©ããªã¯ãŒã¯ãããŒã§ãAPI圢åŒã§ä¿åã§ãããç§ã¯ãã§ã«ããªãã®äœ¿çšã®ããã«äžã®ããŒãžã§ã³ãä¿åããŸãããããComfyUI APIã®äœ¿ãæ¹ã«ã€ããŠãã£ãšè©³ãããããªã¬ã€ããå¿ èŠã§ããã°ãæšå¹Žäœã£ãåç»ãããã«ãããŸãã
ComfyUIãèµ·åããŠããããšã確èªããŠããã以äžã®ã¹ã¯ãªãããå®è¡ããããŸããã¹ã¯ãªãããå®è¡ããå Žæãšåãå Žæã«`.env`ãã¡ã€ã«ãã»ããã¢ããããå¿ èŠããããŸãã
import os
import json
import random
from urllib import request
import datetime
from PIL import Image, ImageDraw, ImageFont
import time
import re
import urllib.error
from dotenv import load_dotenv
load_dotenv()
# Configuration
api_workflow_dir = os.getenv("API_WORKFLOW_DIR")
finetune_dir = os.getenv("FINETUNE_DIR")
api_workflow_file = os.getenv("API_WORKFLOW_FILE")
api_endpoint = os.getenv("API_ENDPOINT")
image_output_dir = os.getenv("IMAGE_OUTPUT_DIR")
font_ttf_path = os.getenv("FONT_TTF_PATH")
comfyui_output_dir = os.getenv("COMFYUI_OUTPUT_DIR")
api_endpoint = f"http://{api_endpoint}/prompt"
workflow_file_path = os.path.join(api_workflow_dir, api_workflow_file)
workflow = json.load(open(workflow_file_path))
current_datetime = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
relative_output_path = current_datetime
directory_creation_timeout = 3000 # Timeout for directory creation in seconds
image_generation_timeout = 30000 # Timeout for image generation in seconds
def get_checkpoint_number(filename):
match = re.search(r'checkpoint-(\d+)', filename)
if match:
return int(match.group(1))
match = re.search(r'/checkpoint-(\d+)/', filename)
if match:
return int(match.group(1))
return None
def get_most_recent_output_folder(base_dir):
folders = [f for f in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, f))]
if not folders:
return None
return max(folders, key=lambda f: os.path.getctime(os.path.join(base_dir, f)))
def process_safetensors(safetensor_dir, workflow):
print(f"Scanning directory: {safetensor_dir}")
last_dir = os.path.basename(os.path.normpath(safetensor_dir))
all_items = os.listdir(safetensor_dir)
safetensor_items = [f for f in all_items if f.endswith('.safetensors')]
safetensor_items.sort(key=lambda x: int(x.split('-')[1].split('.')[0]))
print(f"Found items: {safetensor_items}")
for item in safetensor_items:
unet_name = f"{last_dir}/{item}"
print(f"Processing: {unet_name}")
unet_loader_node = workflow["273"]
unet_loader_node["inputs"]["unet_name"] = unet_name
checkpoint_num = item.split('-')[1].split('.')[0]
save_image = workflow["275"]
filename_prefix = f"checkpoint-{checkpoint_num}"
save_image["inputs"]["output_path"] = relative_output_path
save_image["inputs"]["filename_prefix"] = filename_prefix
success = queue_prompt(workflow)
if not success:
print(f"Failed to queue prompt for checkpoint {checkpoint_num}")
else:
print(f"Successfully queued prompt for checkpoint {checkpoint_num}")
if not safetensor_items:
print("No .safetensors files found in the directory.")
return len(safetensor_items)
def create_image_strip(safetensor_dir, image_folder, output_filename):
safetensor_files = [f for f in os.listdir(safetensor_dir) if f.endswith('.safetensors')]
safetensor_files.sort(key=get_checkpoint_number)
checkpoints = [get_checkpoint_number(f) for f in safetensor_files if get_checkpoint_number(f) is not None]
images = []
for checkpoint in checkpoints:
filename = f"checkpoint-{checkpoint}_0001.png"
filepath = os.path.join(image_folder, filename)
if os.path.exists(filepath):
try:
img = Image.open(filepath)
images.append(img)
except IOError as e:
print(f"Cannot open image: {filepath}")
print(f"Error: {e}")
if not images:
print("No valid images found.")
return
img_width, img_height = images[0].size
strip_width = img_width * len(images)
label_height = 50 # Space for labels
strip_height = img_height + label_height
strip_image = Image.new('RGB', (strip_width, strip_height), 'white')
draw = ImageDraw.Draw(strip_image)
font = ImageFont.truetype(font_ttf_path, 20)
for i, (img, checkpoint) in enumerate(zip(images, checkpoints)):
strip_image.paste(img, (i * img_width, label_height))
label = f"checkpoint-{checkpoint}"
label_width = draw.textlength(label, font=font)
label_x = i * img_width + (img_width - label_width) // 2
draw.text((label_x, 10), label, fill="black", font=font)
strip_image.save(output_filename)
print(f"Image strip saved to: {output_filename}")
def queue_prompt(workflow):
p = {"prompt": workflow}
data = json.dumps(p).encode('utf-8')
req = request.Request(api_endpoint, data=data, headers={'Content-Type': 'application/json'})
try:
with request.urlopen(req) as response:
print(f"API request successful. Status code: {response.getcode()}")
return True
except urllib.error.URLError as e:
if hasattr(e, 'reason'):
print(f"Failed to reach the server. Reason: {e.reason}")
elif hasattr(e, 'code'):
print(f"The server couldn't fulfill the request. Error code: {e.code}")
print(f"API endpoint: {api_endpoint}")
except Exception as e:
print(f"An error occurred: {str(e)}")
return False
def wait_for_directory_creation(directory, timeout):
print(f"Waiting for directory {directory} to be created...")
start_time = time.time()
while time.time() - start_time < timeout:
if os.path.exists(directory):
print(f"Directory {directory} found.")
return True
time.sleep(5) # Check every 5 seconds
print(f"Timeout waiting for directory {directory} to be created.")
return False
def wait_for_images(image_folder, expected_count, timeout):
print("Waiting for images to be generated...")
start_time = time.time()
while time.time() - start_time < timeout:
if os.path.exists(image_folder):
image_files = [f for f in os.listdir(image_folder) if f.endswith('.png')]
if len(image_files) >= expected_count:
print(f"Found all {expected_count} images.")
return True
time.sleep(5) # Check every 5 seconds
print("Timeout waiting for images to be generated.")
return False
if __name__ == "__main__":
safetensor_dir = finetune_dir
print(f"Finetune directory: {safetensor_dir}")
# Generate images
expected_image_count = process_safetensors(safetensor_dir, workflow)
absolute_output_path = os.path.join(comfyui_output_dir, current_datetime)
print(f"Absolute output path: {absolute_output_path}")
# Create the image strip
if wait_for_directory_creation(absolute_output_path, directory_creation_timeout):
print(f"Expected image count: {expected_image_count}")
if wait_for_images(absolute_output_path, expected_image_count, image_generation_timeout):
output_strip_filename = os.path.join(absolute_output_path, "output_image_strip.png")
create_image_strip(safetensor_dir, absolute_output_path, output_strip_filename)
else:
print("Failed to generate all images in time.")
else:
print("Output directory was not created.")ãµã³ãã«ã® .env ãã¡ã€ã«
API_WORKFLOW_DIR=/weka2/home-yeo/workflows
COMFYUI_OUTPUT_DIR = /weka2/home-yeo/ComfyUI/output/
FINETUNE_DIR=/admin/home-yeo/workspace/ComfyUI/models/unet/simpletuner_09
API_WORKFLOW_FILE=SD3_SINGLE_BASIC_API_02.json
API_ENDPOINT=127.0.0.1:8188
FONT_TTF_PATH=/weka2/home-yeo/fonts/arial.ttf
BOLD_FONT_TTF_PATH=/weka2/home-yeo/fonts/arialbd.ttfçµå±ãç§ã¯çµäºééã®ã¹ããã`20,800`ã§ãã§ãã¯ãã€ã³ããéžã³ãŸããã
ã®ã£ã©ãªãŒ
æåŸã«ããã€ãäŸãæããŠãããŸãã





LoRA ã®ãã¬ãŒãã³ã°
ïŒãã軜éãªïŒLoRAã®ãã¬ãŒãã³ã°ã詊ãããå Žåãããã¯ç§ã䜿ã£ãconfigã§ããŸããŸãã®çµæãåŸãããŸãããå®å
šãªåŸ®èª¿æŽã®æ¹ãå質çã«ã¯è¯ãããã§ããLoRAã® rank ãš alpha 㯠export TRAINER_EXTRA_ARGS å
ã§èšå®ã§ããŸãã
â
蚳泚ïŒã«ã¹ã¿ã SD3 LoRA `config.env`ããåç
§ãã ãã
è¿æ¥çºè¡šäºå®ã®ã¢ãã«ã®ãã©èŠã
詳现ã¯ãŸã ãäŒãã§ããŸããããè¿æ¥çºè¡šäºå®ã®ã¢ãã«ã®åºåã®ã¹ããŒã¯ããŒã¯ïŒãã©èŠãïŒã§ã
a close up three fourth perspective portrait view of a young woman with dark hair and dark blue eyes, looking upwards and to the left, head tilted slightly downwards and to the left, exposed forehead, wearing a nun habit with white lining, wearing a white collared shirt, barely visible ear, cropped, a dark brown background
é»é«ªã§æ¿ãéã®ç³ãããè¥ã女æ§ã®ã¯ããŒãºã¢ããäžåéèŠå³ãäžãèŠãŠå·Šãåããé ã¯ããäžåãã§å·Šã«åŸããŠãããé¡ã¯é²åºããŠããŸããçœãè£å°ã®ã€ããä¿®éæã«èº«ãå ã¿ãçœãè¥ä»ãã·ã£ããçãŠãããè³ã¯ãããããŠèŠããŠããŸãã

a front wide view of a small cyberpunk city with futuristic skyscrapers with gold rooftops situated on the side of a cliff overlooking an ocean, day time view with green tones, some boats floating in the foreground on top of reflective orange water, large mechanical robot structure reaching high above the clouds in the far background, atmospheric perspective, teal sky
æµ·ãèŠäžããåŽã®äžè ¹ã«å»ºã€ãéè²ã®å±æ ¹ãæã€è¿æªæ¥çãªé«å±€ãã«ã建ã¡äžŠã¶å°ããªãµã€ããŒãã³ã¯éœåžã®æ£é¢ããã®ã¯ã€ããã¥ãŒãç·ãåºèª¿ãšããæ¥äžã®çºããåå°ãããªã¬ã³ãžè²ã®æ°Žã®äžã«æµ®ãã¶æåã®ããã€ãã®ããŒããé æ¯ã®é²ã®äžãŸã§å±ã倧ããªæ©æ¢°ä»æãã®ããããæ§é ã倧æ°é è¿æ³ããã£ãŒã«è²ã®ç©º

a medium view of a city square alongside a river, two large red boats in the foreground with cargo on them, two people in a smaller boat in the bottom right cruising along, reflective dark yellow water in the river, a congregation of people walking along the street parallel to the river in the midground, a dark white palazzo building with dark white tower on the right with red tones, golden hour, red and yellow flag in the top left foreground, light blue flag with yellow accents in the right midground, aerial perspective
å·æ²¿ãã®åºå Žã®äžæ¯ã忝ã«è·ãèŒãã2é»ã®èµ€ã倧åããŒããå³äžã®å°ããªããŒãã«ä¹ã£ã2人ã®ã¯ã«ãŒãžã³ã°ãåå°ããæ¿ãé»è²ã®å·ã®æ°Žãäžæ¯ã«å·ãšå¹³è¡ããéããæ©ã人ã ã峿ã«èµ€ãåºèª¿ãšããæ¿ãçœã®å¡ãæã€æ¿ãçœã®ãã©ããã©ã®å»ºç©ããŽãŒã«ãã³ã¿ã€ã ãå·Šäžã®åæ¯ã«èµ€ãšé»è²ã®æãå³ã®äžæ¯ã«é»è²ã®ã¢ã¯ã»ã³ãã®ããæ°Žè²ã®æã空ããã®èŠç¹ã

è¿æ¥å ¬éäºå®ã®ã¢ãã«ã®ã¹ããŒã¯ããŒã¯ ãŸã ããŸãå€ãã®è©³çްã¯ãäŒãã§ããŸããããè¿æ¥å ¬éäºå®ã®ã¢ãã«ã®1人ããã®åºåã®ã¹ããŒã¯ããŒã¯ã§ãïŒé»é«ªã§ããŒã¯ãã«ãŒã®ç®ãããè¥ã女æ§ã®ã¯ããŒãºã¢ãã3/4é è¿æ³ã®ããŒãã¬ãŒããã¥ãŒãäžåãã§å·Šãåããé ã¯ããäžåãã§å·Šã«åŸããŠãããé¡ãé²åºããŠããŸããçœãè£å°ã®ä»ããä¿®é女ã®ç¿æ £ã身ã«çããçœãè¥ä»ãã·ã£ããçãŠããããããããŠè³ãèŠããŠããŸããããªãã³ã°æžã¿ãããŒã¯ãã©ãŠã³ã®èæ¯ã
ããŸã: ãªãŒããã£ãã·ã§ã³
ããŒã¿ã»ãããæºåã§ããã°ãäžèšã®æé ã§ã¢ãã«ã®ãã¡ã€ã³ãã¥ãŒãã³ã°ãå§ããããšãã§ããŸãããçµæã¯ããŒã¿ã»ããã«ãã£ãŠå€§ããç°ãªããŸããæ£ãããã£ãã·ã§ã³ãä»ããããç»åãèŠã€ããã®ã¯ãããªãé£ããããšã§ããæäœæ¥ã§ãã£ãã·ã§ã³ãã€ããã®ã¯ãããªãé¢åã§ããã¹ãèµ·ãããããäœæ¥ã§ããã§ããããäœããã®åœ¢ã§èªååããããœãªã¥ãŒã·ã§ã³ã詊ããŠã¿ãã®ã¯çã«ããªã£ãŠããŸãã
å®éãSD3èªèº«ãããã£ãã·ã§ã³ãçæããããã®äºååŠç¿ã§CogVLM(ããžã§ã³èšèªã¢ãã«)ã䜿çšããŸããããããããã®ã¢ãã«ãæ£ãã䜿çšããã«ã¯ãå€ãã®VRAMãå€§èŠæš¡ãªããŒã¿ã»ãããè€éãªã¯ãŒã¯ãããŒãå¿ èŠã§ããããã§ããã®ããã°ãã¹ãã§ã¯ãå°èŠæš¡ãªã³ãã¥ããã£ã®åŸ®èª¿æŽã«ããå°ãé©ãããã®ã«çŠç¹ãåœãŠãŸãïŒJoyCaptionã§ããJoyCaptionã¯ãæ°ããã¢ãã«ã®åŸ®èª¿æŽãè¡ãããã«ãã³ãã¥ããã£ã®ã¡ã³ããŒã«ãã£ãŠéçºãããç©æ¥µçãªãã£ãã·ã§ã³ã¢ãã«ã§ãã
ã¢ã¯ã»ã·ãã«ãªãã£ãã·ã§ã³ã»ã€ã³ã¿ãŒãã§ãŒã¹ã§åããã«ã¯ãTagGUIã䜿ããŸãããã®ããŒãžã®æç€ºã«åŸãããšã§ãGUIãã€ã³ã¹ããŒã«ããç»åãåŠçããæºåãã§ããŸãããã ããããã€ãéèŠãªãã€ã³ãããããŸãïŒãªãŒããã£ãã·ã§ã³ã¯ïŒãŸã ïŒå®å šã§ã¯ãªãã®ã§ãèªåçãªè§£æ±ºçã«ãã£ãŠãç»åã®äžéšãé©åã«ãã£ãã·ã§ã³ä»ããããŠããªãããšã«æ°ã¥ããããããŸããããã®ãããªãã¹ãåé¡ã¯ãå€ãã®ããŒã¿ãæã£ãŠããã°å€å°ã¯ããæ¶ãããšãã§ããŸãããã»ãšãã©ã®è¶£å³äººã«ãšã£ãŠã¯é£ããããšã§ãããããã®å Žåãäœãééã£ãŠããã®ããçè§£ãããããå埩ããããã«ããã£ãã·ã§ã³ãæåã§æ€æ»ããå¿ èŠããããããããªããèŠããŠãããŠã»ããã®ã¯ãäžæ£ç¢ºãªãã£ãã·ã§ã³ã§ãã¬ãŒãã³ã°ããŠããè¿ éãªéµå®ã¯æåŸ ã§ããªãã®ã§ããã®èšå®ã«è²»ããæéã¯éåžžã«è²Žéã ãšããããšã§ããããã§ã®å埩æéãççž®ããããã«ãå®å šãªåŸ®èª¿æŽã®åã«ãããŒã¿ã䜿ã£ãŠããã€ãã®LoRAãèšç·Žãããšããã§ããããé 匵ã£ãŠãã ããïŒ
kasukanra@GitHubãªããžããª
ããããã®ããŒã«ã眮ãããŠããŸãã
åäœããæäŸããã ããYeo WangãããããããšãããããŸããïŒ