-
Notifications
You must be signed in to change notification settings - Fork 66
/
run_controlnext.py
350 lines (306 loc) · 11.7 KB
/
run_controlnext.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
import os
import torch
import contextlib
import time
import cv2
import numpy as np
from PIL import Image
import argparse
from safetensors.torch import load_file
import torch.nn as nn
from models.unet import UNet2DConditionModel
from models.controlnext import ControlNeXtModel
from pipeline.pipeline_controlnext import StableDiffusionControlNeXtPipeline
from diffusers import UniPCMultistepScheduler, AutoencoderKL
from transformers import AutoTokenizer, PretrainedConfig
def log_validation(
vae,
text_encoder,
tokenizer,
unet,
controlnext,
args,
device='cuda'
):
pipeline = StableDiffusionControlNeXtPipeline.from_pretrained(
args.pretrained_model_name_or_path,
vae=vae,
text_encoder=text_encoder,
tokenizer=tokenizer,
unet=unet,
controlnext=controlnext,
safety_checker=None,
revision=args.revision,
variant=args.variant,
)
pipeline.scheduler = UniPCMultistepScheduler.from_config(pipeline.scheduler.config)
pipeline = pipeline.to(device)
pipeline.set_progress_bar_config()
if args.lora_path is not None:
pipeline.load_lora_weights(args.lora_path)
if args.enable_xformers_memory_efficient_attention:
pipeline.enable_xformers_memory_efficient_attention()
if args.seed is None:
generator = None
else:
generator = torch.Generator(device=device).manual_seed(args.seed)
if len(args.validation_image) == len(args.validation_prompt):
validation_images = args.validation_image
validation_prompts = args.validation_prompt
elif len(args.validation_image) == 1:
validation_images = args.validation_image * len(args.validation_prompt)
validation_prompts = args.validation_prompt
elif len(args.validation_prompt) == 1:
validation_images = args.validation_image
validation_prompts = args.validation_prompt * len(args.validation_image)
else:
raise ValueError(
"number of `args.validation_image` and `args.validation_prompt` should be checked in `parse_args`"
)
if args.negative_prompt is not None:
negative_prompts = args.negative_prompt
assert len(validation_prompts) == len(validation_prompts)
else:
negative_prompts = None
image_logs = []
inference_ctx = torch.autocast(device)
for i, (validation_prompt, validation_image) in enumerate(zip(validation_prompts, validation_images)):
validation_image = Image.open(validation_image).convert("RGB")
images = []
negative_prompt = negative_prompts[i] if negative_prompts is not None else None
for _ in range(args.num_validation_images):
with inference_ctx:
image = pipeline(
validation_prompt, validation_image, num_inference_steps=20, generator=generator, negative_prompt=negative_prompt
).images[0]
images.append(image)
image_logs.append(
{"validation_image": validation_image, "images": images, "validation_prompt": validation_prompt}
)
save_dir_path = os.path.join(args.output_dir, "eval_img")
if not os.path.exists(save_dir_path):
os.makedirs(save_dir_path)
for log in image_logs:
images = log["images"]
validation_prompt = log["validation_prompt"]
validation_image = log["validation_image"]
formatted_images = []
formatted_images.append(np.asarray(validation_image))
for image in images:
formatted_images.append(np.asarray(image))
formatted_images = np.concatenate(formatted_images, 1)
file_path = os.path.join(save_dir_path, "{}.png".format(time.time()))
formatted_images = cv2.cvtColor(formatted_images, cv2.COLOR_BGR2RGB)
print("Save images to:", file_path)
cv2.imwrite(file_path, formatted_images)
return image_logs
def parse_args(input_args=None):
parser = argparse.ArgumentParser(description="Simple example of a ControlNeXt training script.")
parser.add_argument(
"--pretrained_model_name_or_path",
type=str,
default=None,
required=True,
help="Path to pretrained model or model identifier from huggingface.co/models.",
)
parser.add_argument(
"--controlnet_model_name_or_path",
type=str,
default=None,
help="Path to pretrained controlnext model or model identifier from huggingface.co/models."
" If not specified controlnext weights are initialized from unet.",
)
parser.add_argument(
"--unet_model_name_or_path",
type=str,
default=None,
help="Path to pretrained unet model or subset"
)
parser.add_argument(
"--lora_path",
type=str,
default=None,
help="Path to lora"
)
parser.add_argument(
"--revision",
type=str,
default=None,
required=False,
help="Revision of pretrained model identifier from huggingface.co/models.",
)
parser.add_argument(
"--controlnext_scale",
type=float,
default=1.0,
help="Control level for the controlnext",
)
parser.add_argument(
"--variant",
type=str,
default=None,
help="Variant of the model files of the pretrained model identifier from huggingface.co/models, 'e.g.' fp16",
)
parser.add_argument(
"--output_dir",
type=str,
default="controlnext-model",
help="The output directory where the model predictions and checkpoints will be written.",
)
parser.add_argument("--seed", type=int, default=None, help="A seed for reproducible training.")
parser.add_argument(
"--resolution",
type=int,
default=512,
help=(
"The resolution for input images, all the images in the train/validation dataset will be resized to this"
" resolution"
),
)
parser.add_argument(
"--enable_xformers_memory_efficient_attention", action="store_true", help="Whether or not to use xformers."
)
parser.add_argument(
"--validation_prompt",
type=str,
default=None,
nargs="+",
help=(
"A set of prompts evaluated every `--validation_steps` and logged to `--report_to`."
" Provide either a matching number of `--validation_image`s, a single `--validation_image`"
" to be used with all prompts, or a single prompt that will be used with all `--validation_image`s."
),
)
parser.add_argument(
"--negative_prompt",
type=str,
default=None,
nargs="+",
help=(
"A set of prompts evaluated every `--validation_steps` and logged to `--report_to`."
" Provide either a matching number of `--validation_image`s, a single `--validation_image`"
" to be used with all prompts, or a single prompt that will be used with all `--validation_image`s."
),
)
parser.add_argument(
"--validation_image",
type=str,
default=None,
nargs="+",
help=(
"A set of paths to the controlnext conditioning image be evaluated every `--validation_steps`"
" and logged to `--report_to`. Provide either a matching number of `--validation_prompt`s, a"
" a single `--validation_prompt` to be used with all `--validation_image`s, or a single"
" `--validation_image` that will be used with all `--validation_prompt`s."
),
)
parser.add_argument(
"--num_validation_images",
type=int,
default=4,
help="Number of images to be generated for each `--validation_image`, `--validation_prompt` pair",
)
parser.add_argument(
"--save_load_weights_increaments",
action="store_true",
help=(
"whether to store the weights_increaments"
),
)
if input_args is not None:
args = parser.parse_args(input_args)
else:
args = parser.parse_args()
if args.validation_prompt is not None and args.validation_image is None:
raise ValueError("`--validation_image` must be set if `--validation_prompt` is set")
if args.validation_prompt is None and args.validation_image is not None:
raise ValueError("`--validation_prompt` must be set if `--validation_image` is set")
if (
args.validation_image is not None
and args.validation_prompt is not None
and len(args.validation_image) != 1
and len(args.validation_prompt) != 1
and len(args.validation_image) != len(args.validation_prompt)
):
raise ValueError(
"Must provide either 1 `--validation_image`, 1 `--validation_prompt`,"
" or the same number of `--validation_prompt`s and `--validation_image`s"
)
if args.resolution % 8 != 0:
raise ValueError(
"`--resolution` must be divisible by 8 for consistently sized encoded images between the VAE and the controlnet encoder."
)
return args
def import_model_class_from_model_name_or_path(pretrained_model_name_or_path: str, revision: str):
text_encoder_config = PretrainedConfig.from_pretrained(
pretrained_model_name_or_path,
subfolder="text_encoder",
revision=revision,
)
model_class = text_encoder_config.architectures[0]
if model_class == "CLIPTextModel":
from transformers import CLIPTextModel
return CLIPTextModel
else:
raise ValueError(f"{model_class} is not supported.")
def load_safetensors(model, safetensors_path, strict=True, load_weight_increasement=False):
if not load_weight_increasement:
if safetensors_path.endswith('.safetensors'):
state_dict = load_file(safetensors_path)
else:
state_dict = torch.load(safetensors_path)
model.load_state_dict(state_dict, strict=strict)
else:
if safetensors_path.endswith('.safetensors'):
state_dict = load_file(safetensors_path)
else:
state_dict = torch.load(safetensors_path)
pretrained_state_dict = model.state_dict()
for k in state_dict.keys():
state_dict[k] = state_dict[k] + pretrained_state_dict[k]
model.load_state_dict(state_dict, strict=False)
if __name__ == "__main__":
args = parse_args()
vae = AutoencoderKL.from_pretrained(
args.pretrained_model_name_or_path,
subfolder="vae",
revision=args.revision,
variant=args.variant
)
text_encoder_cls = import_model_class_from_model_name_or_path(
args.pretrained_model_name_or_path,
args.revision
)
text_encoder = text_encoder_cls.from_pretrained(
args.pretrained_model_name_or_path,
subfolder="text_encoder",
revision=args.revision,
variant=args.variant
)
tokenizer = AutoTokenizer.from_pretrained(
args.pretrained_model_name_or_path,
subfolder="tokenizer",
revision=args.revision,
use_fast=False,
)
controlnext = ControlNeXtModel(controlnext_scale=args.controlnext_scale)
if args.controlnet_model_name_or_path is not None:
load_safetensors(controlnext, args.controlnet_model_name_or_path)
else:
controlnext.scale = 0.
unet = UNet2DConditionModel.from_pretrained(
args.pretrained_model_name_or_path,
subfolder="unet",
revision=args.revision,
variant=args.variant
)
if args.unet_model_name_or_path is not None:
load_safetensors(unet, args.unet_model_name_or_path, strict=False, load_weight_increasement=args.save_load_weights_increaments)
log_validation(
vae=vae,
text_encoder=text_encoder,
tokenizer=tokenizer,
unet=unet,
controlnext=controlnext,
args=args,
)