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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
import os
|
| 3 |
+
import random
|
| 4 |
+
from collections import OrderedDict
|
| 5 |
+
from typing import List
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from diffusers import T2IAdapter
|
| 10 |
+
from diffusers.utils.torch_utils import randn_tensor
|
| 11 |
+
from torch.utils.data import DataLoader
|
| 12 |
+
from diffusers import StableDiffusionXLImg2ImgPipeline, PixArtSigmaPipeline
|
| 13 |
+
from tqdm import tqdm
|
| 14 |
+
|
| 15 |
+
from toolkit.config_modules import ModelConfig, GenerateImageConfig, preprocess_dataset_raw_config, DatasetConfig
|
| 16 |
+
from toolkit.data_transfer_object.data_loader import FileItemDTO, DataLoaderBatchDTO
|
| 17 |
+
from toolkit.sampler import get_sampler
|
| 18 |
+
from toolkit.stable_diffusion_model import StableDiffusion
|
| 19 |
+
import gc
|
| 20 |
+
import torch
|
| 21 |
+
from jobs.process import BaseExtensionProcess
|
| 22 |
+
from toolkit.data_loader import get_dataloader_from_datasets
|
| 23 |
+
from toolkit.train_tools import get_torch_dtype
|
| 24 |
+
from controlnet_aux.midas import MidasDetector
|
| 25 |
+
from diffusers.utils import load_image
|
| 26 |
+
from torchvision.transforms import ToTensor
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def flush():
|
| 30 |
+
torch.cuda.empty_cache()
|
| 31 |
+
gc.collect()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
class GenerateConfig:
|
| 38 |
+
|
| 39 |
+
def __init__(self, **kwargs):
|
| 40 |
+
self.prompts: List[str]
|
| 41 |
+
self.sampler = kwargs.get('sampler', 'ddpm')
|
| 42 |
+
self.neg = kwargs.get('neg', '')
|
| 43 |
+
self.seed = kwargs.get('seed', -1)
|
| 44 |
+
self.walk_seed = kwargs.get('walk_seed', False)
|
| 45 |
+
self.guidance_scale = kwargs.get('guidance_scale', 7)
|
| 46 |
+
self.sample_steps = kwargs.get('sample_steps', 20)
|
| 47 |
+
self.guidance_rescale = kwargs.get('guidance_rescale', 0.0)
|
| 48 |
+
self.ext = kwargs.get('ext', 'png')
|
| 49 |
+
self.denoise_strength = kwargs.get('denoise_strength', 0.5)
|
| 50 |
+
self.trigger_word = kwargs.get('trigger_word', None)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class Img2ImgGenerator(BaseExtensionProcess):
|
| 54 |
+
|
| 55 |
+
def __init__(self, process_id: int, job, config: OrderedDict):
|
| 56 |
+
super().__init__(process_id, job, config)
|
| 57 |
+
self.output_folder = self.get_conf('output_folder', required=True)
|
| 58 |
+
self.copy_inputs_to = self.get_conf('copy_inputs_to', None)
|
| 59 |
+
self.device = self.get_conf('device', 'cuda')
|
| 60 |
+
self.model_config = ModelConfig(**self.get_conf('model', required=True))
|
| 61 |
+
self.generate_config = GenerateConfig(**self.get_conf('generate', required=True))
|
| 62 |
+
self.is_latents_cached = True
|
| 63 |
+
raw_datasets = self.get_conf('datasets', None)
|
| 64 |
+
if raw_datasets is not None and len(raw_datasets) > 0:
|
| 65 |
+
raw_datasets = preprocess_dataset_raw_config(raw_datasets)
|
| 66 |
+
self.datasets = None
|
| 67 |
+
self.datasets_reg = None
|
| 68 |
+
self.dtype = self.get_conf('dtype', 'float16')
|
| 69 |
+
self.torch_dtype = get_torch_dtype(self.dtype)
|
| 70 |
+
self.params = []
|
| 71 |
+
if raw_datasets is not None and len(raw_datasets) > 0:
|
| 72 |
+
for raw_dataset in raw_datasets:
|
| 73 |
+
dataset = DatasetConfig(**raw_dataset)
|
| 74 |
+
is_caching = dataset.cache_latents or dataset.cache_latents_to_disk
|
| 75 |
+
if not is_caching:
|
| 76 |
+
self.is_latents_cached = False
|
| 77 |
+
if dataset.is_reg:
|
| 78 |
+
if self.datasets_reg is None:
|
| 79 |
+
self.datasets_reg = []
|
| 80 |
+
self.datasets_reg.append(dataset)
|
| 81 |
+
else:
|
| 82 |
+
if self.datasets is None:
|
| 83 |
+
self.datasets = []
|
| 84 |
+
self.datasets.append(dataset)
|
| 85 |
+
|
| 86 |
+
self.progress_bar = None
|
| 87 |
+
self.sd = StableDiffusion(
|
| 88 |
+
device=self.device,
|
| 89 |
+
model_config=self.model_config,
|
| 90 |
+
dtype=self.dtype,
|
| 91 |
+
)
|
| 92 |
+
print(f"Using device {self.device}")
|
| 93 |
+
self.data_loader: DataLoader = None
|
| 94 |
+
self.adapter: T2IAdapter = None
|
| 95 |
+
|
| 96 |
+
def to_pil(self, img):
|
| 97 |
+
# image comes in -1 to 1. convert to a PIL RGB image
|
| 98 |
+
img = (img + 1) / 2
|
| 99 |
+
img = img.clamp(0, 1)
|
| 100 |
+
img = img[0].permute(1, 2, 0).cpu().numpy()
|
| 101 |
+
img = (img * 255).astype(np.uint8)
|
| 102 |
+
image = Image.fromarray(img)
|
| 103 |
+
return image
|
| 104 |
+
|
| 105 |
+
def run(self):
|
| 106 |
+
with torch.no_grad():
|
| 107 |
+
super().run()
|
| 108 |
+
print("Loading model...")
|
| 109 |
+
self.sd.load_model()
|
| 110 |
+
device = torch.device(self.device)
|
| 111 |
+
|
| 112 |
+
if self.model_config.is_xl:
|
| 113 |
+
pipe = StableDiffusionXLImg2ImgPipeline(
|
| 114 |
+
vae=self.sd.vae,
|
| 115 |
+
unet=self.sd.unet,
|
| 116 |
+
text_encoder=self.sd.text_encoder[0],
|
| 117 |
+
text_encoder_2=self.sd.text_encoder[1],
|
| 118 |
+
tokenizer=self.sd.tokenizer[0],
|
| 119 |
+
tokenizer_2=self.sd.tokenizer[1],
|
| 120 |
+
scheduler=get_sampler(self.generate_config.sampler),
|
| 121 |
+
).to(device, dtype=self.torch_dtype)
|
| 122 |
+
elif self.model_config.is_pixart:
|
| 123 |
+
pipe = self.sd.pipeline.to(device, dtype=self.torch_dtype)
|
| 124 |
+
else:
|
| 125 |
+
raise NotImplementedError("Only XL models are supported")
|
| 126 |
+
pipe.set_progress_bar_config(disable=True)
|
| 127 |
+
|
| 128 |
+
# pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
| 129 |
+
# midas_depth = torch.compile(midas_depth, mode="reduce-overhead", fullgraph=True)
|
| 130 |
+
|
| 131 |
+
self.data_loader = get_dataloader_from_datasets(self.datasets, 1, self.sd)
|
| 132 |
+
|
| 133 |
+
num_batches = len(self.data_loader)
|
| 134 |
+
pbar = tqdm(total=num_batches, desc="Generating images")
|
| 135 |
+
seed = self.generate_config.seed
|
| 136 |
+
# load images from datasets, use tqdm
|
| 137 |
+
for i, batch in enumerate(self.data_loader):
|
| 138 |
+
batch: DataLoaderBatchDTO = batch
|
| 139 |
+
|
| 140 |
+
gen_seed = seed if seed > 0 else random.randint(0, 2 ** 32 - 1)
|
| 141 |
+
generator = torch.manual_seed(gen_seed)
|
| 142 |
+
|
| 143 |
+
file_item: FileItemDTO = batch.file_items[0]
|
| 144 |
+
img_path = file_item.path
|
| 145 |
+
img_filename = os.path.basename(img_path)
|
| 146 |
+
img_filename_no_ext = os.path.splitext(img_filename)[0]
|
| 147 |
+
img_filename = img_filename_no_ext + '.' + self.generate_config.ext
|
| 148 |
+
output_path = os.path.join(self.output_folder, img_filename)
|
| 149 |
+
output_caption_path = os.path.join(self.output_folder, img_filename_no_ext + '.txt')
|
| 150 |
+
|
| 151 |
+
if self.copy_inputs_to is not None:
|
| 152 |
+
output_inputs_path = os.path.join(self.copy_inputs_to, img_filename)
|
| 153 |
+
output_inputs_caption_path = os.path.join(self.copy_inputs_to, img_filename_no_ext + '.txt')
|
| 154 |
+
else:
|
| 155 |
+
output_inputs_path = None
|
| 156 |
+
output_inputs_caption_path = None
|
| 157 |
+
|
| 158 |
+
caption = batch.get_caption_list()[0]
|
| 159 |
+
if self.generate_config.trigger_word is not None:
|
| 160 |
+
caption = caption.replace('[trigger]', self.generate_config.trigger_word)
|
| 161 |
+
|
| 162 |
+
img: torch.Tensor = batch.tensor.clone()
|
| 163 |
+
image = self.to_pil(img)
|
| 164 |
+
|
| 165 |
+
# image.save(output_depth_path)
|
| 166 |
+
if self.model_config.is_pixart:
|
| 167 |
+
pipe: PixArtSigmaPipeline = pipe
|
| 168 |
+
|
| 169 |
+
# Encode the full image once
|
| 170 |
+
encoded_image = pipe.vae.encode(
|
| 171 |
+
pipe.image_processor.preprocess(image).to(device=pipe.device, dtype=pipe.dtype))
|
| 172 |
+
if hasattr(encoded_image, "latent_dist"):
|
| 173 |
+
latents = encoded_image.latent_dist.sample(generator)
|
| 174 |
+
elif hasattr(encoded_image, "latents"):
|
| 175 |
+
latents = encoded_image.latents
|
| 176 |
+
else:
|
| 177 |
+
raise AttributeError("Could not access latents of provided encoder_output")
|
| 178 |
+
latents = pipe.vae.config.scaling_factor * latents
|
| 179 |
+
|
| 180 |
+
# latents = self.sd.encode_images(img)
|
| 181 |
+
|
| 182 |
+
# self.sd.noise_scheduler.set_timesteps(self.generate_config.sample_steps)
|
| 183 |
+
# start_step = math.floor(self.generate_config.sample_steps * self.generate_config.denoise_strength)
|
| 184 |
+
# timestep = self.sd.noise_scheduler.timesteps[start_step].unsqueeze(0)
|
| 185 |
+
# timestep = timestep.to(device, dtype=torch.int32)
|
| 186 |
+
# latent = latent.to(device, dtype=self.torch_dtype)
|
| 187 |
+
# noise = torch.randn_like(latent, device=device, dtype=self.torch_dtype)
|
| 188 |
+
# latent = self.sd.add_noise(latent, noise, timestep)
|
| 189 |
+
# timesteps_to_use = self.sd.noise_scheduler.timesteps[start_step + 1:]
|
| 190 |
+
batch_size = 1
|
| 191 |
+
num_images_per_prompt = 1
|
| 192 |
+
|
| 193 |
+
shape = (batch_size, pipe.transformer.config.in_channels, image.height // pipe.vae_scale_factor,
|
| 194 |
+
image.width // pipe.vae_scale_factor)
|
| 195 |
+
noise = randn_tensor(shape, generator=generator, device=pipe.device, dtype=pipe.dtype)
|
| 196 |
+
|
| 197 |
+
# noise = torch.randn_like(latents, device=device, dtype=self.torch_dtype)
|
| 198 |
+
num_inference_steps = self.generate_config.sample_steps
|
| 199 |
+
strength = self.generate_config.denoise_strength
|
| 200 |
+
# Get timesteps
|
| 201 |
+
init_timestep = min(int(num_inference_steps * strength), num_inference_steps)
|
| 202 |
+
t_start = max(num_inference_steps - init_timestep, 0)
|
| 203 |
+
pipe.scheduler.set_timesteps(num_inference_steps, device="cpu")
|
| 204 |
+
timesteps = pipe.scheduler.timesteps[t_start:]
|
| 205 |
+
timestep = timesteps[:1].repeat(batch_size * num_images_per_prompt)
|
| 206 |
+
latents = pipe.scheduler.add_noise(latents, noise, timestep)
|
| 207 |
+
|
| 208 |
+
gen_images = pipe.__call__(
|
| 209 |
+
prompt=caption,
|
| 210 |
+
negative_prompt=self.generate_config.neg,
|
| 211 |
+
latents=latents,
|
| 212 |
+
timesteps=timesteps,
|
| 213 |
+
width=image.width,
|
| 214 |
+
height=image.height,
|
| 215 |
+
num_inference_steps=num_inference_steps,
|
| 216 |
+
num_images_per_prompt=num_images_per_prompt,
|
| 217 |
+
guidance_scale=self.generate_config.guidance_scale,
|
| 218 |
+
# strength=self.generate_config.denoise_strength,
|
| 219 |
+
use_resolution_binning=False,
|
| 220 |
+
output_type="np"
|
| 221 |
+
).images[0]
|
| 222 |
+
gen_images = (gen_images * 255).clip(0, 255).astype(np.uint8)
|
| 223 |
+
gen_images = Image.fromarray(gen_images)
|
| 224 |
+
else:
|
| 225 |
+
pipe: StableDiffusionXLImg2ImgPipeline = pipe
|
| 226 |
+
|
| 227 |
+
gen_images = pipe.__call__(
|
| 228 |
+
prompt=caption,
|
| 229 |
+
negative_prompt=self.generate_config.neg,
|
| 230 |
+
image=image,
|
| 231 |
+
num_inference_steps=self.generate_config.sample_steps,
|
| 232 |
+
guidance_scale=self.generate_config.guidance_scale,
|
| 233 |
+
strength=self.generate_config.denoise_strength,
|
| 234 |
+
).images[0]
|
| 235 |
+
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
| 236 |
+
gen_images.save(output_path)
|
| 237 |
+
|
| 238 |
+
# save caption
|
| 239 |
+
with open(output_caption_path, 'w') as f:
|
| 240 |
+
f.write(caption)
|
| 241 |
+
|
| 242 |
+
if output_inputs_path is not None:
|
| 243 |
+
os.makedirs(os.path.dirname(output_inputs_path), exist_ok=True)
|
| 244 |
+
image.save(output_inputs_path)
|
| 245 |
+
with open(output_inputs_caption_path, 'w') as f:
|
| 246 |
+
f.write(caption)
|
| 247 |
+
|
| 248 |
+
pbar.update(1)
|
| 249 |
+
batch.cleanup()
|
| 250 |
+
|
| 251 |
+
pbar.close()
|
| 252 |
+
print("Done generating images")
|
| 253 |
+
# cleanup
|
| 254 |
+
del self.sd
|
| 255 |
+
gc.collect()
|
| 256 |
+
torch.cuda.empty_cache()
|