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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
from collections import OrderedDict
|
| 4 |
+
from typing import List
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from diffusers import T2IAdapter
|
| 9 |
+
from torch.utils.data import DataLoader
|
| 10 |
+
from diffusers import StableDiffusionXLAdapterPipeline, StableDiffusionAdapterPipeline
|
| 11 |
+
from tqdm import tqdm
|
| 12 |
+
|
| 13 |
+
from toolkit.config_modules import ModelConfig, GenerateImageConfig, preprocess_dataset_raw_config, DatasetConfig
|
| 14 |
+
from toolkit.data_transfer_object.data_loader import FileItemDTO, DataLoaderBatchDTO
|
| 15 |
+
from toolkit.sampler import get_sampler
|
| 16 |
+
from toolkit.stable_diffusion_model import StableDiffusion
|
| 17 |
+
import gc
|
| 18 |
+
import torch
|
| 19 |
+
from jobs.process import BaseExtensionProcess
|
| 20 |
+
from toolkit.data_loader import get_dataloader_from_datasets
|
| 21 |
+
from toolkit.train_tools import get_torch_dtype
|
| 22 |
+
from controlnet_aux.midas import MidasDetector
|
| 23 |
+
from diffusers.utils import load_image
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def flush():
|
| 27 |
+
torch.cuda.empty_cache()
|
| 28 |
+
gc.collect()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class GenerateConfig:
|
| 32 |
+
|
| 33 |
+
def __init__(self, **kwargs):
|
| 34 |
+
self.prompts: List[str]
|
| 35 |
+
self.sampler = kwargs.get('sampler', 'ddpm')
|
| 36 |
+
self.neg = kwargs.get('neg', '')
|
| 37 |
+
self.seed = kwargs.get('seed', -1)
|
| 38 |
+
self.walk_seed = kwargs.get('walk_seed', False)
|
| 39 |
+
self.t2i_adapter_path = kwargs.get('t2i_adapter_path', None)
|
| 40 |
+
self.guidance_scale = kwargs.get('guidance_scale', 7)
|
| 41 |
+
self.sample_steps = kwargs.get('sample_steps', 20)
|
| 42 |
+
self.prompt_2 = kwargs.get('prompt_2', None)
|
| 43 |
+
self.neg_2 = kwargs.get('neg_2', None)
|
| 44 |
+
self.prompts = kwargs.get('prompts', None)
|
| 45 |
+
self.guidance_rescale = kwargs.get('guidance_rescale', 0.0)
|
| 46 |
+
self.ext = kwargs.get('ext', 'png')
|
| 47 |
+
self.adapter_conditioning_scale = kwargs.get('adapter_conditioning_scale', 1.0)
|
| 48 |
+
if kwargs.get('shuffle', False):
|
| 49 |
+
# shuffle the prompts
|
| 50 |
+
random.shuffle(self.prompts)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class ReferenceGenerator(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.device = self.get_conf('device', 'cuda')
|
| 59 |
+
self.model_config = ModelConfig(**self.get_conf('model', required=True))
|
| 60 |
+
self.generate_config = GenerateConfig(**self.get_conf('generate', required=True))
|
| 61 |
+
self.is_latents_cached = True
|
| 62 |
+
raw_datasets = self.get_conf('datasets', None)
|
| 63 |
+
if raw_datasets is not None and len(raw_datasets) > 0:
|
| 64 |
+
raw_datasets = preprocess_dataset_raw_config(raw_datasets)
|
| 65 |
+
self.datasets = None
|
| 66 |
+
self.datasets_reg = None
|
| 67 |
+
self.dtype = self.get_conf('dtype', 'float16')
|
| 68 |
+
self.torch_dtype = get_torch_dtype(self.dtype)
|
| 69 |
+
self.params = []
|
| 70 |
+
if raw_datasets is not None and len(raw_datasets) > 0:
|
| 71 |
+
for raw_dataset in raw_datasets:
|
| 72 |
+
dataset = DatasetConfig(**raw_dataset)
|
| 73 |
+
is_caching = dataset.cache_latents or dataset.cache_latents_to_disk
|
| 74 |
+
if not is_caching:
|
| 75 |
+
self.is_latents_cached = False
|
| 76 |
+
if dataset.is_reg:
|
| 77 |
+
if self.datasets_reg is None:
|
| 78 |
+
self.datasets_reg = []
|
| 79 |
+
self.datasets_reg.append(dataset)
|
| 80 |
+
else:
|
| 81 |
+
if self.datasets is None:
|
| 82 |
+
self.datasets = []
|
| 83 |
+
self.datasets.append(dataset)
|
| 84 |
+
|
| 85 |
+
self.progress_bar = None
|
| 86 |
+
self.sd = StableDiffusion(
|
| 87 |
+
device=self.device,
|
| 88 |
+
model_config=self.model_config,
|
| 89 |
+
dtype=self.dtype,
|
| 90 |
+
)
|
| 91 |
+
print(f"Using device {self.device}")
|
| 92 |
+
self.data_loader: DataLoader = None
|
| 93 |
+
self.adapter: T2IAdapter = None
|
| 94 |
+
|
| 95 |
+
def run(self):
|
| 96 |
+
super().run()
|
| 97 |
+
print("Loading model...")
|
| 98 |
+
self.sd.load_model()
|
| 99 |
+
device = torch.device(self.device)
|
| 100 |
+
|
| 101 |
+
if self.generate_config.t2i_adapter_path is not None:
|
| 102 |
+
self.adapter = T2IAdapter.from_pretrained(
|
| 103 |
+
self.generate_config.t2i_adapter_path,
|
| 104 |
+
torch_dtype=self.torch_dtype,
|
| 105 |
+
varient="fp16"
|
| 106 |
+
).to(device)
|
| 107 |
+
|
| 108 |
+
midas_depth = MidasDetector.from_pretrained(
|
| 109 |
+
"valhalla/t2iadapter-aux-models", filename="dpt_large_384.pt", model_type="dpt_large"
|
| 110 |
+
).to(device)
|
| 111 |
+
|
| 112 |
+
if self.model_config.is_xl:
|
| 113 |
+
pipe = StableDiffusionXLAdapterPipeline(
|
| 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 |
+
adapter=self.adapter,
|
| 122 |
+
).to(device, dtype=self.torch_dtype)
|
| 123 |
+
else:
|
| 124 |
+
pipe = StableDiffusionAdapterPipeline(
|
| 125 |
+
vae=self.sd.vae,
|
| 126 |
+
unet=self.sd.unet,
|
| 127 |
+
text_encoder=self.sd.text_encoder,
|
| 128 |
+
tokenizer=self.sd.tokenizer,
|
| 129 |
+
scheduler=get_sampler(self.generate_config.sampler),
|
| 130 |
+
safety_checker=None,
|
| 131 |
+
feature_extractor=None,
|
| 132 |
+
requires_safety_checker=False,
|
| 133 |
+
adapter=self.adapter,
|
| 134 |
+
).to(device, dtype=self.torch_dtype)
|
| 135 |
+
pipe.set_progress_bar_config(disable=True)
|
| 136 |
+
|
| 137 |
+
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
|
| 138 |
+
# midas_depth = torch.compile(midas_depth, mode="reduce-overhead", fullgraph=True)
|
| 139 |
+
|
| 140 |
+
self.data_loader = get_dataloader_from_datasets(self.datasets, 1, self.sd)
|
| 141 |
+
|
| 142 |
+
num_batches = len(self.data_loader)
|
| 143 |
+
pbar = tqdm(total=num_batches, desc="Generating images")
|
| 144 |
+
seed = self.generate_config.seed
|
| 145 |
+
# load images from datasets, use tqdm
|
| 146 |
+
for i, batch in enumerate(self.data_loader):
|
| 147 |
+
batch: DataLoaderBatchDTO = batch
|
| 148 |
+
|
| 149 |
+
file_item: FileItemDTO = batch.file_items[0]
|
| 150 |
+
img_path = file_item.path
|
| 151 |
+
img_filename = os.path.basename(img_path)
|
| 152 |
+
img_filename_no_ext = os.path.splitext(img_filename)[0]
|
| 153 |
+
output_path = os.path.join(self.output_folder, img_filename)
|
| 154 |
+
output_caption_path = os.path.join(self.output_folder, img_filename_no_ext + '.txt')
|
| 155 |
+
output_depth_path = os.path.join(self.output_folder, img_filename_no_ext + '.depth.png')
|
| 156 |
+
|
| 157 |
+
caption = batch.get_caption_list()[0]
|
| 158 |
+
|
| 159 |
+
img: torch.Tensor = batch.tensor.clone()
|
| 160 |
+
# image comes in -1 to 1. convert to a PIL RGB image
|
| 161 |
+
img = (img + 1) / 2
|
| 162 |
+
img = img.clamp(0, 1)
|
| 163 |
+
img = img[0].permute(1, 2, 0).cpu().numpy()
|
| 164 |
+
img = (img * 255).astype(np.uint8)
|
| 165 |
+
image = Image.fromarray(img)
|
| 166 |
+
|
| 167 |
+
width, height = image.size
|
| 168 |
+
min_res = min(width, height)
|
| 169 |
+
|
| 170 |
+
if self.generate_config.walk_seed:
|
| 171 |
+
seed = seed + 1
|
| 172 |
+
|
| 173 |
+
if self.generate_config.seed == -1:
|
| 174 |
+
# random
|
| 175 |
+
seed = random.randint(0, 1000000)
|
| 176 |
+
|
| 177 |
+
torch.manual_seed(seed)
|
| 178 |
+
torch.cuda.manual_seed(seed)
|
| 179 |
+
|
| 180 |
+
# generate depth map
|
| 181 |
+
image = midas_depth(
|
| 182 |
+
image,
|
| 183 |
+
detect_resolution=min_res, # do 512 ?
|
| 184 |
+
image_resolution=min_res
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
# image.save(output_depth_path)
|
| 188 |
+
|
| 189 |
+
gen_images = pipe(
|
| 190 |
+
prompt=caption,
|
| 191 |
+
negative_prompt=self.generate_config.neg,
|
| 192 |
+
image=image,
|
| 193 |
+
num_inference_steps=self.generate_config.sample_steps,
|
| 194 |
+
adapter_conditioning_scale=self.generate_config.adapter_conditioning_scale,
|
| 195 |
+
guidance_scale=self.generate_config.guidance_scale,
|
| 196 |
+
).images[0]
|
| 197 |
+
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
| 198 |
+
gen_images.save(output_path)
|
| 199 |
+
|
| 200 |
+
# save caption
|
| 201 |
+
with open(output_caption_path, 'w') as f:
|
| 202 |
+
f.write(caption)
|
| 203 |
+
|
| 204 |
+
pbar.update(1)
|
| 205 |
+
batch.cleanup()
|
| 206 |
+
|
| 207 |
+
pbar.close()
|
| 208 |
+
print("Done generating images")
|
| 209 |
+
# cleanup
|
| 210 |
+
del self.sd
|
| 211 |
+
gc.collect()
|
| 212 |
+
torch.cuda.empty_cache()
|