-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_module.py
More file actions
215 lines (196 loc) · 8.49 KB
/
model_module.py
File metadata and controls
215 lines (196 loc) · 8.49 KB
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
import torch
import numpy as np
# Data
n_tracks = 5 # number of tracks
n_pitches = 72 # number of pitches
lowest_pitch = 24 # MIDI note number of the lowest pitch
n_samples_per_song = 8 # number of samples to extract from each song in the datset
n_measures = 4 # number of measures per sample
beat_resolution = 4 # temporal resolution of a beat (in timestep)
programs = [0, 0, 25, 33, 48] # program number for each track
is_drums = [True, False, False, False, False] # drum indicator for each track
track_names = ['Drums', 'Piano', 'Guitar', 'Bass', 'Strings'] # name of each track
tempo = 100
# Training
batch_size = 16
latent_dim = 128
n_steps = 20000
# Sampling
sample_interval = 100 # interval to run the sampler (in step)
n_samples = 4
measure_resolution = 4 * beat_resolution
tempo_array = np.full((4 * 4 * measure_resolution, 1), tempo)
assert 24 % beat_resolution == 0, (
"beat_resolution must be a factor of 24 (the beat resolution used in "
"the source dataset)."
)
assert len(programs) == len(is_drums) and len(programs) == len(track_names), (
"Lengths of programs, is_drums and track_names must be the same."
)
# Assuming 'latent_dim' is the size of the noise vector for the generator
latent_dim = 128 # Replace with the actual dimension of your latent space
class Generator(torch.nn.Module):
"""A convolutional neural network (CNN) based generator. The generator takes
as input a latent vector and outputs a fake sample."""
def __init__(self):
super().__init__()
self.transconv0 = GeneraterBlock(128, 256, (4, 1, 1), (4, 1, 1))
self.transconv1 = GeneraterBlock(256, 128, (1, 4, 1), (1, 4, 1))
self.transconv2 = GeneraterBlock(128, 64, (1, 1, 4), (1, 1, 4))
self.transconv3 = GeneraterBlock(64, 32, (1, 1, 3), (1, 1, 1))
self.transconv4 = torch.nn.ModuleList([
GeneraterBlock(32, 16, (1, 4, 1), (1, 4, 1))
for _ in range(n_tracks)
])
self.transconv5 = torch.nn.ModuleList([
GeneraterBlock(16, 1, (1, 1, 12), (1, 1, 12))
for _ in range(n_tracks)
])
def forward(self, x):
x = x.view(-1, latent_dim, 1, 1, 1)
x = self.transconv0(x)
x = self.transconv1(x)
x = self.transconv2(x)
x = self.transconv3(x)
x = [transconv(x) for transconv in self.transconv4]
x = torch.cat([transconv(x_) for x_, transconv in zip(x, self.transconv5)], 1)
x = x.view(-1, n_tracks, n_measures * measure_resolution, n_pitches)
return x
class GeneraterBlock(torch.nn.Module):
def __init__(self, in_dim, out_dim, kernel, stride):
super().__init__()
self.transconv = torch.nn.ConvTranspose3d(in_dim, out_dim, kernel, stride)
self.batchnorm = torch.nn.BatchNorm3d(out_dim)
def forward(self, x):
x = self.transconv(x)
x = self.batchnorm(x)
return torch.nn.functional.relu(x)
class LayerNorm(torch.nn.Module):
"""An implementation of Layer normalization that does not require size
information. Copied from https://github.com/pytorch/pytorch/issues/1959."""
def __init__(self, n_features, eps=1e-5, affine=True):
super().__init__()
self.n_features = n_features
self.affine = affine
self.eps = eps
if self.affine:
self.gamma = torch.nn.Parameter(torch.Tensor(n_features).uniform_())
self.beta = torch.nn.Parameter(torch.zeros(n_features))
def forward(self, x):
shape = [-1] + [1] * (x.dim() - 1)
mean = x.view(x.size(0), -1).mean(1).view(*shape)
std = x.view(x.size(0), -1).std(1).view(*shape)
y = (x - mean) / (std + self.eps)
if self.affine:
shape = [1, -1] + [1] * (x.dim() - 2)
y = self.gamma.view(*shape) * y + self.beta.view(*shape)
return y
class DiscriminatorBlock(torch.nn.Module):
def __init__(self, in_dim, out_dim, kernel, stride):
super().__init__()
self.transconv = torch.nn.Conv3d(in_dim, out_dim, kernel, stride)
self.layernorm = LayerNorm(out_dim)
def forward(self, x):
x = self.transconv(x)
x = self.layernorm(x)
return torch.nn.functional.leaky_relu(x)
class Discriminator(torch.nn.Module):
"""A convolutional neural network (CNN) based discriminator. The
discriminator takes as input either a real sample (in the training data) or
a fake sample (generated by the generator) and outputs a scalar indicating
its authentity.
"""
def __init__(self):
super().__init__()
self.conv0 = torch.nn.ModuleList([
DiscriminatorBlock(1, 16, (1, 1, 12), (1, 1, 12)) for _ in range(n_tracks)
])
self.conv1 = torch.nn.ModuleList([
DiscriminatorBlock(16, 16, (1, 4, 1), (1, 4, 1)) for _ in range(n_tracks)
])
self.conv2 = DiscriminatorBlock(16 * 5, 64, (1, 1, 3), (1, 1, 1))
self.conv3 = DiscriminatorBlock(64, 64, (1, 1, 4), (1, 1, 4))
self.conv4 = DiscriminatorBlock(64, 128, (1, 4, 1), (1, 4, 1))
self.conv5 = DiscriminatorBlock(128, 128, (2, 1, 1), (1, 1, 1))
self.conv6 = DiscriminatorBlock(128, 256, (3, 1, 1), (3, 1, 1))
self.dense = torch.nn.Linear(256, 1)
def forward(self, x):
x = x.view(-1, n_tracks, n_measures, measure_resolution, n_pitches)
x = [conv(x[:, [i]]) for i, conv in enumerate(self.conv0)]
x = torch.cat([conv(x_) for x_, conv in zip(x, self.conv1)], 1)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
x = self.conv6(x)
x = x.view(-1, 256)
x = self.dense(x)
return x
def compute_gradient_penalty(discriminator, real_samples, fake_samples):
"""Compute the gradient penalty for regularization. Intuitively, the
gradient penalty help stablize the magnitude of the gradients that the
discriminator provides to the generator, and thus help stablize the training
of the generator."""
# Get random interpolations between real and fake samples
alpha = torch.rand(real_samples.size(0), 1, 1, 1)
interpolates = (alpha * real_samples + ((1 - alpha) * fake_samples))
interpolates = interpolates.requires_grad_(True)
# Get the discriminator output for the interpolations
d_interpolates = discriminator(interpolates)
# Get gradients w.r.t. the interpolations
fake = torch.ones(real_samples.size(0), 1)
gradients = torch.autograd.grad(
outputs=d_interpolates,
inputs=interpolates,
grad_outputs=fake,
create_graph=True,
retain_graph=True,
only_inputs=True
)[0]
# Compute gradient penalty
gradients = gradients.view(gradients.size(0), -1)
gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean()
return gradient_penalty
def train_one_step(d_optimizer, g_optimizer, real_samples):
"""Train the networks for one step."""
# Sample from the lantent distribution
latent = torch.randn(batch_size, latent_dim)
# Transfer data to GPU
# === Train the discriminator ===
# Reset cached gradients to zero
d_optimizer.zero_grad()
# Get discriminator outputs for the real samples
prediction_real = discriminator(real_samples)
# Compute the loss function
# d_loss_real = torch.mean(torch.nn.functional.relu(1. - prediction_real))
d_loss_real = -torch.mean(prediction_real)
# Backpropagate the gradients
d_loss_real.backward()
# Generate fake samples with the generator
fake_samples = generator(latent)
# Get discriminator outputs for the fake samples
prediction_fake_d = discriminator(fake_samples.detach())
# Compute the loss function
# d_loss_fake = torch.mean(torch.nn.functional.relu(1. + prediction_fake_d))
d_loss_fake = torch.mean(prediction_fake_d)
# Backpropagate the gradients
d_loss_fake.backward()
# Compute gradient penalty
gradient_penalty = 10.0 * compute_gradient_penalty(
discriminator, real_samples.data, fake_samples.data)
# Backpropagate the gradients
gradient_penalty.backward()
# Update the weights
d_optimizer.step()
# === Train the generator ===
# Reset cached gradients to zero
g_optimizer.zero_grad()
# Get discriminator outputs for the fake samples
prediction_fake_g = discriminator(fake_samples)
# Compute the loss function
g_loss = -torch.mean(prediction_fake_g)
# Backpropagate the gradients
g_loss.backward()
# Update the weights
g_optimizer.step()
return d_loss_real + d_loss_fake, g_loss