-
Notifications
You must be signed in to change notification settings - Fork 9
/
trainTwoloss1.py
178 lines (145 loc) · 6.34 KB
/
trainTwoloss1.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
# coding: utf-8
# In[1]:
from __future__ import print_function
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
import numpy as np
import torch.utils.data as utils
import librosa
import soundfile as sf
import time
import os
from torch.utils import data
from wavenet3 import Wavenet
from transformData import x_mu_law_encode,y_mu_law_encode,mu_law_decode,onehot,cateToSignal
from readDataset3 import Dataset,Testset,RandomCrop,ToTensor
from unet import Unet
import h5py
# In[2]:
sampleSize = 16000 # the length of the sample size
quantization_channels = 256
sample_rate = 16000
dilations = [2 ** i for i in range(9)] * 7 # idea from wavenet, have more receptive field
residualDim = 128 #
skipDim = 512
shapeoftest = 190500
songnum=4
filterSize = 3
savemusic0='vsCorpus/nus00xtr{}.wav'
savemusic1='vsCorpus/nus01xtr{}.wav'
resumefile = 'model/instrument0' # name of checkpoint
lossname = 'instrumentloss0.txt' # name of loss file
continueTrain = False # whether use checkpoint
pad = np.sum(dilations) # padding for dilate convolutional layers
lossrecord = [] # list for record loss
sampleCnt=0
#pad=0
# # |----------------------------------------| *residual*
# # | |
# # | |-- conv -- tanh --| |
# # -> dilate -|----| * ----|-- 1x1 -- + --> *input*
# # |-- conv -- sigm --| | ||
# # 1x1=residualDim
# # |
# # ---------------------------------------> + -------------> *skip=skipDim*
# image changed from https://github.com/vincentherrmann/pytorch-wavenet/blob/master/wavenet_model.py
# In[3]:
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0" # use specific GPU
# In[4]:
use_cuda = torch.cuda.is_available() # whether have available GPU
torch.manual_seed(1)
device = torch.device("cuda" if use_cuda else "cpu")
# device = 'cpu'
# torch.set_default_tensor_type('torch.cuda.FloatTensor') #set_default_tensor_type as cuda tensor
transform=transforms.Compose([RandomCrop(),ToTensor()])
training_set = Dataset(np.arange(0, songnum), np.arange(0, songnum), 'ccmixter3/x/', 'ccmixter3/y/',transform)
validation_set = Testset(np.arange(0, songnum), 'ccmixter3/x/')
loadtr = data.DataLoader(training_set, batch_size=1,shuffle=True,num_workers=3) # pytorch dataloader, more faster than mine
loadval = data.DataLoader(validation_set,batch_size=1,num_workers=3)
# In[6]:
#model = Unet(skipDim, quantization_channels, residualDim,device)
model = Wavenet(pad, skipDim, quantization_channels, residualDim, dilations,device)
model = nn.DataParallel(model)
model = model.cuda()
criterion = nn.CrossEntropyLoss()
# in wavenet paper, they said crossentropyloss is far better than MSELoss
optimizer = optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-5)
# use adam to train
# In[7]:
start_epoch=0
if continueTrain: # if continueTrain, the program will find the checkpoints
if os.path.isfile(resumefile):
print("=> loading checkpoint '{}'".format(resumefile))
checkpoint = torch.load(resumefile)
start_epoch = checkpoint['epoch']
# best_prec1 = checkpoint['best_prec1']
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(resumefile, checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(resumefile))
# In[9]:
def test(iloader, xtrain): # testing data
model.eval()
start_time = time.time()
with torch.no_grad():
listofpred0,listofpred1 = [],[]
for ind in range(pad, xtrain.shape[-1] - pad, sampleSize):
output0,output1 = model(xtrain[:, :, ind - pad:ind + sampleSize + pad].to(device))
pred0 = output0.max(1, keepdim=True)[1].cpu().numpy().reshape(-1)
pred1 = output1.max(1, keepdim=True)[1].cpu().numpy().reshape(-1)
listofpred0.append(pred0)
listofpred1.append(pred1)
ans0 = mu_law_decode(np.concatenate(listofpred0))
ans1 = mu_law_decode(np.concatenate(listofpred1))
if not os.path.exists('vsCorpus/'): os.makedirs('vsCorpus/')
sf.write(savemusic0.format(iloader), ans0, sample_rate)
sf.write(savemusic1.format(iloader), ans1, sample_rate)
print('test stored done', np.round(time.time() - start_time))
def train(epoch): # training data, the audio except for last 15 seconds
model.train()
for iloader, (xtrain, ytrain,ztrain) in enumerate(loadtr):
idx = np.arange(pad, xtrain.shape[-1] - pad - sampleSize, 1000)
np.random.shuffle(idx)
lens = idx.shape[-1] // 2
idx = idx[:lens]
for i, ind in enumerate(idx):
start_time = time.time()
data = xtrain[:, :, ind - pad:ind + sampleSize + pad].to(device)
target0 = ytrain[:, ind:ind + sampleSize].to(device)
target1 = ztrain[:, ind:ind + sampleSize].to(device)
output = model(data)
loss = criterion(output[0], target0) + criterion(output[1], target1)
lossrecord.append(loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
global sampleCnt
sampleCnt+=1
print('Train Epoch: {} iloader:{},{},{} Loss:{:.6f}: , ({:.3f} sec/step)'.format(
epoch, iloader,i, idx.shape[-1], loss.item(), time.time() - start_time))
if sampleCnt % 10000 == 0 and sampleCnt > 0:
for param in optimizer.param_groups:
param['lr'] *= 0.98
if epoch % 2 == 0:test(iloader, xtrain)
if epoch % 1 == 0:
with open("lossRecord/" + lossname, "w") as f:
for s in lossrecord:
f.write(str(s) + "\n")
print('write finish')
if not os.path.exists('model/'): os.makedirs('model/')
state = {'epoch': epoch,
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict()}
torch.save(state, resumefile)
#if epoch % 2 == 0 and epoch > 0:
#test()
# In[ ]:
for epoch in range(100000):
train(epoch+start_epoch)