-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain_separation.py
250 lines (224 loc) · 10.5 KB
/
main_separation.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
import sys
sys.path.append("..")
from models.dedicated import dedicated_model
from dataloader import dataloader
from torch.utils.data import DataLoader
from util.subband.subband_util import before_forward_f
import torch
import time
from util.separation_util import SeparationUtil
import logging as lg
import os
from config.mainConfig import Config
from config.global_tool import GlobalTool
if(len(sys.argv) <= 1):
raise ValueError("Error: You must specify a config file, example: python xxx.py config_xxx.json")
Config.refresh_configuration(sys.argv[1])
GlobalTool.refresh_subband(Config.subband)
lg.basicConfig(level=lg.INFO,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename= "logs/"+Config.trail_name+'.log',
filemode='w')
print("Reminder: Logging file is ","./logs/"+Config.trail_name+'.log')
if(not os.path.exists(Config.MUSDB18_PATH)):
raise RuntimeError("Error: MUSDB18_PATH not found: ", Config.MUSDB18_PATH,". You can download MUSDB18-hq from https://zenodo.org/record/3338373#.Xu8CqS2caCM")
else:
lg.info("Find musdb dataset success!")
if (not os.path.exists(Config.project_root + "saved_models/" + Config.trail_name)):
os.mkdir(Config.project_root + "saved_models/" + Config.trail_name + "/")
lg.info("MakeDir: " + Config.project_root + "saved_models/" + Config.trail_name)
lg.info("Alias of this expriment: "+Config.trail_name)
lg.info("Write config file at: "+ Config.project_root + "config/json/" + Config.trail_name + ".json")
print("Reminder: You can modify the decrease_ratio of validation dynamically during training by changing its value in: ",Config.project_root + "config/json/" + Config.trail_name + ".json")
# Cache for data
freq_bac_loss_cache = []
freq_voc_loss_cache = []
freq_cons_loss_cache = []
validate_score = (None, None)
loss = torch.nn.L1Loss()
if (Config.split_band):
inchannels = 4 * Config.subband
outchannels = 4 * Config.subband
else:
inchannels = outchannels = 4
model = dedicated_model(model_name=Config.model_name,
device=Config.device,
inchannels=inchannels,
outchannels=outchannels,
sources=2,
drop_rate=Config.drop_rate)
if (Config.use_gpu):
model = model.cuda(Config.device)
# MODEL
if (not Config.start_point == 0):
lg.info("Load model from "+ Config.load_model_path + "/model" + str(Config.start_point) + ".pth")
model.load_state_dict(torch.load(Config.load_model_path + "/model" + str(Config.start_point) + ".pth"))
lg.info("Start training from ", model.cnt, Config.model_name)
model.cnt = Config.start_point
else:
lg.info("Start training "+ Config.model_name+ " from the very beginning")
if(Config.show_model_structure):
lg.info(model)
# DATALOADER
dl = torch.utils.data.DataLoader(
dataloader.WavenetDataloader(frame_length=Config.frame_length,
sample_rate=Config.sample_rate,
num_worker=Config.num_workers,
MUSDB18_PATH=Config.MUSDB18_PATH,
BIG_DATA=Config.BIG_DATA,
additional_background_data=Config.additional_accompaniment_data,
additional_vocal_data=Config.additional_vocal_data
),
batch_size=Config.batch_size,
shuffle=True,
num_workers=Config.num_workers)
# OPTIMIZER
optimizer = torch.optim.Adam(model.parameters(), lr=Config.learning_rate)
optimizer.zero_grad()
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=Config.step_size, gamma=Config.gamma)
def save_and_validate():
global validate_score
su = SeparationUtil(model=model,
device=Config.device,
MUSDB_PATH=Config.MUSDB18_PATH,
split_band=Config.subband,
sample_rate=Config.sample_rate,
project_root=Config.project_root,
trail_name=Config.trail_name)
lg.info("Start validation process...")
try:
validate_score = su.validate(validate_score)
except Exception as e:
lg.error("Error occured while evaluating...")
lg.exception(e)
del su
def train( # Time Domain
target_background,
target_vocal,
target_song,
):
def pre_pro(tensor: torch.Tensor):
# move channel axis to the second dimension
return tensor.permute(0, 2, 1).float()
target_background, target_vocal, target_song = pre_pro(target_background), pre_pro(target_vocal), pre_pro(
target_song)
gt_bac, gt_voc, gt_song = before_forward_f(target_background, target_vocal, target_song,
subband_num=Config.subband,
device=Config.device,
sample_rate=Config.sample_rate,
normalize=False)
if ('l1' in Config.loss_component):
output_track = []
for track_i in range(Config.sources):
mask = model(track_i, gt_song)
out = mask * gt_song
output_track.append(out)
# All tracks is done
if (track_i == Config.sources - 1):
# Preprocessing
output_track_sum = sum(output_track)
output_background = output_track[0]
output_vocal = output_track[1]
# Calculate loss function
## conservation loss
lossVal = loss(output_track_sum, gt_song) / Config.accumulation_step
freq_cons_loss_cache.append(float(lossVal) * Config.accumulation_step)
## l1 loss (accompaniment)
temp2 = loss(output_background, gt_bac) / Config.accumulation_step
lossVal += temp2
freq_bac_loss_cache.append(float(temp2) * Config.accumulation_step)
## l1 loss (vocal)
temp3 = loss(output_vocal, gt_voc) / Config.accumulation_step
lossVal += temp3
freq_voc_loss_cache.append(float(temp3) * Config.accumulation_step)
# Backward
lossVal.backward()
if (model.cnt % Config.accumulation_step == 0 and model.cnt != Config.start_point):
# Optimize
optimizer.step()
optimizer.zero_grad()
else:
freq_bac_loss, freq_voc_loss = 0, 0
# An momory efficient version
for track_i in range(Config.sources):
mask = model(track_i, gt_song)
out = mask * gt_song
if (track_i == 1):
lossVal = loss(out, gt_voc)
freq_voc_loss = float(lossVal)
else:
lossVal = loss(out, gt_bac)
freq_bac_loss = float(lossVal)
# Backward
lossVal.backward()
# Optimize
optimizer.step()
optimizer.zero_grad()
freq_bac_loss_cache.append(freq_bac_loss)
freq_voc_loss_cache.append(freq_voc_loss)
print("Training Start!")
print("Logging file: ","logs/"+Config.trail_name+'.log')
t0 = time.time()
for epoch in range(Config.epoches):
lg.info("EPOCH: "+ str(epoch))
start = time.time()
if (Config.use_gpu):
pref = dataloader.data_prefetcher(dl, device=Config.device)
background, vocal, song, name = pref.next()
while background is not None:
if model.cnt % Config.validation_interval == 0 and model.cnt != Config.start_point:
save_and_validate()
if model.cnt % Config.every_n == 0 and model.cnt != Config.start_point:
t1 = time.time()
lg.info(str(model.cnt) +
" Freq L1loss voc:" +
str(format((sum(freq_voc_loss_cache[-Config.every_n:]) / Config.every_n) * 10000, '.7f')) +
" Freq L1loss bac:" +
str(format((sum(freq_bac_loss_cache[-Config.every_n:]) / Config.every_n) * 10000, '.7f')) +
" Freq conserv-loss:" +
str(format((sum(freq_cons_loss_cache[-Config.every_n:]) / Config.every_n) * 10000, '.7f')) +
" lr:"+str(optimizer.param_groups[0]['lr']) +
" speed:"+str(format((Config.frame_length * Config.batch_size) / (t1 - t0), '.2f')))
freq_voc_loss_cache = []
freq_bac_loss_cache = []
freq_cons_loss_cache = []
t0 = time.time()
train(
target_background=background,
target_vocal=vocal,
target_song=song)
background, vocal, song, name = pref.next()
if model.cnt > 100:
scheduler.step(1)
model.cnt += 1
end = time.time()
lg.info("Epoch " + str(epoch) + " finish, total time: " + str(end - start))
else:
for background, vocal, song, name in dl:
if model.cnt % Config.validation_interval == 0 and model.cnt != Config.start_point:
save_and_validate()
if model.cnt % Config.every_n == 0 and model.cnt != Config.start_point:
t1 = time.time()
lg.info(str(model.cnt) +
" Freq L1loss voc:" +
str(format((sum(freq_voc_loss_cache[-Config.every_n:]) / Config.every_n) * 10000, '.7f')) +
" Freq L1loss bac:" +
str(format((sum(freq_bac_loss_cache[-Config.every_n:]) / Config.every_n) * 10000, '.7f')) +
" Freq conserv-loss:" +
str(format((sum(freq_cons_loss_cache[-Config.every_n:]) / Config.every_n) * 10000, '.7f')) +
" lr:" + str(optimizer.param_groups[0]['lr']) +
" speed:" + str(format((Config.frame_length * Config.batch_size) / (t1 - t0), '.2f')))
freq_voc_loss_cache = []
freq_bac_loss_cache = []
freq_cons_loss_cache = []
t0 = time.time()
train(
target_background=background,
target_vocal=vocal,
target_song=song)
if model.cnt > 100:
scheduler.step(1)
model.cnt += 1
end = time.time()
lg.info("Epoch " + str(epoch) + " finish, total time: " + str(end - start))