-
Notifications
You must be signed in to change notification settings - Fork 6
/
ctm_inference.py
215 lines (197 loc) · 7.35 KB
/
ctm_inference.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
import argparse
import csv
import json
import os
import random
import time
import numpy as np
import soundfile as sf
import torch
from accelerate.utils import set_seed
from ctm.inference_sampling import karras_sample
from ctm.script_util import (
create_model_and_diffusion,
)
from tango_edm.models_edm import build_pretrained_models
from tqdm import tqdm
class dotdict(dict):
"""dot.notation access to dictionary attributes"""
__getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
def rand_fix(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def parse_args():
parser = argparse.ArgumentParser(description="Inference for text to audio generation task.")
parser.add_argument(
"--training_args", type=str, default=None,
help="Path for 'summary.jsonl' file saved during training."
)
parser.add_argument(
"--output_dir", type=str, default=None,
help="Where to store the output."
)
parser.add_argument(
"--seed", type=int, default=5031,
help="A seed for reproducible inference."
)
parser.add_argument(
"--text_encoder_name", type=str, default="google/flan-t5-large",
help="Text encoder identifier from huggingface.co/models.",
)
parser.add_argument(
"--ctm_unet_model_config", type=str, default="configs/diffusion_model_config.json",
help="UNet model config json path.",
)
parser.add_argument(
"--sampling_rate", type=float, default=16000,
help="Sampling rate of training data",
)
parser.add_argument(
"--target_length", type=float, default=10,
help="Audio length of training data",
)
parser.add_argument(
"--model", type=str, default=None,
help="Path for saved model bin file."
)
parser.add_argument(
"--ema_model", type=str, default=None,
help="Path for saved EMA model bin file."
)
parser.add_argument(
"--sampler", type=str, default='determinisitc',
help="Inference sampling methods. You can choose ['determinisitc' (gamma=0), 'cm_multistep' (gamma=1), 'gamma_multistep', 'onestep']."
)
parser.add_argument(
"--sampling_gamma", type=float, default=0.9,
help="\gamma for gamma-sampling if we use 'gamma_multistep'."
)
parser.add_argument(
"--test_file", type=str, default="data/test_audiocaps_subset.json",
help="json file containing the test prompts for generation."
)
parser.add_argument(
"--test_references", type=str, default="data/audiocaps_test_references/subset",
help="Folder containing the test reference wav files."
)
parser.add_argument(
"--num_steps", type=int, default=1,
help="How many denoising steps for generation.",
)
parser.add_argument(
"--nu", type=float, default=1.,
help="Guidance scale for \nu interpolation."
)
parser.add_argument(
"--omega", type=float, default=3.5,
help="Omega for student model."
)
parser.add_argument(
"--batch_size", type=int, default=1,
help="Batch size for generation.",
)
parser.add_argument(
"--num_samples", type=int, default=1,
help="How many samples per prompt.",
)
parser.add_argument(
"--sigma_data", type=float, default=0.25,
help="Sigma data",
)
parser.add_argument(
"--prefix", type=str, default=None,
help="Add prefix in text prompts.",
)
parser.add_argument(
"--stage1_ckpt", type=str, default='ckpt/audioldm-s-full.ckpt',
help="Path for stage1 model (VAE part)'s checkpoint",
)
args = parser.parse_args()
return args
def main():
if torch.cuda.is_available():
device = torch.device("cuda")
print("GPU is available. Using GPU...")
else:
device = torch.device("cpu")
print("GPU is not available. Using CPU...")
args = parse_args()
if args.seed is not None:
set_seed(args.seed)
rand_fix(args.seed)
train_args = dotdict(json.loads(open(args.training_args).readlines()[0]))
# Load decoder and vocoder
name = "audioldm-s-full"
vae, stft = build_pretrained_models(name, args.stage1_ckpt)
vae, stft = vae.to(device), stft.to(device)
# Load Main network
model, diffusion = create_model_and_diffusion(train_args, teacher=False)
model.to(device)
model.eval()
model.load_state_dict(torch.load(args.model, map_location=device))
ema_ckpt = torch.load(args.ema_model, map_location=device)
state_dict = model.ctm_unet.state_dict()
for i, (name, _value) in enumerate(model.ctm_unet.named_parameters()):
assert name in state_dict
state_dict[name] = ema_ckpt[name]
del state_dict
# Load Data #
if args.prefix:
prefix = args.prefix
else:
prefix = ""
with open(args.test_file, mode='r', encoding='utf-8') as file:
reader = csv.DictReader(file)
text_prompts = [row['caption'] for row in reader]
text_prompts = [prefix + inp for inp in text_prompts]
with open(args.test_file, mode='r', encoding='utf-8') as file:
reader = csv.DictReader(file)
file_names = [row['file_name'] for row in reader]
# Generate #
num_steps, nu, batch_size, num_samples = args.num_steps, args.nu, args.batch_size, args.num_samples
all_outputs = []
for k in tqdm(range(0, len(text_prompts), batch_size)):
text = text_prompts[k: k+batch_size]
with torch.no_grad():
latents = karras_sample(
diffusion=diffusion,
model=model,
shape=(batch_size, train_args.latent_channels, train_args.latent_t_size, train_args.latent_f_size),
steps=num_steps,
cond=text,
nu=args.nu,
model_kwargs={},
device=device,
omega=args.omega,
sampler=args.sampler,
gamma=args.sampling_gamma,
x_T=None,
sigma_min=train_args.sigma_min,
sigma_max=train_args.sigma_max,
)
mel = vae.decode_first_stage(latents)
wave = vae.decode_to_waveform(mel)
wave = (wave.cpu().numpy() * 32768).astype("int16") # This is fixed by pretrained vocoder
wave = wave[:, :int(args.sampling_rate * args.target_length)]
all_outputs += [item for item in wave]
# Save #
exp_id = str(int(time.time()))
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
if num_samples == 1:
if args.omega is not None:
output_dir = "outputs/{}_steps_{}_nu_{}_omega_{}_seed_{}".format(exp_id, num_steps, nu, args.omega, args.seed)
else:
output_dir = "outputs/{}_steps_{}_nu_{}_seed_{}".format(exp_id, num_steps, nu, args.seed)
output_dir = os.path.join(args.output_dir, output_dir)
os.makedirs(output_dir, exist_ok=True)
for j, wav in enumerate(all_outputs):
filename = os.path.splitext(os.path.basename(file_names[j]))[0]
sf.write("{}/{}.wav".format(output_dir, filename), wav, samplerate=args.sampling_rate)
if __name__ == "__main__":
main()