-
Notifications
You must be signed in to change notification settings - Fork 52
/
cnn_utils.py
155 lines (126 loc) · 6.01 KB
/
cnn_utils.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
__author__ = 'yihanjiang'
import torch
import torch.nn.functional as F
# utility for Same Shape CNN 1D
class SameShapeConv1d(torch.nn.Module):
def __init__(self, num_layer, in_channels, out_channels, kernel_size, activation = 'elu', no_act = False):
super(SameShapeConv1d, self).__init__()
self.cnns = torch.nn.ModuleList()
self.num_layer = num_layer
self.no_act = no_act
for idx in range(num_layer):
if idx == 0:
self.cnns.append(torch.nn.Conv1d(in_channels = in_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=1, padding=(kernel_size // 2),
dilation=1, groups=1, bias=True)
)
else:
self.cnns.append(torch.nn.Conv1d(in_channels = out_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=1, padding=(kernel_size // 2),
dilation=1, groups=1, bias=True)
)
if activation == 'elu':
self.activation = F.elu
elif activation == 'relu':
self.activation = F.relu
elif activation == 'selu':
self.activation = F.selu
elif activation == 'prelu':
self.activation = F.prelu
else:
self.activation = F.elu
def forward(self, inputs):
inputs = torch.transpose(inputs, 1,2)
x = inputs
for idx in range(self.num_layer):
if self.no_act:
x = self.cnns[idx](x)
else:
x = self.activation(self.cnns[idx](x))
outputs = torch.transpose(x, 1,2)
return outputs
class DenseSameShapeConv1d(torch.nn.Module):
def __init__(self, num_layer, in_channels, out_channels, kernel_size):
super(DenseSameShapeConv1d, self).__init__()
self.cnns = torch.nn.ModuleList()
self.num_layer = num_layer
for idx in range(num_layer):
if idx == 0:
self.cnns.append(torch.nn.Conv1d(in_channels = in_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=1, padding=(kernel_size // 2),
dilation=1, groups=1, bias=True)
)
else:
self.cnns.append(torch.nn.Conv1d(in_channels = in_channels + idx * out_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=1, padding=(kernel_size // 2),
dilation=1, groups=1, bias=True)
)
def forward(self, inputs):
inputs = torch.transpose(inputs, 1,2)
for idx in range(self.num_layer):
if idx == 0:
this_input = inputs
else:
this_input = torch.cat([this_input, output], dim=1)
x = self.cnns[idx](this_input)
output = F.elu(x)
outputs = torch.transpose(output, 1,2)
return outputs
###################################################
# utility for Same Shape CNN 2D: experimental!
# not working very well yet!
###################################################
class SameShapeConv2d(torch.nn.Module):
def __init__(self, num_layer, in_channels, out_channels, kernel_size, no_act = False):
super(SameShapeConv2d, self).__init__()
self.no_act = no_act
self.cnns = torch.nn.ModuleList()
self.num_layer = num_layer
for idx in range(num_layer):
if idx == 0:
self.cnns.append(torch.nn.Conv2d(in_channels = in_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=1, padding=(kernel_size // 2),
dilation=1, groups=1, bias=True)
)
else:
self.cnns.append(torch.nn.Conv2d(in_channels = out_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=1, padding=(kernel_size // 2),
dilation=1, groups=1, bias=True)
)
def forward(self, inputs):
x = inputs
for idx in range(self.num_layer):
if self.no_act:
x = self.cnns[idx](x)
else:
x = F.elu(self.cnns[idx](x))
return x
class DenseSameShapeConv2d(torch.nn.Module):
def __init__(self, num_layer, in_channels, out_channels, kernel_size, no_act = False):
super(DenseSameShapeConv2d, self).__init__()
self.no_act = no_act
self.cnns = torch.nn.ModuleList()
self.num_layer = num_layer
for idx in range(num_layer):
if idx == 0:
self.cnns.append(torch.nn.Conv2d(in_channels = in_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=1, padding=(kernel_size // 2),
dilation=1, groups=1, bias=True)
)
else:
self.cnns.append(torch.nn.Conv2d(in_channels = in_channels + idx * out_channels, out_channels=out_channels,
kernel_size=kernel_size, stride=1, padding=(kernel_size // 2),
dilation=1, groups=1, bias=True)
)
def forward(self, inputs):
x = inputs
for idx in range(self.num_layer):
if idx == 0:
this_input = inputs
else:
this_input = torch.cat([this_input, output], dim=1)
if self.no_act:
output = self.cnns[idx](this_input)
else:
output = F.elu(self.cnns[idx](this_input))
return output