-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_MathClem_train_VAEGAN.py
206 lines (165 loc) · 6.67 KB
/
test_MathClem_train_VAEGAN.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
import os
import torch
import torch.nn
import matplotlib.pyplot as plt
import numpy as np
import torchvision
from torchvision.transforms import ToTensor
from src.timbre_transfer.datasets.NSynthDataset import NSynthDataset
from src.timbre_transfer.helpers.audiotransform import AudioTransform
from src.timbre_transfer.models.network.Spectral_Decoder import Spectral_Decoder
from src.timbre_transfer.models.network.Spectral_Encoder import Spectral_Encoder
from src.timbre_transfer.models.network.Spectral_Discriminator import Spectral_Discriminator
from src.timbre_transfer.models.VAE_GAN import SpectralVAE_GAN
from src.timbre_transfer.train_VAE_GAN import trainStep_VAE_GAN , computeLoss_VAE_GAN
from torch.utils.tensorboard import SummaryWriter
dataset_folder = "data"
preTrained_loadName = "pretrained/exp_VAEGAN/exp1"
preTrained_saveName = "pretrained/exp_VAEGAN/exp1"
writer = SummaryWriter(os.path.join('runs','test_VAEGAN'))
## Name of the saved trained network
## Training parameters
# Proportion of the train dataset used for training
train_ratio = 1
# Number of Epochs
epochs = 50
# Learning rate
lr = 1e-4
# Reconstruction Loss (always use reduction='none')
recons_criterion = torch.nn.MSELoss(reduction = 'none')
# Beta-VAE Beta coefficient and warm up length
beta_end = 1
warm_up_length = 1 #epochs
# Dataloaders parameters
train_batch_size = 256
valid_batch_size = 1024
num_threads = 0
## Model Parameters
# Dimension of the linear layer
hidden_dim = 256
# Dimension of the latent space
latent_dim = 8
# Number of filters of the first convolutionnal layer
base_depth = 64
# Max number of channels of te convolutionnal layers
max_depth = 512
# Number of convolutionnal layers
n_convLayers = 3
# Kernel size of convolutionnal layers (recommended : stride*2+3)
kernel_size = 11
# Stride of convolutionnal layers (recommended : 2 or 4)
stride = 4
# Models returns images of size freqs_dim*len_dim
freqs_dim = 128
len_dim = 128
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
#device = 'cpu'
print(device)
AT = AudioTransform(input_freq = 16000, n_fft = 1024, n_mel = freqs_dim, stretch_factor=.8)
## Loading the NSynth dataset
train_dataset = NSynthDataset('data/', usage = 'train', filter_key='vocal_acoustic', transform=AT)
valid_dataset = NSynthDataset('data/', usage = 'valid', filter_key='vocal_acoustic', transform=AT)
nb_train = int(train_ratio * len(train_dataset))
nb_valid = len(valid_dataset)
print(f"Number of training examples : {nb_train}")
print(f"Number of validation examples : {nb_valid}")
train_dataset, _ = torch.utils.data.dataset.random_split(train_dataset, [nb_train, len(train_dataset)-nb_train])
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=train_batch_size, num_workers=num_threads, shuffle=True)
valid_loader = torch.utils.data.DataLoader(dataset=valid_dataset, batch_size=valid_batch_size, num_workers=num_threads, shuffle=True)
## Model definition
encoder = Spectral_Encoder(
freqs_dim = freqs_dim,
len_dim = len_dim,
latent_dim = latent_dim,
hidden_dim = hidden_dim,
base_depth = base_depth,
n_convLayers = n_convLayers,
kernel_size = kernel_size,
max_depth = max_depth,
stride = stride)
decoder = Spectral_Decoder(
freqs_dim = freqs_dim,
len_dim = len_dim,
latent_dim = latent_dim,
hidden_dim = hidden_dim,
base_depth = base_depth,
n_convLayers = n_convLayers,
kernel_size = kernel_size,
max_depth = max_depth,
stride = stride)
discriminator = Spectral_Discriminator(
freqs_dim = freqs_dim,
len_dim = len_dim,
latent_dim = latent_dim,
hidden_dim = hidden_dim,
base_depth = base_depth,
n_convLayers = n_convLayers,
kernel_size = kernel_size,
max_depth = max_depth,
stride = stride)
model = SpectralVAE_GAN(encoder, decoder, discriminator, freqs_dim = freqs_dim, len_dim = len_dim, encoding_dim = hidden_dim, latent_dim = latent_dim).to(device)
## Loading pre-trained model
if os.path.isfile(preTrained_loadName+'.pt'):
model.load_state_dict(torch.load('./'+preTrained_loadName+'.pt'))
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
beta = 0
## Training
for epoch in range(epochs):
model = model.to(device)
train_losses_vect = np.zeros(4)
valid_losses_vect = np.zeros(4)
beta = min(beta_end, epoch/warm_up_length*beta_end)
for i, (x,_) in enumerate(iter(train_loader)):
x = x.to(device)
optimizer.zero_grad()
losses = trainStep_VAE_GAN(model, discriminator, x, optimizer, beta)
full_loss = losses[0]
for j,l in enumerate(losses):
train_losses_vect[j]+=l.cpu().detach().numpy()*x.size()[0]/nb_train
full_loss.backward()
optimizer.step()
with torch.no_grad():
for i , (x,_) in enumerate(iter(valid_loader)):
x = x.to(device)
losses = computeLoss_VAE_GAN(model, x, beta)
for j,l in enumerate(losses):
valid_losses_vect[j]+=l.cpu().detach().numpy()*x.size()[0]/nb_valid
# Saving trained model
torch.save(model.state_dict(), preTrained_saveName+'.pt')
writer.add_scalars("Full Loss",
{'Training': train_losses_vect[0],
'Validation': valid_losses_vect[0]}, epoch)
writer.add_scalars("Reconstruction Loss",
{'Training': train_losses_vect[1],
'Validation': valid_losses_vect[1]}, epoch)
writer.add_scalars("KL Divergence",
{'Training': train_losses_vect[2],
'Validation': valid_losses_vect[2]}, epoch)
writer.add_scalars("Discriminator Loss",
{'Training': train_losses_vect[3],
'Validation': valid_losses_vect[3]}, epoch)
print(f'epoch : {epoch}')
x_test = next(iter(valid_loader))
x_test = x_test[0]
x_test = x_test[0:16]
x_test = x_test.to(device)
model = model.to(device)
y_test = model(x_test)[0]
x_grid = torchvision.utils.make_grid(x_test/2+.4)
y_grid = torchvision.utils.make_grid(y_test/2+.4)
writer.add_image("input_image",x_grid, epoch)
writer.add_image("output_image",y_grid, epoch)
if (epoch+1)%20==0:
print('Exporting sound')
AT = AT.to('cpu')
x_test = x_test.to('cpu')
y_test = y_test.to('cpu').detach()
x_test_sound = AT.inverse(mel = x_test[0])
y_test_sound = AT.inverse(mel = y_test[0])
x_test_sound = x_test_sound/torch.max(torch.abs(x_test_sound))
y_test_sound = y_test_sound/torch.max(torch.abs(y_test_sound))
writer.add_audio("input", x_test_sound, sample_rate=16000, global_step=epoch)
writer.add_audio("output", y_test_sound, sample_rate=16000, global_step=epoch)
print('Exported !\n')
writer.flush()
writer.close()