File size: 7,962 Bytes
1ecb721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import librosa
import numpy as np
import torch
from tqdm import tqdm

from tools import VAE_out_put_to_spc, rms_normalize, nnData2Audio
from model.DiffSynthSampler import DiffSynthSampler

def sample_pipeline(device, uNet, VAE, MMM, CLAP_tokenizer,

                    positive_prompts, negative_prompts, batchsize, sample_steps, CFG, seed=None, duration=3.0,

                    freq_resolution=512, time_resolution=256, channels=4, VAE_scale=4, timesteps=1000, noise_strategy="repeat", sampler="ddim", return_latent=True):

    height = int(freq_resolution/VAE_scale)
    width = int(time_resolution/VAE_scale)
    VAE_encoder, VAE_quantizer, VAE_decoder = VAE._encoder, VAE._vq_vae, VAE._decoder

    text2sound_embedding = \
        MMM.get_text_features(**CLAP_tokenizer([positive_prompts], padding=True, return_tensors="pt"))[0].to(device)
    negative_condition = \
        MMM.get_text_features(**CLAP_tokenizer([negative_prompts], padding=True, return_tensors="pt"))[0].to(device)

    mySampler = DiffSynthSampler(timesteps, height=height, channels=channels, noise_strategy=noise_strategy, mute=True)
    mySampler.activate_classifier_free_guidance(CFG, negative_condition)

    mySampler.respace(list(np.linspace(0, timesteps - 1, sample_steps, dtype=np.int32)))

    condition = text2sound_embedding.repeat(batchsize, 1)

    latent_representations, initial_noise = \
    mySampler.sample(model=uNet, shape=(batchsize, channels, height, width), seed=seed,
                      return_tensor=True, condition=condition, sampler=sampler)

    latent_representations = latent_representations[-1]

    quantized_latent_representations, _, (_, _, _) = VAE_quantizer(latent_representations)

    if return_latent:
        return quantized_latent_representations.detach()
    reconstruction_batch = VAE_decoder(quantized_latent_representations).to("cpu").detach().numpy()
    time_resolution = int(time_resolution * ((duration+1) / 4))

    rec_signals = nnData2Audio(reconstruction_batch, resolution=(freq_resolution, time_resolution))
    rec_signals = [rms_normalize(rec_signal) for rec_signal in rec_signals]

    return quantized_latent_representations.detach(), reconstruction_batch, rec_signals

def sample_pipeline_GAN(device, gan_generator, VAE, MMM, CLAP_tokenizer,

                    positive_prompts, negative_prompts, batchsize, sample_steps, CFG, seed=None, duration=3.0,

                    freq_resolution=512, time_resolution=256, channels=4, VAE_scale=4, timesteps=1000, noise_strategy="repeat", sampler="ddim", return_latent=True):

    height = int(freq_resolution/VAE_scale)
    width = int(time_resolution/VAE_scale)
    VAE_encoder, VAE_quantizer, VAE_decoder = VAE._encoder, VAE._vq_vae, VAE._decoder

    text2sound_embedding = \
        MMM.get_text_features(**CLAP_tokenizer([positive_prompts], padding=True, return_tensors="pt"))[0].to(device)

    condition = text2sound_embedding.repeat(batchsize, 1)

    noise = torch.randn(batchsize, channels, height, width).to(device)
    latent_representations = gan_generator(noise, condition)

    quantized_latent_representations, _, (_, _, _) = VAE_quantizer(latent_representations)

    if return_latent:
        return quantized_latent_representations.detach()
    reconstruction_batch = VAE_decoder(quantized_latent_representations).to("cpu").detach().numpy()
    time_resolution = int(time_resolution * ((duration+1) / 4))

    rec_signals = nnData2Audio(reconstruction_batch, resolution=(freq_resolution, time_resolution))
    rec_signals = [rms_normalize(rec_signal) for rec_signal in rec_signals]

    return quantized_latent_representations.detach(), reconstruction_batch, rec_signals

def inpaint_pipeline(device, uNet, VAE, MMM, CLAP_tokenizer, use_dynamic_mask, noising_strength, guidance,

                    positive_prompts, negative_prompts, batchsize, sample_steps, CFG, seed=None, duration=3.0, mask_flexivity=0.99,

                    freq_resolution=512, time_resolution=256, channels=4, VAE_scale=4, timesteps=1000, noise_strategy="repeat", sampler="ddim", return_latent=True):

    height = int(freq_resolution/VAE_scale)
    width = int(time_resolution * ((duration + 1) / 4) / VAE_scale)
    VAE_encoder, VAE_quantizer, VAE_decoder = VAE._encoder, VAE._vq_vae, VAE._decoder


    text2sound_embedding = \
        MMM.get_text_features(**CLAP_tokenizer([positive_prompts], padding=True, return_tensors="pt"))[0]
    negative_condition = \
        MMM.get_text_features(**CLAP_tokenizer([negative_prompts], padding=True, return_tensors="pt"))[0]


    mySampler = DiffSynthSampler(timesteps, height=height, channels=channels, noise_strategy=noise_strategy, mute=True)
    mySampler.activate_classifier_free_guidance(CFG, negative_condition)
    mySampler.respace(list(np.linspace(0, timesteps - 1, sample_steps, dtype=np.int32)))

    condition = text2sound_embedding.repeat(batchsize, 1)
    guidance = guidance.repeat(batchsize, 1, 1, 1).to(device)

    # mask = 1, freeze
    latent_mask = torch.zeros((batchsize, 1, height, width), dtype=torch.float32).to(device)
    latent_mask[:, :, :, -int(time_resolution * (1 / 4) / VAE_scale):] = 1.0

    latent_representations, initial_noise = \
        mySampler.inpaint_sample(model=uNet, shape=(batchsize, channels, height, width),
                                  noising_strength=noising_strength,
                                  guide_img=guidance, mask=latent_mask, return_tensor=True,
                                  condition=condition, sampler=sampler,
                                  use_dynamic_mask=use_dynamic_mask,
                                  end_noise_level_ratio=0.0,
                                  mask_flexivity=mask_flexivity)

    latent_representations = latent_representations[-1]

    quantized_latent_representations, _, (_, _, _) = VAE_quantizer(latent_representations)

    if return_latent:
        return quantized_latent_representations.detach()
    reconstruction_batch = VAE_decoder(quantized_latent_representations).to("cpu").detach().numpy()
    time_resolution = int(time_resolution * ((duration+1) / 4))

    rec_signals = nnData2Audio(reconstruction_batch, resolution=(freq_resolution, time_resolution))
    rec_signals = [rms_normalize(rec_signal) for rec_signal in rec_signals]

    return quantized_latent_representations.detach(), reconstruction_batch, rec_signals


def generate_audios_with_diffuSynth_sample(device, uNet, VAE, MMM, CLAP_tokenizer, num_batches, positive_prompts, negative_prompts="", CFG=6, sample_steps=10):
    diffuSynth_signals = []
    for _ in tqdm(range(num_batches)):
        _, _, signals = sample_pipeline(device, uNet, VAE, MMM, CLAP_tokenizer,
                                        positive_prompts=positive_prompts, negative_prompts=negative_prompts,
                                                      batchsize=16, sample_steps=sample_steps, CFG=CFG, seed=None, return_latent=False)
        diffuSynth_signals.extend(signals)
    return np.array(diffuSynth_signals)


def generate_audios_with_diffuSynth_inpaint(device, uNet, VAE, MMM, CLAP_tokenizer, num_batches, guidance, duration, use_dynamic_mask, noising_strength, positive_prompts, negative_prompts="", CFG=6, sample_steps=10):

    diffuSynth_signals = []
    for _ in tqdm(range(num_batches)):
        _, _, signals = inpaint_pipeline(device, uNet, VAE, MMM, CLAP_tokenizer,
                                         use_dynamic_mask=use_dynamic_mask, noising_strength=noising_strength, guidance=guidance,
                    positive_prompts=positive_prompts, negative_prompts=negative_prompts, batchsize=16, sample_steps=sample_steps, CFG=CFG, seed=None, duration=duration, mask_flexivity=0.999,
                    return_latent=False)
        diffuSynth_signals.extend(signals)
    return np.array(diffuSynth_signals)