-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
221 lines (167 loc) · 6.92 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
221
#!/usr/bin/env python
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
import math
import sys
from torch.autograd import Variable
from dataset import KshDataset
from torch.utils.data import DataLoader
from net.model import voltexNet
import music_processer as mp
import torch.nn.functional as F
from torch.optim import lr_scheduler
from tqdm import tqdm
import os
def infer(model, device, batch, filename, savename) :
# Training End, infer #
input = KshDataset.music_load(filename + '/nofx.ogg')
input = input.reshape(input.shape[0], 1, -1)
input = input.to(device, dtype=torch.float)
output = []
for i in range(0,input.shape[0], batch):
if i+batch < input.shape[0] :
pred = model(input[i:i+batch],batch)
pred = pred.to(torch.device("cpu"))
if i == 0 :
output = pred.tolist()
else :
pred = pred.tolist()
for i in pred:
output.append(i)
#print(output.shape)
#torch.save(model.state_dict(), "./model/model.pth")
index = 0
note_time_Stamp_output = []
fx_time_Stamp_output = []
for time in output :
#print(time.index(max(time)))
if time.index(max(time)) == 1 :
note_time_Stamp_output.append(index)
if time.index(max(time)) == 2 :
fx_time_Stamp_output.append(index)
if time.index(max(time)) == 3 :
note_time_Stamp_output.append(index)
fx_time_Stamp_output.append(index)
index = index + 1
print(note_time_Stamp_output)
print(fx_time_Stamp_output)
#print(fx_time_Stamp_output)
song = mp.Audio(filename = (filename + "nofx.ogg"), note_timestamp = note_time_Stamp_output, fx_timestamp = fx_time_Stamp_output)
song.synthesize(diff='ka')
song.save(filename = savename)
def main():
model = voltexNet()
#model.load_state_dict(torch.load("./model/model_3_.pth"))
#print ("load model")
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# move model to the right device
model.to(device)
#input = torch.rand(128,3,80,15)
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
scheduler = lr_scheduler.StepLR(optimizer, step_size=200, gamma=0.1)
# kshDataset에서는 (곡의 길이 * 25 = batch) * (sample rate * 0.04)의 tensor가 넘어옴
# for문은 (batch)만큼 반복
# CNN input Tensor = (sample rate * 0.04) 1D Tensor
# CNN output = (1,1) sigmoid unit = RNN input
# 구현 예정
# RNN output = (batch,1) 1D Tensor
# RNN Target = .ksh file => 10ms True/False TimeStamp
dirname = "./data/songs/"
valname = "./data_test/songs/"
filenames = os.listdir(dirname)
valnames = os.listdir(valname)
batch = 1024
song_index = 0
best_Acc = 0
epoch_loss = 0.0
for epoch in range(0,800) :
epoch_loss = 0.0
train_loss = 0.0
index = 0
song_index = 0
print("train...")
for filename in tqdm(filenames):
song_index += 1
full_filename = os.path.join(dirname, filename)
#print(full_filename)
input = KshDataset.music_load("./cache/" + filename+'.npy')
#print(input.shape)
#input = input.reshape(input.shape[0], 1, -1)
target = KshDataset.timeStamp(full_filename + '/exh.ksh', input.shape[0])
try :
if target == None:
continue
except TypeError:
__ = 1
input = input.to(device, dtype=torch.float)
target = target.to(device, dtype=torch.float)
#print(input.shape)
#print(target.shape)
model.train(True) # Set model to training mode
for i in range(0,input.shape[0], batch):
if i+batch < input.shape[0] :
optimizer.zero_grad()
pred = model(input[i:i+batch])
loss = criterion(pred.squeeze(), target[i:i+batch].squeeze())
loss.backward()
optimizer.step()
epoch_loss += loss.item()
index += 1
else :
optimizer.zero_grad()
tmp_batch = input.shape[0] - i
if tmp_batch > 1 :
pred = model(input[i:i+tmp_batch-1])
loss = criterion(pred.squeeze(), target[i:i+tmp_batch-1].squeeze())
loss.backward()
optimizer.step()
epoch_loss += loss.item()
index += 1
scheduler.step()
print("eval...")
noneScore = 0
acc = 0
#model.to(torch.device("cpu"))
for filename in tqdm(valnames):
full_filename = os.path.join(valname, filename)
input = KshDataset.music_load("./cache/" + filename+'.npy')
input = input.to(device, dtype=torch.float)
target = KshDataset.timeStamp(full_filename + '/exh.ksh', input.shape[0])
output = []
for i in range(0,input.shape[0], batch):
if i+batch < input.shape[0] :
pred = model(input[i:i+batch])
pred = pred.to(torch.device("cpu"))
if i == 0 :
output = pred.tolist()
else :
pred = pred.tolist()
for i in pred:
output.append(i)
#output = output.to(torch.device("cpu"))
#output = output.tolist()
#loss = criterion(pred.squeeze(), target[i*batch:i*batch+batch].squeeze())
acc_tmp = 0
try :
for (out, tar) in zip(output, target):
if out.index(max(out)) == tar:
acc_tmp += 1
acc += acc_tmp / input.shape[0]
except TypeError:
noneScore += 1
continue
if acc != 0 :
acc = acc / (len(valnames) - noneScore)
print("epoch : " + str(epoch) + "\ttrain_loss : " + str(epoch_loss/index) + "\ttest_acc : " + str(acc) + "\n")
if acc > best_Acc :
best_Acc = acc
torch.save(model.state_dict(), "./model/model_bestAcc_.pth")
print("save " + str(best_Acc))
if best_Acc > 0.9 :
break
torch.save(model.state_dict(), "./model/model_"+ str(epoch)+ "_.pth")
if __name__ == "__main__":
main()