-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_video.py
1117 lines (981 loc) · 41.7 KB
/
api_video.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
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
# Originally made by Katherine Crowson (https://github.com/crowsonkb, https://twitter.com/RiversHaveWings)
# The original BigGAN+CLIP method was by https://twitter.com/advadnoun
import ffmpeg
import cv2
import warnings
import re
from subprocess import Popen, PIPE
from PIL import ImageFile, Image, PngImagePlugin, ImageChops
import imageio
import numpy as np
import kornia.augmentation as K
from CLIP import clip
from taming.models import cond_transformer, vqgan
from omegaconf import OmegaConf
from torch_optimizer import DiffGrad, AdamP, RAdam
import math
from typing import Any, Union, List
import time
# from email.policy import default
from urllib.request import urlopen
from tqdm import tqdm
import sys
import os
import wav2clip
import librosa
import torch
from torch import nn, optim
from torch.nn import functional as F
from torchvision import transforms
from torchvision.transforms import functional as TF
from torch.cuda.amp import autocast
# torch.use_deterministic_algorithms(True) # NR: grid_sampler_2d_backward_cuda does not have a deterministic implementation
def clean_file_name(filename: str):
invalid_chars = '[\\\/:*?"<>|]'
replace_char = "-"
return re.sub(invalid_chars, replace_char, filename)
def generate(
filemusic: str,
video_output: str = "output.mp4",
iterations_per_second: int = 30,
output_video_fps: float = 0,
audio_sampling_freq: int = 16000,
display_freq: int = 20,
size: Union[int, int] = [368, 368],
calc_device: str = "cuda:0" if torch.cuda.is_available() else "cpu",
init_image: str = None,
init_noise: str = None,
clip_model: str = "ViT-B/32",
vqgan_config: str = f"checkpoints/vqgan_imagenet_f16_16384.yaml",
vqgan_checkpoint: str = f"checkpoints/vqgan_imagenet_f16_16384.ckpt",
learningrate: float = 0.1,
cut_method: str = "latest",
num_cuts: int = 32,
cut_power: float = 1.0,
seed: int = None,
optimiser: str = "Adam",
display_picname: str = "output.png",
workplace: str = "workplace",
toclean: bool = True,
augments: list = [],
transrate: float = 0,
transimg: str = None,
):
"""
## Function Summary
This function generates an image using the WAV2CLIP+VQGAN algorithm. It takes an audio prompt as input and iteratively updates an initial image to match the prompt using gradient descent. The generated image is saved as output.png.
## Arguments
- `filemusic` (str): The file path of the audio prompt.
- `video_output` (str, optional): The file path to save the generated video. Defaults to "output.mp4".
- `iterations_per_second` (int, optional): The number of iterations to perform per second. Defaults to 30.
- `output_video_fps` (float, optional): The frames per second for the output video. Defaults to 0.
- `audio_sampling_freq` (int, optional): The sampling frequency of the audio. Defaults to 16000.
- `display_freq` (int, optional): The number of iterations between displaying progress. Defaults to 20.
- `size` (Union[int, int], optional): The output size of the image. Defaults to [368, 368].
- `calc_device` (str, optional): The device to use for calculations, either "cuda:0" or "cpu". Defaults to "cuda:0" if torch.cuda.is_available() else "cpu".
- `init_image` (str, optional): The file path of the initial image. Defaults to None.
- `init_noise` (str, optional): The file path of the initial noise image (pixels or gradient). Defaults to None.
- `clip_model` (str, optional): The CLIP model to use (e.g. "ViT-B/32", "ViT-B/16"). Defaults to "ViT-B/32".
- `vqgan_config` (str, optional): The file path of the VQGAN config. Defaults to "checkpoints/vqgan_imagenet_f16_16384.yaml".
- `vqgan_checkpoint` (str, optional): The file path of the VQGAN checkpoint. Defaults to "checkpoints/vqgan_imagenet_f16_16384.ckpt".
- `learningrate` (float, optional): The learning rate for gradient descent. Defaults to 0.1.
- `cut_method` (str, optional): The method to use for cutting the image. Available options are "original", "updated", "nrupdated", "updatedpooling", and "latest". Defaults to "latest".
- `num_cuts` (int, optional): The number of cuts to perform. Defaults to 32.
- `cut_power` (float, optional): The power to use for cutting. Defaults to 1.0.
- `seed` (int, optional): The seed for random number generation. Defaults to None.
- `optimiser` (str, optional): The optimiser to use for gradient descent. Available options are "Adam", "AdamW", "Adagrad", "Adamax", "DiffGrad", "AdamP", "RAdam", and "RMSprop". Defaults to "Adam".
- `display_picname` (str, optional): The filename for the output picture. Defaults to "output.png".
- `workplace` (str, optional): The folder name for the workspace. Defaults to "workplace".
- `toclean` (bool, optional): Whether to reset the workspace folder. Defaults to True.
- `augments` (list, optional): The enabled augmentations for the latest cut method. Available options are "Ji", "Sh", "Gn", "Pe", "Ro", "Af", "Et", "Ts", "Cr", "Er", and "Re". Defaults to [].
- `transrate` (float, optional): The image transform rate. Defaults to 0.
- `transimg` (str, optional): The file path of the image input for transformation. Defaults to None.
## Raises
- `ValueError`: Exception raised if there is an error in the parameters or during execution.
## Returns
- `_type_`: Description of the returned value. (Missing information)
"""
# Setting gpu id to use
# pip install taming-transformers doesn't work with Gumbel, but does not yet work with coco etc
# appending the path does work with Gumbel, but gives ModuleNotFoundError: No module named 'transformers' for coco etc
sys.path.append("taming-transformers")
# import taming.modules
ImageFile.LOAD_TRUNCATED_IMAGES = True
# Supress warnings
warnings.filterwarnings("ignore")
if not augments:
augments = [["Af", "Pe", "Ji", "Er"]]
# Clean up
# input(">> Cleanup the output folder?(Press ENTER to continue)")
os.system("mkdir " + workplace)
if toclean:
for file in os.listdir(workplace):
os.remove(workplace + "/" + file)
# Make steps directory
steps_folder = f"./{clean_file_name(calc_device)}"
if not os.path.exists(steps_folder):
os.mkdir(steps_folder)
# Fallback to CPU if CUDA is not found and make sure GPU video rendering is also disabled
# NB. May not work for AMD cards?
calc_device = torch.device(calc_device)
if calc_device != "cpu" and not torch.cuda.is_available():
calc_device = "cpu"
print(
"Warning: No GPU found! Using the CPU instead. The iterations will be slow."
)
print(
"Perhaps CUDA/ROCm or the right pytorch version is not properly installed?"
)
## Too slow
# if calc_device != "cpu" and torch.backends.cudnn.is_available():
# print("Using cudnn to boost up!")
# torch.backends.cudnn.deterministic = True
# Loading Img inputed by user
if transimg != None:
print("Using Trans Img:", transimg)
User_img = imageio.imread(transimg)
User_img = cv2.resize(User_img, np.array(size))
User_img = torch.from_numpy(User_img).to(calc_device)
# Various functions and classes
def sinc(x):
return torch.where(
x != 0, torch.sin(math.pi * x) / (math.pi * x), x.new_ones([])
)
def lanczos(x, a):
cond = torch.logical_and(-a < x, x < a)
out = torch.where(cond, sinc(x) * sinc(x / a), x.new_zeros([]))
return out / out.sum()
def ramp(ratio, width):
n = math.ceil(width / ratio + 1)
out = torch.empty([n])
cur = 0
for i in range(out.shape[0]):
out[i] = cur
cur += ratio
return torch.cat([-out[1:].flip([0]), out])[1:-1]
# NR: Testing with different intital images
def random_noise_image(w, h):
random_image = Image.fromarray(
np.random.randint(0, 255, (w, h, 3), dtype=np.dtype("uint8"))
)
return random_image
# create initial gradient image
def gradient_2d(start, stop, width, height, is_horizontal):
if is_horizontal:
return np.tile(np.linspace(start, stop, width), (height, 1))
else:
return np.tile(np.linspace(start, stop, height), (width, 1)).T
def gradient_3d(width, height, start_list, stop_list, is_horizontal_list):
result = np.zeros((height, width, len(start_list)), dtype=float)
for i, (start, stop, is_horizontal) in enumerate(
zip(start_list, stop_list, is_horizontal_list)
):
result[:, :, i] = gradient_2d(start, stop, width, height, is_horizontal)
return result
def random_gradient_image(w, h):
array = gradient_3d(
w,
h,
(0, 0, np.random.randint(0, 255)),
(
np.random.randint(1, 255),
np.random.randint(2, 255),
np.random.randint(3, 128),
),
(True, False, False),
)
random_image = Image.fromarray(np.uint8(array))
return random_image
# Used in older MakeCutouts
def resample(input, size, align_corners=True):
n, c, h, w = input.shape
dh, dw = size
input = input.view([n * c, 1, h, w])
if dh < h:
kernel_h = lanczos(ramp(dh / h, 2), 2).to(input.device, input.dtype)
pad_h = (kernel_h.shape[0] - 1) // 2
input = F.pad(input, (0, 0, pad_h, pad_h), "reflect")
input = F.conv2d(input, kernel_h[None, None, :, None])
if dw < w:
kernel_w = lanczos(ramp(dw / w, 2), 2).to(input.device, input.dtype)
pad_w = (kernel_w.shape[0] - 1) // 2
input = F.pad(input, (pad_w, pad_w, 0, 0), "reflect")
input = F.conv2d(input, kernel_w[None, None, None, :])
input = input.view([n, c, h, w])
return F.interpolate(input, size, mode="bicubic", align_corners=align_corners)
class ReplaceGrad(torch.autograd.Function):
@staticmethod
def forward(ctx, x_forward, x_backward):
with autocast():
ctx.shape = x_backward.shape
return x_forward
@staticmethod
def backward(ctx, grad_in):
return None, grad_in.sum_to_size(ctx.shape)
replace_grad = ReplaceGrad.apply
class ClampWithGrad(torch.autograd.Function):
@staticmethod
def forward(ctx, input, min, max):
with autocast():
ctx.min = min
ctx.max = max
ctx.save_for_backward(input)
return input.clamp(min, max)
@staticmethod
def backward(ctx, grad_in):
(input,) = ctx.saved_tensors
return (
grad_in * (grad_in * (input - input.clamp(ctx.min, ctx.max)) >= 0),
None,
None,
)
clamp_with_grad = ClampWithGrad.apply
def vector_quantize(x, codebook):
d = (
x.pow(2).sum(dim=-1, keepdim=True)
+ codebook.pow(2).sum(dim=1)
- 2 * x @ codebook.T
)
indices = d.argmin(-1)
x_q = F.one_hot(indices, codebook.shape[0]).to(d.dtype) @ codebook
return replace_grad(x_q, x)
class Prompt(nn.Module):
def __init__(self, embed, weight=1.0, stop=float("-inf")):
super().__init__()
self.register_buffer("embed", embed)
self.register_buffer("weight", torch.as_tensor(weight))
self.register_buffer("stop", torch.as_tensor(stop))
def forward(self, input):
with autocast():
input_normed = F.normalize(input.unsqueeze(1), dim=2)
embed_normed = F.normalize(self.embed.unsqueeze(0), dim=2)
dists = (
input_normed.sub(embed_normed)
.norm(dim=2)
.div(2)
.arcsin()
.pow(2)
.mul(2)
)
dists = dists * self.weight.sign()
return (
self.weight.abs()
* replace_grad(dists, torch.maximum(dists, self.stop)).mean()
)
# NR: Split prompts and weights
def split_prompt(prompt):
vals = prompt.rsplit(":", 2)
# vals = vals + ['', '1', '-inf'][len(vals):]
vals = vals + ["1", "-inf"]
return vals[0], float(vals[1]), float(vals[2])
class MakeCutouts(nn.Module):
def __init__(self, cut_size, cutn, cut_pow=1.0):
super().__init__()
self.cut_size = cut_size
self.cutn = cutn
self.cut_pow = cut_pow # not used with pooling
# Pick your own augments & their order
augment_list = []
for item in augments[0]:
if item == "Ji":
augment_list.append(
K.ColorJitter(
brightness=0.1,
contrast=0.1,
saturation=0.1,
hue=0.1,
p=0.7,
)
)
elif item == "Sh":
augment_list.append(K.RandomSharpness(sharpness=0.3, p=0.5))
elif item == "Gn":
augment_list.append(K.RandomGaussianNoise(mean=0.0, std=1.0, p=0.5))
elif item == "Pe":
augment_list.append(
K.RandomPerspective(distortion_scale=0.7, p=0.7)
)
elif item == "Ro":
augment_list.append(K.RandomRotation(degrees=15, p=0.7))
elif item == "Af":
augment_list.append(
K.RandomAffine(
degrees=15,
translate=0.1,
shear=5,
p=0.7,
padding_mode="zeros",
keepdim=True,
)
) # border, reflection, zeros
elif item == "Et":
augment_list.append(K.RandomElasticTransform(p=0.7))
elif item == "Ts":
augment_list.append(
K.RandomThinPlateSpline(scale=0.8, same_on_batch=True, p=0.7)
)
elif item == "Cr":
augment_list.append(
K.RandomCrop(
size=(self.cut_size, self.cut_size),
pad_if_needed=True,
padding_mode="reflect",
p=0.5,
)
)
elif item == "Er":
augment_list.append(
K.RandomErasing(
scale=(0.1, 0.4),
ratio=(0.3, 1 / 0.3),
same_on_batch=True,
p=0.7,
)
)
elif item == "Re":
augment_list.append(
K.RandomResizedCrop(
size=(self.cut_size, self.cut_size),
scale=(0.1, 1),
ratio=(0.75, 1.333),
cropping_mode="resample",
p=0.5,
)
)
self.augs = nn.Sequential(*augment_list)
self.noise_fac = 0.1
# self.noise_fac = False
# Uncomment if you like seeing the list ;)
# print(augment_list)
# Pooling
self.av_pool = nn.AdaptiveAvgPool2d((self.cut_size, self.cut_size))
self.max_pool = nn.AdaptiveMaxPool2d((self.cut_size, self.cut_size))
def forward(self, input):
with autocast():
cutouts = []
for _ in range(self.cutn):
# Use Pooling
cutout = (self.av_pool(input) + self.max_pool(input)) / 2
cutouts.append(cutout)
batch = self.augs(torch.cat(cutouts, dim=0))
if self.noise_fac:
facs = batch.new_empty([self.cutn, 1, 1, 1]).uniform_(
0, self.noise_fac
)
batch = batch + facs * torch.randn_like(batch)
return batch
# An updated version with Kornia augments and pooling (where my version started):
class MakeCutoutsPoolingUpdate(nn.Module):
def __init__(self, cut_size, cutn, cut_pow=1.0):
super().__init__()
self.cut_size = cut_size
self.cutn = cutn
self.cut_pow = cut_pow # Not used with pooling
self.augs = nn.Sequential(
K.RandomAffine(degrees=15, translate=0.1, p=0.7, padding_mode="border"),
K.RandomPerspective(0.7, p=0.7),
K.ColorJitter(hue=0.1, saturation=0.1, p=0.7),
K.RandomErasing((0.1, 0.4), (0.3, 1 / 0.3), same_on_batch=True, p=0.7),
)
self.noise_fac = 0.1
self.av_pool = nn.AdaptiveAvgPool2d((self.cut_size, self.cut_size))
self.max_pool = nn.AdaptiveMaxPool2d((self.cut_size, self.cut_size))
def forward(self, input):
with autocast():
sideY, sideX = input.shape[2:4]
max_size = min(sideX, sideY)
min_size = min(sideX, sideY, self.cut_size)
cutouts = []
for _ in range(self.cutn):
cutout = (self.av_pool(input) + self.max_pool(input)) / 2
cutouts.append(cutout)
batch = self.augs(torch.cat(cutouts, dim=0))
if self.noise_fac:
facs = batch.new_empty([self.cutn, 1, 1, 1]).uniform_(
0, self.noise_fac
)
batch = batch + facs * torch.randn_like(batch)
return batch
# An Nerdy updated version with selectable Kornia augments, but no pooling:
class MakeCutoutsNRUpdate(nn.Module):
def __init__(self, cut_size, cutn, cut_pow=1.0):
super().__init__()
self.cut_size = cut_size
self.cutn = cutn
self.cut_pow = cut_pow
self.noise_fac = 0.1
# Pick your own augments & their order
augment_list = []
for item in augments[0]:
if item == "Ji":
augment_list.append(
K.ColorJitter(
brightness=0.1, contrast=0.1, saturation=0.1, hue=0.1, p=0.7
)
)
elif item == "Sh":
augment_list.append(K.RandomSharpness(sharpness=0.3, p=0.5))
elif item == "Gn":
augment_list.append(K.RandomGaussianNoise(mean=0.0, std=1.0, p=0.5))
elif item == "Pe":
augment_list.append(
K.RandomPerspective(distortion_scale=0.5, p=0.7)
)
elif item == "Ro":
augment_list.append(K.RandomRotation(degrees=15, p=0.7))
elif item == "Af":
augment_list.append(
K.RandomAffine(
degrees=30,
translate=0.1,
shear=5,
p=0.7,
padding_mode="zeros",
keepdim=True,
)
) # border, reflection, zeros
elif item == "Et":
augment_list.append(K.RandomElasticTransform(p=0.7))
elif item == "Ts":
augment_list.append(
K.RandomThinPlateSpline(scale=0.8, same_on_batch=True, p=0.7)
)
elif item == "Cr":
augment_list.append(
K.RandomCrop(
size=(self.cut_size, self.cut_size),
pad_if_needed=True,
padding_mode="reflect",
p=0.5,
)
)
elif item == "Er":
augment_list.append(
K.RandomErasing(
scale=(0.1, 0.4),
ratio=(0.3, 1 / 0.3),
same_on_batch=True,
p=0.7,
)
)
elif item == "Re":
augment_list.append(
K.RandomResizedCrop(
size=(self.cut_size, self.cut_size),
scale=(0.1, 1),
ratio=(0.75, 1.333),
cropping_mode="resample",
p=0.5,
)
)
self.augs = nn.Sequential(*augment_list)
def forward(self, input):
with autocast():
sideY, sideX = input.shape[2:4]
max_size = min(sideX, sideY)
min_size = min(sideX, sideY, self.cut_size)
cutouts = []
for _ in range(self.cutn):
size = int(
torch.rand([]) ** self.cut_pow * (max_size - min_size)
+ min_size
)
offsetx = torch.randint(0, sideX - size + 1, ())
offsety = torch.randint(0, sideY - size + 1, ())
cutout = input[
:, :, offsety : offsety + size, offsetx : offsetx + size
]
cutouts.append(resample(cutout, (self.cut_size, self.cut_size)))
batch = self.augs(torch.cat(cutouts, dim=0))
if self.noise_fac:
facs = batch.new_empty([self.cutn, 1, 1, 1]).uniform_(
0, self.noise_fac
)
batch = batch + facs * torch.randn_like(batch)
return batch
# An updated version with Kornia augments, but no pooling:
class MakeCutoutsUpdate(nn.Module):
def __init__(self, cut_size, cutn, cut_pow=1.0):
super().__init__()
self.cut_size = cut_size
self.cutn = cutn
self.cut_pow = cut_pow
self.augs = nn.Sequential(
K.RandomHorizontalFlip(p=0.5),
K.ColorJitter(hue=0.01, saturation=0.01, p=0.7),
# K.RandomSolarize(0.01, 0.01, p=0.7),
K.RandomSharpness(0.3, p=0.4),
K.RandomAffine(degrees=30, translate=0.1, p=0.8, padding_mode="border"),
K.RandomPerspective(0.2, p=0.4),
)
self.noise_fac = 0.1
def forward(self, input):
with autocast():
sideY, sideX = input.shape[2:4]
max_size = min(sideX, sideY)
min_size = min(sideX, sideY, self.cut_size)
cutouts = []
for _ in range(self.cutn):
size = int(
torch.rand([]) ** self.cut_pow * (max_size - min_size)
+ min_size
)
offsetx = torch.randint(0, sideX - size + 1, ())
offsety = torch.randint(0, sideY - size + 1, ())
cutout = input[
:, :, offsety : offsety + size, offsetx : offsetx + size
]
cutouts.append(resample(cutout, (self.cut_size, self.cut_size)))
batch = self.augs(torch.cat(cutouts, dim=0))
if self.noise_fac:
facs = batch.new_empty([self.cutn, 1, 1, 1]).uniform_(
0, self.noise_fac
)
batch = batch + facs * torch.randn_like(batch)
return batch
# This is the original version (No pooling)
class MakeCutoutsOrig(nn.Module):
def __init__(self, cut_size, cutn, cut_pow=1.0):
super().__init__()
self.cut_size = cut_size
self.cutn = cutn
self.cut_pow = cut_pow
def forward(self, input):
with autocast():
sideY, sideX = input.shape[2:4]
max_size = min(sideX, sideY)
min_size = min(sideX, sideY, self.cut_size)
cutouts = []
for _ in range(self.cutn):
size = int(
torch.rand([]) ** self.cut_pow * (max_size - min_size)
+ min_size
)
offsetx = torch.randint(0, sideX - size + 1, ())
offsety = torch.randint(0, sideY - size + 1, ())
cutout = input[
:, :, offsety : offsety + size, offsetx : offsetx + size
]
cutouts.append(resample(cutout, (self.cut_size, self.cut_size)))
return clamp_with_grad(torch.cat(cutouts, dim=0), 0, 1)
def load_vqgan_model(config_path, checkpoint_path):
config = OmegaConf.load(config_path)
if config.model.target == "taming.models.vqgan.VQModel":
model = vqgan.VQModel(**config.model.params)
model.eval().requires_grad_(False)
model.init_from_ckpt(checkpoint_path)
elif config.model.target == "taming.models.vqgan.GumbelVQ":
ValueError(f"Gumble model not supported now: {config.model.target}")
elif config.model.target == "taming.models.cond_transformer.Net2NetTransformer":
parent_model = cond_transformer.Net2NetTransformer(**config.model.params)
parent_model.eval().requires_grad_(False)
parent_model.init_from_ckpt(checkpoint_path)
model = parent_model.first_stage_model
else:
raise ValueError(f"unknown model type: {config.model.target}")
del model.loss
return model
def resize_image(image, out_size):
ratio = image.size[0] / image.size[1]
area = min(image.size[0] * image.size[1], out_size[0] * out_size[1])
size = round((area * ratio) ** 0.5), round((area / ratio) ** 0.5)
return image.resize(size, Image.LANCZOS)
# Do it
model = load_vqgan_model(vqgan_config, vqgan_checkpoint).to(calc_device)
jit = True if "1.7.1" in torch.__version__ else False
perceptor = (
clip.load(clip_model, calc_device, jit=jit)[0].eval().requires_grad_(False)
)
# clock=deepcopy(perceptor.visual.positional_embedding.data)
# perceptor.visual.positional_embedding.data = clock/clock.max()
# perceptor.visual.positional_embedding.data=clamp_with_grad(clock,0,1)
cut_size = perceptor.visual.input_resolution
f = 2 ** (model.decoder.num_resolutions - 1)
# Cutout class options:
# 'latest','original','updated' or 'updatedpooling'
if cut_method == "latest":
make_cutouts = MakeCutouts(cut_size, num_cuts, cut_pow=cut_power)
elif cut_method == "original":
make_cutouts = MakeCutoutsOrig(cut_size, num_cuts, cut_pow=cut_power)
elif cut_method == "updated":
make_cutouts = MakeCutoutsUpdate(cut_size, num_cuts, cut_pow=cut_power)
elif cut_method == "nrupdated":
make_cutouts = MakeCutoutsNRUpdate(cut_size, num_cuts, cut_pow=cut_power)
else:
make_cutouts = MakeCutoutsPoolingUpdate(cut_size, num_cuts, cut_pow=cut_power)
toksX, toksY = size[0] // f, size[1] // f
sideX, sideY = toksX * f, toksY * f
# Gumbel or not?
e_dim = model.quantize.e_dim
n_toks = model.quantize.n_e
z_min = model.quantize.embedding.weight.min(dim=0).values[None, :, None, None]
z_max = model.quantize.embedding.weight.max(dim=0).values[None, :, None, None]
if init_image:
if "http" in init_image:
img = Image.open(urlopen(init_image))
else:
img = Image.open(init_image)
pil_image = img.convert("RGB")
pil_image = pil_image.resize((sideX, sideY), Image.LANCZOS)
pil_tensor = TF.to_tensor(pil_image)
z, *_ = model.encode(pil_tensor.to(calc_device).unsqueeze(0) * 2 - 1)
elif init_noise == "pixels":
img = random_noise_image(size[0], size[1])
pil_image = img.convert("RGB")
pil_image = pil_image.resize((sideX, sideY), Image.LANCZOS)
pil_tensor = TF.to_tensor(pil_image)
z, *_ = model.encode(pil_tensor.to(calc_device).unsqueeze(0) * 2 - 1)
elif init_noise == "gradient":
img = random_gradient_image(size[0], size[1])
pil_image = img.convert("RGB")
pil_image = pil_image.resize((sideX, sideY), Image.LANCZOS)
pil_tensor = TF.to_tensor(pil_image)
z, *_ = model.encode(pil_tensor.to(calc_device).unsqueeze(0) * 2 - 1)
else:
one_hot = F.one_hot(
torch.randint(n_toks, [toksY * toksX], device=calc_device), n_toks
).float()
z = one_hot @ model.quantize.embedding.weight
z = z.view([-1, toksY, toksX, e_dim]).permute(0, 3, 1, 2)
# z = torch.rand_like(z)*2 # NR: check
z_orig = z.clone()
z.requires_grad_(True)
pMs = []
normalize = transforms.Normalize(
mean=[0.48145466, 0.4578275, 0.40821073],
std=[0.26862954, 0.26130258, 0.27577711],
)
# From imagenet - Which is better?
# normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
# std=[0.229, 0.224, 0.225])
# Set the optimiser
def get_opt(opt_name, opt_lr):
if opt_name == "Adam":
opt = optim.Adam([z], lr=opt_lr) # LR=0.1 (Default)
elif opt_name == "AdamW":
opt = optim.AdamW([z], lr=opt_lr)
elif opt_name == "Adagrad":
opt = optim.Adagrad([z], lr=opt_lr)
elif opt_name == "Adamax":
opt = optim.Adamax([z], lr=opt_lr)
elif opt_name == "DiffGrad":
opt = DiffGrad(
[z], lr=opt_lr, eps=1e-9, weight_decay=1e-9
) # NR: Playing for reasons
elif opt_name == "AdamP":
opt = AdamP([z], lr=opt_lr)
elif opt_name == "RAdam":
opt = RAdam([z], lr=opt_lr)
elif opt_name == "RMSprop":
opt = optim.RMSprop([z], lr=opt_lr)
else:
print("Unknown optimiser. Are choices broken?")
opt = optim.Adam([z], lr=opt_lr)
return opt
opt = get_opt(optimiser, learningrate)
# Output for the user
print("Using device:", calc_device)
print("Optimising using:", optimiser)
# if args.prompts:
# print("Using text prompts:", args.prompts)
if filemusic:
print("Using audio prompts:", filemusic)
if init_image:
print("Using initial image:", init_image)
# Vector quantize
def synth(z):
z_q = vector_quantize(z.movedim(1, 3), model.quantize.embedding.weight).movedim(
3, 1
)
return clamp_with_grad(model.decode(z_q).add(1).div(2), 0, 1)
# @torch.no_grad()
@torch.inference_mode()
def checkin(i, losses):
losses_str = ", ".join(f"{loss.item():g}" for loss in losses)
tqdm.write(f"i: {i}, loss: {sum(losses).item():g}, losses: {losses_str}")
out = synth(z)
info = PngImagePlugin.PngInfo()
# info.add_text("comment", f"{args.prompts}")
TF.to_pil_image(out[0].cpu()).save(
workplace + "/" + display_picname, pnginfo=info
)
def clac_loss(i):
out = synth(z)
iii = (
perceptor.encode_image(normalize(make_cutouts(out))).to(calc_device).float()
)
result = []
for prompt in pMs:
result.append(prompt(iii))
if transimg != None:
img = torch.squeeze(out, 0).transpose(0, 1).transpose(1, 2)
minus = (img - User_img / 255).reshape(-1)
loss_trans = torch.dot(minus, minus) / minus.shape[0] * 4 * transrate
result.append(loss_trans)
img = np.array(
out.mul(255).clamp(0, 255)[0].cpu().detach().numpy().astype(np.uint8)
)[:, :, :]
img = np.transpose(img, (1, 2, 0))
imageio.imwrite(f"{steps_folder}/" + str(i) + ".png", np.array(img))
return result # return loss
def train(i):
opt.zero_grad(set_to_none=True)
lossAll = clac_loss(i)
if i % display_freq == 0:
checkin(i, lossAll)
loss = sum(lossAll)
loss.backward()
opt.step()
# with torch.no_grad():
with torch.inference_mode():
z.copy_(z.maximum(z_min).minimum(z_max))
return f"{steps_folder}/" + str(i) + ".png"
# Loading the models & Getting total video length
wav2clip_model = wav2clip.get_model()
audio_lst = []
fps_lst = []
audiotimestamp_lst = []
audio_length = []
iterations_num = []
audio, sr = librosa.load(filemusic, sr=audio_sampling_freq)
total_seconds = int(len(audio) // sr)
tempo, beats_raw = librosa.beat.beat_track(y=audio, sr=sr)
spec = librosa.feature.melspectrogram(
y=audio, sr=sr, n_mels=128, fmax=8000, hop_length=512
)
# get mean power at each time point
specm = np.mean(spec, axis=0)
# normalize mean power between 0-1
specm = (specm - np.min(specm)) / np.ptp(specm)
beats_raw = beats_raw * 512 # indexed audio before sampling
beats = [0]
for i in range(len(beats_raw)): # 每两拍记一次
if i % 2 == 0:
beats.append(beats_raw[i])
beats.append(len(audio) - 1)
def interpolate_array(arr, interval):
i = 0
while i < len(arr) - 1:
diff = abs(arr[i + 1] - arr[i])
if diff > interval:
new_value = (arr[i + 1] + arr[i]) / 2
arr.insert(i + 1, int(new_value + 0.5))
else:
i += 1
return arr
beats = interpolate_array(beats, 4 * sr)
length_cum = 0
# input()
for i in range(len(beats) - 1):
audio_seg = audio[beats[i] : beats[i + 1]]
length = (beats[i + 1] - beats[i]) / sr
audio_lst.append(audio_seg)
audio_length.append(length)
# fps_lst.append()
length_cum += length
iterations_num.append(int(length * iterations_per_second + 0.5))
print(
"Clip: " + str(i) + "/" + str(len(beats) - 1),
": length",
time.ctime(16 * 3600 + length_cum - length)[-13:-5],
"~",
time.ctime(16 * 3600 + length_cum)[-13:-5],
length,
length * iterations_per_second,
int(length * iterations_per_second + 0.5),
)
audiotimestamp_lst.append(
"Clip: "
+ str(i)
+ "/"
+ str(len(beats) - 1)
+ " "
+ ": length"
+ " "
+ time.ctime(16 * 3600 + length_cum - length)[-13:-5]
+ "~"
+ time.ctime(16 * 3600 + length_cum)[-13:-5]
+ " "
+ str(length)
+ " "
+ str(length * iterations_per_second)
+ " "
+ str(int(length * iterations_per_second + 0.5))
)
print("Iterations_num:", np.sum(iterations_num))
# input("Ready to start?")
# lyrics_index = 0
# Looping to create segments of mp4 files
for a in range(len(audio_length)):
print("\n" + audiotimestamp_lst[a])
# text_turn = False
# Randomly initializing seed in each video clip
if seed == None:
seed = torch.seed()
torch.manual_seed(seed)
pMs = []
# WavCLIP embedding
audio = audio_lst[a]
# print(audio.shape)
# input("Are you OK?")
pMs.append(
Prompt(
torch.from_numpy(wav2clip.embed_audio(audio, wav2clip_model)),
float(1.0),
float("-inf"),
).to(calc_device)
)
# input("Go Now?")
if a != 0:
# Initial Image embedding
prompt = workplace + "/" + display_picname
path, weight, stop = split_prompt(prompt)
img = Image.open(path)
pil_image = img.convert("RGB")
img = resize_image(pil_image, (sideX, sideY))
batch = make_cutouts(TF.to_tensor(img).unsqueeze(0).to(calc_device))
pMs.append(
Prompt(
perceptor.encode_image(normalize(batch)).to(calc_device).float(),
weight,
stop,
).to(calc_device)
)
# print(batch)
i = 0 # Iteration counter
p = 1 # Phrase counter
smoother = 0 # Smoother counter
this_video_frame = 0 # for video styling
# Do it
with tqdm() as pbar:
while True:
# Training time
if i == 1 and a > 0:
pMs.pop() # Getting rid of initial image prompt after first iteration
train(i)
# input("Trained...")
# Ready to stop yet?
if i == iterations_num[a]:
break
i += 1
pbar.update()
# All done :)
# Video generation
init_frame = 0 # Initial video frame
last_frame = (
i # This will raise an error if that number of frames does not exist.
)
length = audio_length[a]
# length = args.video_length # Desired time of the video in seconds
min_fps = 10