-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha.py
192 lines (153 loc) · 7.04 KB
/
a.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
from torch import nn
import torch
from networks import *
from models import *
from data_utils import *
import torch.utils.data as data
class WaveNetModel(nn.Module):
"""
A Complete Wavenet Model
Args:
layers (Int): Number of layers in each block
blocks (Int): Number of wavenet blocks of this model
dilation_channels (Int): Number of channels for the dilated convolution
residual_channels (Int): Number of channels for the residual connection
skip_channels (Int): Number of channels for the skip connections
classes (Int): Number of possible values each sample can have
output_length (Int): Number of samples that are generated for each input
kernel_size (Int): Size of the dilation kernel
dtype: Parameter type of this model
Shape:
- Input: :math:`(N, C_{in}, L_{in})`
- Output: :math:`()`
L should be the length of the receptive field
"""
def __init__(self,
layers=10,
blocks=5,
dilation_channels=32,
residual_channels=32,
skip_channels=512,
classes=256,
output_length=32,
kernel_size=2,
dtype=torch.FloatTensor,
bias=False,
fast=False):
super(WaveNetModel, self).__init__()
self.layers = layers
self.blocks = blocks
self.dilation_channels = dilation_channels
self.residual_channels = residual_channels
self.skip_channels = skip_channels
self.classes = classes
self.kernel_size = kernel_size
self.dtype = dtype
self.fast = fast
# build model
receptive_field = 1
init_dilation = 1
self.dilations = []
self.filter_convs = nn.ModuleList()
self.gate_convs = nn.ModuleList()
self.residual_convs = nn.ModuleList()
self.skip_convs = nn.ModuleList()
# 1x1 convolution to create channels
self.start_conv = nn.Conv1d(in_channels=self.classes,
out_channels=residual_channels,
kernel_size=1,
bias=bias)
for b in range(blocks):
additional_scope = kernel_size - 1
new_dilation = 1
for i in range(layers):
# dilations of this layer
self.dilations.append((new_dilation, init_dilation))
# dilated convolutions
self.filter_convs.append(nn.Conv1d(in_channels=residual_channels,
out_channels=dilation_channels,
kernel_size=kernel_size,
bias=bias,
dilation=new_dilation))
self.gate_convs.append(nn.Conv1d(in_channels=residual_channels,
out_channels=dilation_channels,
kernel_size=kernel_size,
bias=bias,
dilation=new_dilation))
# 1x1 convolution for residual connection
self.residual_convs.append(nn.Conv1d(in_channels=dilation_channels,
out_channels=residual_channels,
kernel_size=1,
bias=bias))
# 1x1 convolution for skip connection
self.skip_convs.append(nn.Conv1d(in_channels=dilation_channels,
out_channels=skip_channels,
kernel_size=1,
bias=bias))
receptive_field += additional_scope
additional_scope *= 2
init_dilation = new_dilation
new_dilation *= 2
self.end_conv_1 = nn.Conv1d(in_channels=skip_channels,
out_channels=skip_channels,
kernel_size=1,
bias=True)
self.end_conv_2 = nn.Conv1d(in_channels=skip_channels,
out_channels=classes,
kernel_size=1,
bias=True)
# self.output_length = 2 ** (layers - 1)
self.output_size = output_length
self.receptive_field = receptive_field
self.input_size = receptive_field + output_length - 1
def forward(self, input, mode="normal"):
if mode == "save":
self.inputs = [None]* (self.blocks * self.layers)
x = self.start_conv(input)
skip = 0
# WaveNet layers
for i in range(self.blocks * self.layers):
# |----------------------------------------| *residual*
# | |
# | |-- conv -- tanh --| |
# -> dilate -|----| * ----|-- 1x1 -- + --> *input*
# |-- conv -- sigm --| |
# 1x1
# |
# ---------------------------------------> + -------------> *skip*
(dilation, init_dilation) = self.dilations[i]
if mode == "save":
self.inputs[i] = x[:,:,-(dilation*(self.kernel_size-1) + 1):]
elif mode == "step":
self.inputs[i] = torch.cat([self.inputs[i][:,:,1:], x], dim=2)
x = self.inputs[i]
# dilated convolution
residual = x
filter = self.filter_convs[i](x)
filter = torch.tanh(filter)
gate = self.gate_convs[i](x)
gate = torch.sigmoid(gate)
x = filter * gate
# parametrized skip connection
s = self.skip_convs[i](x)
if skip is not 0:
skip = skip[:, :, -s.size(2):]
skip = s + skip
x = self.residual_convs[i](x)
x = x + residual[:, :, dilation * (self.kernel_size - 1):]
x = torch.relu(skip)
x = torch.relu(self.end_conv_1(x))
x = self.end_conv_2(x)
return x
# def calc_receptive_field(n_layers, n_blocks):
# # this is actually the receptive_field - 1
# return int(sum([2**i for i in range(n_layers)]))*n_blocks
# n_layers = 6
# n_blocks = 3
# dataset = Dataset("../data/ptb-xl/", calc_receptive_field(n_layers, n_blocks), in_channels=256, data_len = 100, conditioned=False)
# e = dataset[0]
# x = e[0].unsqueeze(0)
# net = WaveNetModel(layers=n_layers,blocks=n_blocks,)
# out = net(x)
# print(out.shape)
# print(out)