-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase_model.py
352 lines (255 loc) · 12.6 KB
/
base_model.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import numpy as np
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from utils import count_params
class BaseCnnModel(nn.Module):
def __init__(self, input_length:int, input_ch:int=1, conv1_out_fea:int=32, conv2_out_fea:int=64, gap=False):
super().__init__()
self.input_length = input_length
# the number of filters shold be around 32~64
assert 32<= conv1_out_fea <= 64, 'number of filters in conv1 must in the range of 32~64'
assert 32<= conv2_out_fea <= 64, 'number of filters in conv1 must in the range of 32~64'
self.conv1_out_fea = conv1_out_fea
self.conv2_out_fea = conv2_out_fea
self.conv1 = nn.Conv2d(input_ch, self.conv1_out_fea, 9) # (self.conv1_out_fea, input_length-9+1, same)
self.pool1 = nn.MaxPool2d(2) # (self.conv1_out_fea, (input_length-9+1)//2, same)
self.act = nn.ReLU()
self.conv2 = nn.Conv2d(self.conv1_out_fea, self.conv2_out_fea, 7) # (self.conv2_out_fea, (input_length-9+1)//2-7+1, same)
self.pool2 = nn.MaxPool2d(2) # (self.conv2_out_fea, ((input_length-9+1)//2-7+1)//2, same)
self.gap = nn.AdaptiveAvgPool2d(1) if gap else None
self.cls = nn.Linear(self.conv2_out_fea, 10) if gap else nn.Linear(self.conv2_out_fea * (((input_length-9+1)//2-7+1)//2)**2, 10)
#TODO the number of learnable parameter should be around 400,000
#assert 399500 <= self.num_params() <= 400500, 'number of parameters should around 400k'
def features(self, input):
x = self.act(self.pool1(self.conv1(input))) # (B, self.conv1_out_fea, (input_length-9+1)//2, same)
x = self.pool2(self.conv2(x)) # (B, self.conv2_out_fea, ((input_length-9+1)//2-7+1)//2, same)
return x
def logits(self, features):
x = self.gap(features) if self.gap else features
x = x.view(x.size(0), -1)
x = self.cls(x)
return x
def forward(self, input):
x = self.features(input)
x = self.logits(x)
# since nn.CrossEntropy wiil do the softmax at first
# we only need logit output here
#x = F.softmax(x, dim=1)
return x
def num_params(self):
return count_params(self)
class BaseFcnModel(nn.Module):
def __init__(self, input_length:int, input_ch:int=1, fc1_unit:int=128, fc2_unit:int=256):
super().__init__()
# the number of unit should be around 128~256
assert 128 <= fc1_unit <= 256, 'number of unit in fc1 should around 128~256'
assert 128 <= fc2_unit <= 256, 'number of unit in fc2 should around 128~256'
self.fc1_unit = fc1_unit
self.fc2_unit = fc2_unit
self.fc1 = nn.Linear(input_ch*input_length*input_length, self.fc1_unit)
self.act = nn.ReLU()
self.fc2 = nn.Linear(self.fc1_unit, self.fc2_unit)
self.cls = nn.Linear(self.fc2_unit, 10)
#TODO
#assert 399500 <= self.num_params() <= 400500, 'number of parameters should around 400k'
def forward(self, input):
input = input.view(input.size(0), -1)
output = self.act(self.fc1(input))
output = self.cls(self.act(self.fc2(output)))
return output
def num_params(self):
return count_params(self)
class BaseStn(nn.Module):
def __init__(self, model_name:str, input_ch:int, input_length:int, trans_type:str,
conv1_kernel:int = 5, conv2_kernel:int = 5, conv1_outdim:int = 20,
conv2_outdim:int = 20, theta_row:int=2, theta_col:int=3,
fc1_outdim:int = 32, fc2_outdim:int = 32, fc3_outdim:int = 1, trans_task:str = 'Aff'
):
"""The base STN
Arguments:
model_name {str} -- ST-CNN or ST-FCN
trans_type {str} -- ['R', 'RTS', 'P', 'E', 'T', 'TU', None]
input_ch {int} -- the input object channel
input_length {int} -- the input object length
Keyword Arguments:
conv1_kernel {int} -- kernel size of convolution layer 1 (default: {5})
conv2_kernel {int} -- kernel size of convolution layer 2 (default: {5})
conv1_outdim {int} -- the output dim of convolution layer 1 (default: {20})
conv2_outdim {int} -- the output dim of convolution layer 2 (default: {20})
theta_row {int} -- the row count of parameters of the transformation (default: {2})
theta_col {int} -- the col count of parameters of the transformation (default: {3})
fc1_outdim {int} -- the output dim of fully connected layer 1 (default: {32})
fc2_outdim {int} -- the output dim of fully connected layer 2 (default: {32})
fc3_outdim {int} -- the output dim of fully connected layer 3 (default: {6})
#TODO
trans_task {str} -- the type of transformation (default: {'Aff'})
"""
assert model_name in ['ST-CNN', 'ST-FCN'], 'model name must be either ST-CNN or ST-FCN'
assert trans_task in ['Aff','Proj','TPS'], 'trans_task must be one of Aff, Proj, TPS'
assert trans_type in ['R', 'RTS', 'P', 'E', 'T', 'TU', None], 'trans_type must be R, RTS, P, E, T, TU, None'
super().__init__()
self.model_name = model_name
self.trans_task = trans_task
self.trans_type = trans_type
self.input_ch = input_ch
self.input_length= input_length
self.conv1_kernel = conv1_kernel
self.conv2_kernel = conv2_kernel
self.conv1_outdim = conv1_outdim
self.conv2_outdim = conv2_outdim
self.conv_out_dim = self.conv2_outdim*((((self.input_length - self.conv1_kernel)+1)//2 -
self.conv2_kernel)+1)**2
self.theta_row = theta_row
self.theta_col = theta_col
self.theta = None
if trans_type == 'R':
self.fc_outdim = 1
self.register_buffer('cos_matrix', torch.tensor([[1., 0, 0],
[0, 1., 0]], requires_grad=False).unsqueeze(0)) # (1,2,3)
self.register_buffer('sin_matrix', torch.tensor([[0, -1., 0],
[1., 0, 0]], requires_grad=False).unsqueeze(0)) # (1,2,3)
elif trans_type == 'RTS':
self.fc_outdim = 6
else:
raise(TypeError)
self.fc1_outdim = fc1_outdim
self.fc2_outdim = fc2_outdim
self.fc3_outdim = fc3_outdim
# --localisation networks --
# For ST-CNN
if model_name == 'ST-CNN':
self.conv_loc = nn.Sequential(
nn.Conv2d(self.input_ch, self.conv1_outdim, self.conv1_kernel), # (20, 24, 24)
nn.MaxPool2d(2), # (20, 12, 12)
nn.ReLU(),
nn.Conv2d(self.conv1_outdim, self.conv2_outdim, self.conv2_kernel), # (20, 8, 8)
nn.ReLU()
)
self.fc_loc = nn.Linear(self.conv_out_dim, self.fc_outdim) # (self.fc_outdim)
# For ST-FCN
else:
self.fc_loc = nn.Sequential(
nn.Linear(self.input_ch*self.input_length**2, self.fc1_outdim), # (32)
nn.ReLU(),
nn.Linear(self.fc1_outdim, self.fc2_outdim), # (32)
nn.ReLU(),
nn.Linear(self.fc2_outdim, self.fc3_outdim) # (self.fc_outdim)
)
def forward(self, input):
if self.model_name == 'ST-CNN':
output = self.conv_loc(input)
output = output.view(output.size(0), -1)
theta = self.fc_loc(output) #(N, self.fc_outdim)
# 1. for only R transformation case
if self.trans_type == 'R':
theta = theta.unsqueeze(-1) # (N, 1, 1)
theta = torch.cos(theta) * self.cos_matrix + torch.sin(theta) * self.sin_matrix
# 2. for general affine
elif self.trans_type == 'RTS':
theta = theta.view(-1, self.theta_row , self.theta_col) # (N, 2, 3)
self.theta = theta
else:
#TODO
pass
# grid generator
if self.trans_task == 'Aff':
grid = F.affine_grid(theta, input.size(), align_corners=False)
grid_sample = F.grid_sample(input, grid, align_corners=False, padding_mode="border", mode='bilinear')
return grid_sample
elif self.trans_task == 'Proj':
#TODO
pass
else:
#TODO
pass
else:
theta = self.fc_loc(input.view(input.size(0),-1))
# 1. for only R transformation case
if self.trans_type == 'R':
theta = theta.unsqueeze(-1) # (N, 1, 1)
theta = torch.cos(theta) * self.cos_matrix + torch.sin(theta) * self.sin_matrix
# 2. for general affine
elif self.trans_type == 'RTS':
theta = theta.view(-1, self.theta_row , self.theta_col) # (n, 2, 3)
else:
#TODO
pass
# grid generator
grid = F.affine_grid(theta, input.size(), align_corners=False)
grid_sample = F.grid_sample(input, grid, align_corners=False, padding_mode="border", mode='bilinear')
return grid_sample
def num_params(self):
return count_params(self)
def gen_theta(self, input):
if self.model_name == 'ST-CNN':
output = self.conv_loc(input)
output = output.view(output.size(0), -1)
theta = self.fc_loc(output) #(N, self.fc_outdim)
# 1. for only R transformation case
if self.trans_type == 'R':
theta = theta.unsqueeze(-1) # (N, 1, 1)
theta = torch.cos(theta) * self.cos_matrix + torch.sin(theta) * self.sin_matrix
# 2. for general affine
elif self.trans_type == 'RTS':
theta = theta.view(-1, self.theta_row , self.theta_col) # (N, 2, 3)
self.theta = theta
return self.theta
else:
#TODO
pass
else:
theta = self.fc_loc(input.view(input.size(0),-1))
# 1. for only R transformation case
if self.trans_type == 'R':
theta = theta.unsqueeze(-1) # (N, 1, 1)
theta = torch.cos(theta) * self.cos_matrix + torch.sin(theta) * self.sin_matrix
self.theta = theta
# 2. for general affine
elif self.trans_type == 'RTS':
theta = theta.view(-1, self.theta_row , self.theta_col) # (n, 2, 3)
self.theta = theta
else:
#TODO
pass
return self.theta
if __name__ == '__main__':
#--test image
# rand_img = torch.randn(1,1,28,28)
# stn = BaseStn(model_name='ST-CNN', trans_task = 'Aff', trans_type = 'RTS',input_ch=rand_img.size(1) , input_length=rand_img.size(2))
# out = stn(rand_img)
# print("Output from stn:", out.size())
# cnn = BaseCnnModel(input_length=rand_img.size(2), gap=True)
# out = cnn(rand_img)
# print("Output from CNN:", out.size())
#fcn = BaseFcnModel(input_length=rand_img.size(2))
#out = fcn(rand_img)
#print("Output from FCN:", out.size())
#--real image
from torchvision.datasets import MNIST
from matplotlib import pyplot as plt
filepath = '/home/jarvis1121/AI/Rico_Repo/data'
dataset = MNIST(root=filepath, train=False)
# print(len(dataset)) # 60k for train, 10k for test
idk = 25
img, _ = dataset[idk]
img_np = np.array(img)
img = torch.from_numpy(img_np.reshape(1,1,28,28)).float()
stn = BaseStn(model_name='ST-CNN', trans_task = 'Aff', trans_type = 'RTS',input_ch=1 , input_length=28)
out = stn(img)
print("Output from stn:", out.size())
out_np = out.detach().numpy().reshape(28,28)
f, axarr = plt.subplots(1,2)
axarr[0].imshow(img_np, cmap='gray')
axarr[1].imshow(out_np, cmap='gray')
plt.show()
#modules = stn.named_children()
#for name, module in modules:
# if name == 'conv_loc':
# module.register_backward_hook(hook_fn_backward)
#output = stn(rand_img)
#output = output.
#output.backward()
#print(grad_in)
# pass