-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_latents.py
352 lines (298 loc) · 12.6 KB
/
get_latents.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
import time
import argparse
import json
import logging
import math
import os
from tqdm import tqdm
from pathlib import Path
from torch.utils.tensorboard import SummaryWriter
import datasets
import numpy as np
import pandas as pd
# import wandb
import torch
from accelerate import Accelerator
from accelerate.logging import get_logger
from accelerate.utils import set_seed
from datasets import load_dataset
from torch.utils.data import Dataset, DataLoader
from tqdm.auto import tqdm
import soundfile as sf
import diffusers
import transformers
import tools.torch_tools as torch_tools
from huggingface_hub import snapshot_download
from models import build_pretrained_models, AudioDiffusion
from transformers import SchedulerType, get_scheduler
def parse_args():
parser = argparse.ArgumentParser(description="Finetune a diffusion model for text to audio generation task.")
parser.add_argument(
"--train_file", type=str, default="data/train_audiocaps.json",
help="A csv or a json file containing the training data."
)
parser.add_argument(
"--validation_file", type=str, default="data/valid_audiocaps.json",
help="A csv or a json file containing the validation data."
)
parser.add_argument(
"--test_file", type=str, default="data/test_audiocaps_subset.json",
help="A csv or a json file containing the test data for generation."
)
parser.add_argument(
"--num_examples", type=int, default=-1,
help="How many examples to use for training and validation.",
)
parser.add_argument(
"--subset", type=str, default="train",
help="Text encoder identifier from huggingface.co/models.",
)
parser.add_argument(
"--scheduler_name", type=str, default="/home/huangqiaochu/dtj/tango/huggingface/stabilityai--stable-diffusion-2-1/scheduler_config.json",
help="Scheduler identifier.",
)
parser.add_argument(
"--unet_model_name", type=str, default=None,
help="UNet model identifier from huggingface.co/models.",
)
parser.add_argument(
"--unet_model_config", type=str, default='configs/diffusion_model_config.json',
help="UNet model config json path.",
)
parser.add_argument(
"--hf_model", type=str, default=None,
help="Tango model identifier from huggingface: declare-lab/tango",
)
parser.add_argument(
"--snr_gamma", type=float, default=None,
help="SNR weighting gamma to be used if rebalancing the loss. Recommended value is 5.0. "
"More details here: https://arxiv.org/abs/2303.09556.",
)
parser.add_argument(
"--freeze_text_encoder", action="store_true", default=True,
help="Freeze the text encoder model.",
)
parser.add_argument(
"--text_column", type=str, default="captions",
help="The name of the column in the datasets containing the input texts.",
)
parser.add_argument(
"--audio_column", type=str, default="location",
help="The name of the column in the datasets containing the audio paths.",
)
parser.add_argument(
"--augment", action="store_true", default=False,
help="Augment training data.",
)
parser.add_argument(
"--uncondition", action="store_true", default=False,
help="10% uncondition for training.",
)
parser.add_argument(
"--prefix", type=str, default=None,
help="Add prefix in text prompts.",
)
parser.add_argument(
"--per_device_train_batch_size", type=int, default=32,
help="Batch size (per device) for the training dataloader.",
)
parser.add_argument(
"--per_device_eval_batch_size", type=int, default=2,
help="Batch size (per device) for the validation dataloader.",
)
parser.add_argument(
"--learning_rate", type=float, default=3e-5,
help="Initial learning rate (after the potential warmup period) to use.",
)
parser.add_argument(
"--weight_decay", type=float, default=1e-8,
help="Weight decay to use."
)
parser.add_argument(
"--num_train_epochs", type=int, default=40,
help="Total number of training epochs to perform."
)
parser.add_argument(
"--max_train_steps", type=int, default=None,
help="Total number of training steps to perform. If provided, overrides num_train_epochs.",
)
parser.add_argument(
"--gradient_accumulation_steps", type=int, default=4,
help="Number of updates steps to accumulate before performing a backward/update pass.",
)
parser.add_argument(
"--lr_scheduler_type", type=SchedulerType, default="linear",
help="The scheduler type to use.",
choices=["linear", "cosine", "cosine_with_restarts", "polynomial", "constant", "constant_with_warmup"],
)
parser.add_argument(
"--num_warmup_steps", type=int, default=0,
help="Number of steps for the warmup in the lr scheduler."
)
parser.add_argument(
"--adam_beta1", type=float, default=0.9,
help="The beta1 parameter for the Adam optimizer."
)
parser.add_argument(
"--adam_beta2", type=float, default=0.999,
help="The beta2 parameter for the Adam optimizer."
)
parser.add_argument(
"--adam_weight_decay", type=float, default=1e-2,
help="Weight decay to use."
)
parser.add_argument(
"--adam_epsilon", type=float, default=1e-08,
help="Epsilon value for the Adam optimizer"
)
parser.add_argument(
"--output_dir", type=str, default=None,
help="Where to store the final model."
)
parser.add_argument(
"--seed", type=int, default=1234,
help="A seed for reproducible training."
)
parser.add_argument(
"--checkpointing_steps", type=str, default="best",
help="Whether the various states should be saved at the end of every 'epoch' or 'best' whenever validation loss decreases.",
)
parser.add_argument(
"--resume_from_checkpoint", type=str, default=None,
help="If the training should continue from a local checkpoint folder.",
)
parser.add_argument(
"--with_tracking", action="store_true",
help="Whether to enable experiment trackers for logging.",
)
parser.add_argument(
"--report_to", type=str, default="all",
help=(
'The integration to report the results and logs to. Supported platforms are `"tensorboard"`,'
' `"wandb"`, `"comet_ml"` and `"clearml"`. Use `"all"` (default) to report to all integrations.'
"Only applicable when `--with_tracking` is passed."
),
)
args = parser.parse_args()
# Sanity checks
if args.train_file is None and args.validation_file is None:
raise ValueError("Need a training/validation file.")
else:
if args.train_file is not None:
extension = args.train_file.split(".")[-1]
assert extension in ["csv", "json"], "`train_file` should be a csv or a json file."
if args.validation_file is not None:
extension = args.validation_file.split(".")[-1]
assert extension in ["csv", "json"], "`validation_file` should be a csv or a json file."
return args
class Text2AudioDataset(Dataset):
def __init__(self, dataset, prefix, text_column, audio_column, num_examples=-1):
inputs = list(dataset[text_column])
self.inputs = [prefix + inp for inp in inputs]
self.audios = list(dataset[audio_column])
self.indices = list(range(len(self.inputs)))
self.mapper = {}
for index, audio, text in zip(self.indices, self.audios, inputs):
self.mapper[index] = [audio, text]
if num_examples != -1:
self.inputs, self.audios = self.inputs[:num_examples], self.audios[:num_examples]
self.indices = self.indices[:num_examples]
def __len__(self):
return len(self.inputs)
def get_num_instances(self):
return len(self.inputs)
def __getitem__(self, index):
s1, s2, s3 = self.inputs[index], self.audios[index], self.indices[index]
return s1, s2, s3
def collate_fn(self, data):
dat = pd.DataFrame(data)
return [dat[i].tolist() for i in dat]
def save_latent(latent, savepath, name="outwav"):
for i in range(latent.shape[0]):
fname = "%s.pth" % os.path.basename(name[i]) if (not ".pth" in name[i]) else os.path.basename(name[i]).split(".")[0]
path = os.path.join(
savepath, fname
)
# print("Save latents to %s" % path)
torch.save(latent[i].clone(), path)
def save_mel(mel, savepath, name="outwav"):
for i in range(mel.shape[0]):
fname = "%s.pth" % os.path.basename(name[i]) if (not ".pth" in name[i]) else os.path.basename(name[i]).split(".")[0]
path = os.path.join(
savepath, fname
)
# print("Save latents to %s" % path)
torch.save(mel[i].clone(), path)
def main():
args = parse_args()
accelerator_log_kwargs = {}
if args.with_tracking:
accelerator_log_kwargs["log_with"] = args.report_to
accelerator_log_kwargs["logging_dir"] = args.output_dir
accelerator = Accelerator(gradient_accumulation_steps=args.gradient_accumulation_steps, **accelerator_log_kwargs)
# Make one log on every process with the configuration for debugging.
# If passed along, set the training seed now.
if args.seed is not None:
set_seed(args.seed)
# Handle output directory creation and wandb tracking, 只在主进程设置
# Get the datasets
data_files = {}
if args.train_file is not None:
data_files["train"] = args.train_file
if args.validation_file is not None:
data_files["validation"] = args.validation_file
if args.test_file is not None:
data_files["test"] = args.test_file
else:
if args.validation_file is not None:
data_files["test"] = args.validation_file
extension = args.train_file.split(".")[-1]
raw_datasets = load_dataset(extension, data_files=data_files)
text_column, audio_column = args.text_column, args.audio_column
# Initialize models
pretrained_model_name = "audioldm-s-full"
vae, stft = build_pretrained_models(pretrained_model_name)
vae.eval()
stft.eval()
if args.prefix:
prefix = args.prefix
else:
prefix = ""
subset = args.subset
assert subset in ["train", "validation", "test"], "Subset must be one of train, validation, test."
with accelerator.main_process_first():
dataset = Text2AudioDataset(raw_datasets[subset], prefix, text_column, audio_column, args.num_examples)
accelerator.print("Num instances in dataset_{}: {}".format(dataset.get_num_instances(), subset))
dataloader = DataLoader(dataset, shuffle=False, batch_size=args.per_device_train_batch_size, collate_fn=dataset.collate_fn)
# We need to initialize the trackers we use, and also store our configuration.
# The trackers initializes automatically on the main process.
# Only show the progress bar once on each machine.
progress_bar = tqdm(range(len(dataloader)), disable=not accelerator.is_local_main_process)
# Duration of the audio clips in seconds
mel_path = '/home/huangqiaochu/dtj/data/audiocaps/mel/' + subset
latent_path = '/home/huangqiaochu/dtj/data/audiocaps/latents/' + subset
os.makedirs(mel_path, exist_ok=True)
os.makedirs(latent_path, exist_ok=True)
if subset == 'validation' :
subset = 'val'
dataset_path = '/cfs3/share/corpus/audio/audiocaps/' + subset
for epoch in range(1):
for step, batch in enumerate(dataloader):
device = accelerator.device
text, audios, _ = batch
target_length = int(10 * 102.4)
for i, audio in enumerate(audios):
# temp = audio.split('/')[-1].split('_')[-1]
name ='Y'+ '_'.join(audio.split('/')[-1].split('_')[:-1]) + '.wav'
audios[i] = os.path.join(dataset_path, name)
text[i] = name.split('/')[-1].split('.')[0]
with torch.no_grad():
unwrapped_vae = accelerator.unwrap_model(vae).to(device)
mel, _, waveform = torch_tools.wav_to_fbank(audios, target_length, stft)
mel = mel.unsqueeze(1).to(device)
true_latent = unwrapped_vae.get_first_stage_encoding(unwrapped_vae.encode_first_stage(mel))
save_mel(mel, mel_path, text)
save_latent(true_latent, latent_path, text)
progress_bar.update(1)
if __name__ == "__main__":
main()