-
Notifications
You must be signed in to change notification settings - Fork 1
/
img_gen.py
188 lines (160 loc) · 10.2 KB
/
img_gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import os
import numpy as np
import torch
from PIL import Image
from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline, StableDiffusionInpaintPipeline
from mask import get_crop_region, expand_crop_region
LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS)
def apply_overlay(base_image, paste_loc, overlay):
if overlay is not None and paste_loc is not None:
x, y, width, height = paste_loc
overlay = resize_image(1, overlay, width, height)
base_image.paste(overlay, (x, y))
return base_image
def resize_image(resize_mode, im, width, height):
def resize(im, w, h):
return im.resize((w, h), resample=LANCZOS)
if resize_mode == 0:
res = resize(im, width, height)
elif resize_mode == 1:
ratio = width / height
src_ratio = im.width / im.height
src_w = width if ratio > src_ratio else im.width * height // im.height
src_h = height if ratio <= src_ratio else im.height * width // im.width
resized = resize(im, src_w, src_h)
res = Image.new("RGB", (width, height))
res.paste(resized, box=(width // 2 - src_w // 2, height // 2 - src_h // 2))
else:
ratio = width / height
src_ratio = im.width / im.height
src_w = width if ratio < src_ratio else im.width * height // im.height
src_h = height if ratio >= src_ratio else im.height * width // im.width
resized = resize(im, src_w, src_h)
res = Image.new("RGB", (width, height))
res.paste(resized, box=(width // 2 - src_w // 2, height // 2 - src_h // 2))
if ratio < src_ratio:
fill_height = height // 2 - src_h // 2
res.paste(resized.resize((width, fill_height), box=(0, 0, width, 0)), box=(0, 0))
res.paste(resized.resize((width, fill_height), box=(0, resized.height, width, resized.height)),
box=(0, fill_height + src_h))
elif ratio > src_ratio:
fill_width = width // 2 - src_w // 2
res.paste(resized.resize((fill_width, height), box=(0, 0, 0, height)), box=(0, 0))
res.paste(resized.resize((fill_width, height), box=(resized.width, 0, resized.width, height)),
box=(fill_width + src_w, 0))
return res
def generate_images(args, images, masks):
hf_token = args['hf_token']
output_path = args['output_path']
height = args['height']
width = args['width']
model_paths = args['model']
prompts = args['prompt']
cfg_scale_list = args['cfg_scale']
denoising_strength_list = args['denoising_strength']
negative_prompts = args['negative_prompt']
seeds = args['seed']
inference_type = args['type']
inpaint_full_res = args['inpaint_full_res']
inpaint_full_res_padding = args['inpaint_full_res_padding']
steps_list = args['steps']
output_counter = 0
print('Generating images...')
if not inference_type == 'txt2img' and len(images) == 0:
raise ValueError('No images provided for img2img/inpainting')
for model_path in model_paths:
model = get_model(hf_token, model_path, inference_type).to("cuda")
for prompt in prompts:
for negative_prompt in negative_prompts:
for cfg_scale in cfg_scale_list:
for denoising_strength in denoising_strength_list:
for steps in steps_list:
for seed in seeds:
generator = torch.Generator("cuda").manual_seed(seed)
if inference_type == 'txt2img':
folder = f'{output_path}/mo_{model_path.split("/")[-1]}/pr_{prompt}/' \
f'ne_{negative_prompt}/cf_{cfg_scale}/st_{steps}/se_{seed}'
if not os.path.exists(folder):
os.makedirs(folder)
try:
print('Generating image with params: ' + str(prompt) + ' ' + str(negative_prompt)
+ ' ' + str(cfg_scale) + ' ' + str(denoising_strength))
# Call txt2img
output = model(prompt=prompt, guidance_scale=cfg_scale, generator=generator,
negative_prompt=negative_prompt, height=height, width=width,
num_inference_steps=steps).images[0]
# Generate image name as increment of previous image
output.save(folder + '/output_' + str(output_counter) + '.png')
output_counter += 1
except Exception as e:
print('Error generating image with params: ' + str(prompt) + ' ' + str(
negative_prompt)
+ ' ' + str(cfg_scale) + ' ' + str(denoising_strength))
print(e)
else:
folder = f'{output_path}/mo_{model_path.split("/")[-1]}/pr_{prompt}/' \
f'ne_{negative_prompt}/cf_{cfg_scale}/de_{denoising_strength}/st_{steps}/' \
f'se_{seed}'
if not os.path.exists(folder):
os.makedirs(folder)
for idx, image in enumerate(images):
print(image)
try:
pil_image = Image.open(image)
pil_mask = Image.open(masks[idx])
image_name = 'im_' + image.split('/')[-1]
if inference_type == 'inpaint':
print('Inpainting image with params: ' + str(prompt) + ' ' + str(
negative_prompt)
+ ' ' + str(cfg_scale) + ' ' + str(denoising_strength))
if inpaint_full_res:
paste_to, pil_image, pil_mask = full_res_transform(
inpaint_full_res_padding,
pil_image,
pil_mask)
output = model(prompt=prompt, negative_prompt=negative_prompt, image=pil_image.convert('RGB'),
mask_image=pil_mask.convert('RGB'),
guidance_scale=cfg_scale, generator=generator,
height=height, width=width, num_inference_steps=steps).images[0]
if inpaint_full_res:
output = apply_overlay(Image.open(image), paste_to, output)
output.save(folder + '/' + str(image_name))
elif inference_type == 'img2img':
print('Generating image with params: ' + str(prompt) + ' ' + str(
negative_prompt) + ' ' + str(cfg_scale) + ' ' + str(denoising_strength))
output = model(prompt=prompt, negative_prompt=negative_prompt, image=pil_image, guidance_scale=cfg_scale,
generator=generator, strength=denoising_strength,
height=height, width=width,num_inference_steps=steps).images[0]
output.save(folder + '/' + str(image_name))
except Exception as e:
print('Error generating image with params: ' + str(prompt) + ' ' + str(
negative_prompt) + ' ' + str(cfg_scale) + ' ' + str(denoising_strength))
print(e)
def full_res_transform(inpaint_full_res_padding, pil_image, pil_mask):
monochannel_mask = pil_mask.convert('L')
crop_region = get_crop_region(np.array(monochannel_mask),
inpaint_full_res_padding)
crop_region = expand_crop_region(crop_region, pil_image.width,
pil_image.height, monochannel_mask.width,
monochannel_mask.height)
x1, y1, x2, y2 = crop_region
monochannel_mask = monochannel_mask.crop(crop_region)
cropped_image = pil_image.crop(crop_region)
pil_mask = resize_image(2, monochannel_mask, pil_image.width,
pil_image.height)
pil_image = resize_image(2, cropped_image, pil_image.width,
pil_image.height)
paste_to = (x1, y1, x2 - x1, y2 - y1)
return paste_to, pil_image, pil_mask
def get_model(hf_token, model_path, inference_type):
if inference_type == 'txt2img':
return StableDiffusionPipeline.from_pretrained(model_path, use_auth_token=hf_token,
torch_dtype=torch.float16)
elif inference_type == 'img2img':
return StableDiffusionImg2ImgPipeline.from_pretrained(model_path, use_auth_token=hf_token,
torch_dtype=torch.float16)
elif inference_type == 'inpaint':
return StableDiffusionInpaintPipeline.from_pretrained(model_path, use_auth_token=hf_token,
torch_dtype=torch.float16)
else:
raise ValueError(f'Invalid type: {inference_type}')