-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
260 lines (205 loc) · 9.59 KB
/
models.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
from __future__ import division
from bilinear_layers import *
from models_lpf import *
class plain_model_3layers(nn.Module):
def __init__(self):
super(plain_model_3layers, self).__init__()
# we define convolutional layers
self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, stride=1, padding=1)
self.bn1 = nn.BatchNorm2d(32)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1)
self.bn3 = nn.BatchNorm2d(128)
# 2 fully connected layers to transform the output of the convolution layers to the final output
self.fc1 = nn.Linear(in_features=128 * 5 * 5, out_features=128)
self.fcbn1 = nn.BatchNorm1d(128)
self.fc2 = nn.Linear(in_features=128, out_features=1)
self.dropout_rate = 0.5
self.drop_layer = nn.Dropout(p=self.dropout_rate)
def forward(self, s, jsw):
# we apply the convolution layers, followed by batch normalisation,
# maxpool and relu x 3
s = self.bn1(self.conv1(s))
s = F.relu(F.max_pool2d(s, 2))
s = self.bn2(self.conv2(s))
s = F.relu(F.max_pool2d(s, 2))
s = self.bn3(self.conv3(s))
s = F.relu(F.max_pool2d(s, 2))
# flatten the output for each image
s = s.view(-1, 5 * 5 * 128)
s = F.dropout(F.relu(self.fcbn1(self.fc1(s))),
p=self.dropout_rate, training=self.training) # batch_size x 128
s = self.fc2(s)
return s
class plain_model_4layers(nn.Module):
def __init__(self):
super(plain_model_4layers, self).__init__()
# we define convolutional layers
self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, stride=1, padding=1)
self.bn1 = nn.BatchNorm2d(32)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1)
self.bn3 = nn.BatchNorm2d(128)
self.conv4 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, stride=1, padding=1)
self.bn4 = nn.BatchNorm2d(256)
# 2 fully connected layers to transform the output of the convolution layers to the final output
self.fc1 = nn.Linear(in_features=256 * 3 * 3, out_features=128)
self.fcbn1 = nn.BatchNorm1d(128)
self.fc2 = nn.Linear(in_features=128, out_features=1)
self.dropout_rate = 0.5
self.drop_layer = nn.Dropout(p=self.dropout_rate)
def forward(self, s, jsw):
# we apply the convolution layers, followed by batch normalisation,
# maxpool and relu x 3
s = self.bn1(self.conv1(s))
s = F.relu(F.max_pool2d(s, 2))
s = self.bn2(self.conv2(s))
s = F.relu(F.max_pool2d(s, 2))
s = self.bn3(self.conv3(s))
s = F.relu(F.max_pool2d(s, 2))
s = self.bn4(self.conv4(s))
s = F.relu(F.max_pool2d(s, 2))
# flatten the output for each image
s = s.view(-1, 3 * 3 * 256)
s = F.dropout(F.relu(self.fcbn1(self.fc1(s))),
p=self.dropout_rate, training=self.training) # batch_size x 128
s = self.fc2(s)
return s
class improved_bcnn(nn.Module):
def __init__(self):
super(improved_bcnn, self).__init__()
# we define convolutional layers
self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, stride=1, padding=1)
self.bn1 = nn.BatchNorm2d(32)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1)
self.bn3 = nn.BatchNorm2d(128)
# 2 fully connected layers to transform the output of the convolution layers to the final output
self.fc1 = nn.Linear(in_features=128 * 128, out_features=128)
self.fcbn1 = nn.BatchNorm1d(128)
self.fc2 = nn.Linear(in_features=128, out_features=1)
self.dropout_rate = 0.5
self.matrix_sqrt = matrix_sqrt.apply
self.sign_sqrt = sign_sqrt.apply
self.classifier = torch.nn.Linear(
in_features=128 * 128, out_features=1, bias=True)
# torch.nn.init.xavier_uniform_(self.classifier.weight)
def forward(self, s, jsw):
# we apply the convolution layers, followed by batch normalisation,
# maxpool and relu x 3
N = s.size()[0] # batch size
s = self.bn1(self.conv1(s))
s = F.relu(F.max_pool2d(s, 2))
s = self.bn2(self.conv2(s))
s = F.relu(F.max_pool2d(s, 2))
s = self.bn3(self.conv3(s))
s = F.relu(F.max_pool2d(s, 2))
s = torch.reshape(s, (N, 128, 6 * 6))
s = torch.bmm(s, torch.transpose(s, 1, 2)) / (6 * 6)
assert s.size() == (N, 128, 128)
s = self.matrix_sqrt(s + 1e-8)
s = self.sign_sqrt(s + 1e-8)
s = torch.reshape(s, (N, 128 * 128))
s = torch.nn.functional.normalize(s)
s = F.dropout(F.relu(self.fcbn1(self.fc1(s))),
p=self.dropout_rate, training=self.training)
s = self.fc2(s)
return s
class antialised_cnn(nn.Module):
def __init__(self):
super(antialised_cnn, self).__init__()
# we define convolutional layers
filter_size = 1
self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=2)
self.bn1 = nn.BatchNorm2d(32)
self.d1 = Downsample(filt_size=filter_size, stride=2, channels=32, pad_off=-1)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=2)
self.bn2 = nn.BatchNorm2d(64)
self.d2 = Downsample(filt_size=filter_size, stride=2, channels=64, pad_off=-1)
self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=2)
self.bn3 = nn.BatchNorm2d(128)
self.d3 = Downsample(filt_size=filter_size, stride=2, channels=128, pad_off=-1)
# 2 fully connected layers to transform the output of the convolution layers to the final output
self.fc1 = nn.Linear(in_features=128 * 6 * 6, out_features=128)
self.fcbn1 = nn.BatchNorm1d(128)
self.fc2 = nn.Linear(in_features=128, out_features=1)
self.dropout_rate = 0.5
self.drop_layer = nn.Dropout(p=self.dropout_rate)
def forward(self, s, jsw):
# we apply the convolution layers, followed by batch normalisation,
# maxpool and relu x 3
N = s.size()[0] # batch size
s = self.bn1(self.conv1(s))
s = self.d1(F.relu(s))
s = self.bn2(self.conv2(s))
s = self.d2(F.relu(s))
s = self.bn3(self.conv3(s))
s = self.d3(F.relu(s))
s = s.contiguous().view(s.size(0), 128 * 6 * 6)
s = F.dropout(F.relu(self.fcbn1(self.fc1(s))),
p=self.dropout_rate, training=self.training)
s = self.fc2(s)
return s
class jsw(nn.Module):
def __init__(self):
super(jsw, self).__init__()
self.fc1 = nn.Linear(in_features=221, out_features=128)
self.fcbn1 = nn.BatchNorm1d(128)
self.fc2 = nn.Linear(in_features=128, out_features=1)
def forward(self, s, jsw):
# we apply the convolution layers, followed by batch normalisation,
# maxpool and relu x 3
input = s
N = jsw.size()[0] # batch size
s = jsw.view(-1, 221)
s = F.relu(self.fcbn1(self.fc1(s)))
s = self.fc2(s)
return s
class combined(nn.Module):
def __init__(self):
super(combined, self).__init__()
# we define convolutional layers
self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, stride=1, padding=1)
self.bn1 = nn.BatchNorm2d(32)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1)
self.bn3 = nn.BatchNorm2d(128)
# 2 fully connected layers to transform the output of the convolution layers to the final output
self.fc1 = nn.Linear(in_features=128 * 6 * 6 + 221, out_features=128)
self.fcbn1 = nn.BatchNorm1d(128)
self.fc2 = nn.Linear(in_features=128, out_features=1)
self.dropout_rate = 0.3
self.drop_layer = nn.Dropout(p=self.dropout_rate)
def forward(self, s, jsw):
# we apply the convolution layers, followed by batch normalisation,
# maxpool and relu x 3
input = s
N = s.size()[0] # batch size
s = self.bn1(self.conv1(s))
s = F.relu(F.max_pool2d(s, 2))
s = self.bn2(self.conv2(s))
s = F.relu(F.max_pool2d(s, 2))
s = self.bn3(self.conv3(s))
s = F.relu(F.max_pool2d(s, 2))
# flatten the output for each image
s = s.view(-1, 6 * 6 * 128)
s = torch.cat((s, jsw), dim=1)
s = F.dropout(F.relu(self.fcbn1(self.fc1(s))),
p=self.dropout_rate, training=self.training)
s = self.fc2(s)
return s
class morphology(nn.Module):
def __init__(self):
super(morphology, self).__init__()
self.fc1 = nn.Linear(in_features=221, out_features=128)
self.fcbn1 = nn.BatchNorm1d(128)
self.fc2 = nn.Linear(in_features=128, out_features=1)
def forward(self, s, jsw):
s = jsw.view(-1, 221)
s = F.relu(self.fcbn1(self.fc1(s)))
s = self.fc2(s)
return s