forked from mindspore-lab/mindone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunclip_image_variation.py
506 lines (455 loc) · 18.2 KB
/
unclip_image_variation.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
"""
Text to image generation
"""
import argparse
import logging
import os
import sys
import time
from typing import Union
import numpy as np
from omegaconf import OmegaConf
from PIL import Image, ImageOps
import mindspore as ms
import mindspore.ops as ops
workspace = os.path.dirname(os.path.abspath(__file__))
sys.path.append(workspace)
from ldm.models.diffusion.ddim import DDIMSampler
from ldm.models.diffusion.dpm_solver import DPMSolverSampler
from ldm.modules.logger import set_logger
from ldm.modules.lora import inject_trainable_lora
from ldm.modules.train.tools import set_random_seed
from ldm.util import instantiate_from_config, str2bool
from utils import model_utils
from utils.download import download_checkpoint
logger = logging.getLogger(__name__)
_version_cfg = {
"2.1-unclip-h": ("sd21-unclip-h-6a73eca5.ckpt", "v2-vpred-inference-unclip-h.yaml", 768),
"2.1-unclip-l": ("sd21-unclip-l-baa7c8b5.ckpt", "v2-vpred-inference-unclip-l.yaml", 768),
}
_URL_PREFIX = "https://download.mindspore.cn/toolkits/mindone/stable_diffusion"
_MIN_CKPT_SIZE = 4.0 * 1e9
_VIT_STATS_CKPT = "ViT-L-14_stats-b668e2ca.ckpt"
def numpy_to_pil(images):
"""
Convert a numpy image or a batch of images to a PIL image.
"""
if images.ndim == 3:
images = images[None, ...]
images = (images * 255).round().astype("uint8")
pil_images = [Image.fromarray(image) for image in images]
return pil_images
def load_model_from_config(config, ckpt, use_lora=False, lora_rank=4, lora_fp16=True, lora_only_ckpt=None):
model = instantiate_from_config(config.model)
def _load_model(_model, ckpt_fp, verbose=True, filter=None):
if os.path.exists(ckpt_fp):
param_dict = ms.load_checkpoint(ckpt_fp)
if param_dict:
param_not_load, ckpt_not_load = model_utils.load_param_into_net_with_filter(
_model, param_dict, filter=filter
)
if verbose:
if len(param_not_load) > 0:
logger.info(
"Net params not loaded: {}".format([p for p in param_not_load if not p.startswith("adam")])
)
if len(ckpt_not_load) > 0:
logger.info(
"Ckpt params not loaded: {}".format([p for p in ckpt_not_load if not p.startswith("adam")])
)
else:
logger.error(f"!!!Error!!!: {ckpt_fp} doesn't exist")
raise FileNotFoundError(f"{ckpt_fp} doesn't exist")
if use_lora:
load_lora_only = True if lora_only_ckpt is not None else False
if not load_lora_only:
logger.info(f"Loading model from {ckpt}")
_load_model(model, ckpt)
else:
if os.path.exists(lora_only_ckpt):
lora_param_dict = ms.load_checkpoint(lora_only_ckpt)
if "lora_rank" in lora_param_dict.keys():
lora_rank = int(lora_param_dict["lora_rank"].value())
logger.info(f"Lora rank is set to {lora_rank} according to the found value in lora checkpoint.")
else:
raise ValueError(f"{lora_only_ckpt} doesn't exist")
# load the main pretrained model
logger.info(f"Loading pretrained model from {ckpt}")
_load_model(model, ckpt, verbose=True, filter=ms.load_checkpoint(ckpt).keys())
# inject lora params
injected_attns, injected_trainable_params = inject_trainable_lora(
model,
rank=lora_rank,
use_fp16=(model.model.diffusion_model.dtype == ms.float16),
)
# load fine-tuned lora params
logger.info(f"Loading LoRA params from {lora_only_ckpt}")
_load_model(model, lora_only_ckpt, verbose=True, filter=injected_trainable_params.keys())
else:
logger.info(f"Loading model from {ckpt}")
_load_model(model, ckpt)
model.set_train(False)
for param in model.trainable_params():
param.requires_grad = False
return model
def load_image(image: Union[str, Image.Image]) -> ms.Tensor:
if isinstance(image, str):
image = Image.open(image)
elif isinstance(image, Image.Image):
image = image
else:
raise ValueError("Uncorrect format for image")
image = ImageOps.exif_transpose(image)
image = image.convert("RGB")
w, h = image.size
w, h = map(lambda x: x - x % 64, (w, h))
image = image.resize((w, h), resample=Image.LANCZOS)
image = np.array(image).astype(np.float32) / 255.0
image = image[None].transpose(0, 3, 1, 2)
image = 2.0 * image - 1
image = ms.Tensor(image)
return image
def main(args):
# set logger
set_logger(
name="",
output_dir=args.output_path,
rank=0,
log_level=eval(args.log_level),
)
work_dir = os.path.dirname(os.path.abspath(__file__))
logger.debug(f"WORK DIR:{work_dir}")
os.makedirs(args.output_path, exist_ok=True)
outpath = args.output_path
# read prompts
batch_size = args.n_samples
if not args.data_path:
prompt = args.prompt
assert prompt is not None
data = [batch_size * [prompt]]
else:
logger.info(f"Reading prompts from {args.data_path}")
with open(args.data_path, "r") as f:
prompts = f.read().splitlines()
# TODO: try to put different prompts in a batch
data = [batch_size * [prompt] for prompt in prompts]
# read negative prompts
if not args.negative_data_path:
negative_prompt = args.negative_prompt
assert negative_prompt is not None
negative_data = [batch_size * [negative_prompt]]
else:
logger.info(f"Reading negative prompts from {args.negative_data_path}")
with open(args.negative_data_path, "r") as f:
negative_prompts = f.read().splitlines()
# TODO: try to put different prompts in a batch
negative_data = [batch_size * [negative_prompt] for negative_prompt in negative_prompts]
# post-process negative prompts
assert len(negative_data) <= len(data), "Negative prompts should be shorter than positive prompts"
if len(negative_data) < len(data):
logger.info("Negative prompts are shorter than positive prompts, padding blank prompts")
blank_negative_prompt = batch_size * [""]
for _ in range(len(data) - len(negative_data)):
negative_data.append(blank_negative_prompt)
sample_path = os.path.join(outpath, "samples")
os.makedirs(sample_path, exist_ok=True)
base_count = len(os.listdir(sample_path))
# set ms context
device_id = int(os.getenv("DEVICE_ID", 0))
ms.context.set_context(mode=args.ms_mode, device_target="Ascend", device_id=device_id, max_device_memory="30GB")
set_random_seed(args.seed)
# create model
if not os.path.isabs(args.config):
args.config = os.path.join(work_dir, args.config)
config = OmegaConf.load(f"{args.config}")
model = load_model_from_config(
config,
ckpt=args.ckpt_path,
use_lora=args.use_lora,
lora_rank=args.lora_rank,
lora_only_ckpt=args.lora_ckpt_path,
)
# read image
image_tensor = load_image(args.image_path)
# get image conditioning
adm_cond = model.embedder(image_tensor)
adm_cond = ops.tile(adm_cond, (batch_size, 1))
# add noise
if args.noise_level > model.noise_augmentor.max_noise_level:
raise ValueError(f"noise_level exceeds the maximum value `{model.noise_augmentor.max_noise_level}`.")
noise_level = ops.tile(ms.Tensor([args.noise_level]), (batch_size,))
c_adm, noise_level_emb = model.noise_augmentor(adm_cond, noise_level=noise_level)
adm_cond = ops.concat((c_adm, noise_level_emb), 1)
prediction_type = getattr(config.model, "prediction_type", "noise")
logger.info(f"Prediction type: {prediction_type}")
# create sampler
if args.ddim:
sampler = DDIMSampler(model)
sname = "ddim"
elif args.dpm_solver:
sampler = DPMSolverSampler(model, "dpmsolver", prediction_type=prediction_type)
sname = "dpm_solver"
else:
sampler = DPMSolverSampler(model, "dpmsolver++", prediction_type=prediction_type)
sname = "dpm_solver_pp"
# log
key_info = "Key Settings:\n" + "=" * 50 + "\n"
key_info += "\n".join(
[
f"MindSpore mode[GRAPH(0)/PYNATIVE(1)]: {args.ms_mode}",
"Distributed mode: False",
f"Number of input prompts: {len(data)}",
f"Number of input negative prompts: {len(negative_data)}",
f"Number of trials for each prompt: {args.n_iter}",
f"Number of samples in each trial: {args.n_samples}",
f"Model: StableDiffusion v-{args.version}",
f"Precision: {model.model.diffusion_model.dtype}",
f"Pretrained ckpt path: {args.ckpt_path}",
f"Lora ckpt path: {args.lora_ckpt_path if args.use_lora else None}",
f"Sampler: {sname}",
f"Sampling steps: {args.sampling_steps}",
f"Uncondition guidance scale: {args.scale}",
f"Target image size (H, W): ({args.H}, {args.W})",
]
)
key_info += "\n" + "=" * 50
logger.info(key_info)
# infer
start_code = None
if args.fixed_code:
start_code = ops.StandardNormal()((args.n_samples, 4, args.H // 8, args.W // 8))
all_samples = list()
for i, prompts in enumerate(data):
negative_prompts = negative_data[i]
logger.info(
"[{}/{}] Generating images with conditions:\nPrompt(s): {}\nNegative prompt(s): {}".format(
i + 1, len(data), prompts[0], negative_prompts[0]
)
)
for n in range(args.n_iter):
start_time = time.time()
uc = None
if args.scale != 1.0:
if isinstance(negative_prompts, tuple):
negative_prompts = list(negative_prompts)
tokenized_negative_prompts = model.tokenize(negative_prompts)
uc = model.get_learned_conditioning(tokenized_negative_prompts)
if isinstance(prompts, tuple):
prompts = list(prompts)
tokenized_prompts = model.tokenize(prompts)
c = model.get_learned_conditioning(tokenized_prompts)
c = {"c_crossattn": c, "c_adm": adm_cond}
uc = {"c_crossattn": uc, "c_adm": ops.zeros_like(adm_cond)}
shape = [4, args.H // 8, args.W // 8]
samples_ddim, _ = sampler.sample(
S=args.sampling_steps,
conditioning=c,
batch_size=args.n_samples,
shape=shape,
verbose=False,
unconditional_guidance_scale=args.scale,
unconditional_conditioning=uc,
eta=args.ddim_eta,
x_T=start_code,
)
x_samples_ddim = model.decode_first_stage(samples_ddim)
x_samples_ddim = ms.ops.clip_by_value((x_samples_ddim + 1.0) / 2.0, clip_value_min=0.0, clip_value_max=1.0)
x_samples_ddim_numpy = x_samples_ddim.asnumpy()
if not args.skip_save:
for x_sample in x_samples_ddim_numpy:
x_sample = 255.0 * x_sample.transpose(1, 2, 0)
img = Image.fromarray(x_sample.astype(np.uint8))
img.save(os.path.join(sample_path, f"{base_count:05}.png"))
base_count += 1
if not args.skip_grid:
all_samples.append(x_samples_ddim_numpy)
end_time = time.time()
logger.info(
"{}/{} images generated, time cost for current trial: {:.3f}s".format(
batch_size * (n + 1), batch_size * args.n_iter, end_time - start_time
)
)
logger.info(f"Done! All generated images are saved in: {outpath}/samples" f"\nEnjoy.")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--ms_mode", type=int, default=0, help="Running in GRAPH_MODE(0) or PYNATIVE_MODE(1) (default=0)"
)
parser.add_argument(
"--data_path",
type=str,
nargs="?",
default="",
help="path to a file containing prompt list (each line in the file corresponds to a prompt to render).",
)
parser.add_argument(
"--negative_data_path",
type=str,
nargs="?",
default="",
help="path to a file containing negative prompt list (each line in the file corresponds to a prompt not to "
"render).",
)
parser.add_argument(
"-v",
"--version",
type=str,
nargs="?",
default="2.1-unclip-l",
help=f"Stable diffusion version. Options: {list(_version_cfg.keys())} ",
)
parser.add_argument("--prompt", type=str, nargs="?", default="", help="the prompt to render")
parser.add_argument("--negative_prompt", type=str, nargs="?", default="", help="the negative prompt not to render")
parser.add_argument("--output_path", type=str, nargs="?", default="output", help="dir to write results to")
parser.add_argument(
"--skip_grid",
action="store_true",
help="do not save a grid, only individual samples. Helpful when evaluating lots of samples",
)
parser.add_argument(
"--skip_save",
action="store_true",
help="do not save individual samples. For speed measurements.",
)
parser.add_argument(
"--sampling_steps",
type=int,
default=20,
help="number of ddim sampling steps. The recommended value is 50 for PLMS, DDIM and 20 for UniPC,DPM-Solver, DPM-Solver++",
)
parser.add_argument(
"--ddim_eta",
type=float,
default=0.0,
help="ddim eta (eta=0.0 corresponds to deterministic sampling",
)
parser.add_argument(
"--fixed_code",
action="store_true",
help="if enabled, uses the same starting code across samples ",
)
parser.add_argument(
"--n_iter",
type=int,
default=2,
help="number of iterations or trials. sample this often, ",
)
parser.add_argument(
"--n_samples",
type=int,
default=2,
help="how many samples to produce for each given prompt in an iteration. A.k.a. batch size",
)
parser.add_argument(
"--H",
type=int,
default=768,
help="image height, in pixel space",
)
parser.add_argument(
"--W",
type=int,
default=768,
help="image width, in pixel space",
)
parser.add_argument(
"--ddim",
action="store_true",
help="use ddim sampling",
)
parser.add_argument(
"--dpm_solver",
action="store_true",
help="use dpm_solver sampling",
)
parser.add_argument(
"--scale",
type=float,
default=None,
help="unconditional guidance scale: eps = eps(x, uncond) + scale * (eps(x, cond) - eps(x, uncond)). "
"Simplified: `uc + scale * (uc - prompt)`",
)
parser.add_argument(
"--config",
type=str,
default=None,
help="path to config which constructs model. If None, select by version",
)
parser.add_argument(
"--use_lora",
default=False,
type=str2bool,
help="whether the checkpoint used for inference is finetuned from LoRA",
)
parser.add_argument(
"--lora_rank",
default=None,
type=int,
help="LoRA rank. If None, lora checkpoint should contain the value for lora rank in its append_dict.",
)
parser.add_argument(
"--ckpt_path",
type=str,
default=None,
help="path to checkpoint of model",
)
parser.add_argument(
"--lora_ckpt_path",
type=str,
default=None,
help="path to lora only checkpoint. Set it if use_lora is not None",
)
parser.add_argument(
"--seed",
type=int,
default=42,
help="the seed (for reproducible sampling)",
)
parser.add_argument(
"--log_level",
type=str,
default="logging.INFO",
help="log level, options: logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR",
)
parser.add_argument("--image_path", type=str, required=True, help="path to input image")
parser.add_argument(
"--noise_level",
type=int,
default=0,
help="the amount of noise add to the image embedding. A higher value increase the varianece in the final image",
)
args = parser.parse_args()
# check args
if args.version:
os.environ["SD_VERSION"] = args.version
if args.version not in _version_cfg:
raise ValueError(f"Unknown version: {args.version}. Supported SD versions are: {list(_version_cfg.keys())}")
if args.ckpt_path is None:
ckpt_name = _version_cfg[args.version][0]
args.ckpt_path = "models/" + ckpt_name
desire_size = _version_cfg[args.version][2]
if args.H != desire_size or args.W != desire_size:
logger.warning(
f"The optimal H, W for SD {args.version} is ({desire_size}, {desire_size}) . But got ({args.H}, {args.W})."
)
# download if not exists or not complete
ckpt_incomplete = False
if os.path.exists(args.ckpt_path):
if os.path.getsize(args.ckpt_path) < _MIN_CKPT_SIZE:
ckpt_incomplete = True
logger.warning(
f"The checkpoint size is too small {args.ckpt_path}. Please check and remove it if it is incomplete!"
)
if not os.path.exists(args.ckpt_path):
logger.info(f"Start downloading checkpoint {ckpt_name} ...")
download_checkpoint(os.path.join(_URL_PREFIX, ckpt_name), "models/")
if args.config is None:
args.config = os.path.join("configs", _version_cfg[args.version][1])
if args.scale is None:
args.scale = 10.0 if args.version.startswith("2.") else 7.5
vit_stat_ckpt_path = os.path.join("models", _VIT_STATS_CKPT)
if not os.path.exists(vit_stat_ckpt_path):
logger.info(f"Start downloading VIT stats checkpoint {_VIT_STATS_CKPT} ...")
download_checkpoint(os.path.join(_URL_PREFIX, "unclip", _VIT_STATS_CKPT), "models/")
# core task
main(args)