forked from YatingMusic/ddsp-singing-vocoders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·167 lines (147 loc) · 4.47 KB
/
main.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
'''
author: Hsiao Wen Yi (wayne391)
email: [email protected]
'''
import os
import argparse
import torch
from logger import utils, report
from data_cnpop import get_data_loaders
from solver import train, test, render
from ddsp.vocoder import SawSub, SawSinSub, Sins, DWS, Full
from ddsp.loss import HybridLoss
def parse_args(args=None, namespace=None):
"""Parse command-line arguments."""
parser = argparse.ArgumentParser()
parser.add_argument(
"-c",
"--config",
type=str,
required=True,
help="path to config file",
)
parser.add_argument(
"-s",
"--stage",
type=str,
required=True,
help="Stages. Options: training/inference",
)
parser.add_argument(
"-m",
"--model",
type=str,
required=True,
help="Models. Options: SawSinSub/Sins/DWS/Full/SinsSub/SawSub",
)
parser.add_argument(
"-k",
"--model_ckpt",
type=str,
required=False,
help="path to existing model ckpt",
)
parser.add_argument(
"-i",
"--input_dir",
type=str,
required=False,
help="[inference] path to input mel-spectrogram",
)
parser.add_argument(
"-o",
"--output_dir",
type=str,
required=False,
help="[inference, validation] path to synthesized audio files",
)
parser.add_argument(
"-p",
"--is_part",
type=str,
required=False,
help="[inference, validation] individual harmonic and noise output",
)
return parser.parse_args(args=args, namespace=namespace)
if __name__ == '__main__':
# parse commands
cmd = parse_args()
# load config
args = utils.load_config(cmd.config)
print(' > config:', cmd.config)
print(' > exp:', args.env.expdir)
# load model
model = None
if cmd.model == 'SawSinSub':
model = SawSinSub(
sampling_rate=args.data.sampling_rate,
block_size=args.data.block_size,
n_mag_harmonic=args.model.n_mag_harmonic,
n_mag_noise=args.model.n_mag_noise,
n_harmonics=args.model.n_harmonics)
elif cmd.model == 'Sins':
model = Sins(
sampling_rate=args.data.sampling_rate,
block_size=args.data.block_size,
n_harmonics=args.model.n_harmonics,
n_mag_noise=args.model.n_mag_noise)
elif cmd.model == 'DWS':
model = DWS(
sampling_rate=args.data.sampling_rate,
block_size=args.data.block_size,
num_wavetables=args.model.num_wavetables,
len_wavetables=args.model.len_wavetables,
is_lpf=args.model.is_lpf)
elif cmd.model == 'Full':
model = Full(
sampling_rate=args.data.sampling_rate,
block_size=args.data.block_size,
n_mag_harmonic=args.model.n_mag_harmonic,
n_mag_noise=args.model.n_mag_noise,
n_harmonics=args.model.n_harmonics)
elif cmd.model == 'SawSub':
model = SawSub(
sampling_rate=args.data.sampling_rate,
block_size=args.data.block_size)
else:
raise ValueError(f" [x] Unknown Model: {cmd.model}")
# load parameters
if cmd.model_ckpt:
model = utils.load_model_params(
cmd.model_ckpt, model, args.device)
# loss
loss_func = HybridLoss(args.loss.n_ffts)
# device
args.device = 'cuda' if torch.cuda.is_available() else 'cpu'
if args.device == 'cuda':
torch.cuda.set_device(args.env.gpu)
model.to(args.device)
loss_func.to(args.device)
# datas
loader_train, loader_valid = get_data_loaders(args, whole_audio=False)
# stage
if cmd.stage == 'training':
train(args, model, loss_func, loader_train, loader_valid)
elif cmd.stage == 'validation':
output_dir = 'valid_gen'
if cmd.output_dir:
output_dir = cmd.output_dir
test(
args,
model,
loss_func,
loader_valid,
path_gendir=output_dir,
is_part=cmd.is_part)
elif cmd.stage == 'inference':
output_dir = 'infer_gen'
if cmd.output_dir:
output_dir = cmd.output_dir
render(
args,
model,
path_mel_dir=cmd.input_dir,
path_gendir=output_dir,
is_part=cmd.is_part)
else:
raise ValueError(f" [x] Unkown Stage: {cmd.stage }")