-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
256 lines (228 loc) · 11 KB
/
model.py
File metadata and controls
256 lines (228 loc) · 11 KB
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
from enum import Enum
import gc
import numpy as np
#import tomesd
import torch
import json
from einops import rearrange
from diffusers import StableDiffusionInstructPix2PixPipeline, StableDiffusionControlNetPipeline, ControlNetModel
from diffusers.schedulers import EulerAncestralDiscreteScheduler, DDIMScheduler
import utils
import os
on_huggingspace = False
class ModelType(Enum):
Pix2Pix_Video = 1,
Text2Video = 2,
ControlNetCanny = 3,
ControlNetCannyDB = 4,
ControlNetPose = 5,
ControlNetDepth = 6,
class Model:
def __init__(self, device, dtype, **kwargs):
self.device = device
self.dtype = dtype
self.generator = torch.Generator(device=device)
self.pipe_dict = {
ModelType.Pix2Pix_Video: StableDiffusionInstructPix2PixPipeline,
ModelType.ControlNetCanny: StableDiffusionControlNetPipeline,
}
self.controlnet_attn_proc = utils.CrossFrameAttnProcessor(
unet_chunk_size=2)
self.pix2pix_attn_proc = utils.CrossFrameAttnProcessor(
unet_chunk_size=3)
self.text2video_attn_proc = utils.CrossFrameAttnProcessor(
unet_chunk_size=2)
self.pipe = None
self.model_type = None
self.states = {}
self.model_name = ""
def set_model(self, model_type: ModelType, model_id: str, **kwargs):
if hasattr(self, "pipe") and self.pipe is not None:
del self.pipe
self.pipe = None
torch.cuda.empty_cache()
gc.collect()
safety_checker = kwargs.pop('safety_checker', None)
self.pipe = self.pipe_dict[model_type].from_pretrained(
model_id, safety_checker=safety_checker, **kwargs).to(self.device).to(self.dtype)
self.model_type = model_type
self.model_name = model_id
def inference_chunk(self, frame_ids, **kwargs):
if not hasattr(self, "pipe") or self.pipe is None:
return
prompt = np.array(kwargs.pop('prompt'))
negative_prompt = np.array(kwargs.pop('negative_prompt', ''))
latents = None
if 'latents' in kwargs:
latents = kwargs.pop('latents')[frame_ids]
if 'image' in kwargs:
kwargs['image'] = kwargs['image'][frame_ids]
if 'video_length' in kwargs:
kwargs['video_length'] = len(frame_ids)
if self.model_type == ModelType.Text2Video:
kwargs["frame_ids"] = frame_ids
return self.pipe(prompt=prompt[frame_ids].tolist(),
negative_prompt=negative_prompt[frame_ids].tolist(),
latents=latents,
generator=self.generator,
**kwargs)
def inference(self, split_to_chunks=False, chunk_size=8, **kwargs):
if not hasattr(self, "pipe") or self.pipe is None:
return
# if "merging_ratio" in kwargs:
# merging_ratio = kwargs.pop("merging_ratio")
# # if merging_ratio > 0:
# tomesd.apply_patch(self.pipe, ratio=merging_ratio)
seed = kwargs.pop('seed', 0)
if seed < 0:
seed = self.generator.seed()
kwargs.pop('generator', '')
if 'image' in kwargs:
f = kwargs['image'].shape[0]
else:
f = kwargs['video_length']
assert 'prompt' in kwargs
prompt = [kwargs.pop('prompt')] * f
negative_prompt = [kwargs.pop('negative_prompt', '')] * f
frames_counter = 0
# Processing chunk-by-chunk
if split_to_chunks:
chunk_ids = np.arange(0, f, chunk_size - 1)
result = []
for i in range(len(chunk_ids)):
ch_start = chunk_ids[i]
ch_end = f if i == len(chunk_ids) - 1 else chunk_ids[i + 1]
frame_ids = [0] + list(range(ch_start, ch_end))
self.generator.manual_seed(seed)
print(f'Processing chunk {i + 1} / {len(chunk_ids)}')
result.append(self.inference_chunk(frame_ids=frame_ids,
prompt=prompt,
negative_prompt=negative_prompt,
**kwargs).images[1:])
frames_counter += len(chunk_ids)-1
if on_huggingspace and frames_counter >= 80:
break
result = np.concatenate(result)
return result
else:
self.generator.manual_seed(seed)
return self.pipe(prompt=prompt, negative_prompt=negative_prompt, generator=self.generator, **kwargs).images
def process_controlnet_canny(self,
video_path,
prompt,
chunk_size=8,
num_inference_steps=20,
controlnet_conditioning_scale=1.0,
guidance_scale=9.0,
seed=42,
eta=0.0,
start_t=0,
end_t=-1,
out_fps=-1,
low_threshold=100,
high_threshold=200,
resolution=512,
use_cf_attn=True,
save_path=None):
print("Module Canny")
print("-----CONFIG-----")
print("video_path", video_path)
print("prompt", prompt)
print("chunk_size", chunk_size)
print("num_inference_steps", num_inference_steps)
print("controlnet_conditioning_scale", controlnet_conditioning_scale)
print("guidance_scale", guidance_scale)
print("seed", seed)
print("eta", eta)
print("start_t", start_t)
print("end_t", end_t)
print("out_fps", out_fps)
print("low_threshold", low_threshold)
print("high_threshold", high_threshold)
print("resolution", resolution)
print("save_path", save_path)
if self.model_type != ModelType.ControlNetCanny:
controlnet = ControlNetModel.from_pretrained(
"lllyasviel/sd-controlnet-canny")
self.set_model(ModelType.ControlNetCanny,
model_id="runwayml/stable-diffusion-v1-5", controlnet=controlnet)
self.pipe.scheduler = DDIMScheduler.from_config(
self.pipe.scheduler.config)
if use_cf_attn:
self.pipe.unet.set_attn_processor(
processor=self.controlnet_attn_proc)
self.pipe.controlnet.set_attn_processor(
processor=self.controlnet_attn_proc)
added_prompt = "masterpiece, best quality, realistic, photorealistic, ultra detailed, extremely detailed face, solo, perfect face, ((detailed face)), (high detailed skin:1.2), 8k, dslr, right lighting, film grain, Fujifilm XT3"
negative_prompts = "(deformed pupils, deformed eyes, semi-realistic, cgi, 3d, render, sketch, cartoon, drawing, anime:1.4), text, close up, cropped, out of frame, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck"
video, fps = utils.prepare_video(
video_path, resolution, self.device, self.dtype, False, start_t, end_t, out_fps)
control = utils.pre_process_canny(
video, low_threshold, high_threshold).to(self.device).to(self.dtype)
canny_to_save = list(rearrange(control, 'f c w h -> f w h c').cpu().detach().numpy())
_ = utils.create_video(canny_to_save, 4, path=save_path.replace(".mp4", "_edgemap.mp4"), watermark=None)
f, _, h, w = video.shape
self.generator.manual_seed(seed)
latents = torch.randn((1, 4, h//8, w//8), dtype=self.dtype,
device=self.device, generator=self.generator)
latents = latents.repeat(f, 1, 1, 1)
result = self.inference(image=control,
prompt=prompt + ', ' + added_prompt,
height=h,
width=w,
negative_prompt=negative_prompts,
num_inference_steps=num_inference_steps,
guidance_scale=guidance_scale,
controlnet_conditioning_scale=controlnet_conditioning_scale,
eta=eta,
latents=latents,
seed=seed,
output_type='numpy',
split_to_chunks=True,
chunk_size=chunk_size)
return utils.create_video(result, fps, path=save_path, watermark=None)
def process_pix2pix(self,
video,
prompt,
resolution=512,
seed=0,
image_guidance_scale=1.0,
start_t=0,
end_t=-1,
out_fps=-1,
chunk_size=8,
merging_ratio=0.0,
use_cf_attn=True,
save_path=None,):
print("Module Pix2Pix")
print("-----CONFIG-----"),
print("video:", video)
print("prompt:", prompt)
print("resolution:", resolution)
print("seed:", seed)
print("image_guidance_scale:", image_guidance_scale)
print("start_t:", start_t)
print("end_t:", end_t)
print("out_fps:", out_fps)
print("chunk_size:", chunk_size)
print("save_path", save_path)
if self.model_type != ModelType.Pix2Pix_Video:
self.set_model(ModelType.Pix2Pix_Video,
model_id="timbrooks/instruct-pix2pix")
self.pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(
self.pipe.scheduler.config)
if use_cf_attn:
self.pipe.unet.set_attn_processor(
processor=self.pix2pix_attn_proc)
video, fps = utils.prepare_video(
video, resolution, self.device, self.dtype, True, start_t, end_t, out_fps)
self.generator.manual_seed(seed)
result = self.inference(image=video,
prompt=prompt,
seed=seed,
output_type='numpy',
num_inference_steps=50,
image_guidance_scale=image_guidance_scale,
split_to_chunks=True,
chunk_size=chunk_size)
return utils.create_video(result, fps, path=save_path, watermark=None)