-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmotion_discovery.py
More file actions
1520 lines (1212 loc) · 54.5 KB
/
motion_discovery.py
File metadata and controls
1520 lines (1212 loc) · 54.5 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
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
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import gradio as gr
import gc
import numpy as np
import cv2
from PIL import Image, ImageFilter
import uuid
from scipy.interpolate import interp1d, PchipInterpolator
# import torchvision
from flowgen.models.controlnet import ControlNetModel
from scripts.utils import *
import os
from omegaconf import OmegaConf
import torch
import diffusers
from diffusers import AutoencoderKL, DDIMScheduler
from tqdm import tqdm
from transformers import CLIPTextModel, CLIPTokenizer
from flowgen.models.unet3d import UNet3DConditionModel as UNet3DConditionModelFlow
from animation.models.forward_unet import UNet3DConditionModel
from flowgen.pipelines.pipeline_flow_gen_ours import FlowGenPipeline
from animation.pipelines.pipeline_animation import AnimationPipeline
from animation.utils.util import save_videos_grid, save_flow_grid_pillow_arrow
from animation.utils.util import load_weights
from diffusers.utils.import_utils import is_xformers_available
from einops import rearrange, repeat
import csv, pdb, glob
import math
from pathlib import Path
import numpy as np
import torch.nn as nn
import matplotlib.pyplot as plt
import shutil
from datetime import datetime
# output_dir = "outputs"
# ensure_dirname(output_dir)
class ForwardWarp(nn.Module):
"""docstring for WarpLayer"""
def __init__(
self,
):
super(ForwardWarp, self).__init__()
def forward(self, img, flo):
"""
-img: image (N, C, H, W)
-flo: optical flow (N, 2, H, W)
elements of flo is in [0, H] and [0, W] for dx, dy
"""
# (x1, y1) (x1, y2)
# +---------------+
# | |
# | o(x, y) |
# | |
# | |
# | |
# | |
# +---------------+
# (x2, y1) (x2, y2)
N, C, _, _ = img.size()
# translate start-point optical flow to end-point optical flow
y = flo[:, 0:1:, :]
x = flo[:, 1:2, :, :]
x = x.repeat(1, C, 1, 1)
y = y.repeat(1, C, 1, 1)
# Four point of square (x1, y1), (x1, y2), (x2, y1), (y2, y2)
x1 = torch.floor(x)
x2 = x1 + 1
y1 = torch.floor(y)
y2 = y1 + 1
# firstly, get gaussian weights
w11, w12, w21, w22 = self.get_gaussian_weights(x, y, x1, x2, y1, y2)
# secondly, sample each weighted corner
img11, o11 = self.sample_one(img, x1, y1, w11)
img12, o12 = self.sample_one(img, x1, y2, w12)
img21, o21 = self.sample_one(img, x2, y1, w21)
img22, o22 = self.sample_one(img, x2, y2, w22)
imgw = img11 + img12 + img21 + img22
o = o11 + o12 + o21 + o22
return imgw, o
def get_gaussian_weights(self, x, y, x1, x2, y1, y2):
w11 = torch.exp(-((x - x1) ** 2 + (y - y1) ** 2))
w12 = torch.exp(-((x - x1) ** 2 + (y - y2) ** 2))
w21 = torch.exp(-((x - x2) ** 2 + (y - y1) ** 2))
w22 = torch.exp(-((x - x2) ** 2 + (y - y2) ** 2))
return w11, w12, w21, w22
def sample_one(self, img, shiftx, shifty, weight):
"""
Input:
-img (N, C, H, W)
-shiftx, shifty (N, c, H, W)
"""
N, C, H, W = img.size()
# flatten all (all restored as Tensors)
flat_shiftx = shiftx.view(-1)
flat_shifty = shifty.view(-1)
flat_basex = (
torch.arange(0, H, requires_grad=False)
.view(-1, 1)[None, None]
.cuda()
.long()
.repeat(N, C, 1, W)
.view(-1)
)
flat_basey = (
torch.arange(0, W, requires_grad=False)
.view(1, -1)[None, None]
.cuda()
.long()
.repeat(N, C, H, 1)
.view(-1)
)
flat_weight = weight.view(-1)
flat_img = img.view(-1)
# The corresponding positions in I1
idxn = (
torch.arange(0, N, requires_grad=False)
.view(N, 1, 1, 1)
.long()
.cuda()
.repeat(1, C, H, W)
.view(-1)
)
idxc = (
torch.arange(0, C, requires_grad=False)
.view(1, C, 1, 1)
.long()
.cuda()
.repeat(N, 1, H, W)
.view(-1)
)
# ttype = flat_basex.type()
idxx = flat_shiftx.long() + flat_basex
idxy = flat_shifty.long() + flat_basey
# recording the inside part the shifted
mask = idxx.ge(0) & idxx.lt(H) & idxy.ge(0) & idxy.lt(W)
# Mask off points out of boundaries
ids = idxn * C * H * W + idxc * H * W + idxx * W + idxy
ids_mask = torch.masked_select(ids, mask).clone().cuda()
# (zero part - gt) -> difference
# difference back propagate -> No influence! Whether we do need mask? mask?
# put (add) them together
# Note here! accmulate fla must be true for proper bp
img_warp = torch.zeros(
[
N * C * H * W,
]
).cuda()
img_warp.put_(
ids_mask, torch.masked_select(flat_img * flat_weight, mask), accumulate=True
)
one_warp = torch.zeros(
[
N * C * H * W,
]
).cuda()
one_warp.put_(ids_mask, torch.masked_select(flat_weight, mask), accumulate=True)
return img_warp.view(N, C, H, W), one_warp.view(N, C, H, W)
def interpolate_trajectory(points, n_points):
x = [point[0] for point in points]
y = [point[1] for point in points]
t = np.linspace(0, 1, len(points))
# fx = interp1d(t, x, kind='cubic')
# fy = interp1d(t, y, kind='cubic')
fx = PchipInterpolator(t, x)
fy = PchipInterpolator(t, y)
new_t = np.linspace(0, 1, n_points)
new_x = fx(new_t)
new_y = fy(new_t)
new_points = list(zip(new_x, new_y))
return new_points
def interpolate_trajectory_linear(points, n_points):
x = [point[0] for point in points]
y = [point[1] for point in points]
t = np.linspace(0, 1, len(points))
fx = interp1d(t, x, kind='linear')
fy = interp1d(t, y, kind='linear')
new_t = np.linspace(0, 1, n_points)
new_x = fx(new_t)
new_y = fy(new_t)
new_points = list(zip(new_x, new_y))
return new_points
def visualize_drag_v2(background_image_path, brush_mask, splited_tracks, width, height):
trajectory_maps = []
background_image = Image.open(background_image_path).convert("RGBA")
background_image = background_image.resize((width, height))
w, h = background_image.size
# Create a half-transparent background
transparent_background = np.array(background_image)
transparent_background[:, :, -1] = 128
# Create a purple overlay layer
purple_layer = np.zeros((h, w, 4), dtype=np.uint8)
purple_layer[:, :, :3] = [128, 0, 128] # Purple color
purple_alpha = np.where(brush_mask < 0.5, 64, 0) # Alpha values based on brush_mask
purple_layer[:, :, 3] = purple_alpha
# Convert to PIL image for alpha_composite
purple_layer = Image.fromarray(purple_layer)
transparent_background = Image.fromarray(transparent_background)
# Blend the purple layer with the background
transparent_background = Image.alpha_composite(transparent_background, purple_layer)
# Create a transparent layer with the same size as the background image
transparent_layer = np.zeros((h, w, 4))
for splited_track in splited_tracks:
if len(splited_track) > 1:
splited_track = interpolate_trajectory(splited_track, 16)
splited_track = splited_track[:16]
for i in range(len(splited_track) - 1):
start_point = (int(splited_track[i][0]), int(splited_track[i][1]))
end_point = (int(splited_track[i + 1][0]), int(splited_track[i + 1][1]))
vx = end_point[0] - start_point[0]
vy = end_point[1] - start_point[1]
arrow_length = np.sqrt(vx**2 + vy**2)
if i == len(splited_track) - 2:
cv2.arrowedLine(
transparent_layer,
start_point,
end_point,
(255, 0, 0, 192),
2,
tipLength=8 / arrow_length,
)
else:
cv2.line(
transparent_layer, start_point, end_point, (255, 0, 0, 192), 2
)
else:
cv2.circle(
transparent_layer,
(int(splited_track[0][0]), int(splited_track[0][1])),
5,
(255, 0, 0, 192),
-1,
)
transparent_layer = Image.fromarray(transparent_layer.astype(np.uint8))
trajectory_map = Image.alpha_composite(transparent_background, transparent_layer)
trajectory_maps.append(trajectory_map)
return trajectory_maps, transparent_layer
# Main Class for motion stuff
# In[2]:
def accumulate_warped_mask_vectorized(flow: torch.Tensor, brush_mask: torch.Tensor):
"""
Accumulate the brush_mask across frames based on the optical flow.
Automatically saves mask visualizations during the warping and accumulation steps.
Parameters:
- flow: Tensor of shape (b, c, f, h, w), where c = 2 (optical flow for u, v components).
- brush_mask: Tensor of shape (1, 1, 1, h, w), representing the initial mask.
Returns:
- Accumulated brush_mask over all frames (shape: 1, 1, 1, h, w).
"""
b, c, f, h, w = flow.shape # Extract dimensions
assert c == 2, "Flow tensor must have 2 channels (u, v) for horizontal and vertical components"
brush_mask = 1 - brush_mask
flow_copy = flow.clone()
flow_copy[:, 1, :, :, :] = -flow[:, 1, :, :, :]
flow_copy[:, 0, :, :, :] = -flow[:, 0, :, :, :]
# Reshape the flow for parallel processing
flow_copy = flow_copy.permute(0, 2, 3, 4, 1).reshape(b * f, h, w, 2) # Shape (b*f, h, w, 2)
# Generate a meshgrid for pixel coordinates (this will be the same for all frames)
grid_y, grid_x = torch.meshgrid(torch.arange(h), torch.arange(w), indexing='ij')
grid = torch.stack((grid_x, grid_y), dim=-1).float().to(flow.device) # Shape (h, w, 2)
# Expand the grid for all frames
grid = grid.unsqueeze(0).expand(b * f, h, w, 2) # Shape (b*f, h, w, 2)
# Apply the flow vectors to the grid coordinates (i.e., warp the grid for all frames)
warped_grid = grid + flow_copy # Shape (b*f, h, w, 2)
# Normalize the grid for sampling with F.grid_sample
warped_grid[..., 0] = (warped_grid[..., 0] / (w - 1)) * 2 - 1 # Normalize x
warped_grid[..., 1] = (warped_grid[..., 1] / (h - 1)) * 2 - 1 # Normalize y
# Expand brush_mask to apply across all frames
brush_mask_expanded = brush_mask[0].expand(b * f, 1, h, w).to(flow.device) # Shape (b*f, 1, h, w)
warped_grid = warped_grid.to(flow.device)
print(brush_mask_expanded.squeeze(1).shape)
# Warp the brush_mask using grid_sample for all frames in parallel
warped_brush_masks = F.grid_sample(brush_mask_expanded,
warped_grid,
mode='bilinear',
padding_mode='border',
align_corners=True) # Shape (b*f, 1, h, w)
# Save warped masks for each frame (in parallel processing, consider showing a representative frame)
for frame_idx in range(f):
save_mask_visualization(warped_brush_masks[frame_idx], step="warping", frame_idx=frame_idx)
# Reshape back to (b, f, 1, h, w)
warped_brush_masks = warped_brush_masks.view(b, f, 1, h, w)
# Accumulate the warped masks by taking the union (max operation) over all frames
accumulated_mask = torch.max(warped_brush_masks, dim=1, keepdim=True)[0] # Shape (b, 1, 1, h, w)
# Save the accumulated mask
save_mask_visualization(accumulated_mask.squeeze(0), step="accumulation")
accumulated_mask = torch.max(accumulated_mask, brush_mask.to(accumulated_mask.device))
save_mask_visualization(accumulated_mask.squeeze(0), step="accumulation_plus")
return accumulated_mask
def sample_flow_points(flow_map, mask, num_points=100, threshold=0.01):
"""
Sample flow points from areas where the mask is active.
Args:
flow_map (np.array): Flow map with shape (C, F, H, W)
mask (np.array): Binary mask with shape (H, W)
num_points (int): Number of points to sample
threshold (float): Minimum magnitude of flow to consider
Returns:
List of sampled points
"""
C, F, H, W = flow_map.shape
# Compute flow magnitude
flow_magnitude = np.linalg.norm(flow_map, axis=0)
# Find the frame with the highest total flow magnitude
max_flow_frame = np.argmax(flow_magnitude.sum(axis=(1, 2)))
# Extract the flow for the frame with maximum flow
max_frame_flow = flow_map[:, max_flow_frame, :, :]
max_frame_magnitude = flow_magnitude[max_flow_frame]
# Apply mask and threshold
valid_points = (max_frame_magnitude > threshold) & (mask > 0)
# Find coordinates of valid points
valid_coords = np.column_stack(np.where(valid_points))
# Sample points
if len(valid_coords) > num_points:
sampled_indices = np.random.choice(len(valid_coords), num_points, replace=False)
sampled_coords = valid_coords[sampled_indices]
else:
sampled_coords = valid_coords
# Create list of sampled points with their flow values
sampled_points = []
for y, x in sampled_coords:
flow = max_frame_flow[:, y, x]
start_point = (x, y)
end_point = (x + flow[0], y + flow[1])
sampled_points.append((start_point, end_point))
return sampled_points
def sample_flow_points_traj(flow_map, mask, num_points=100, threshold=0.01, frames_to_sample=8):
"""
Sample flow points from specified intermediate frames where the mask is active.
Args:
flow_map (np.array): Flow map with shape (C, F, H, W)
mask (np.array): Binary mask with shape (H, W)
num_points (int): Number of points to sample
threshold (float): Minimum magnitude of flow to consider
frames_to_sample (int): Number of intermediate frames to sample from
Returns:
List of dictionaries containing sampled points and their corresponding frames
"""
C, F, H, W = flow_map.shape
# Compute flow magnitude for all frames
flow_magnitude = np.linalg.norm(flow_map, axis=0) # Shape: (F, H, W)
# Determine frames to sample from
if frames_to_sample >= F:
sampled_frames = np.arange(F)
else:
# Evenly spaced frames excluding the first frame (since flow at frame 0 is zero)
sampled_frames = np.linspace(1, F - 1, frames_to_sample, dtype=int)
# Prepare a list to collect all valid points across sampled frames
all_valid_coords = []
for frame_idx in sampled_frames:
# Extract flow magnitude for the current frame
frame_magnitude = flow_magnitude[frame_idx] # Shape: (H, W)
# Apply mask and threshold
valid_points = (frame_magnitude > threshold) & (mask > 0)
# Find coordinates of valid points
valid_coords = np.column_stack(np.where(valid_points))
# Append frame index to valid coordinates
for coord in valid_coords:
all_valid_coords.append((frame_idx, coord[0], coord[1]))
# Sample points
if len(all_valid_coords) > num_points:
sampled_indices = np.random.choice(len(all_valid_coords), num_points, replace=False)
sampled_coords = [all_valid_coords[idx] for idx in sampled_indices]
else:
sampled_coords = all_valid_coords
# Create list of sampled points with their flow values
sampled_points = []
for frame_idx, y, x in sampled_coords:
flow = flow_map[:, frame_idx, y, x] # Shape: (C,)
start_point = (x, y)
sampled_points.append({
'frame_idx': frame_idx,
'start_point': start_point,
'flow': flow
})
return sampled_points
def generate_control_tensor_sample(dense_flow_map, mask, model_length, flow_unit_id, vae_flow, control_ratio=1.0, threshold=0.1, frames_to_sample=5):
"""
Generate a control tensor from a dense flow map for use in generation algorithms.
Args:
dense_flow_map (torch.Tensor): Dense flow map with shape (1, C, F, H, W)
mask (np.array): Binary mask with shape (H, W)
model_length (int): Number of frames in the output sparse flow map
flow_unit_id (int): Size of the area around each point where flow is applied
vae_flow: VAE model for encoding flow
control_ratio (float): Ratio to determine the number of points to sample
threshold (float): Minimum magnitude of flow to consider
frames_to_sample (int): Number of intermediate frames to sample from
Returns:
torch.Tensor: Control tensor for use in generation algorithms
"""
num_points = int(control_ratio * np.sum(1 - mask))
print('Number of sample points:', num_points)
# Convert dense flow map to numpy array for processing
dense_flow_map_np = dense_flow_map.cpu().numpy()[0] # Shape: (C, F, H, W)
C, F, H, W = dense_flow_map_np.shape
# Sample flow points from intermediate frames
sampled_points = sample_flow_points_traj(dense_flow_map_np, mask, num_points, threshold, frames_to_sample)
# Initialize output tensors
input_drag = torch.zeros(1, model_length - 1, H, W, 2).to(dense_flow_map.device) # Added batch dimension
mask_drag = torch.zeros(1, model_length - 1, H, W, 1).to(dense_flow_map.device) # Added batch dimension
for point in sampled_points:
frame_idx = point['frame_idx']
start_point = point['start_point']
flow = point['flow']
# Since flows are already cumulative, we can directly use the flow at frame_idx
cumulative_flow = flow # Shape: (2,)
end_point = (start_point[0] + cumulative_flow[0], start_point[1] + cumulative_flow[1])
# Interpolate trajectory from start_point to end_point over model_length frames
trajectory_points = [start_point, end_point]
interpolated_track = interpolate_trajectory_linear(trajectory_points, model_length)
for i in range(model_length - 1):
# Compute cumulative flow from start_point to the point at frame i + 1
next_point = interpolated_track[i + 1]
flow_vector = (next_point[0] - start_point[0], next_point[1] - start_point[1])
# Calculate flow area
y_start = max(int(start_point[1]) - flow_unit_id, 0)
y_end = min(int(start_point[1]) + flow_unit_id, H)
x_start = max(int(start_point[0]) - flow_unit_id, 0)
x_end = min(int(start_point[0]) + flow_unit_id, W)
# Apply cumulative flow to the area
input_drag[0, i, y_start:y_end, x_start:x_end, 0] = flow_vector[0]
input_drag[0, i, y_start:y_end, x_start:x_end, 1] = flow_vector[1]
mask_drag[0, i, y_start:y_end, x_start:x_end, 0] = 1 # Added channel dimension
input_drag[..., 0] /= W
input_drag[..., 1] /= H
input_drag = (input_drag + 1) / 2 # Normalize to [0, 1]
# Process drag and mask to create control tensor
with torch.no_grad():
b, l, h, w, c = input_drag.size()
drag = rearrange(input_drag, "b l h w c -> b c l h w")
mask = rearrange(mask_drag, "b l h w c -> b c l h w")
sparse_flow = drag.to('cuda')
sparse_mask = mask.to('cuda')
# Adjust sparse flow values
sparse_flow = (sparse_flow - 1/2) + 1/2 # Scale back to [-1, 1]
# Encode flow using VAE
flow_mask_latent = rearrange(
vae_flow.encode(
rearrange(sparse_flow, "b c f h w -> (b f) c h w")
).latent_dist.sample(),
"(b f) c h w -> b c f h w",
f=l,
)
print('Flow mask latent shape:', flow_mask_latent.shape)
return flow_mask_latent
# def sample_flow_points(flow_map, num_points=1000, threshold=50.0):
# """
# Sample a set of start and end points from the frame with the highest magnitude of flow.
# Args:
# flow_map (torch.Tensor): Flow map with shape (C, F, H, W)
# num_points (int): Number of points to sample
# threshold (float): Minimum magnitude of flow to consider
# Returns:
# List of tuples, where each tuple contains (start_point, end_point)
# """
# C, F, H, W = flow_map.shape
# assert C == 2, "Flow map should have 2 channels (dx, dy)"
# # Compute flow magnitude for each frame
# flow_magnitude = torch.norm(flow_map, dim=0)
# # Find the frame with the highest total flow magnitude
# max_flow_frame = torch.argmax(flow_magnitude.sum(dim=(1, 2)))
# # Extract the flow for the frame with maximum flow
# max_frame_flow = flow_map[:, max_flow_frame, :, :]
# max_frame_magnitude = flow_magnitude[max_flow_frame]
# # Find points with significant flow
# significant_flow = (max_frame_magnitude > threshold).nonzero(as_tuple=False)
# # Randomly sample from significant flow points
# if len(significant_flow) > num_points:
# indices = np.random.choice(len(significant_flow), num_points, replace=False)
# sampled_points = significant_flow[indices]
# else:
# sampled_points = significant_flow
# # Generate start and end points
# tracks = []
# for point in sampled_points:
# y, x = point.tolist()
# dx = max_frame_flow[0, y, x].item()
# dy = max_frame_flow[1, y, x].item()
# start_point = (x, y)
# end_point = (x + dx, y + dy)
# tracks.append((start_point, end_point))
# return tracks
def generate_control_tensor(dense_flow_map, mask, model_length, flow_unit_id, vae_flow, control_ratio=1.0, threshold=0.1):
"""
Generate a control tensor from a dense flow map for use in generation algorithms.
Args:
dense_flow_map (torch.Tensor): Dense flow map with shape (C, F, H, W)
model_length (int): Number of frames in the output sparse flow map
flow_unit_id (int): Size of the area around each point where flow is applied
vae_flow: VAE model for encoding flow
num_points (int): Number of points to sample
threshold (float): Minimum magnitude of flow to consider
Returns:
torch.Tensor: Control tensor for use in generation algorithms
"""
num_points = int(control_ratio*np.sum(1 - mask))
print('num sample points')
print(num_points)
dense_flow_map = dense_flow_map[0]
C, F, H, W = dense_flow_map.shape
# Sample flow points
sampled_tracks = sample_flow_points(dense_flow_map, mask, num_points, threshold)
# Initialize output tensors
input_drag = torch.zeros(1, model_length - 1, H, W, 2).to('cuda') # Added batch dimension
mask_drag = torch.zeros(1, model_length - 1, H, W, 1).to('cuda') # Added batch dimension
for track in sampled_tracks:
start_point, end_point = track
# Handle stationary points
if start_point == end_point:
end_point = (start_point[0] + 1, start_point[1] + 1)
# Interpolate the track
interpolated_track = interpolate_trajectory([start_point, end_point], model_length)
# Ensure the track has the correct length
if len(interpolated_track) < model_length:
interpolated_track += [interpolated_track[-1]] * (model_length - len(interpolated_track))
for i in range(model_length - 1):
start_point = interpolated_track[0]
end_point = interpolated_track[i + 1]
# Calculate flow area
y_start = max(int(start_point[1]) - flow_unit_id, 0)
y_end = min(int(start_point[1]) + flow_unit_id, H)
x_start = max(int(start_point[0]) - flow_unit_id, 0)
x_end = min(int(start_point[0]) + flow_unit_id, W)
# Apply flow to the area
input_drag[0, i, y_start:y_end, x_start:x_end, 0] = end_point[0] - start_point[0]
input_drag[0, i, y_start:y_end, x_start:x_end, 1] = end_point[1] - start_point[1]
mask_drag[0, i, y_start:y_end, x_start:x_end] = 1
#input_drag = dense_flow_map.unsqueeze(0).permute(0, 2, 3, 4, 1).to('cuda')
visualization_tensor = input_drag.squeeze(0).permute(0, 3, 1, 2)
test_flow_list = []
test_flow_list.append(visualization_tensor)
save_flow_grid_pillow_arrow(test_flow_list, 'debug_vis/test_control_flow.gif')
# Normalize flow values
input_drag[..., 0] /= W
input_drag[..., 1] /= H
input_drag = (input_drag + 1) / 2
# Process drag and mask to create control tensor
with torch.no_grad():
b, l, h, w, c = input_drag.size()
drag = rearrange(input_drag, "b l h w c -> b c l h w")
mask = rearrange(mask_drag, "b l h w c -> b c l h w")
sparse_flow = drag
sparse_mask = mask
sparse_flow = (sparse_flow - 1 / 2) + 1 / 2
flow_mask_latent = rearrange(
vae_flow.encode(
rearrange(sparse_flow, "b c f h w -> (b f) c h w")
).latent_dist.sample(),
"(b f) c h w -> b c f h w",
f=l,
)
print('flow mask latent shape')
print(flow_mask_latent.shape)
#sparse_mask = sparse_mask.to(device='cuda', dtype = torch.float32)
# sparse_mask = torch.nn.functional.interpolate(sparse_mask, scale_factor=(1, 1/8, 1/8))
# control = torch.cat([flow_mask_latent, sparse_mask], dim=1)
# control = control.to(device='cuda', dtype=torch.float32)
return flow_mask_latent
def save_mask_visualization(mask: torch.Tensor, step: str, frame_idx: int = None):
"""
Automatically saves the mask visualization at each step of the process.
Parameters:
- mask: Tensor of shape (1, h, w) or (b, 1, 1, h, w), representing the mask.
- step: String to indicate the step (e.g., 'warping', 'accumulation').
- frame_idx: The current frame index if it's during the warping step.
"""
path = "mask_visualizations"
os.makedirs(path, exist_ok=True)
# Move the mask to CPU and convert to NumPy for visualization
mask = mask.squeeze().detach().cpu().numpy()
# If a frame index is provided, include it in the file name
if frame_idx is not None:
filename = f"{step}_frame_{frame_idx}.png"
else:
filename = f"{step}.png"
# Plot and save the mask
plt.imshow(mask, cmap='gray')
plt.colorbar()
plt.title(f"{step} {'Frame ' + str(frame_idx) if frame_idx is not None else ''}")
plt.savefig(os.path.join(path, filename))
plt.close()
print(f"Saved mask at {step} step to {filename}")
class Drag:
def __init__(
self,
device,
pretrained_model_path,
inference_config,
height,
width,
model_length,
):
self.device = device
self.num_gen = 0
self.inference_config = OmegaConf.load(inference_config)
inference_config = self.inference_config
### >>> create validation pipeline >>> ###
print("start loading")
tokenizer = CLIPTokenizer.from_pretrained(
pretrained_model_path, subfolder="tokenizer"
)
text_encoder = CLIPTextModel.from_pretrained(
pretrained_model_path, subfolder="text_encoder"
)
# unet = UNet3DConditionModel.from_pretrained_2d(args.pretrained_model_path, subfolder="unet", unet_additional_kwargs=OmegaConf.to_container(inference_config.unet_additional_kwargs))
unet = UNet3DConditionModelFlow.from_config_2d(
pretrained_model_path,
subfolder="unet",
unet_additional_kwargs=OmegaConf.to_container(
inference_config.unet_additional_kwargs
),
)
vae_img = AutoencoderKL.from_pretrained(
"models/stage2/StableDiffusion", subfolder="vae"
)
import json
with open("./models/stage1/StableDiffusion-FlowGen/vae/config.json", "r") as f:
vae_config = json.load(f)
vae = AutoencoderKL.from_config(vae_config)
vae_pretrained_path = (
"models/stage1/StableDiffusion-FlowGen/vae_flow/diffusion_pytorch_model.bin"
)
print("[Load vae weights from {}]".format(vae_pretrained_path))
processed_ckpt = {}
weight = torch.load(vae_pretrained_path, map_location="cpu")
vae.load_state_dict(weight, strict=True)
controlnet = ControlNetModel.from_unet(unet)
unet.controlnet = controlnet
unet.control_scale = 1.0
unet_pretrained_path = (
"models/stage1/StableDiffusion-FlowGen/unet/diffusion_pytorch_model.bin"
)
print("[Load unet weights from {}]".format(unet_pretrained_path))
weight = torch.load(unet_pretrained_path, map_location="cpu")
m, u = unet.load_state_dict(weight, strict=False)
controlnet_pretrained_path = (
"models/stage1/StableDiffusion-FlowGen/controlnet/controlnet.bin"
)
print("[Load controlnet weights from {}]".format(controlnet_pretrained_path))
weight = torch.load(controlnet_pretrained_path, map_location="cpu")
m, u = unet.load_state_dict(weight, strict=False)
print("finish loading")
if is_xformers_available():
unet.enable_xformers_memory_efficient_attention()
else:
print('no xformers!!')
assert False
print(f"Allocated memory: {torch.cuda.memory_allocated()} bytes")
print(f"Cached memory: {torch.cuda.memory_reserved()} bytes")
pipeline = FlowGenPipeline(
vae_img=vae_img,
vae_flow=vae,
text_encoder=text_encoder,
tokenizer=tokenizer,
unet=unet,
scheduler=DDIMScheduler(
**OmegaConf.to_container(inference_config.noise_scheduler_kwargs)
),
) # .to("cuda")
pipeline = pipeline.to("cuda")
del tokenizer, vae_img, vae, unet
gc.collect()
print(f"Allocated memory: {torch.cuda.memory_allocated()} bytes")
print(f"Cached memory: {torch.cuda.memory_reserved()} bytes")
self.pipeline = pipeline
self.height = height
self.width = width
self.ouput_prefix = f"flow_debug"
self.model_length = model_length
def setup_animate_pipeline(self):
### >>> create validation pipeline >>> ###
inference_config = self.inference_config
inference_config['inference_batch_size'] = 1
#inference_config = OmegaConf.load(inference_config)
pretrained_model_path = "models/stage2/StableDiffusion"
print("start loading")
tokenizer = CLIPTokenizer.from_pretrained(
pretrained_model_path, subfolder="tokenizer"
)
text_encoder = CLIPTextModel.from_pretrained(
pretrained_model_path, subfolder="text_encoder"
)
vae = AutoencoderKL.from_pretrained(pretrained_model_path, subfolder="vae")
unet = UNet3DConditionModel.from_pretrained_2d(
pretrained_model_path,
subfolder="unet",
unet_additional_kwargs=OmegaConf.to_container(
inference_config.unet_additional_kwargs
),
)
# 3. text_model
motion_module_path = "models/stage2/Motion_Module/motion_block.bin"
print("[Loading motion module ckpt from {}]".format(motion_module_path))
weight = torch.load(motion_module_path, map_location="cpu")
unet.load_state_dict(weight, strict=False)
from safetensors import safe_open
dreambooth_state_dict = {}
with safe_open(
"models/stage2/DreamBooth_LoRA/realisticVisionV51_v20Novae.safetensors",
framework="pt",
device="cpu",
) as f:
for key in f.keys():
dreambooth_state_dict[key] = f.get_tensor(key)
from animation.utils.convert_from_ckpt import (
convert_ldm_unet_checkpoint,
convert_ldm_clip_checkpoint,
convert_ldm_vae_checkpoint,
)
converted_vae_checkpoint = convert_ldm_vae_checkpoint(
dreambooth_state_dict, vae.config
)
vae.load_state_dict(converted_vae_checkpoint)
personalized_unet_path = "models/stage2/DreamBooth_LoRA/realistic_unet.ckpt"
print("[Loading personalized unet ckpt from {}]".format(personalized_unet_path))
unet.load_state_dict(torch.load(personalized_unet_path), strict=False)
print("finish loading")
if is_xformers_available():
unet.enable_xformers_memory_efficient_attention()
else:
assert False
pipeline = AnimationPipeline(
vae=vae,
text_encoder=text_encoder,
tokenizer=tokenizer,
unet=unet,
scheduler=DDIMScheduler(
**OmegaConf.to_container(inference_config.noise_scheduler_kwargs)
),
) # .to("cuda")
pipeline = pipeline.to("cuda")
self.animate_pipeline = pipeline
#@torch.no_grad()
def forward_sample(
self,
input_drag,
mask_drag,
brush_mask,
input_first_frame,
prompt,
n_prompt,
inference_steps,
guidance_scale,
outputs=dict(),
output_dir = None
):
device = self.device
num_samples = 3
with torch.no_grad():
b, l, h, w, c = input_drag.size()
# drag = torch.cat([torch.zeros_like(drag[:, 0]).unsqueeze(1), drag], dim=1) # pad the first frame with zero flow
drag = rearrange(input_drag, "b l h w c -> b c l h w")
mask = rearrange(mask_drag, "b l h w c -> b c l h w")
brush_mask = rearrange(brush_mask, "b l h w c -> b c l h w")
#zero_mask = torch.zeros_like(brush_mask)
sparse_flow = drag
sparse_mask = mask
sparse_flow = (sparse_flow - 1 / 2) * (1 - sparse_mask) + 1 / 2 # used to be sparse_flow into sparse_mask
print('sparse mask and flow shapes')
print(sparse_mask.shape)
print(sparse_flow.shape)
sparse_flow_vis = rearrange(sparse_flow, 'b c f h w -> b f c h w')
#print(sparse_flow.shape)
#sparse_flow_vis = sparse_flow_vis.expand_as(torch.zeros([sparse_flow_vis.shape[0], 14*sparse_flow_vis.shape[1], sparse_flow_vis.shape[2], sparse_flow_vis.shape[3]]))
#save_flow_grid_pillow_arrow(sparse_flow_vis, "debug_vis/test_sparse_flow.gif")
flow_mask_latent = rearrange(
self.pipeline.vae_flow.encode(
rearrange(sparse_flow, "b c f h w -> (b f) c h w")
).latent_dist.sample(),
"(b f) c h w -> b c f h w",
f=l,
)
# flow_mask_latent = vae.encode(sparse_flow).latent_dist.sample()*0.18215
sparse_mask = F.interpolate(sparse_mask, scale_factor=(1, 1 / 8, 1 / 8))
control = torch.cat([flow_mask_latent, sparse_mask], dim=1)
# print(drag)
stride = list(range(8, 121, 8))
print('control shape')
print(control.shape)
print('brush mask shape')
print(brush_mask.shape)
print(f"Allocated memory: {torch.cuda.memory_allocated()} bytes")
print(f"Cached memory: {torch.cuda.memory_reserved()} bytes")
torch.cuda.empty_cache()
print(f"Allocated memory: {torch.cuda.memory_allocated()} bytes")
print(f"Cached memory: {torch.cuda.memory_reserved()} bytes")