-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaudio_generation.py
More file actions
1116 lines (970 loc) · 51.2 KB
/
audio_generation.py
File metadata and controls
1116 lines (970 loc) · 51.2 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
import os
import time
import json
import sys
from pathlib import Path
from random import sample, shuffle
import argparse
import glob
import soundfile
import torch
import torchvision.transforms as transforms
from specvqgan.data.transforms import *
from matplotlib import pyplot as plt
from torch.utils.data.dataloader import default_collate
from tqdm import tqdm
from PIL import Image
import cv2
from moviepy.editor import VideoFileClip, AudioFileClip, ImageSequenceClip
import numpy as np
import copy
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision.models as models
FRAME_TRANS = transforms.Compose([
Resize3D(128),
CenterCrop3D(112),
ToTensor3D(),
Normalize3D(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
from feature_extraction.demo_utils import (extract_melspectrogram, load_model,
trim_video, load_frames,
reencode_video_with_diff_fps)
from feature_extraction.demo_utils import (extract_melspectrogram,
get_duration)
from sample_visualization import spec_to_audio_to_st
from specvqgan.modules.losses.vggishish.transforms import Crop
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# desired depth layers to compute style/content losses :
content_layers_default = ['conv_4']
style_layers_default = ['conv_1', 'conv_2', 'conv_3', 'conv_4', 'conv_5']
parser = argparse.ArgumentParser()
parser.add_argument('--model_name', type=str, default=None,
help='folder name of the pre-trained model')
parser.add_argument('--target_log_dir', type=str, default=None,
help='output folder name under logs/ dir')
parser.add_argument('--slide_win_mode', type=str, default='half', choices=['half', 'last'],
help='slide window method when generating longer audio')
parser.add_argument('--W_scale', type=int, default=1,
help='length scale of the generate audio, output will be W_scale*2s')
parser.add_argument('--max_W_scale', type=int, default=3,
help='maximum W_scale to iterate')
parser.add_argument('--min_W_scale', type=int, default=1,
help='minimum W_scale to iterate')
parser.add_argument('--gen_cnt', type=int, default=30,
help='generation count when generating multiple result')
parser.add_argument('--spec_take_first', type=int, default=160,
help='cut the spectrogram to this size')
parser.add_argument('--temperature', type=float, default=1.0,
help='temperature of softmax for logits to probability')
parser.add_argument('--split', type=int, default=-1,
help='split idx when running multi-process generation')
parser.add_argument('--total_split', type=int, default=1,
help='total number of multi-process')
parser.add_argument('--tmp_idx', type=int, default=-1,
help='temperate folder idx to place intermediate result')
parser.add_argument('--new_codebook', action='store_true',
help='load a different codebook according to config')
parser.add_argument('--gh_testset', action='store_true',
help='running greatest hit testset')
parser.add_argument('--gh_demo', action='store_true',
help='running the greatest hit demo')
parser.add_argument('--gh_gen', action='store_true',
help='generate audio with greatest hit model')
parser.add_argument('--countix_av_gen', action='store_true',
help='generate audio with countix-AV model')
parser.add_argument('--multiple', action='store_true',
help='generate multiple audio for each pair of input for re-ranking')
parser.add_argument('--style_transfer', action='store_true',
help='add to set this option to true')
class CropImage(Crop):
def __init__(self, *crop_args):
super().__init__(*crop_args)
class ContentLoss(nn.Module):
def __init__(self, target,):
super(ContentLoss, self).__init__()
# we 'detach' the target content from the tree used
# to dynamically compute the gradient: this is a stated value,
# not a variable. Otherwise the forward method of the criterion
# will throw an error.
self.target = target.detach()
def forward(self, input):
self.loss = F.mse_loss(input, self.target)
return input
def gram_matrix(input):
a, b, c, d = input.size() # a=batch size(=1)
# b=number of feature maps
# (c,d)=dimensions of a f. map (N=c*d)
features = input.view(a * b, c * d) # resise F_XL into \hat F_XL
G = torch.mm(features, features.t()) # compute the gram product
# we 'normalize' the values of the gram matrix
# by dividing by the number of element in each feature maps.
return G.div(a * b * c * d)
cnn_normalization_mean = torch.tensor([0.485, 0.456, 0.406]).to(device)
cnn_normalization_std = torch.tensor([0.229, 0.224, 0.225]).to(device)
# create a module to normalize input image so we can easily put it in a
# nn.Sequential
class Normalization(nn.Module):
def __init__(self, mean, std):
super(Normalization, self).__init__()
# .view the mean and std to make them [C x 1 x 1] so that they can
# directly work with image Tensor of shape [B x C x H x W].
# B is batch size. C is number of channels. H is height and W is width.
self.mean = torch.tensor(mean).view(-1, 1, 1)
self.std = torch.tensor(std).view(-1, 1, 1)
def forward(self, img):
# normalize img
return (img - self.mean) / self.std
class StyleLoss(nn.Module):
def __init__(self, target_feature):
super(StyleLoss, self).__init__()
self.target = gram_matrix(target_feature).detach()
def forward(self, input):
G = gram_matrix(input)
self.loss = F.mse_loss(G, self.target)
return input
def get_style_model_and_losses(cnn, normalization_mean, normalization_std,
style_img, content_img,
content_layers=content_layers_default,
style_layers=style_layers_default):
# normalization module
normalization = Normalization(normalization_mean, normalization_std).to(device)
# just in order to have an iterable access to or list of content/syle
# losses
content_losses = []
style_losses = []
# assuming that cnn is a nn.Sequential, so we make a new nn.Sequential
# to put in modules that are supposed to be activated sequentially
model = nn.Sequential(normalization)
i = 0 # increment every time we see a conv
for layer in cnn.children():
if isinstance(layer, nn.Conv2d):
i += 1
name = 'conv_{}'.format(i)
elif isinstance(layer, nn.ReLU):
name = 'relu_{}'.format(i)
# The in-place version doesn't play very nicely with the ContentLoss
# and StyleLoss we insert below. So we replace with out-of-place
# ones here.
layer = nn.ReLU(inplace=False)
elif isinstance(layer, nn.MaxPool2d):
name = 'pool_{}'.format(i)
elif isinstance(layer, nn.BatchNorm2d):
name = 'bn_{}'.format(i)
else:
raise RuntimeError('Unrecognized layer: {}'.format(layer.__class__.__name__))
model.add_module(name, layer)
if name in content_layers:
# add content loss:
target = model(content_img).detach()
content_loss = ContentLoss(target)
model.add_module("content_loss_{}".format(i), content_loss)
content_losses.append(content_loss)
if name in style_layers:
# add style loss:
target_feature = model(style_img).detach()
style_loss = StyleLoss(target_feature)
model.add_module("style_loss_{}".format(i), style_loss)
style_losses.append(style_loss)
# now we trim off the layers after the last content and style losses
for i in range(len(model) - 1, -1, -1):
if isinstance(model[i], ContentLoss) or isinstance(model[i], StyleLoss):
break
model = model[:(i + 1)]
return model, style_losses, content_losses
def load_specs_as_img(spec, spec_take_first=192):
loader = transforms.Compose([
transforms.Resize((80, spec_take_first)), # scale imported image
transforms.ToTensor()]) # transform it into a torch tensor
spec = spec[:, :spec_take_first]
spec_img = Image.fromarray((spec * 255).astype(np.uint8)).convert('RGB')
spec_img = loader(spec_img).unsqueeze(0)
return spec_img.to(device, torch.float)
def get_input_optimizer(input_img):
# this line to show that input is a parameter that requires a gradient
optimizer = optim.LBFGS([input_img])
return optimizer
def run_style_transfer(normalization_mean, normalization_std,
content_img, style_img, input_img, num_steps=300,
style_weight=1000000, content_weight=1):
"""Run the style transfer."""
print('Building the style transfer model..')
cnn = models.vgg19(pretrained=True).features.to(device).eval()
model, style_losses, content_losses = get_style_model_and_losses(cnn,
normalization_mean, normalization_std, style_img, content_img)
# We want to optimize the input and not the model parameters so we
# update all the requires_grad fields accordingly
input_img.requires_grad_(True)
model.requires_grad_(False)
optimizer = get_input_optimizer(input_img)
print('Optimizing..')
run = [0]
while run[0] <= num_steps:
def closure():
# correct the values of updated input image
with torch.no_grad():
input_img.clamp_(0, 1)
optimizer.zero_grad()
model(input_img)
style_score = 0
content_score = 0
for sl in style_losses:
style_score += sl.loss
for cl in content_losses:
content_score += cl.loss
style_score *= style_weight
content_score *= content_weight
loss = style_score + content_score
loss.backward()
run[0] += 1
if run[0] % 150 == 0:
print("run {}:".format(run))
print('Style Loss : {:4f} Content Loss: {:4f}'.format(
style_score.item(), content_score.item()))
print()
return style_score + content_score
optimizer.step(closure)
# a last correction...
with torch.no_grad():
input_img.clamp_(0, 1)
return input_img
def attach_audio_to_video(video_path, audio_path, dest, start_step, FPS=15, recon_only=False, put_text=False, v_duration=2):
clip = VideoFileClip(video_path).set_fps(FPS)
if put_text:
frames = [f for f in clip.iter_frames()]
H, W, _ = frames[0].shape
for i in range(len(frames)):
text = 'Original Audio' if i < start_step else 'Generated Audio'
if recon_only:
text = 'Reconstructed Sound'
img_w_text = cv2.putText(frames[i], text, (W//50, H//6),
cv2.FONT_HERSHEY_SIMPLEX, fontScale=1,
color=(255, 0, 0), thickness=3)
clip = ImageSequenceClip(frames, fps=FPS)
clip = clip.subclip(0, v_duration)
clip = clip.set_audio(AudioFileClip(audio_path))
clip.write_videofile(dest, audio=True, fps=FPS, verbose=False, logger=None)
return clip
def draw_spec(spec, dest, cmap='magma'):
plt.imshow(spec, cmap=cmap, origin='lower')
plt.axis('off')
plt.savefig(dest, bbox_inches='tight', pad_inches=0.)
plt.close()
def gen_audio_condImage_fast(video_path,
extra_cond_video_path,
model,
target_log_dir = 'CondAVTransformer',
cond_cnt=0,
SR = 22050,
FPS = 15,
L = 2.0,
normalize=False,
using_torch=False,
show_griffin_lim=False,
vqgan_L=10.0,
style_transfer=False,
target_start_time=0,
cond_start_time=0,
outside=False,
remove_noise=False,
spec_take_first=160,
W_scale=1,
slide_win_mode='half',
temperature = 1.0,
ignore_input_spec=False,
tmp_path='./tmp',
fix_cond=True):
'''
parameters:
video_path: path to the target video, will be trimmed to 2s and re-encode into 15 fps.
extra_cond_video_path: path to the conditional video, will be trimmed to 2s and re-encode into 15 fps.
model: model object, returned by load_model function
target_log_dir: target output dir name in the 'logs' directory, e.g. output will be saved to 'logs/<target_log_dir>'
cond_cnt: index of current condition video
SR: sampling rate
FPS: Frame rate
L: length of generated sound
normalize: whether to normaliza input waveform
using_torch: use torchaudio to extrac spectrogram
show_griffin_lim: use griffin_lim algorithm vocoder
vqgan_L: length of VQ-GAN codebook, use 2 if using GreatestHit codebook
style_transfer: generate style transfer sound
target_start_time: if target video is from outside, trim from <target_start_time> to <target_start_time>+2
cond_start_time: if conditional video is from outside, trim from <cond_start_time> to <cond_start_time>+2
outside: indicate whether the video from outside source
remove_noise: denoise for outside videos
spec_take_first: size of the spectrogram to use
W_scale: scale of audio duration as multiples of 2sec
slide_win_mode: mode of sliding window, choose from ['half', 'last']
temperature: temperature of multinomial sampling.
ignore_input_spec: ignore input spec when input video is silent
tmp_path: tmp dir to save intermediate files
fix_cond: use only 2 sec condition regardless to input length.
'''
config, sampler, melgan, melception = model
# feature extractor
L = int(L * W_scale)
vqgan_L = int(vqgan_L * W_scale)
spec_take_first = int(spec_take_first * W_scale)
if '_denoised_' not in video_path or outside:
new_fps_video_path = reencode_video_with_diff_fps(video_path, tmp_path, FPS)
video_path = trim_video(new_fps_video_path, target_start_time, vqgan_L, tmp_path=tmp_path)
frames = [Image.fromarray(f) for f in load_frames(video_path)][:int(FPS * L)]
frames = FRAME_TRANS(frames)
if '_denoised_' not in extra_cond_video_path or outside:
new_fps_video_path = reencode_video_with_diff_fps(extra_cond_video_path, tmp_path, FPS)
extra_cond_video_path = trim_video(new_fps_video_path, cond_start_time, vqgan_L, tmp_path=tmp_path)
cond_frames = [Image.fromarray(f) for f in load_frames(extra_cond_video_path)][:int(FPS * L)]
cond_frames = FRAME_TRANS(cond_frames)
feats = {'feature': np.stack(cond_frames + frames, axis=0)}
cond_video_path = extra_cond_video_path
tar_video_path = video_path
# Extract Features
visual_features = feats
# Prepare Input
batch = default_collate([visual_features])
batch['feature'] = batch['feature'].to(device)
with torch.no_grad():
c = sampler.get_input(sampler.cond_stage_key, batch)
# Extract Spectrogram
if not ignore_input_spec:
spectrogram = extract_melspectrogram(tar_video_path, SR, normalize=normalize, using_torch=using_torch, remove_noise=remove_noise, duration=vqgan_L, tmp_path=tmp_path)
spec_H, spec_W = spectrogram.shape
if spec_W > spec_take_first:
spectrogram = spectrogram[:, :spec_take_first]
else:
pad = np.zeros((spec_H, spec_take_first), dtype=spectrogram.dtype)
pad[:, :spec_W] = spectrogram
spectrogram = pad
spectrogram = {'input': spectrogram}
if config.data.params.spec_crop_len is None or W_scale != 1:
config.data.params.spec_crop_len = spec_take_first
if spectrogram['input'].shape[1] > config.data.params.spec_crop_len:
random_crop = False
crop_img_fn = CropImage([config.data.params.mel_num, config.data.params.spec_crop_len], random_crop)
spectrogram = crop_img_fn(spectrogram)
# Prepare input
batch = default_collate([spectrogram])
batch['image'] = batch['input'].to(device)
x = sampler.get_input(sampler.first_stage_key, batch)
mel_x = x.detach().cpu().numpy()
# Encode and Decode the Spectrogram
with torch.no_grad():
quant_z, z_indices = sampler.encode_to_z(x)
# print(z_indices)
xrec = sampler.first_stage_model.decode(quant_z)
mel_xrec = xrec.detach().cpu().numpy()
# Conditional
# Extract Spectrogram
spectrogram = extract_melspectrogram(cond_video_path, SR, normalize=normalize, using_torch=using_torch, remove_noise=remove_noise, duration=vqgan_L, tmp_path=tmp_path)
spec_H, spec_W = spectrogram.shape
if spec_W > spec_take_first:
padded = False
spectrogram = spectrogram[:, :spec_take_first]
else:
padded = True
pad = np.zeros((spec_H, spec_take_first), dtype=spectrogram.dtype)
pad[:, :spec_W] = spectrogram
orig_width = spec_W
spectrogram = pad
spectrogram = {'input': spectrogram}
if config.data.params.spec_crop_len is None or W_scale != 1:
config.data.params.spec_crop_len = spec_take_first
if spectrogram['input'].shape[1] > config.data.params.spec_crop_len:
random_crop = False
crop_img_fn = CropImage([config.data.params.mel_num, config.data.params.spec_crop_len], random_crop)
spectrogram = crop_img_fn(spectrogram)
# Prepare input
batch = default_collate([spectrogram])
batch['cond_image'] = batch['input'].to(device)
xp = sampler.get_input(sampler.cond_first_stage_key, batch)
mel_xp = xp.detach().cpu().numpy()
# Encode and Decode the Spectrogram
with torch.no_grad():
quant_zp, zp_indices = sampler.encode_to_z(xp)
# print(zp_indices)
xprec = sampler.first_stage_model.decode(quant_zp)
mel_xprec = xprec.detach().cpu().numpy()
if ignore_input_spec:
z_indices = torch.zeros_like(zp_indices)
xrec = torch.zeros_like(xprec)
mel_xrec = np.zeros_like(mel_xprec)
# Define Sampling Parameters
# take top 1024 / 512 code
top_x = sampler.first_stage_model.quantize.n_e // 2
if not os.path.exists(f'logs/{target_log_dir}'):
os.mkdir(f'logs/{target_log_dir}')
target_dir = os.path.join(f'logs/{target_log_dir}', f'2sec_full_generated_sound_{cond_cnt}')
target_v_dir = os.path.join(f'logs/{target_log_dir}', f'2sec_full_generated_video_{cond_cnt}')
target_cond_v_dir = os.path.join(f'logs/{target_log_dir}', f'2sec_full_cond_video_{cond_cnt}')
target_orig_v_dir = os.path.join(f'logs/{target_log_dir}', f'2sec_full_orig_video')
if not os.path.exists(target_dir):
os.mkdir(target_dir)
if not os.path.exists(target_v_dir):
os.mkdir(target_v_dir)
if not os.path.exists(target_cond_v_dir):
os.mkdir(target_cond_v_dir)
if not os.path.exists(target_orig_v_dir):
os.mkdir(target_orig_v_dir)
# Start sampling
if style_transfer:
content_img = load_specs_as_img(mel_xrec[0, 0, :, :spec_take_first])
style_img = load_specs_as_img(mel_xprec[0, 0, :, :spec_take_first])
generated_spec = run_style_transfer(
cnn_normalization_mean.to(),
cnn_normalization_std.to(),
content_img.clone().to(device),
style_img.clone().to(device),
content_img.clone().to(device),
)
z_pred_img = torch.mean(generated_spec, dim=1, keepdim=True)
mel_z = z_pred_img.detach().cpu().numpy()
else:
with torch.no_grad():
start_t = time.time()
quant_c, c_indices = sampler.encode_to_c(c)
z_indices_clip = z_indices[:, :sampler.clip * W_scale]
zp_indices_clip = zp_indices[:, :sampler.clip * W_scale]
z_indices_rec = z_indices.clone()
# crec = sampler.cond_stage_model.decode(quant_c)
patch_size_i = 5
c_window_size = int(2 * FPS)
downsampled_size = spec_take_first // 16
cond_patch_shift_j = (W_scale - 1) * (downsampled_size // W_scale)
if 'dropcond_' in target_log_dir:
B, D, hr_h, hr_w = sampling_shape = (1, 256, 5, int(downsampled_size))
patch_size_j = int(downsampled_size // W_scale)
else:
B, D, hr_h, hr_w = sampling_shape = (1, 256, 5, int(2*downsampled_size))
patch_size_j = int(2*downsampled_size // W_scale)
z_pred_indices = torch.zeros((B, hr_h*hr_w)).long().to(device)
if 'dropcond_' not in target_log_dir:
start_step = zp_indices_clip.shape[1]
z_pred_indices[:, :start_step] = zp_indices_clip[:, :start_step]
elif 'dropcond_' in target_log_dir:
start_step = 0
pbar = tqdm(range(start_step, hr_w * hr_h), desc='Sampling Codebook Indices')
for step in pbar:
i = step % hr_h
j = step // hr_h
i_start = min(max(0, i - (patch_size_i // 2)), hr_h - patch_size_i)
# only last
#
if slide_win_mode == 'half':
j_start = min(max(0, j - (3 * patch_size_j // 4)), hr_w - patch_size_j)
elif slide_win_mode == 'last':
j_start = min(max(0, j - patch_size_j + 1), hr_w - patch_size_j)
else:
raise NotImplementedError
i_end = i_start + patch_size_i
j_end = j_start + patch_size_j
local_i = i - i_start
patch_2d_shape = (B, D, patch_size_i, patch_size_j)
if W_scale != 1:
cond_j_start = 0 if fix_cond else max(0, j_start - cond_patch_shift_j)
cond_j_end = cond_j_start + (patch_size_j // 2)
tar_j_start = max(0, j_start - cond_patch_shift_j) + (hr_w // 2)
tar_j_end = tar_j_start + (patch_size_j // 2)
local_j = j - tar_j_start + (patch_size_j // 2)
pbar.set_postfix(
Step=f'({i},{j}) | Local: ({local_i},{local_j}) | Crop: ({i_start}:{i_end}, {cond_j_start}:{cond_j_end}|{tar_j_start}:{tar_j_end})'
)
cond_patch = z_pred_indices \
.reshape(B, hr_w, hr_h) \
.permute(0, 2, 1)[:, i_start:i_end, cond_j_start:cond_j_end].permute(0, 2, 1)
tar_patch = z_pred_indices \
.reshape(B, hr_w, hr_h) \
.permute(0, 2, 1)[:, i_start:i_end, tar_j_start:tar_j_end].permute(0, 2, 1)
patch = torch.cat([cond_patch, tar_patch], dim=1).reshape(B, patch_size_i * patch_size_j)
cond_t_start = cond_j_start * 0.2
cond_frame_start = int(cond_t_start * FPS)
cond_frame_end = cond_frame_start + c_window_size
tar_frame_start = int(cond_frame_start + c_window_size * W_scale)
tar_frame_end = tar_frame_start + c_window_size
cpatch = torch.cat([c_indices[:, :, cond_frame_start:cond_frame_end], c_indices[:, :, tar_frame_start:tar_frame_end]], dim=2)
else:
local_j = j - j_start
pbar.set_postfix(
Step=f'({i},{j}) | Local: ({local_i},{local_j}) | Crop: ({i_start}:{i_end},{j_start}:{j_end})'
)
patch = z_pred_indices
# assuming we don't crop the conditioning and just use the whole c, if not desired uncomment the above
cpatch = c_indices
logits, _, attention = sampler.transformer(patch[:, :-1], cpatch)
# remove conditioning
logits = logits[:, -patch_size_j*patch_size_i:, :]
local_pos_in_flat = local_j * patch_size_i + local_i
logits = logits[:, local_pos_in_flat, :]
logits = logits / temperature
logits = sampler.top_k_logits(logits, top_x)
# apply softmax to convert to probabilities
probs = torch.nn.functional.softmax(logits, dim=-1)
# sample from the distribution
ix = torch.multinomial(probs, num_samples=1)
z_pred_indices[:, j * hr_h + i] = ix
# quant_z_shape = sampling_shape
if 'dropcond_' in target_log_dir:
z_indices_rec[:, :sampler.clip * W_scale] = z_pred_indices
else:
z_indices_rec[:, :sampler.clip * W_scale] = z_pred_indices[:, sampler.clip * W_scale:]
# print(z_indices_rec)
z_pred_img = sampler.decode_to_img(z_indices_rec,
(1, 256, 5, 53 if vqgan_L == 10.0 else downsampled_size))
mel_z = z_pred_img.detach().cpu().numpy()
with torch.no_grad():
config.data.params.spec_dir_path = 'melspec_10s_22050hz'
if padded:
z_pred_img = z_pred_img[:, :, :, :orig_width]
xrec = xrec[:, :, :, :orig_width]
xprec = xprec[:, :, :, :orig_width]
spec_take_first = orig_width
waves = spec_to_audio_to_st(z_pred_img, config.data.params.spec_dir_path,
config.data.params.sample_rate, show_griffin_lim=show_griffin_lim,
vocoder=melgan, show_in_st=False)
# Original Reconstruction
orig_waves = spec_to_audio_to_st(xrec, config.data.params.spec_dir_path,
config.data.params.sample_rate, show_griffin_lim=show_griffin_lim,
vocoder=melgan, show_in_st=False)
# Conditional Reconstruction
cond_waves = spec_to_audio_to_st(xprec, config.data.params.spec_dir_path,
config.data.params.sample_rate, show_griffin_lim=show_griffin_lim,
vocoder=melgan, show_in_st=False)
if show_griffin_lim:
waves['vocoder'] = waves['inv_transforms'][:int(22050 * L)]
else:
waves['vocoder'] = waves['vocoder'][:int(22050 * L)]
_cond_video_path = cond_video_path
save_path = os.path.join(target_dir, Path(tar_video_path).stem +
'_to_' + Path(_cond_video_path).stem + '.wav')
soundfile.write(save_path, waves['vocoder'], config.data.params.sample_rate, 'PCM_24')
print(f'The sample has been saved @ {save_path}')
save_video_path = os.path.join(target_v_dir, Path(tar_video_path).stem +
'_to_' + Path(_cond_video_path).stem + '.mp4')
attach_audio_to_video(tar_video_path, save_path, save_video_path, 0, v_duration=L)
# Original sound attach
if show_griffin_lim:
waves['vocoder'] = waves['inv_transforms'][:int(22050 * L)]
else:
waves['vocoder'] = waves['vocoder'][:int(22050 * L)]
orig_save_path = os.path.join(target_orig_v_dir, Path(tar_video_path).stem + '.wav')
soundfile.write(orig_save_path, orig_waves['vocoder'], config.data.params.sample_rate, 'PCM_24')
print(f'The sample has been saved @ {orig_save_path}')
orig_save_video_path = os.path.join(target_orig_v_dir, Path(tar_video_path).stem + '.mp4')
attach_audio_to_video(tar_video_path, orig_save_path, orig_save_video_path, 0, recon_only=True, v_duration=L)
# Conditional sound attach
_cond_video_path = cond_video_path
# Save only the first 2sec conditional audio+video if fix_cond
_L = L // W_scale if fix_cond else L
if show_griffin_lim:
waves['vocoder'] = waves['inv_transforms'][:int(22050 * _L)]
else:
waves['vocoder'] = waves['vocoder'][:int(22050 * _L)]
cond_save_path = os.path.join(target_cond_v_dir, Path(tar_video_path).stem +
'_to_' + Path(_cond_video_path).stem + '.wav')
soundfile.write(cond_save_path, cond_waves['vocoder'], config.data.params.sample_rate, 'PCM_24')
print(f'The sample has been saved @ {cond_save_path}')
cond_save_video_path = os.path.join(target_cond_v_dir, Path(tar_video_path).stem +
'_to_' + Path(_cond_video_path).stem + '.mp4')
attach_audio_to_video(_cond_video_path, cond_save_path, cond_save_video_path, 0, recon_only=True, v_duration=_L)
# plot melspec
# target
plt.imshow(mel_xrec[0, 0, :, :spec_take_first], cmap='coolwarm', origin='lower')
plt.axis('off')
plt.savefig(orig_save_video_path.replace('.mp4', '.jpg'), bbox_inches='tight', pad_inches=0.)
plt.close()
# condition
_spec_take_first = int(spec_take_first // W_scale) if fix_cond else spec_take_first
plt.imshow(mel_xprec[0, 0, :, :_spec_take_first], cmap='coolwarm', origin='lower')
plt.axis('off')
plt.savefig(cond_save_video_path.replace('.mp4', '.jpg'), bbox_inches='tight', pad_inches=0.)
plt.close()
# generated
draw_spec(mel_z[0, 0, :, :spec_take_first], save_video_path.replace('.mp4', '.jpg'), cmap='coolwarm')
return
def gen_audio_condImage_fast_multiple(video_path,
extra_cond_video_path,
model,
all_gen_dict,
target_log_dir = 'CondAVTransformer',
SR = 22050,
FPS = 15,
L = 2.0,
normalize=False,
using_torch=False,
show_griffin_lim=False,
vqgan_L=10.0,
style_transfer=False,
target_start_time=0,
cond_start_time=0,
outside=False,
remove_noise=False,
spec_take_first=160,
gen_cnt=25,
W_scale=1,
slide_win_mode='half',
temperature=1.0,
ignore_input_spec=False,
tmp_path='./tmp',
fix_cond=True):
'''
parameters:
video_path: path to the target video, will be trimmed to 2s and re-encode into 15 fps.
extra_cond_video_path: path to the conditional video, will be trimmed to 2s and re-encode into 15 fps.
model: model object, returned by load_model function
target_log_dir: target output dir name in the 'logs' directory, e.g. output will be saved to 'logs/<target_log_dir>'
SR: sampling rate
FPS: Frame rate
L: length of generated sound
normalize: whether to normaliza input waveform
using_torch: use torchaudio to extrac spectrogram
show_griffin_lim: use griffin_lim algorithm vocoder
vqgan_L: length of VQ-GAN codebook, use 2 if using GreatestHit codebook
style_transfer: generate style transfer sound
target_start_time: if target video is from outside, trim from <target_start_time> to <target_start_time>+2
cond_start_time: if conditional video is from outside, trim from <cond_start_time> to <cond_start_time>+2
outside: indicate whether the video from outside source
remove_noise: denoise for outside videos
spec_take_first: size of the spectrogram to use
gen_cnt: count of generation times
W_scale: scale of audio duration as multiples of 2sec
slide_win_mode: mode of sliding window, choose from ['half', 'last']
temperature: temperature of multinomial sampling.
ignore_input_spec: ignore input spec when input video is silent
tmp_path: tmp dir to save intermediate files
fix_cond: use only 2 sec condition regardless to input length.
'''
config, sampler, melgan, melception = model
# feature extractor
L = int(L * W_scale)
vqgan_L = int(vqgan_L * W_scale)
spec_take_first = int(spec_take_first * W_scale)
if '_denoised_' not in video_path or outside:
new_fps_video_path = reencode_video_with_diff_fps(video_path, tmp_path, FPS)
video_path = trim_video(new_fps_video_path, target_start_time, vqgan_L, tmp_path=tmp_path)
frames = [Image.fromarray(f) for f in load_frames(video_path)][:int(FPS * L)]
frames = FRAME_TRANS(frames)
if '_denoised_' not in extra_cond_video_path or outside:
new_fps_video_path = reencode_video_with_diff_fps(extra_cond_video_path, tmp_path, FPS)
extra_cond_video_path = trim_video(new_fps_video_path, cond_start_time, vqgan_L, tmp_path=tmp_path)
cond_frames = [Image.fromarray(f) for f in load_frames(extra_cond_video_path)][:int(FPS * L)]
cond_frames = FRAME_TRANS(cond_frames)
feats = {'feature': np.stack(cond_frames + frames, axis=0)}
cond_video_path = extra_cond_video_path
tar_video_path = video_path
# Extract Features
visual_features = feats
# Prepare Input
batch = default_collate([visual_features])
batch['feature'] = batch['feature'].to(device)
with torch.no_grad():
c = sampler.get_input(sampler.cond_stage_key, batch)
if not ignore_input_spec:
# Extract Spectrogram
spectrogram = extract_melspectrogram(tar_video_path, SR, normalize=normalize, using_torch=using_torch, remove_noise=remove_noise, duration=vqgan_L, tmp_path=tmp_path)
spec_H, spec_W = spectrogram.shape
if spec_W > spec_take_first:
spectrogram = spectrogram[:, :spec_take_first]
else:
pad = np.zeros((spec_H, spec_take_first), dtype=spectrogram.dtype)
pad[:, :spec_W] = spectrogram
spectrogram = pad
spectrogram = {'input': spectrogram}
if config.data.params.spec_crop_len is None or W_scale != 1:
config.data.params.spec_crop_len = spec_take_first
if spectrogram['input'].shape[1] > config.data.params.spec_crop_len:
random_crop = False
crop_img_fn = CropImage([config.data.params.mel_num, config.data.params.spec_crop_len], random_crop)
spectrogram = crop_img_fn(spectrogram)
# Prepare input
batch = default_collate([spectrogram])
batch['image'] = batch['input'].to(device)
x = sampler.get_input(sampler.first_stage_key, batch)
mel_x = x.detach().cpu().numpy()
# Encode and Decode the Spectrogram
with torch.no_grad():
quant_z, z_indices = sampler.encode_to_z(x)
# print(z_indices)
xrec = sampler.first_stage_model.decode(quant_z)
mel_xrec = xrec.detach().cpu().numpy()
# Conditional
# Extract Spectrogram
spectrogram = extract_melspectrogram(cond_video_path, SR, normalize=normalize, using_torch=using_torch, remove_noise=remove_noise, duration=vqgan_L, tmp_path=tmp_path)
spec_H, spec_W = spectrogram.shape
if spec_W > spec_take_first:
spectrogram = spectrogram[:, :spec_take_first]
else:
pad = np.zeros((spec_H, spec_take_first), dtype=spectrogram.dtype)
pad[:, :spec_W] = spectrogram
spectrogram = pad
spectrogram = {'input': spectrogram}
if config.data.params.spec_crop_len is None or W_scale != 1:
config.data.params.spec_crop_len = spec_take_first
if spectrogram['input'].shape[1] > config.data.params.spec_crop_len:
random_crop = False
crop_img_fn = CropImage([config.data.params.mel_num, config.data.params.spec_crop_len], random_crop)
spectrogram = crop_img_fn(spectrogram)
# Prepare input
batch = default_collate([spectrogram])
batch['cond_image'] = batch['input'].to(device)
xp = sampler.get_input(sampler.cond_first_stage_key, batch)
mel_xp = xp.detach().cpu().numpy()
# Encode and Decode the Spectrogram
with torch.no_grad():
quant_zp, zp_indices = sampler.encode_to_z(xp)
# print(zp_indices)
xprec = sampler.first_stage_model.decode(quant_zp)
mel_xprec = xprec.detach().cpu().numpy()
if ignore_input_spec:
z_indices = torch.zeros_like(zp_indices)
xrec = torch.zeros_like(xprec)
mel_xrec = np.zeros_like(mel_xprec)
# Define Sampling Parameters
# take top 1024 / 512 code
top_x = sampler.first_stage_model.quantize.n_e // 2
if not os.path.exists(f'logs/{target_log_dir}'):
os.mkdir(f'logs/{target_log_dir}')
if video_path not in all_gen_dict.keys():
all_gen_dict[video_path] = {}
all_gen_dict[video_path][extra_cond_video_path] = []
# Start sampling
if style_transfer:
content_img = load_specs_as_img(mel_xrec[0, 0, :, :spec_take_first])
style_img = load_specs_as_img(mel_xprec[0, 0, :, :spec_take_first])
generated_spec = run_style_transfer(
cnn_normalization_mean.to(),
cnn_normalization_std.to(),
content_img.clone().to(device),
style_img.clone().to(device),
content_img.clone().to(device),
)
z_pred_img = torch.mean(generated_spec, dim=1, keepdim=True)
mel_z = z_pred_img.detach().cpu().numpy()
else:
for _ in range(gen_cnt):
with torch.no_grad():
start_t = time.time()
quant_c, c_indices = sampler.encode_to_c(c)
z_indices_clip = z_indices[:, :sampler.clip * W_scale]
zp_indices_clip = zp_indices[:, :sampler.clip * W_scale]
z_indices_rec = z_indices.clone()
# crec = sampler.cond_stage_model.decode(quant_c)
patch_size_i = 5
c_window_size = int(2 * FPS)
#TODO: modify the shape if drop condition info
downsampled_size = spec_take_first // 16
cond_patch_shift_j = (W_scale - 1) * (downsampled_size // W_scale)
if 'dropcond_' in target_log_dir:
B, D, hr_h, hr_w = sampling_shape = (1, 256, 5, int(downsampled_size))
patch_size_j = int(downsampled_size // W_scale)
else:
B, D, hr_h, hr_w = sampling_shape = (1, 256, 5, int(2*downsampled_size))
patch_size_j = int(2*downsampled_size // W_scale)
z_pred_indices = torch.zeros((B, hr_h*hr_w)).long().to(device)
if 'dropcond_' not in target_log_dir:
start_step = zp_indices_clip.shape[1]
z_pred_indices[:, :start_step] = zp_indices_clip[:, :start_step]
elif 'dropcond_' in target_log_dir:
start_step = 0
for step in range(start_step, hr_w * hr_h):
i = step % hr_h
j = step // hr_h
i_start = min(max(0, i - (patch_size_i // 2)), hr_h - patch_size_i)
if slide_win_mode == 'half':
j_start = min(max(0, j - (3 * patch_size_j // 4)), hr_w - patch_size_j)
elif slide_win_mode == 'last':
j_start = min(max(0, j - patch_size_j + 1), hr_w - patch_size_j)
else:
raise NotImplementedError
i_end = i_start + patch_size_i
j_end = j_start + patch_size_j
local_i = i - i_start
patch_2d_shape = (B, D, patch_size_i, patch_size_j)
if W_scale != 1:
# if fix cond, we always use first 2 sec of cond audio.
cond_j_start = 0 if fix_cond else max(0, j_start - cond_patch_shift_j)
cond_j_end = cond_j_start + (patch_size_j // 2)
tar_j_start = max(0, j_start - cond_patch_shift_j) + (hr_w // 2)
tar_j_end = tar_j_start + (patch_size_j // 2)
local_j = j - tar_j_start + (patch_size_j // 2)
cond_patch = z_pred_indices \
.reshape(B, hr_w, hr_h) \
.permute(0, 2, 1)[:, i_start:i_end, cond_j_start:cond_j_end].permute(0, 2, 1)
tar_patch = z_pred_indices \
.reshape(B, hr_w, hr_h) \
.permute(0, 2, 1)[:, i_start:i_end, tar_j_start:tar_j_end].permute(0, 2, 1)
patch = torch.cat([cond_patch, tar_patch], dim=1).reshape(B, patch_size_i * patch_size_j)
cond_t_start = cond_j_start * 0.2
cond_frame_start = int(cond_t_start * FPS)
cond_frame_end = cond_frame_start + c_window_size
tar_frame_start = int(cond_frame_start + c_window_size * W_scale)
tar_frame_end = tar_frame_start + c_window_size
cpatch = torch.cat([c_indices[:, :, cond_frame_start:cond_frame_end], c_indices[:, :, tar_frame_start:tar_frame_end]], dim=2)
else:
local_j = j - j_start
patch = z_pred_indices
# assuming we don't crop the conditioning and just use the whole c, if not desired uncomment the above
cpatch = c_indices
logits, _, attention = sampler.transformer(patch[:, :-1], cpatch)
# remove conditioning
logits = logits[:, -patch_size_j*patch_size_i:, :]
local_pos_in_flat = local_j * patch_size_i + local_i
logits = logits[:, local_pos_in_flat, :]
logits = logits / temperature
logits = sampler.top_k_logits(logits, top_x)
# apply softmax to convert to probabilities
probs = torch.nn.functional.softmax(logits, dim=-1)
# sample from the distribution
ix = torch.multinomial(probs, num_samples=1)
z_pred_indices[:, j * hr_h + i] = ix
# quant_z_shape = sampling_shape
if 'dropcond_' in target_log_dir:
z_indices_rec[:, :sampler.clip * W_scale] = z_pred_indices
else:
z_indices_rec[:, :sampler.clip * W_scale] = z_pred_indices[:, sampler.clip * W_scale:]
# print(z_indices_rec)
z_pred_img = sampler.decode_to_img(z_indices_rec,
(1, 256, 5, 53 if vqgan_L == 10.0 else downsampled_size))
mel_z = z_pred_img.detach().cpu().numpy()
config.data.params.spec_dir_path = 'melspec_10s_22050hz'
waves = spec_to_audio_to_st(z_pred_img, config.data.params.spec_dir_path,
config.data.params.sample_rate, show_griffin_lim=show_griffin_lim,
vocoder=melgan, show_in_st=False)
waves['vocoder'] = waves['vocoder'][:int(22050 * L)]
all_gen_dict[video_path][extra_cond_video_path].append(waves['vocoder'])
return
if __name__ == '__main__':
args = parser.parse_args()
model_name = args.model_name
log_dir = './logs'
model = load_model(model_name, log_dir, device, args.new_codebook)
target_log_dir = args.target_log_dir
if args.tmp_idx == -1:
tmp_path = f'./tmp/tmp_{args.split}'
else:
tmp_path = f'./tmp/tmp_{args.tmp_idx}'
os.makedirs(tmp_path, exist_ok=True)
os.makedirs(f'logs/{target_log_dir}', exist_ok=True)
random.seed(3704)
slide_win_mode = args.slide_win_mode
style_transfer = args.style_transfer
if args.gh_demo:
orig_videos = ['data/demo_video/hitting_metal.mp4']
cond_videos = ['data/demo_video/hitting_bag.mp4']