-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathEMOAnimationPipeline.py
955 lines (801 loc) · 41.2 KB
/
EMOAnimationPipeline.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
# *************************************************************************
# This file may have been modified by Bytedance Inc. (“Bytedance Inc.'s Mo-
# difications”). All Bytedance Inc.'s Modifications are Copyright (2023) B-
# ytedance Inc..
# *************************************************************************
# Adapted from https://github.com/showlab/Tune-A-Video/blob/main/tuneavideo/pipelines/pipeline_tuneavideo.py
# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
TODO:
1. Finish this
"""
from animated_diff import AnimatedDiff
import inspect, math
from typing import Callable, List, Optional, Union
from dataclasses import dataclass
from PIL import Image
import numpy as np
import torch
import torch.distributed as dist
from tqdm import tqdm
from diffusers.utils import is_accelerate_available
from packaging import version
from transformers import CLIPTextModel, CLIPTokenizer
from diffusers.configuration_utils import FrozenDict
from diffusers.models import AutoencoderKL
from diffusers.pipeline_utils import DiffusionPipeline
from diffusers.schedulers import (
DDIMScheduler,
DPMSolverMultistepScheduler,
EulerAncestralDiscreteScheduler,
EulerDiscreteScheduler,
LMSDiscreteScheduler,
PNDMScheduler,
)
from diffusers.utils import deprecate, logging, BaseOutput
from einops import rearrange
from magicanimate.models.unet_controlnet import UNet3DConditionModel
from magicanimate.models.controlnet import ControlNetModel
from magicanimate.models.mutual_self_attention import ReferenceAttentionControl
from magicanimate.pipelines.context import (
get_context_scheduler,
get_total_steps
)
from magicanimate.utils.util import get_tensor_interpolation_method
from Net import Wav2VecFeatureExtractor,SpeedEncoder
from accelerate import Accelerator
from transformers import CLIPVisionModelWithProjection
from omegaconf import OmegaConf
import os
import argparse
import random
import yaml
import torch.nn.functional as F
from Net import EMOModel
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
@dataclass
class AnimationPipelineOutput(BaseOutput):
videos: Union[torch.Tensor, np.ndarray]
class EMOAnimationPipeline(DiffusionPipeline):
_optional_components = []
def __init__(
self,
vae: AutoencoderKL,
text_encoder: CLIPTextModel,
tokenizer: CLIPTokenizer,
unet: UNet3DConditionModel,
controlnet: ControlNetModel,
scheduler: Union[
DDIMScheduler,
PNDMScheduler,
LMSDiscreteScheduler,
EulerDiscreteScheduler,
EulerAncestralDiscreteScheduler,
DPMSolverMultistepScheduler,
],
):
super().__init__()
if hasattr(scheduler.config, "steps_offset") and scheduler.config.steps_offset != 1:
deprecation_message = (
f"The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`"
f" should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure "
"to update the config accordingly as leaving `steps_offset` might led to incorrect results"
" in future versions. If you have downloaded this checkpoint from the Hugging Face Hub,"
" it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`"
" file"
)
deprecate("steps_offset!=1", "1.0.0", deprecation_message, standard_warn=False)
new_config = dict(scheduler.config)
new_config["steps_offset"] = 1
scheduler._internal_dict = FrozenDict(new_config)
if hasattr(scheduler.config, "clip_sample") and scheduler.config.clip_sample is True:
deprecation_message = (
f"The configuration file of this scheduler: {scheduler} has not set the configuration `clip_sample`."
" `clip_sample` should be set to False in the configuration file. Please make sure to update the"
" config accordingly as not setting `clip_sample` in the config might lead to incorrect results in"
" future versions. If you have downloaded this checkpoint from the Hugging Face Hub, it would be very"
" nice if you could open a Pull request for the `scheduler/scheduler_config.json` file"
)
deprecate("clip_sample not set", "1.0.0", deprecation_message, standard_warn=False)
new_config = dict(scheduler.config)
new_config["clip_sample"] = False
scheduler._internal_dict = FrozenDict(new_config)
is_unet_version_less_0_9_0 = hasattr(unet.config, "_diffusers_version") and version.parse(
version.parse(unet.config._diffusers_version).base_version
) < version.parse("0.9.0.dev0")
is_unet_sample_size_less_64 = hasattr(unet.config, "sample_size") and unet.config.sample_size < 64
if is_unet_version_less_0_9_0 and is_unet_sample_size_less_64:
deprecation_message = (
"The configuration file of the unet has set the default `sample_size` to smaller than"
" 64 which seems highly unlikely. If your checkpoint is a fine-tuned version of any of the"
" following: \n- CompVis/stable-diffusion-v1-4 \n- CompVis/stable-diffusion-v1-3 \n-"
" CompVis/stable-diffusion-v1-2 \n- CompVis/stable-diffusion-v1-1 \n- runwayml/stable-diffusion-v1-5"
" \n- runwayml/stable-diffusion-inpainting \n you should change 'sample_size' to 64 in the"
" configuration file. Please make sure to update the config accordingly as leaving `sample_size=32`"
" in the config might lead to incorrect results in future versions. If you have downloaded this"
" checkpoint from the Hugging Face Hub, it would be very nice if you could open a Pull request for"
" the `unet/config.json` file"
)
deprecate("sample_size<64", "1.0.0", deprecation_message, standard_warn=False)
new_config = dict(unet.config)
new_config["sample_size"] = 64
unet._internal_dict = FrozenDict(new_config)
self.register_modules(
vae=vae,
text_encoder=text_encoder,
tokenizer=tokenizer,
unet=unet,
controlnet=controlnet,
scheduler=scheduler,
)
self.vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1)
# Initialize Wav2Vec feature extractor and SpeedEncoder
self.feature_extractor = Wav2VecFeatureExtractor(model_name='facebook/wav2vec2-base-960h', device=self.device)
num_speed_buckets = 10
speed_embedding_dim = 64
self.speed_encoder = SpeedEncoder(num_speed_buckets, speed_embedding_dim)
def enable_vae_slicing(self):
self.vae.enable_slicing()
def disable_vae_slicing(self):
self.vae.disable_slicing()
def enable_sequential_cpu_offload(self, gpu_id=0):
if is_accelerate_available():
from accelerate import cpu_offload
else:
raise ImportError("Please install accelerate via `pip install accelerate`")
device = torch.device(f"cuda:{gpu_id}")
for cpu_offloaded_model in [self.unet, self.text_encoder, self.vae]:
if cpu_offloaded_model is not None:
cpu_offload(cpu_offloaded_model, device)
@property
def _execution_device(self):
if self.device != torch.device("meta") or not hasattr(self.unet, "_hf_hook"):
return self.device
for module in self.unet.modules():
if (
hasattr(module, "_hf_hook")
and hasattr(module._hf_hook, "execution_device")
and module._hf_hook.execution_device is not None
):
return torch.device(module._hf_hook.execution_device)
return self.device
def _encode_prompt(self, prompt, device, num_videos_per_prompt, do_classifier_free_guidance, negative_prompt):
batch_size = len(prompt) if isinstance(prompt, list) else 1
text_inputs = self.tokenizer(
prompt,
padding="max_length",
max_length=self.tokenizer.model_max_length,
truncation=True,
return_tensors="pt",
)
text_input_ids = text_inputs.input_ids
untruncated_ids = self.tokenizer(prompt, padding="longest", return_tensors="pt").input_ids
if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(text_input_ids, untruncated_ids):
removed_text = self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1])
logger.warning(
"The following part of your input was truncated because CLIP can only handle sequences up to"
f" {self.tokenizer.model_max_length} tokens: {removed_text}"
)
if hasattr(self.text_encoder.config, "use_attention_mask") and self.text_encoder.config.use_attention_mask:
attention_mask = text_inputs.attention_mask.to(device)
else:
attention_mask = None
text_embeddings = self.text_encoder(
text_input_ids.to(device),
attention_mask=attention_mask,
)
text_embeddings = text_embeddings[0]
# duplicate text embeddings for each generation per prompt, using mps friendly method
bs_embed, seq_len, _ = text_embeddings.shape
text_embeddings = text_embeddings.repeat(1, num_videos_per_prompt, 1)
text_embeddings = text_embeddings.view(bs_embed * num_videos_per_prompt, seq_len, -1)
# get unconditional embeddings for classifier free guidance
if do_classifier_free_guidance:
uncond_tokens: List[str]
if negative_prompt is None:
uncond_tokens = [""] * batch_size
elif type(prompt) is not type(negative_prompt):
raise TypeError(
f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !="
f" {type(prompt)}."
)
elif isinstance(negative_prompt, str):
uncond_tokens = [negative_prompt]
elif batch_size != len(negative_prompt):
raise ValueError(
f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:"
f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"
" the batch size of `prompt`."
)
else:
uncond_tokens = negative_prompt
max_length = text_input_ids.shape[-1]
uncond_input = self.tokenizer(
uncond_tokens,
padding="max_length",
max_length=max_length,
truncation=True,
return_tensors="pt",
)
if hasattr(self.text_encoder.config, "use_attention_mask") and self.text_encoder.config.use_attention_mask:
attention_mask = uncond_input.attention_mask.to(device)
else:
attention_mask = None
uncond_embeddings = self.text_encoder(
uncond_input.input_ids.to(device),
attention_mask=attention_mask,
)
uncond_embeddings = uncond_embeddings[0]
# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
seq_len = uncond_embeddings.shape[1]
uncond_embeddings = uncond_embeddings.repeat(1, num_videos_per_prompt, 1)
uncond_embeddings = uncond_embeddings.view(batch_size * num_videos_per_prompt, seq_len, -1)
# For classifier free guidance, we need to do two forward passes.
# Here we concatenate the unconditional and text embeddings into a single batch
# to avoid doing two forward passes
text_embeddings = torch.cat([uncond_embeddings, text_embeddings])
return text_embeddings
def decode_latents(self, latents, rank, decoder_consistency=None):
video_length = latents.shape[2]
latents = 1 / 0.18215 * latents
latents = rearrange(latents, "b c f h w -> (b f) c h w")
# video = self.vae.decode(latents).sample
video = []
for frame_idx in tqdm(range(latents.shape[0]), disable=(rank!=0)):
if decoder_consistency is not None:
video.append(decoder_consistency(latents[frame_idx:frame_idx+1]))
else:
video.append(self.vae.decode(latents[frame_idx:frame_idx+1]).sample)
video = torch.cat(video)
video = rearrange(video, "(b f) c h w -> b c f h w", f=video_length)
video = (video / 2 + 0.5).clamp(0, 1)
# we always cast to float32 as this does not cause significant overhead and is compatible with bfloa16
video = video.cpu().float().numpy()
return video
def prepare_extra_step_kwargs(self, generator, eta):
# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
# eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
# eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
# and should be between [0, 1]
accepts_eta = "eta" in set(inspect.signature(self.scheduler.step).parameters.keys())
extra_step_kwargs = {}
if accepts_eta:
extra_step_kwargs["eta"] = eta
# check if the scheduler accepts generator
accepts_generator = "generator" in set(inspect.signature(self.scheduler.step).parameters.keys())
if accepts_generator:
extra_step_kwargs["generator"] = generator
return extra_step_kwargs
def check_inputs(self, prompt, height, width, callback_steps):
if not isinstance(prompt, str) and not isinstance(prompt, list):
raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}")
if height % 8 != 0 or width % 8 != 0:
raise ValueError(f"`height` and `width` have to be divisible by 8 but are {height} and {width}.")
if (callback_steps is None) or (
callback_steps is not None and (not isinstance(callback_steps, int) or callback_steps <= 0)
):
raise ValueError(
f"`callback_steps` has to be a positive integer but is {callback_steps} of type"
f" {type(callback_steps)}."
)
def prepare_latents(self, batch_size, num_channels_latents, video_length, height, width, dtype, device, generator, latents=None, clip_length=16):
shape = (batch_size, num_channels_latents, clip_length, height // self.vae_scale_factor, width // self.vae_scale_factor)
if isinstance(generator, list) and len(generator) != batch_size:
raise ValueError(
f"You have passed a list of generators of length {len(generator)}, but requested an effective batch"
f" size of {batch_size}. Make sure the batch size matches the length of the generators."
)
if latents is None:
rand_device = "cpu" if device.type == "mps" else device
if isinstance(generator, list):
latents = [
torch.randn(shape, generator=generator[i], device=rand_device, dtype=dtype)
for i in range(batch_size)
]
latents = torch.cat(latents, dim=0).to(device)
else:
latents = torch.randn(shape, generator=generator, device=rand_device, dtype=dtype).to(device)
latents = latents.repeat(1, 1, video_length//clip_length, 1, 1)
else:
if latents.shape != shape:
raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {shape}")
latents = latents.to(device)
# scale the initial noise by the standard deviation required by the scheduler
latents = latents * self.scheduler.init_noise_sigma
return latents
def prepare_condition(self, condition, num_videos_per_prompt, device, dtype, do_classifier_free_guidance):
# prepare conditions for controlnet
condition = torch.from_numpy(condition.copy()).to(device=device, dtype=dtype) / 255.0
condition = torch.stack([condition for _ in range(num_videos_per_prompt)], dim=0)
condition = rearrange(condition, 'b f h w c -> (b f) c h w').clone()
if do_classifier_free_guidance:
condition = torch.cat([condition] * 2)
return condition
def next_step(
self,
model_output: torch.FloatTensor,
timestep: int,
x: torch.FloatTensor,
eta=0.,
verbose=False
):
"""
Inverse sampling for DDIM Inversion
"""
if verbose:
print("timestep: ", timestep)
next_step = timestep
timestep = min(timestep - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps, 999)
alpha_prod_t = self.scheduler.alphas_cumprod[timestep] if timestep >= 0 else self.scheduler.final_alpha_cumprod
alpha_prod_t_next = self.scheduler.alphas_cumprod[next_step]
beta_prod_t = 1 - alpha_prod_t
pred_x0 = (x - beta_prod_t**0.5 * model_output) / alpha_prod_t**0.5
pred_dir = (1 - alpha_prod_t_next)**0.5 * model_output
x_next = alpha_prod_t_next**0.5 * pred_x0 + pred_dir
return x_next, pred_x0
@torch.no_grad()
def images2latents(self, images, dtype):
"""
Convert RGB image to VAE latents
"""
device = self._execution_device
images = torch.from_numpy(images).float().to(dtype) / 127.5 - 1
images = rearrange(images, "f h w c -> f c h w").to(device)
latents = []
for frame_idx in range(images.shape[0]):
latents.append(self.vae.encode(images[frame_idx:frame_idx+1])['latent_dist'].mean * 0.18215)
latents = torch.cat(latents)
return latents
@torch.no_grad()
def invert(
self,
image: torch.Tensor,
prompt,
num_inference_steps=20,
num_actual_inference_steps=10,
eta=0.0,
return_intermediates=False,
**kwargs):
"""
Adapted from: https://github.com/Yujun-Shi/DragDiffusion/blob/main/drag_pipeline.py#L440
invert a real image into noise map with determinisc DDIM inversion
"""
device = self._execution_device
batch_size = image.shape[0]
if isinstance(prompt, list):
if batch_size == 1:
image = image.expand(len(prompt), -1, -1, -1)
elif isinstance(prompt, str):
if batch_size > 1:
prompt = [prompt] * batch_size
# text embeddings
text_input = self.tokenizer(
prompt,
padding="max_length",
max_length=77,
return_tensors="pt"
)
text_embeddings = self.text_encoder(text_input.input_ids.to(device))[0]
print("input text embeddings :", text_embeddings.shape)
# define initial latents
latents = self.images2latents(image)
print("latents shape: ", latents.shape)
# interative sampling
self.scheduler.set_timesteps(num_inference_steps)
print("Valid timesteps: ", reversed(self.scheduler.timesteps))
latents_list = [latents]
pred_x0_list = [latents]
for i, t in enumerate(tqdm(reversed(self.scheduler.timesteps), desc="DDIM Inversion")):
if num_actual_inference_steps is not None and i >= num_actual_inference_steps:
continue
model_inputs = latents
# predict the noise
# NOTE: the u-net here is UNet3D, therefore the model_inputs need to be of shape (b c f h w)
model_inputs = rearrange(model_inputs, "f c h w -> 1 c f h w")
noise_pred = self.unet(model_inputs, t, encoder_hidden_states=text_embeddings).sample
noise_pred = rearrange(noise_pred, "b c f h w -> (b f) c h w")
# compute the previous noise sample x_t-1 -> x_t
latents, pred_x0 = self.next_step(noise_pred, t, latents)
latents_list.append(latents)
pred_x0_list.append(pred_x0)
if return_intermediates:
# return the intermediate laters during inversion
return latents, latents_list
return latents
def interpolate_latents(self, latents: torch.Tensor, interpolation_factor:int, device ):
if interpolation_factor < 2:
return latents
new_latents = torch.zeros(
(latents.shape[0],latents.shape[1],((latents.shape[2]-1) * interpolation_factor)+1, latents.shape[3],latents.shape[4]),
device=latents.device,
dtype=latents.dtype,
)
org_video_length = latents.shape[2]
rate = [i/interpolation_factor for i in range(interpolation_factor)][1:]
new_index = 0
v0 = None
v1 = None
for i0,i1 in zip( range( org_video_length ),range( org_video_length )[1:] ):
v0 = latents[:,:,i0,:,:]
v1 = latents[:,:,i1,:,:]
new_latents[:,:,new_index,:,:] = v0
new_index += 1
for f in rate:
v = get_tensor_interpolation_method()(v0.to(device=device),v1.to(device=device),f)
new_latents[:,:,new_index,:,:] = v.to(latents.device)
new_index += 1
new_latents[:,:,new_index,:,:] = v1
new_index += 1
return new_latents
def select_controlnet_res_samples(self, controlnet_res_samples_cache_dict, context, do_classifier_free_guidance, b, f):
_down_block_res_samples = []
_mid_block_res_sample = []
for i in np.concatenate(np.array(context)):
_down_block_res_samples.append(controlnet_res_samples_cache_dict[i][0])
_mid_block_res_sample.append(controlnet_res_samples_cache_dict[i][1])
down_block_res_samples = [[] for _ in range(len(controlnet_res_samples_cache_dict[i][0]))]
for res_t in _down_block_res_samples:
for i, res in enumerate(res_t):
down_block_res_samples[i].append(res)
down_block_res_samples = [torch.cat(res) for res in down_block_res_samples]
mid_block_res_sample = torch.cat(_mid_block_res_sample)
# reshape controlnet output to match the unet3d inputs
b = b // 2 if do_classifier_free_guidance else b
_down_block_res_samples = []
for sample in down_block_res_samples:
sample = rearrange(sample, '(b f) c h w -> b c f h w', b=b, f=f)
if do_classifier_free_guidance:
sample = sample.repeat(2, 1, 1, 1, 1)
_down_block_res_samples.append(sample)
down_block_res_samples = _down_block_res_samples
mid_block_res_sample = rearrange(mid_block_res_sample, '(b f) c h w -> b c f h w', b=b, f=f)
if do_classifier_free_guidance:
mid_block_res_sample = mid_block_res_sample.repeat(2, 1, 1, 1, 1)
return down_block_res_samples, mid_block_res_sample
@torch.no_grad()
def __call__(
self,
prompt: Union[str, List[str]],
video_length: Optional[int],
height: Optional[int] = None,
width: Optional[int] = None,
num_inference_steps: int = 50,
guidance_scale: float = 7.5,
negative_prompt: Optional[Union[str, List[str]]] = None,
num_videos_per_prompt: Optional[int] = 1,
eta: float = 0.0,
generator: Optional[Union[torch.Generator, List[torch.Generator]]] = None,
latents: Optional[torch.FloatTensor] = None,
output_type: Optional[str] = "tensor",
return_dict: bool = True,
callback: Optional[Callable[[int, int, torch.FloatTensor], None]] = None,
callback_steps: Optional[int] = 1,
controlnet_condition: list = None,
controlnet_conditioning_scale: float = 1.0,
context_frames: int = 16,
context_stride: int = 1,
context_overlap: int = 4,
context_batch_size: int = 1,
context_schedule: str = "uniform",
init_latents: Optional[torch.FloatTensor] = None,
num_actual_inference_steps: Optional[int] = None,
appearance_encoder = None,
reference_control_writer = None,
reference_control_reader = None,
source_image: str = None,
decoder_consistency = None,
audio: Optional[str] = None,
head_rotation_speeds: Optional[List[float]] = None,
**kwargs,
):
"""
New args:
- controlnet_condition : condition map (e.g., depth, canny, keypoints) for controlnet
- controlnet_conditioning_scale : conditioning scale for controlnet
- init_latents : initial latents to begin with (used along with invert())
- num_actual_inference_steps : number of actual inference steps (while total steps is num_inference_steps)
"""
controlnet = self.controlnet
feature_extractor = Wav2VecFeatureExtractor(model_name='facebook/wav2vec2-base-960h', device=self.device)
num_speed_buckets = 10
speed_embedding_dim = 64
speed_encoder = SpeedEncoder(num_speed_buckets, speed_embedding_dim)
if audio is not None:
audio_features = feature_extractor.extract_features_from_mp4(audio, m=2, n=2)
else:
audio_features = None
if head_rotation_speeds is not None:
speed_embeddings = speed_encoder(head_rotation_speeds)
else:
speed_embeddings = None
# Default height and width to unet
height = height or self.unet.config.sample_size * self.vae_scale_factor
width = width or self.unet.config.sample_size * self.vae_scale_factor
# Check inputs. Raise error if not correct
self.check_inputs(prompt, height, width, callback_steps)
# Define call parameters
# batch_size = 1 if isinstance(prompt, str) else len(prompt)
batch_size = 1
if latents is not None:
batch_size = latents.shape[0]
if isinstance(prompt, list):
batch_size = len(prompt)
device = self._execution_device
# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
# corresponds to doing no classifier free guidance.
do_classifier_free_guidance = guidance_scale > 1.0
# Encode input prompt
prompt = prompt if isinstance(prompt, list) else [prompt] * batch_size
if negative_prompt is not None:
negative_prompt = negative_prompt if isinstance(negative_prompt, list) else [negative_prompt] * batch_size
text_embeddings = self._encode_prompt(
prompt, device, num_videos_per_prompt, do_classifier_free_guidance, negative_prompt
)
text_embeddings = torch.cat([text_embeddings] * context_batch_size)
reference_control_writer = ReferenceAttentionControl(appearance_encoder, do_classifier_free_guidance=True, mode='write', batch_size=context_batch_size)
reference_control_reader = ReferenceAttentionControl(self.unet, do_classifier_free_guidance=True, mode='read', batch_size=context_batch_size)
is_dist_initialized = kwargs.get("dist", False)
rank = kwargs.get("rank", 0)
world_size = kwargs.get("world_size", 1)
# Prepare video
assert num_videos_per_prompt == 1 # FIXME: verify if num_videos_per_prompt > 1 works
assert batch_size == 1 # FIXME: verify if batch_size > 1 works
control = self.prepare_condition(
condition=controlnet_condition,
device=device,
dtype=controlnet.dtype,
num_videos_per_prompt=num_videos_per_prompt,
do_classifier_free_guidance=do_classifier_free_guidance,
)
controlnet_uncond_images, controlnet_cond_images = control.chunk(2)
# Prepare timesteps
self.scheduler.set_timesteps(num_inference_steps, device=device)
timesteps = self.scheduler.timesteps
# Prepare latent variables
if init_latents is not None:
latents = rearrange(init_latents, "(b f) c h w -> b c f h w", f=video_length)
else:
num_channels_latents = self.unet.in_channels
latents = self.prepare_latents(
batch_size * num_videos_per_prompt,
num_channels_latents,
video_length,
height,
width,
text_embeddings.dtype,
device,
generator,
latents,
)
latents_dtype = latents.dtype
# Prepare extra step kwargs.
extra_step_kwargs = self.prepare_extra_step_kwargs(generator, eta)
# Prepare text embeddings for controlnet
controlnet_text_embeddings = text_embeddings.repeat_interleave(video_length, 0)
_, controlnet_text_embeddings_c = controlnet_text_embeddings.chunk(2)
controlnet_res_samples_cache_dict = {i:None for i in range(video_length)}
# For img2img setting
if num_actual_inference_steps is None:
num_actual_inference_steps = num_inference_steps
if isinstance(source_image, str):
ref_image_latents = self.images2latents(np.array(Image.open(source_image).resize((width, height)))[None, :], latents_dtype).cuda()
elif isinstance(source_image, np.ndarray):
ref_image_latents = self.images2latents(source_image[None, :], latents_dtype).cuda()
context_scheduler = get_context_scheduler(context_schedule)
# Denoising loop
for i, t in tqdm(enumerate(timesteps), total=len(timesteps), disable=(rank!=0)):
if num_actual_inference_steps is not None and i < num_inference_steps - num_actual_inference_steps:
continue
noise_pred = torch.zeros(
(latents.shape[0] * (2 if do_classifier_free_guidance else 1), *latents.shape[1:]),
device=latents.device,
dtype=latents.dtype,
)
counter = torch.zeros(
(1, 1, latents.shape[2], 1, 1), device=latents.device, dtype=latents.dtype
)
appearance_encoder(
ref_image_latents.repeat(context_batch_size * (2 if do_classifier_free_guidance else 1), 1, 1, 1),
t,
encoder_hidden_states=text_embeddings,
return_dict=False,
)
context_queue = list(context_scheduler(
0, num_inference_steps, latents.shape[2], context_frames, context_stride, 0
))
num_context_batches = math.ceil(len(context_queue) / context_batch_size)
for i in range(num_context_batches):
context = context_queue[i*context_batch_size: (i+1)*context_batch_size]
# expand the latents if we are doing classifier free guidance
controlnet_latent_input = (
torch.cat([latents[:, :, c] for c in context])
.to(device)
)
controlnet_latent_input = self.scheduler.scale_model_input(controlnet_latent_input, t)
# prepare inputs for controlnet
b, c, f, h, w = controlnet_latent_input.shape
controlnet_latent_input = rearrange(controlnet_latent_input, "b c f h w -> (b f) c h w")
# controlnet inference
down_block_res_samples, mid_block_res_sample = self.controlnet(
controlnet_latent_input,
t,
encoder_hidden_states=torch.cat([controlnet_text_embeddings_c[c] for c in context]),
controlnet_cond=torch.cat([controlnet_cond_images[c] for c in context]),
conditioning_scale=controlnet_conditioning_scale,
return_dict=False,
)
for j, k in enumerate(np.concatenate(np.array(context))):
controlnet_res_samples_cache_dict[k] = ([sample[j:j+1] for sample in down_block_res_samples], mid_block_res_sample[j:j+1])
context_queue = list(context_scheduler(
0, num_inference_steps, latents.shape[2], context_frames, context_stride, context_overlap
))
num_context_batches = math.ceil(len(context_queue) / context_batch_size)
global_context = []
for i in range(num_context_batches):
global_context.append(context_queue[i*context_batch_size: (i+1)*context_batch_size])
for context in global_context[rank::world_size]:
# expand the latents if we are doing classifier free guidance
latent_model_input = (
torch.cat([latents[:, :, c] for c in context])
.to(device)
.repeat(2 if do_classifier_free_guidance else 1, 1, 1, 1, 1)
)
latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)
b, c, f, h, w = latent_model_input.shape
down_block_res_samples, mid_block_res_sample = self.select_controlnet_res_samples(
controlnet_res_samples_cache_dict,
context,
do_classifier_free_guidance,
b, f
)
reference_control_reader.update(reference_control_writer)
# predict the noise residual
pred = self.unet(
latent_model_input,
t,
encoder_hidden_states=text_embeddings[:b],
down_block_additional_residuals=down_block_res_samples,
mid_block_additional_residual=mid_block_res_sample,
audio_features=audio_features,
speed_embeddings=speed_embeddings,
return_dict=False,
)[0]
reference_control_reader.clear()
pred_uc, pred_c = pred.chunk(2)
pred = torch.cat([pred_uc.unsqueeze(0), pred_c.unsqueeze(0)])
for j, c in enumerate(context):
noise_pred[:, :, c] = noise_pred[:, :, c] + pred[:, j]
counter[:, :, c] = counter[:, :, c] + 1
if is_dist_initialized:
noise_pred_gathered = [torch.zeros_like(noise_pred) for _ in range(world_size)]
if rank == 0:
dist.gather(tensor=noise_pred, gather_list=noise_pred_gathered, dst=0)
else:
dist.gather(tensor=noise_pred, gather_list=[], dst=0)
dist.barrier()
if rank == 0:
for k in range(1, world_size):
for context in global_context[k::world_size]:
for j, c in enumerate(context):
noise_pred[:, :, c] = noise_pred[:, :, c] + noise_pred_gathered[k][:, :, c]
counter[:, :, c] = counter[:, :, c] + 1
# perform guidance
if do_classifier_free_guidance:
noise_pred_uncond, noise_pred_text = (noise_pred / counter).chunk(2)
noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
# compute the previous noisy sample x_t -> x_t-1
latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs).prev_sample
if is_dist_initialized:
dist.broadcast(latents, 0)
dist.barrier()
reference_control_writer.clear()
interpolation_factor = 1
latents = self.interpolate_latents(latents, interpolation_factor, device)
# Post-processing
video = self.decode_latents(latents, rank, decoder_consistency=decoder_consistency)
if is_dist_initialized:
dist.barrier()
# Convert to tensor
if output_type == "tensor":
video = torch.from_numpy(video)
if not return_dict:
return video
return AnimationPipelineOutput(videos=video)
def seed_everything(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed % (2**32))
random.seed(seed)
def main(cfg):
accelerator = Accelerator(log_with="mlflow", project_dir="./mlruns")
logging.basicConfig(level=logging.INFO)
if cfg.seed is not None:
seed_everything(cfg.seed)
save_dir = f"{cfg.output_dir}/{cfg.exp_name}"
os.makedirs(save_dir, exist_ok=True)
vae = AutoencoderKL.from_pretrained(cfg.vae_model_path).to("cuda")
image_enc = CLIPVisionModelWithProjection.from_pretrained(cfg.image_encoder_path).to("cuda")
# Load the YAML configuration file
with open('./configs/config.yaml', 'r') as file:
config = yaml.safe_load(file)
# Access the reference_unet_config based on args.v2
if args.v2:
unet_config = config['reference_unet_config']['v2']
denoise_unet_config = config['denoising_unet_config']['v2']
else:
unet_config = config['reference_unet_config']['default']
denoise_unet_config = config['denoising_unet_config']['default']
emo_config = {
"reference_unet_config": unet_config,
"denoising_unet_config": denoise_unet_config,
"num_speed_buckets": cfg.num_speed_buckets,
"speed_embedding_dim": cfg.speed_embedding_dim,
}
emo_model = EMOModel(vae, image_enc, emo_config).to("cuda")
optimizer = torch.optim.AdamW(emo_model.parameters(), lr=cfg.solver.learning_rate)
# Load pretrained AnimateDiff model
animated_diff_model = AnimatedDiff.from_pretrained("path/to/animated_diff_weights")
# Assign AnimateDiff weights to the backbone network (UNet3DConditionModel)
emo_model.denoising_unet.load_state_dict(animated_diff_model.unet.state_dict(), strict=False)
# Assign AnimateDiff weights to the temporal modules
emo_model.denoising_unet.temporal_module.load_state_dict(animated_diff_model.temporal_module.state_dict(), strict=False)
# Assign AnimateDiff weights to relevant attention layers
emo_model.audio_attention_layers.load_state_dict(animated_diff_model.attention_layers.state_dict(), strict=False)
# Accelerator preparation
emo_model, optimizer = accelerator.prepare(emo_model, optimizer)
# Initialize EMOAnimationPipeline
scheduler = DDIMScheduler(beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear", num_train_timesteps=1000)
emo_pipeline = EMOAnimationPipeline(
vae=vae,
image_encoder=image_enc,
reference_unet=emo_model.reference_unet,
denoising_unet=emo_model.denoising_unet,
face_locator=emo_model.face_locator,
speed_encoder=emo_model.speed_encoder,
scheduler=scheduler,
).to("cuda")
# Training loop (simplified)
for epoch in range(cfg.num_epochs):
for batch in cfg.train_dataloader:
# Simplified training step
optimizer.zero_grad()
output = emo_model(batch['noisy_latents'], batch['timesteps'], batch['ref_image'], batch['motion_frames'], batch['audio_features'], batch['head_rotation_speeds'])
loss = F.mse_loss(output, batch['target'])
accelerator.backward(loss)
optimizer.step()
# Calculate signal-to-noise ratio using EMOAnimationPipeline
with torch.no_grad():
generated_video = emo_pipeline(
prompt=batch['prompt'],
source_image=batch['ref_image'],
audio=batch['audio_path'],
head_rotation_speeds=batch['head_rotation_speeds'],
num_inference_steps=50,
output_type="numpy",
).videos
# Calculate signal-to-noise ratio
signal = np.mean(generated_video)
noise = np.std(generated_video)
snr = signal / noise
# Log the signal-to-noise ratio
accelerator.log({"snr": snr}, step=epoch)
accelerator.end_training()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", type=str, default="./configs/training/stage1.yaml")
args = parser.parse_args()
config = OmegaConf.load(args.config) # assuming YAML configuration
main(config)