-
Notifications
You must be signed in to change notification settings - Fork 0
/
modules.py
195 lines (160 loc) · 6.4 KB
/
modules.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
import numpy as np
import torch
import torch.nn as nn
class EncoderBlock(nn.Module):
def __init__(self, input_shape):
super(EncoderBlock, self).__init__()
self.input_shape = input_shape
self.width = input_shape[0]
self.conv1 = nn.Sequential(
nn.Conv2d(self.width, self.width, kernel_size=(3, 1), padding='same',dilation=(1, 1)), # 3x1 2D Convolution (d=1)
nn.BatchNorm2d(self.width),
nn.PReLU(num_parameters=self.width, init=0.3),
nn.Conv2d(self.width, self.width, kernel_size=(1, 3), padding='same',dilation=(1, 1)), # 1x3 2D Convolution (d=1)
nn.BatchNorm2d(self.width),
nn.PReLU(num_parameters=self.width, init=0.3),
nn.Conv2d(self.width, self.width, kernel_size=(3, 1), padding='same',dilation=(2, 2)), # 3x1 2D Convolution (d=2)
nn.BatchNorm2d(self.width),
nn.PReLU(num_parameters=self.width, init=0.3),
nn.Conv2d(self.width, self.width, kernel_size=(1, 3), padding='same',dilation=(2, 2)), # 1x3 2D Convolution (d=2)
nn.BatchNorm2d(self.width),
nn.PReLU(num_parameters=self.width, init=0.3),
nn.Conv2d(self.width, self.width, kernel_size=(3, 1), padding='same',dilation=(3, 3)), # 3x1 2D Convolution (d=3)
nn.BatchNorm2d(self.width),
nn.PReLU(num_parameters=self.width, init=0.3),
nn.Conv2d(self.width, self.width, kernel_size=(1, 3), padding='same',dilation=(3, 3)), # 1x3 2D Convolution (d=3)
nn.BatchNorm2d(self.width),
nn.PReLU(num_parameters=self.width, init=0.3),
)
self.conv2 = nn.Sequential(
nn.Conv2d(self.width,self.width, kernel_size=(3, 3), padding='same'),
nn.BatchNorm2d(self.width),
nn.PReLU(num_parameters=self.width, init=0.3)
)
self.prelu1 = nn.PReLU(num_parameters=2*self.width, init=0.3)
self.conv1x1 = nn.Sequential(
nn.Conv2d(2*self.width,self.width, kernel_size=(1, 1), padding='same'),
nn.BatchNorm2d(self.width),
nn.PReLU(num_parameters=self.width, init=0.3)
)
self.prelu2 = nn.PReLU(num_parameters=self.width, init=0.3)
self.Identity = nn.Identity()
def forward(self, x):
identity = self.Identity(x)
res1=self.conv1(x)
res2=self.conv2(x)
res=self.prelu1(torch.cat((res1,res2),dim=1))
res=self.conv1x1(res)
return self.prelu2(identity + res)
class DecoderBlock(nn.Module):
def __init__(self, input_shape, expansion):
super(DecoderBlock, self).__init__()
self.input_shape = input_shape
self.width = input_shape[0]
self.expansion_width = self.width*3*expansion
self.conv1 = nn.Sequential(
nn.Conv2d(self.width,self.expansion_width, kernel_size=(3,3), padding='same', dilation=2),
nn.BatchNorm2d(self.expansion_width),
nn.PReLU(num_parameters=self.expansion_width, init=0.3),
nn.Conv2d(self.expansion_width,self.expansion_width, kernel_size=(1,3), padding='same', dilation=3),
nn.BatchNorm2d(self.expansion_width),
nn.PReLU(num_parameters=self.expansion_width, init=0.3),
nn.Conv2d(self.expansion_width,self.expansion_width, kernel_size=(3,1), padding='same', dilation=3),
nn.BatchNorm2d(self.expansion_width),
nn.PReLU(num_parameters=self.expansion_width, init=0.3),
nn.Conv2d(self.expansion_width,self.width, kernel_size=(3,3), padding='same'),
nn.BatchNorm2d(self.width),
nn.PReLU(num_parameters=self.width, init=0.3),
)
self.conv2 = nn.Sequential(
nn.Conv2d(self.width,self.expansion_width, kernel_size=(1,3), padding='same'),
nn.BatchNorm2d(self.expansion_width),
nn.PReLU(num_parameters=self.expansion_width, init=0.3),
nn.Conv2d(self.expansion_width,self.expansion_width, kernel_size=(5,1), padding='same'),
nn.BatchNorm2d(self.expansion_width),
nn.PReLU(num_parameters=self.expansion_width, init=0.3),
nn.Conv2d(self.expansion_width,self.expansion_width, kernel_size=(1,5), padding='same'),
nn.BatchNorm2d(self.expansion_width),
nn.PReLU(num_parameters=self.expansion_width, init=0.3),
nn.Conv2d(self.expansion_width,self.width, kernel_size=(3,1), padding='same'),
nn.BatchNorm2d(self.width),
nn.PReLU(num_parameters=self.width, init=0.3)
)
self.prelu1 = nn.PReLU(num_parameters=2*self.width, init=0.3)
self.conv1x1 = nn.Sequential(
nn.Conv2d(2*self.width,self.width, kernel_size=(1,1), padding='same'),
nn.BatchNorm2d(self.width),
nn.PReLU(num_parameters=self.width, init=0.3)
)
self.prelu2 = nn.PReLU(num_parameters=self.width, init=0.3)
self.Identity = nn.Identity()
def forward(self, x):
identity = self.Identity(x)
res1=self.conv1(x)
res2=self.conv2(x)
res=self.prelu1(torch.cat((res1,res2),dim=1))
res=self.conv1x1(res)
return self.prelu2(identity + res)
class RecurrentBlock(nn.Module):
def __init__(self, input_size, hidden_size, keep_dim=False):
super(RecurrentBlock, self).__init__()
self.input_size = input_size
self.hidden_size = hidden_size
self.recurrent_block = nn.GRU(self.input_size, self.hidden_size, batch_first=True)
self.keep_dim = keep_dim
if keep_dim:
self.fc = nn.Linear(self.hidden_size, self.input_size)
def forward(self, x):
x, hidden = self.recurrent_block(x)
if self.keep_dim:
x = self.fc(x)
return x, hidden
class Encoder(nn.Module):
def __init__(self, input_shape):
super(Encoder, self).__init__()
self.input_shape = input_shape
self.input_size = np.prod(input_shape)
self.width = input_shape[0]
self.encoder = nn.Sequential(
nn.Conv2d(self.width,self.width, kernel_size=(5,5), padding='same'),
nn.BatchNorm2d(self.width),
nn.PReLU(num_parameters=self.width, init=0.3),
EncoderBlock(self.input_shape),
nn.Flatten(),
)
def forward(self, x):
x = self.encoder(x)
# x = self.encoder_fc(x)
return x
class Decoder(nn.Module):
def __init__(self, input_shape, expansion):
super(Decoder, self).__init__()
self.input_shape = input_shape
self.input_size = np.prod(input_shape)
self.width = input_shape[0]
self.decoder = nn.Sequential(
nn.Conv2d(self.width,self.width,kernel_size=(5,5),padding='same'),
nn.BatchNorm2d(self.width),
nn.PReLU(num_parameters=self.width, init=0.3),
DecoderBlock(self.input_shape, expansion),
DecoderBlock(self.input_shape, expansion),
nn.Sigmoid()
)
def forward(self, x):
x = self.decoder(x)
return x
class Classifier(nn.Module):
def __init__(self, input_size, num_classes):
super(Classifier, self).__init__()
self.num_classes = num_classes
self.classifier = nn.Sequential(
nn.Flatten(),
nn.Linear(input_size, 512),
nn.ReLU(),
nn.Linear(512, 128),
nn.ReLU(),
nn.Linear(128, self.num_classes),
)
def forward(self, x):
x = self.classifier(x)
return x