-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
220 lines (199 loc) · 8.59 KB
/
model.py
File metadata and controls
220 lines (199 loc) · 8.59 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
216
217
218
219
import torch
import torch.nn as nn
from torch.nn import init
import torch.nn.functional as F
import numpy as np
import torch.optim as optim
from sklearn.model_selection import train_test_split
import torch.utils.data as utils
import sys
import pickle as pkl
cuda = torch.device("cuda")
def conv1x3(in_channels, out_channels, stride=1, padding=1, bias=True,groups=1):
return nn.Conv1d(
in_channels,
out_channels,
kernel_size=3,
stride=stride,
padding=padding,
bias=bias,
groups=groups)
def upconv1x2(in_channels, out_channels, kernel):
return nn.ConvTranspose1d(
in_channels,
out_channels,
kernel_size=kernel,
stride=2,
padding=1
)
class DownConv(nn.Module):
def __init__(self, in_channels, out_channels, block_id, pooling = True):
super(DownConv,self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.pooling = pooling
self.activation = nn.LeakyReLU(0.01)
self.conv1 = conv1x3(self.in_channels, self.out_channels)
self.conv1_BN = nn.InstanceNorm1d(self.out_channels)
self.conv2 = conv1x3(self.out_channels, self.out_channels)
self.conv2_BN = nn.InstanceNorm1d(self.out_channels)
self.pool = nn.MaxPool1d(kernel_size=2, stride=2)
def forward(self,x):
x = self.activation(self.conv1_BN(self.conv1(x)))
x = self.activation(self.conv1_BN(self.conv2(x)))
before_pool = x
if self.pooling:
x = self.pool(x)
return x, before_pool
class UpConv(nn.Module):
def __init__(self, in_channels, out_channels, skip_channels, cond_channels, block_id, activation = nn.LeakyReLU(0.01), upconv_kernel=2):
super(UpConv, self).__init__()
self.skip_channels = skip_channels
self.in_channels = in_channels
self.out_channels = out_channels
self.cond_channels = cond_channels
self.activation = activation
self.upconv = upconv1x2(self.in_channels, self.out_channels,kernel=upconv_kernel)
self.upconv_BN = nn.InstanceNorm1d(self.out_channels)
self.conv1 = conv1x3( self.skip_channels + self.out_channels, self.out_channels)
self.conv1_BN = nn.InstanceNorm1d(self.out_channels)
self.conv2 = conv1x3(self.out_channels + self.cond_channels, self.out_channels)
self.conv2_BN = nn.InstanceNorm1d(self.out_channels)
def crop_and_concat(self, upsampled, bypass):
c = (bypass.size()[2] - upsampled.size()[2]) // 2
bypass = F.pad(bypass, (-c, -c))
if bypass.shape[2] > upsampled.shape[2]:
bypass = F.pad(bypass, (0, -(bypass.shape[2] - upsampled.shape[2])))
else:
bypass = F.pad(bypass, ((0, bypass.shape[2] - upsampled.shape[2]) ))
return torch.cat((upsampled, bypass), 1)
def forward(self, res, dec, cond):
x = self.activation(self.upconv_BN(self.upconv(dec)))
x = self.crop_and_concat(x, res)
x = self.activation(self.conv1_BN(self.conv1(x)))
if self.cond_channels:
x = self.crop_and_concat(x, cond)
x = self.conv2(x)
x = self.activation(self.conv2_BN(x))
return x
class Onset_Offset_Encoder(nn.Module):
def __init__(self, depth = 3, start_channels = 128):
super(Onset_Offset_Encoder, self).__init__()
self.start_channels = start_channels
self.depth = depth
self.down_convs = []
self.construct_layers()
self.down_convs = nn.ModuleList(self.down_convs)
self.reset_params()
def construct_layers(self):
for i in range(self.depth):
ins = self.start_channels if i == 0 else outs
outs = self.start_channels * (2 ** (i+1))
pooling = True if i < self.depth else False
DC = DownConv(ins, outs, pooling=pooling, block_id = i + 9)
self.down_convs.append(DC)
@staticmethod
def weight_init(m):
if isinstance(m, nn.Conv1d):
init.xavier_normal_(m.weight)
init.constant_(m.bias, 0)
def reset_params(self):
for i, m in enumerate(self.modules()):
self.weight_init(m)
def forward(self, x):
condition_tensors = []
for i, module in enumerate(self.down_convs):
x,_ = module(x)
if (i > self.depth - 3):
condition_tensors.append(x)
return condition_tensors
class MBRBlock(nn.Module):
def __init__(self, in_channels, num_of_band):
super(MBRBlock, self).__init__()
self.in_dim = in_channels
self.num_of_band = num_of_band
self.conv_list1 = []
self.bn_list1 = []
self.conv_list2 = []
self.bn_list2 = []
self.activation = nn.LeakyReLU(0.01)
self.band_dim = self.in_dim // self.num_of_band
for i in range(self.num_of_band):
self.conv_list1.append(nn.Conv1d(in_channels = self.band_dim, out_channels = self.band_dim, kernel_size = 3, padding = 1))
for i in range(self.num_of_band):
self.conv_list2.append(nn.Conv1d(in_channels = self.band_dim, out_channels = self.band_dim, kernel_size = 3, padding = 1))
for i in range(self.num_of_band):
self.bn_list1.append(nn.InstanceNorm1d(self.band_dim))
for i in range(self.num_of_band):
self.bn_list2.append(nn.InstanceNorm1d(self.band_dim))
self.conv_list1 = nn.ModuleList(self.conv_list1)
self.conv_list2 = nn.ModuleList(self.conv_list2)
self.bn_list1 = nn.ModuleList(self.bn_list1)
self.bn_list2 = nn.ModuleList(self.bn_list2)
def forward(self,x):
bands = torch.chunk(x, self.num_of_band, dim = 1)
for i in range(len(bands)):
t = self.activation(self.bn_list1[i](self.conv_list1[i](bands[i])))
t = self.bn_list2[i](self.conv_list2[i](t))
torch.add(bands[i],1,t)
x = torch.add(x,1,torch.cat(bands, dim = 1))
return x
class SentimentNet(nn.Module):
def __init__(self, depth = 5,start_channels = 128):
super(SentimentNet, self).__init__()
self.depth = depth
self.start_channels = start_channels
self.construct_layers()
self.reset_params()
#@staticmethod
def construct_layers(self):
self.down_convs = []
self.up_convs = []
for i in range(self.depth):
ins = self.start_channels if i == 0 else outs
outs = self.start_channels * (2 ** (i+1))
pooling = True if i < self.depth-1 else False
DC = DownConv(ins, outs, pooling=pooling, block_id=i)
self.down_convs.append(DC)
self.up_convs.append(UpConv(4096,2048,2048, 1024, block_id = 5, upconv_kernel=6))
self.up_convs.append(UpConv(2048,1024,1024, 512, block_id = 6, upconv_kernel=4))
self.up_convs.append(UpConv(1024,1024,512,0,block_id= 7, upconv_kernel=3))
self.up_convs.append(UpConv(1024,1024,256,0, block_id = 8))
self.down_convs = nn.ModuleList(self.down_convs)
self.up_convs = nn.ModuleList(self.up_convs)
self.MBRBlock1 = MBRBlock(1024,2)
self.MBRBlock2 = MBRBlock(1024,4)
self.MBRBlock3 = MBRBlock(1024,8)
self.MBRBlock4 = MBRBlock(1024,16)
self.lastconv = nn.ConvTranspose1d(1024,1025,kernel_size=3, stride=1, padding=1)
self.lrelu = nn.LeakyReLU(0.01)
self.onset_offset_encoder = Onset_Offset_Encoder()
@staticmethod
def weight_init(m):
if isinstance(m, nn.Conv1d):
init.xavier_normal_(m.weight)
init.constant_(m.bias, 0)
if isinstance(m, nn.ConvTranspose1d):
init.xavier_normal_(m.weight)
init.constant_(m.bias, 0)
def reset_params(self):
for i, m in enumerate(self.modules()):
self.weight_init(m)
def forward(self, x, cond):
encoder_layer_outputs = []
for i, module in enumerate(self.down_convs):
x, before_pool = module(x)
encoder_layer_outputs.append(before_pool)
Onoff_Conditions = self.onset_offset_encoder(cond)
for i, module in enumerate(self.up_convs):
before_pool = encoder_layer_outputs[-(i+2)]
if i < self.onset_offset_encoder.depth - 1:
x = module(before_pool, x, Onoff_Conditions[i-1])
else:
x = module(before_pool, x, None)
x = self.MBRBlock1(x)
x = self.MBRBlock2(x)
x = self.MBRBlock3(x)
x = self.MBRBlock4(x)
x = self.lrelu(self.lastconv(x))
return x