File size: 19,851 Bytes
68aadbb
d1d9ff5
87e3aec
32fdae0
0a3b392
99731b2
 
 
 
87e3aec
 
e5a71e9
 
dcf7f47
e5a71e9
 
 
 
 
 
 
 
87e3aec
dcf7f47
6b46943
dcf7f47
68aadbb
 
 
3fc6280
 
90bd936
39b1e18
90bd936
 
 
 
 
 
 
 
773bed3
 
 
 
 
 
90bd936
 
4e5195b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b34b4e8
39b1e18
4e5195b
 
 
b34b4e8
4e5195b
 
 
 
 
b34b4e8
 
4e5195b
 
 
 
 
 
3fc6280
b34b4e8
 
e5a71e9
 
 
 
 
b34b4e8
 
4e5195b
 
 
 
 
b34b4e8
 
 
4e5195b
 
b34b4e8
 
 
 
 
 
 
68957ea
 
 
 
 
 
 
 
 
 
 
 
 
ba1c8f4
4e5195b
68aadbb
 
 
b34b4e8
68aadbb
 
 
0b1c448
aae61bd
68aadbb
 
1b86f61
b34b4e8
4e5195b
87e3aec
68957ea
 
 
 
 
 
 
 
 
 
4e5195b
b34b4e8
4e5195b
51c0369
68aadbb
4e5195b
 
 
 
 
 
 
0b1c448
4ceeb5d
4e5195b
 
88a2ed3
4e5195b
1ec57b2
b34b4e8
 
 
 
 
e9ae397
68aadbb
 
 
 
68957ea
b34b4e8
68957ea
 
 
 
 
 
 
 
 
 
68aadbb
87e3aec
68957ea
 
 
 
 
 
 
 
 
 
b34b4e8
4e5195b
ba1c8f4
4e5195b
 
 
 
b34b4e8
4e5195b
 
 
 
b34b4e8
4e5195b
 
 
 
 
 
 
 
025dc29
8a26b9c
 
4e5195b
68957ea
 
 
 
 
 
 
 
 
 
4e5195b
 
 
 
0b1c448
4e5195b
 
 
 
 
 
 
 
b34b4e8
 
 
88a2ed3
4e5195b
 
b34b4e8
4e5195b
 
 
 
b34b4e8
4e5195b
 
 
 
0b1c448
4e5195b
 
b34b4e8
 
4e5195b
 
 
 
 
 
 
51c0369
68957ea
 
 
 
 
 
 
 
 
 
 
 
4e5195b
b34b4e8
 
 
4e5195b
68957ea
 
 
 
 
 
 
 
 
 
 
b34b4e8
87e3aec
d7d00c9
 
 
 
88a2ed3
d7d00c9
e079314
d7d00c9
bcd02b9
d7d00c9
e079314
1e8a534
e079314
 
d7d00c9
 
88a2ed3
 
d7d00c9
 
 
68aadbb
 
 
b34b4e8
68aadbb
88a2ed3
d7d00c9
4e5195b
 
4588985
 
 
4e5195b
4588985
 
 
b34b4e8
68aadbb
d7d00c9
 
 
68aadbb
4e5195b
096ac3e
4e5195b
 
 
 
 
 
b34b4e8
 
 
 
4e5195b
 
 
88a2ed3
 
7d42e44
88a2ed3
b1af849
b34b4e8
88a2ed3
025dc29
096ac3e
025dc29
096ac3e
 
4e5195b
096ac3e
4e5195b
68aadbb
1b86f61
4e5195b
68aadbb
 
 
b34b4e8
 
 
 
 
68aadbb
db10df2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4e5195b
68aadbb
 
88a2ed3
4e5195b
68aadbb
 
 
b34b4e8
68aadbb
 
 
 
aae61bd
68aadbb
 
b34b4e8
 
68aadbb
 
 
 
b34b4e8
88a2ed3
 
68aadbb
848c1a5
4e5195b
 
 
 
 
b34b4e8
4e5195b
 
 
 
b34b4e8
4e5195b
 
 
 
 
 
 
025dc29
 
b34b4e8
4e5195b
d7d00c9
735c4a0
 
 
286f8ab
 
735c4a0
39b1e18
d7d00c9
b34b4e8
c06160a
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
import gradio as gr
import spaces
import torch
from diffusers import StableDiffusionPipeline, DDIMScheduler
from utils import video_to_frames, add_dict_to_yaml_file, save_video, seed_everything
# from diffusers.utils import export_to_video
from tokenflow_pnp import TokenFlow
from preprocess_utils import *
from tokenflow_utils import *
# load sd model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_id = "stabilityai/stable-diffusion-2-1-base"

# components for the Preprocessor
scheduler = DDIMScheduler.from_pretrained(model_id, subfolder="scheduler")
vae = AutoencoderKL.from_pretrained(model_id, subfolder="vae", revision="fp16",
                                                 torch_dtype=torch.float16).to(device)
tokenizer = CLIPTokenizer.from_pretrained(model_id, subfolder="tokenizer")
text_encoder = CLIPTextModel.from_pretrained(model_id, subfolder="text_encoder", revision="fp16",
                                                  torch_dtype=torch.float16).to(device)
unet = UNet2DConditionModel.from_pretrained(model_id, subfolder="unet", revision="fp16",
                                           torch_dtype=torch.float16).to(device)

# pipe for TokenFlow
tokenflow_pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")

def randomize_seed_fn():
    seed = random.randint(0, np.iinfo(np.int32).max)
    return seed
    
def reset_do_inversion():
    return True

def get_example():
    case = [
        [
            'examples/wolf.mp4',     
        ],
        [
            'examples/woman-running.mp4',     
        ],
        [
            'examples/cutting_bread.mp4',
        ],
        [
            'examples/running_dog.mp4',
        ]
    ]
    return case


def prep(config):
    # timesteps to save
    if config["sd_version"] == '2.1':
        model_key = "stabilityai/stable-diffusion-2-1-base"
    elif config["sd_version"] == '2.0':
        model_key = "stabilityai/stable-diffusion-2-base"
    elif config["sd_version"] == '1.5' or config["sd_version"] == 'ControlNet':
        model_key = "runwayml/stable-diffusion-v1-5"
    elif config["sd_version"] == 'depth':
        model_key = "stabilityai/stable-diffusion-2-depth"
    toy_scheduler = DDIMScheduler.from_pretrained(model_key, subfolder="scheduler")
    toy_scheduler.set_timesteps(config["save_steps"])
    timesteps_to_save, num_inference_steps = get_timesteps(toy_scheduler, num_inference_steps=config["save_steps"],
                                                           strength=1.0,
                                                           device=device)


    # seed_everything(config["seed"])
    if not config["frames"]: # original non demo setting
        save_path = os.path.join(config["save_dir"],
                                 f'inversion_{config[inversion]}',
                                 f'sd_{config["sd_version"]}',
                                 Path(config["data_path"]).stem,
                                 f'steps_{config["steps"]}',
                                 f'nframes_{config["n_frames"]}') 
        os.makedirs(os.path.join(save_path, f'latents'), exist_ok=True)
        if opt[inversion] == 'ddpm':
            os.makedirs(os.path.join(save_path, f'latents'), exist_ok=True)
        add_dict_to_yaml_file(os.path.join(config["save_dir"], 'inversion_prompts.yaml'), Path(config["data_path"]).stem, config["inversion_prompt"])    
        # save inversion prompt in a txt file
        with open(os.path.join(save_path, 'inversion_prompt.txt'), 'w') as f:
            f.write(config["inversion_prompt"])
    else:
        save_path = None
    
    model = Preprocess(device, 
                       config,
                      vae=vae,
                      text_encoder=text_encoder,
                      scheduler=scheduler,
                      tokenizer=tokenizer,
                      unet=unet)

    frames_and_latents, rgb_reconstruction = model.extract_latents(
                                         num_steps=model.config["steps"],
                                         save_path=save_path,
                                         batch_size=model.config["batch_size"],
                                         timesteps_to_save=timesteps_to_save,
                                         inversion_prompt=model.config["inversion_prompt"],
                                         inversion_type=model.config["inversion"],
                                         skip_steps=model.config["skip_steps"],
                                         reconstruction=model.config["reconstruct"]
    )

    if model.config["inversion"] == 'ddpm':
        frames, latents, total_inverted_latents, zs = frames_and_latents
        return frames, latents, total_inverted_latents, zs, rgb_reconstruction
    else:
        frames, latents, total_inverted_latents = frames_and_latents
        return frames, latents, total_inverted_latents, rgb_reconstruction

def move_to_device(data, device):
    """Helper function to recursively move tensors to device"""
    if isinstance(data, torch.Tensor):
        return data.to(device)
    elif isinstance(data, list):
        return [move_to_device(item, device) for item in data]
    elif isinstance(data, dict):
        return {key: move_to_device(value, device) for key, value in data.items()}
    elif isinstance(data, tuple):
        return tuple(move_to_device(item, device) for item in data)
    else:
        return data

@spaces.GPU(duration=120)
def preprocess_and_invert(input_video,
                          frames,
                          latents,
                          inverted_latents,
                          zs,
                          seed, 
                          randomize_seed,
                          do_inversion,
                          steps,
                          n_timesteps = 50,
                          batch_size: int = 8,
                          n_frames: int = 40,
                          inversion_prompt:str = '',
                          skip_steps: int = 15,
                          
              ):
    # Move tensors to CUDA at the beginning
    if frames is not None and hasattr(frames, 'value'):
        frames.value = move_to_device(frames.value, 'cuda')
    if latents is not None and hasattr(latents, 'value'):
        latents.value = move_to_device(latents.value, 'cuda')
    if inverted_latents is not None and hasattr(inverted_latents, 'value'):
        inverted_latents.value = move_to_device(inverted_latents.value, 'cuda')
    if zs is not None and hasattr(zs, 'value'):
        zs.value = move_to_device(zs.value, 'cuda')
    
    sd_version = "2.1"
    height: int = 512
    weidth: int = 512
    
    if do_inversion or randomize_seed:
        preprocess_config = {}
        preprocess_config['H'] = height
        preprocess_config['W'] = weidth
        preprocess_config['save_dir'] = 'latents'
        preprocess_config['sd_version'] = sd_version
        preprocess_config['steps'] = steps
        preprocess_config['batch_size'] = batch_size
        preprocess_config['save_steps'] = int(n_timesteps)
        preprocess_config['n_frames'] = n_frames
        preprocess_config['seed'] = seed
        preprocess_config['inversion_prompt'] = inversion_prompt
        preprocess_config['frames'] = video_to_frames(input_video)
        preprocess_config['data_path'] = input_video.split(".")[0]
        
        preprocess_config['inversion'] = 'ddpm'
        preprocess_config['skip_steps'] = skip_steps
        preprocess_config['reconstruct'] = False
        
        

        if randomize_seed:
            seed = randomize_seed_fn()
        seed_everything(seed)
        
        frames_val, latents_val, total_inverted_latents_val, zs_val, rgb_reconstruction = prep(preprocess_config)
        
        # Move tensors to CPU before creating gr.State
        frames_val = move_to_device(frames_val, 'cpu')
        latents_val = move_to_device(latents_val, 'cpu')
        total_inverted_latents_val = move_to_device(total_inverted_latents_val, 'cpu')
        zs_val = move_to_device(zs_val, 'cpu')
        
        frames = gr.State(value = frames_val)
        latents = gr.State(value = latents_val)
        inverted_latents = gr.State(value = total_inverted_latents_val)
        zs = gr.State(value = zs_val)
        do_inversion = False
   
    # Move all tensors to CPU before returning
    if frames is not None and hasattr(frames, 'value'):
        frames.value = move_to_device(frames.value, 'cpu')
    if latents is not None and hasattr(latents, 'value'):
        latents.value = move_to_device(latents.value, 'cpu')
    if inverted_latents is not None and hasattr(inverted_latents, 'value'):
        inverted_latents.value = move_to_device(inverted_latents.value, 'cpu')
    if zs is not None and hasattr(zs, 'value'):
        zs.value = move_to_device(zs.value, 'cpu')
    
    return frames, latents, inverted_latents, zs, do_inversion

@spaces.GPU(duration=120)
def edit_with_pnp(input_video,
                  frames, 
                  latents,
                  inverted_latents,
                  zs,
                  seed,
                  randomize_seed,
                  do_inversion,
                  steps,
                  skip_steps: int = 15,
                  prompt: str = "a marble sculpture of a woman running, Venus de Milo",
                  # negative_prompt: str = "ugly, blurry, low res, unrealistic, unaesthetic",
                  pnp_attn_t: float = 0.5,
                  pnp_f_t: float = 0.8,
                  batch_size: int = 8, #needs to be the same as for preprocess
                  n_frames: int = 40,#needs to be the same as for preprocess
                  n_timesteps: int = 50,
                  gudiance_scale: float = 7.5,
                  inversion_prompt: str = "", #needs to be the same as for preprocess
                  n_fps: int = 10,
                  progress=gr.Progress(track_tqdm=True)
):
    # Move tensors to CUDA at the beginning
    if frames is not None and hasattr(frames, 'value'):
        frames.value = move_to_device(frames.value, 'cuda')
    if latents is not None and hasattr(latents, 'value'):
        latents.value = move_to_device(latents.value, 'cuda')
    if inverted_latents is not None and hasattr(inverted_latents, 'value'):
        inverted_latents.value = move_to_device(inverted_latents.value, 'cuda')
    if zs is not None and hasattr(zs, 'value'):
        zs.value = move_to_device(zs.value, 'cuda')
    
    config = {}
 
    config["sd_version"] = "2.1"
    config["device"] = device
    config["n_timesteps"] = int(n_timesteps)
    config["n_frames"] = n_frames
    config["batch_size"] = batch_size
    config["guidance_scale"] = gudiance_scale
    config["prompt"] = prompt
    config["negative_prompt"] = "ugly, blurry, low res, unrealistic, unaesthetic",
    config["pnp_attn_t"] = pnp_attn_t
    config["pnp_f_t"] = pnp_f_t
    config["pnp_inversion_prompt"] = inversion_prompt
    config["inversion"] = "ddpm"
    config["skip_steps"] = skip_steps
    
    
    
    if do_inversion:
        frames, latents, inverted_latents, zs, do_inversion =  preprocess_and_invert(
                          input_video,
                          frames,
                          latents,
                          inverted_latents,
                          zs,
                          seed, 
                          randomize_seed,
                          do_inversion,
                          steps,
                          n_timesteps,
                          batch_size,
                          n_frames,
                          inversion_prompt,
                          skip_steps)
        do_inversion = False
        
    
    if randomize_seed:
            seed = randomize_seed_fn()
    seed_everything(seed)
    
    tokenflow_pipe.enable_xformers_memory_efficient_attention()
    
    # Ensure tensors are on CUDA for editing
    frames_cuda = frames.value if hasattr(frames, 'value') else frames
    inverted_latents_cuda = inverted_latents.value if hasattr(inverted_latents, 'value') else inverted_latents
    zs_cuda = zs.value if hasattr(zs, 'value') else zs
    
    # Move to CUDA if needed
    frames_cuda = move_to_device(frames_cuda, 'cuda')
    inverted_latents_cuda = move_to_device(inverted_latents_cuda, 'cuda')
    zs_cuda = move_to_device(zs_cuda, 'cuda')
    
    editor = TokenFlow(config=config, pipe=tokenflow_pipe, frames=frames_cuda, inverted_latents=inverted_latents_cuda, zs=zs_cuda)
    edited_frames = editor.edit_video()
    
    edit_video_path = f'tokenflow_PnP_fps_{n_fps}.mp4'
    save_video(edited_frames, edit_video_path, fps=n_fps)
    # path = export_to_video(edited_frames)
    
    # Move all tensors to CPU before returning
    if frames is not None and hasattr(frames, 'value'):
        frames.value = move_to_device(frames.value, 'cpu')
    if latents is not None and hasattr(latents, 'value'):
        latents.value = move_to_device(latents.value, 'cpu')
    if inverted_latents is not None and hasattr(inverted_latents, 'value'):
        inverted_latents.value = move_to_device(inverted_latents.value, 'cpu')
    if zs is not None and hasattr(zs, 'value'):
        zs.value = move_to_device(zs.value, 'cpu')
    
    return edit_video_path, frames, latents, inverted_latents, zs, do_inversion

########
# demo #
########


intro = """
<div style="text-align:center">
<h1 style="font-weight: 1400; text-align: center; margin-bottom: 7px;">
   TokenFlow - <small>Temporally consistent video editing</small>
</h1>
<span>[<a target="_blank" href="https://diffusion-tokenflow.github.io">Project page</a>], [<a target="_blank" href="https://github.com/omerbt/TokenFlow">GitHub</a>], [<a target="_blank" href="https://huggingface.co/papers/2307.10373">Paper</a>]</span>
<div style="display:flex; justify-content: center;margin-top: 0.5em">Each edit takes ~1 min <a href="https://huggingface.co/weizmannscience/tokenflow?duplicate=true" target="_blank">
<img style="margin-top: 0em; margin-bottom: 0em; margin-left: 0.5em" src="https://bit.ly/3CWLGkA" alt="Duplicate Space"></a></div>
</div>
"""



with gr.Blocks(css="style.css") as demo:
    
    gr.HTML(intro)
    frames = gr.State()
    inverted_latents = gr.State()
    latents = gr.State()
    zs = gr.State()
    do_inversion = gr.State(value=True)

    with gr.Row():
        input_video = gr.Video(label="Input Video", interactive=True, elem_id="input_video")
        output_video = gr.Video(label="Edited Video", interactive=False, elem_id="output_video")


    with gr.Row():
            prompt = gr.Textbox(
                            label="Describe your edited video",
                            max_lines=1, value=""
                        )
    
               
    with gr.Row():
        run_button = gr.Button("Edit your video!", visible=True)

    with gr.Accordion("Advanced Options", open=False):
        with gr.Tabs() as tabs:
            with gr.TabItem('General options'):
                with gr.Row():
                    with gr.Column(min_width=100):
                        seed = gr.Number(value=0, precision=0, label="Seed", interactive=True)
                        randomize_seed = gr.Checkbox(label='Randomize seed', value=False)
                        gudiance_scale = gr.Slider(label='Guidance Scale', minimum=1, maximum=30,
                                              value=7.5, step=0.5, interactive=True)
                        steps = gr.Slider(label='Inversion steps', minimum=10, maximum=200,
                                              value=50, step=1, interactive=True)
                        skip_steps = gr.Slider(label='Skip Steps', minimum=5, maximum=25,
                                              value=5, step=1, interactive=True)
                        
                    with gr.Column(min_width=100):
                        inversion_prompt = gr.Textbox(lines=1, label="Inversion prompt", interactive=True, placeholder="")
                        batch_size = gr.Slider(label='Batch size', minimum=1, maximum=10,
                                              value=8, step=1, interactive=True)
                        n_frames = gr.Slider(label='Num frames', minimum=2, maximum=200,
                                              value=24, step=1, interactive=True)
                        n_timesteps = gr.Slider(label='Diffusion steps', minimum=25, maximum=100,
                                              value=50, step=25, interactive=True)
                        n_fps = gr.Slider(label='Frames per second', minimum=1, maximum=60,
                                              value=10, step=1, interactive=True)
                        
            with gr.TabItem('Plug-and-Play Parameters'):
                 with gr.Column(min_width=100):
                    pnp_attn_t = gr.Slider(label='pnp attention threshold', minimum=0, maximum=1,
                                              value=0.5, step=0.5, interactive=True)
                    pnp_f_t = gr.Slider(label='pnp feature threshold', minimum=0, maximum=1,
                                              value=0.8, step=0.05, interactive=True)
                    
                    
    input_video.change(
        fn = reset_do_inversion,
        outputs = [do_inversion],
        queue = False)
    
    steps.change(
        fn = reset_do_inversion,
        outputs = [do_inversion],
        queue = False)

    inversion_prompt.change(
        fn = reset_do_inversion,
        outputs = [do_inversion],
        queue = False)

    randomize_seed.change(
        fn = reset_do_inversion,
        outputs = [do_inversion],
        queue = False)

    seed.change(
        fn = reset_do_inversion,
        outputs = [do_inversion],
        queue = False)

    

    input_video.upload(
        fn = reset_do_inversion,
        outputs = [do_inversion],
        queue = False).then(fn = preprocess_and_invert,
          inputs = [input_video,
                      frames,
                      latents,
                      inverted_latents,
                      zs,
                      seed, 
                      randomize_seed,
                      do_inversion,
                      steps,
                      n_timesteps,
                      batch_size,
                      n_frames,
                      inversion_prompt,
                      skip_steps
          ],
          outputs = [frames,
                     latents,
                     inverted_latents,
                     zs,
                     do_inversion
              
          ])
    
    run_button.click(fn = edit_with_pnp,
                     inputs = [input_video,
                               frames, 
                              latents,
                              inverted_latents,
                              zs,
                              seed,
                              randomize_seed,
                              do_inversion,
                              steps,
                              skip_steps,
                              prompt,                             
                              pnp_attn_t,
                              pnp_f_t,
                              batch_size,
                              n_frames,
                              n_timesteps,
                              gudiance_scale,
                              inversion_prompt,
                              n_fps ],
                                 outputs = [output_video, frames, latents, inverted_latents, zs, do_inversion]
                                )

    gr.Examples(
        examples=get_example(),
        label='Examples',
        inputs=[input_video],
        outputs=[output_video]
    )

demo.queue()

demo.launch()