-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain.py
220 lines (183 loc) · 7.66 KB
/
train.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
from torch.optim.lr_scheduler import MultiStepLR
from torch.cuda.amp import autocast as autocast
from torch.utils import data
import os
import numpy as np
import torch
from tqdm import tqdm
import random
import time
from config_files.General_config import General_config
from config_files.General_config import Train_config
from config_files.Model_config import Model_config
from separator.utils.create_models import create_model
from separator.utils.multi_loss import MultiLoss
from separator.utils.meter import AverageMeter
# default parameter
LAST_PATH = "first"
# GENERAL_SPECIFICATION
HOME = General_config["HOME"]
MODEL = General_config["model"]
instrument_list = Train_config["instrument_list"]
instrument_weight = Train_config["instrument_weight"]
train_name = General_config["task_name"]
# TRAIN_SPECIFICATION
train_dataset = Train_config["train_dataset"]
save_directory = Train_config["save_directory"]
pretrained_model = Train_config["pretrained_model"]
save_interval = Train_config["save_interval"]
deterministic = Train_config["deterministic"]
log_folder_path = Train_config["log_path"]
# MODEL_SPECIFICATION
accumulation_steps = Model_config["accumulation_steps"]
max_epoch = Model_config['max_epoch']
lr = Model_config['lr']
milestones = Model_config['milestones']
gamma = Model_config["gamma"]
weight_decay = Model_config["weight_decay"]
thread_num = Model_config["thread_num"]
FP = Model_config["FP"]
batch_size = Model_config["batch_size"]
input_type = Model_config["input_type"]
loss_type = Model_config["loss_type"]
scaler = torch.cuda.amp.GradScaler()
def reproducible():
seed = 2021
import torch.backends.cudnn
torch.manual_seed(seed)
if deterministic == True:
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
random.seed(seed)
np.random.seed(seed)
def get_data_loader():
train_dataloader_extra = None
train_dataloader_musdb = None
if "extra" in train_dataset:
if input_type == "F":
from separator.dataset_loader.loader_STFT_extra import loader_STFT_extra
train_dataset_extra = loader_STFT_extra()
train_dataloader_extra = data.DataLoader(train_dataset_extra, batch_size=batch_size, shuffle=True,num_workers=thread_num, drop_last=True, pin_memory=True)
if "musdb" in train_dataset:
if input_type == "F":
from separator.dataset_loader.loader_STFT_musdb import loader_STFT_musdb
train_dataset_musdb = loader_STFT_musdb()
train_dataloader_musdb = data.DataLoader(train_dataset_musdb, batch_size=batch_size, shuffle=True,num_workers=thread_num, drop_last=True, pin_memory=True)
else:
from separator.dataset_loader.loader_TD_musdb import loader_TD_musdb
train_dataset_musdb = loader_TD_musdb()
train_dataloader_musdb = data.DataLoader(train_dataset_musdb, batch_size=batch_size, shuffle=True,num_workers=thread_num, drop_last=True, pin_memory=True)
return train_dataloader_extra,train_dataloader_musdb
def train_one_epoch(model, device, loader, optimizer, criterion):
i = 0
global scaler
model.train()
model.cuda()
meters = dict()
meters['loss'] = AverageMeter()
meters.update({key: AverageMeter() for key in instrument_list})
for spectrograms in tqdm(loader):
for key in instrument_list:
spectrograms[key] = spectrograms[key].to(device)
spectrograms["mixture"] = spectrograms["mixture"].to(device)
if FP == "16":
with autocast():
predict = model(spectrograms["mixture"].float())
loss, sub_loss = criterion(predict, spectrograms)
else:
predict = model(spectrograms["mixture"].float())
loss, sub_loss = criterion(predict, spectrograms)
loss = loss/accumulation_steps
if FP == "16":
if((i+1)%accumulation_steps)==0:
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad()
i = 0
else:
scaler.scale(loss).backward()
i = i+1
else:
if((i+1)%accumulation_steps)==0:
loss.backward()
optimizer.step()
optimizer.zero_grad()
i = 0
else:
loss.backward()
i = i+1
meters['loss'].update(loss.item(), batch_size)
for key in sub_loss:
meters[key].update(sub_loss[key].item(), batch_size)
return meters
def train(model, device):
log_name = train_name + "_" + str(time.time()) + "_log.txt"
log_file_path = os.path.join(log_folder_path, log_name)
os.system("touch " + log_file_path)
log_file = open(log_file_path, "w")
global LAST_PATH
if not os.path.exists(save_directory):
os.makedirs(save_directory)
train_folder = os.path.join(save_directory, train_name)
if not os.path.exists(train_folder):
os.makedirs(train_folder)
if FP == "16":
optimizer = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=weight_decay,eps = 1e-4)
else:
optimizer = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=weight_decay,eps = 1e-8)
criterion = MultiLoss(instrument_list, instrument_weight,loss_type)
scheduler = MultiStepLR(optimizer, milestones, gamma)
train_loader,train_loader_musdb = get_data_loader()
for i in range(1,max_epoch):
if "extra" in train_dataset:
start = time.time()
# Train with custom dataset (TF domain)
t_loss = train_one_epoch(model, device, train_loader_extra, optimizer, criterion)
end = time.time()
scheduler.step()
msg = ''
for key, value in t_loss.items():
value = value.result()
msg += f'{key}:{value:.4f}\t'
msg += f'On extra: time:{(end - start):.1f}\tepoch:{i}'
print(msg)
log_file.write(msg+"\n")
loss_str = "{:.2f}".format(t_loss['loss'].result())
if "musdb" in train_dataset:
start = time.time()
t_loss = train_one_epoch(model, device, train_loader_musdb, optimizer, criterion)
end = time.time()
scheduler.step()
msg = ''
for key, value in t_loss.items():
value = value.result()*accumulation_steps
msg += f'{key}:{value:.4f}\t'
msg += f'On musdb data: time:{(end - start):.1f}\tepoch:{i}'
print(msg)
log_file.write(msg+"\n")
loss_str = "{:.2f}".format(t_loss['loss'].result())
if i%save_interval == 0 and i >= save_interval:
save_path = os.path.join(train_folder, 'epoch_' + str(i) + '_' + loss_str + '.pth')
torch.save(model.state_dict(), save_path)
model.phase = 'train'
else:
save_path = os.path.join(train_folder, 'epoch_' + str(i) + '_' + loss_str + '.pth')
torch.save(model.state_dict(), save_path)
model.phase = 'train'
if LAST_PATH != "first":
os.remove(LAST_PATH)
LAST_PATH = save_path
def main():
reproducible()
device = 'cuda' if torch.cuda.is_available() else 'cpu'
import gc
gc.collect()
torch.cuda.empty_cache()
model = create_model("train")
if pretrained_model is not None:
model.load_state_dict(torch.load(pretrained_model, map_location=device))
print("pretrained model loaded: " + pretrained_model)
train(model, device)
if __name__ == '__main__':
main()