-
Notifications
You must be signed in to change notification settings - Fork 186
/
train_rave.py
159 lines (133 loc) · 4.4 KB
/
train_rave.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
import torch
from torch.utils.data import DataLoader, random_split
from rave.model import RAVE
from rave.core import random_phase_mangle, EMAModelCheckPoint
from rave.core import search_for_run
from udls import SimpleDataset, simple_audio_preprocess
from effortless_config import Config, setting
import pytorch_lightning as pl
from os import environ, path
import numpy as np
import GPUtil as gpu
from udls.transforms import Compose, RandomApply, Dequantize, RandomCrop
if __name__ == "__main__":
class args(Config):
groups = ["small", "large"]
DATA_SIZE = 16
CAPACITY = setting(default=64, small=32, large=64)
LATENT_SIZE = 128
BIAS = True
NO_LATENCY = False
RATIOS = setting(
default=[4, 4, 4, 2],
small=[4, 4, 4, 2],
large=[4, 4, 2, 2, 2],
)
MIN_KL = 1e-1
MAX_KL = 1e-1
CROPPED_LATENT_SIZE = 0
FEATURE_MATCH = True
LOUD_STRIDE = 1
USE_NOISE = True
NOISE_RATIOS = [4, 4, 4]
NOISE_BANDS = 5
D_CAPACITY = 16
D_MULTIPLIER = 4
D_N_LAYERS = 4
WARMUP = setting(default=1000000, small=1000000, large=3000000)
MODE = "hinge"
CKPT = None
PREPROCESSED = None
WAV = None
SR = 48000
N_SIGNAL = 65536
MAX_STEPS = setting(default=3000000, small=3000000, large=6000000)
BATCH = 8
NAME = None
args.parse_args()
assert args.NAME is not None
model = RAVE(
data_size=args.DATA_SIZE,
capacity=args.CAPACITY,
latent_size=args.LATENT_SIZE,
ratios=args.RATIOS,
bias=args.BIAS,
loud_stride=args.LOUD_STRIDE,
use_noise=args.USE_NOISE,
noise_ratios=args.NOISE_RATIOS,
noise_bands=args.NOISE_BANDS,
d_capacity=args.D_CAPACITY,
d_multiplier=args.D_MULTIPLIER,
d_n_layers=args.D_N_LAYERS,
warmup=args.WARMUP,
mode=args.MODE,
no_latency=args.NO_LATENCY,
sr=args.SR,
min_kl=args.MIN_KL,
max_kl=args.MAX_KL,
cropped_latent_size=args.CROPPED_LATENT_SIZE,
feature_match=args.FEATURE_MATCH,
)
x = torch.zeros(args.BATCH, 2**14)
model.validation_step(x, 0)
dataset = SimpleDataset(
args.PREPROCESSED,
args.WAV,
preprocess_function=simple_audio_preprocess(args.SR,
2 * args.N_SIGNAL),
split_set="full",
transforms=Compose([
RandomCrop(args.N_SIGNAL),
RandomApply(
lambda x: random_phase_mangle(x, 20, 2000, .99, args.SR),
p=.8,
),
Dequantize(16),
lambda x: x.astype(np.float32),
]),
)
val = max((2 * len(dataset)) // 100, 1)
train = len(dataset) - val
train, val = random_split(
dataset,
[train, val],
generator=torch.Generator().manual_seed(42),
)
train = DataLoader(train, args.BATCH, True, drop_last=True, num_workers=8)
val = DataLoader(val, args.BATCH, False, num_workers=8)
# CHECKPOINT CALLBACKS
validation_checkpoint = pl.callbacks.ModelCheckpoint(
monitor="validation",
filename="best",
)
last_checkpoint = pl.callbacks.ModelCheckpoint(filename="last")
CUDA = gpu.getAvailable(maxMemory=.05)
VISIBLE_DEVICES = environ.get("CUDA_VISIBLE_DEVICES", "")
if VISIBLE_DEVICES:
use_gpu = int(int(VISIBLE_DEVICES) >= 0)
elif len(CUDA):
environ["CUDA_VISIBLE_DEVICES"] = str(CUDA[0])
use_gpu = 1
elif torch.cuda.is_available():
print("Cuda is available but no fully free GPU found.")
print("Training may be slower due to concurrent processes.")
use_gpu = 1
else:
print("No GPU found.")
use_gpu = 0
val_check = {}
if len(train) >= 10000:
val_check["val_check_interval"] = 10000
else:
nepoch = 10000 // len(train)
val_check["check_val_every_n_epoch"] = nepoch
trainer = pl.Trainer(
logger=pl.loggers.TensorBoardLogger(path.join("runs", args.NAME),
name="rave"),
gpus=use_gpu,
callbacks=[validation_checkpoint, last_checkpoint],
max_epochs=100000,
max_steps=args.MAX_STEPS,
**val_check,
)
trainer.fit(model, train, val, ckpt_path=search_for_run(args.CKPT))