-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
182 lines (156 loc) · 6.7 KB
/
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from __future__ import print_function
import torch.optim as optim
from torchvision import datasets, transforms
from torch.autograd import Variable
import numpy as np
nclasses = 43 # GTSRB as 43 classes
# Taken from : https://github.com/dibyadas/Visualize-Normalizations
# Implementation of LCN Filter as conv2d layer with Gaussian weights with non-triable weights
def gaussian_filter(kernel_shape):
x = np.zeros(kernel_shape, dtype='float32')
def gauss(x, y, sigma=2.0):
Z = 2 * np.pi * sigma ** 2
return 1. / Z * np.exp(-(x ** 2 + y ** 2) / (2. * sigma ** 2))
mid = np.floor(kernel_shape[-1] / 2.)
for kernel_idx in range(0, kernel_shape[1]):
for i in range(0, kernel_shape[2]):
for j in range(0, kernel_shape[3]):
x[0, kernel_idx, i, j] = gauss(i - mid, j - mid)
return x / np.sum(x)
def LCN(image_tensor, gaussian, mid):
filtered= gaussian(image_tensor)
centered_image = image_tensor - filtered[:,:,mid:-mid,mid:-mid]
sum_sqr_XX = gaussian(centered_image.pow(2))
denom = sum_sqr_XX[:,:,mid:-mid,mid:-mid].sqrt()
per_img_mean = denom.mean()
divisor = denom.clone()
divisor[per_img_mean > denom ] =per_img_mean
divisor[divisor < 1e-4 ] = 1e-4
new_image = centered_image / divisor
return new_image
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 200, kernel_size=7 ,stride=1, padding=2)
self.maxpool1 = nn.MaxPool2d(2, stride=2 , ceil_mode=True)
self.gfilter1 = torch.Tensor(gaussian_filter((1,200,9,9)) )
self.gaussian1 = nn.Conv2d(in_channels=200, out_channels=200,
kernel_size=9 , padding= 8 , bias=False)
self.gaussian1.weight.data = self.gfilter1
self.gaussian1.weight.requires_grad = False
self.conv2 = nn.Conv2d(200, 250, kernel_size=4 ,stride=1, padding=2)
self.maxpool2 = nn.MaxPool2d(2, stride=2 , ceil_mode=True)
self.gfilter2 = torch.Tensor(gaussian_filter((1,250,9,9)) )
self.gaussian2 = nn.Conv2d(in_channels=250, out_channels=250,
kernel_size=9 , padding= 8 , bias=False)
self.gaussian2.weight.data = self.gfilter2
self.gaussian2.weight.requires_grad = False
self.conv3 = nn.Conv2d(250, 350, kernel_size=4 ,stride=1, padding=2)
self.maxpool3 = nn.MaxPool2d(2, stride=2)
self.gfilter3 = torch.Tensor(gaussian_filter((1,350,9,9)) )
self.gaussian3 = nn.Conv2d(in_channels=350, out_channels=350,
kernel_size=9 , padding= 8 , bias=False)
self.gaussian3.weight.data = self.gfilter3
self.gaussian3.weight.requires_grad = False
self.FC1 = nn.Linear(12600, 400)
self.FC2 = nn.Linear(400, 43)
#Spatial Attention Model, Spatial Transformers Layers
self.st1 = nn.Sequential(
nn.MaxPool2d(2, stride=2 , ceil_mode=True),
nn.Conv2d(3, 250, kernel_size=5 ,stride=1, padding=2),
nn.ReLU(True),
nn.MaxPool2d(2, stride=2 , ceil_mode=True),
nn.Conv2d(250, 250, kernel_size=5 ,stride=1, padding=2),
nn.ReLU(True),
nn.MaxPool2d(2, stride=2 , ceil_mode=True)
)
self.FC1_ = nn.Sequential(
nn.Linear(9000, 250),
nn.ReLU(True),
nn.Linear( 250 , 6 )
)
self.st2 = nn.Sequential(
nn.MaxPool2d(2, stride=2 , ceil_mode=False),
nn.Conv2d(200, 150, kernel_size=5 ,stride=1, padding=2),
nn.ReLU(True),
nn.MaxPool2d(2, stride=2 , ceil_mode=False),
nn.Conv2d(150, 200, kernel_size=5 ,stride=1, padding=2),
nn.ReLU(True),
nn.MaxPool2d(2, stride=2 , ceil_mode=False)
)
self.FC2_ = nn.Sequential(
nn.Linear(800, 300),
nn.ReLU(True),
nn.Linear( 300 , 6 )
)
self.st3 = nn.Sequential(
nn.MaxPool2d(2, stride=2 , ceil_mode=False),
nn.Conv2d(250, 150, kernel_size=5 ,stride=1, padding=2),
nn.ReLU(True),
nn.MaxPool2d(2, stride=2 , ceil_mode=False),
nn.Conv2d(150, 200, kernel_size=5 ,stride=1, padding=2),
nn.ReLU(True),
nn.MaxPool2d(2, stride=2 , ceil_mode=False)
)
self.FC3_ = nn.Sequential(
nn.Linear(200, 300),
nn.ReLU(True),
nn.Linear( 300 , 6 )
)
self.FC1_[2].weight.data.zero_()
self.FC1_[2].bias.data.copy_(torch.tensor([1, 0, 0, 0, 1, 0], dtype=torch.float))
self.FC2_[2].weight.data.zero_()
self.FC2_[2].bias.data.copy_(torch.tensor([1, 0, 0, 0, 1, 0], dtype=torch.float))
self.FC3_[2].weight.data.zero_()
self.FC3_[2].bias.data.copy_(torch.tensor([1, 0, 0, 0, 1, 0], dtype=torch.float))
def forward(self, x):
#First Layer is the Spatial Transformer Layer
#ST-1
h1 = self.st1(x)
h1 = h1.view(-1, 9000)
h1 = self.FC1_(h1)
theta1 = h1.view(-1, 2, 3)
grid1 = F.affine_grid(theta1, x.size())
x = F.grid_sample(x, grid1)
#Convolution, Relu and Maxpool , SET #1
x = F.relu(self.conv1(x))
x = self.maxpool1(x)
#Paper Says to apply LCN here, but LCN Layer Before Convolution Worked for me better
#ST-2
h2 = self.st2(x)
h2=h2.view(-1,800)
h2 = self.FC2_(h2)
theta2 = h2.view(-1, 2, 3)
grid2 = F.affine_grid(theta2, x.size())
x = F.grid_sample(x, grid2)
#LCN Layer : Based on paper implemntation from the github and Yann Lecun Paper 2009
mid1 = int(np.floor(self.gfilter1.shape[2] / 2.))
x = LCN(x , self.gaussian1, mid1)
#Convolution, Relu and Maxpool , SET #2
x = F.relu(self.conv2(x))
x= self.maxpool2(x)
#ST-2
h3 = self.st3(x)
h3 = h3.view(-1, 200)
h3 = self.FC3_(h3)
theta3 = h3.view(-1, 2, 3)
grid3 = F.affine_grid(theta3, x.size())
x = F.grid_sample(x, grid3)
#LCN Layer : 2
mid2 = int(np.floor(self.gfilter2.shape[2] / 2.))
x = LCN(x , self.gaussian2, mid2)
#Convolution, Relu and Maxpool , SET #3
x = F.relu(self.conv3(x))
x= self.maxpool3(x)
#LCN Layer : 3
mid3 = int(np.floor(self.gfilter3.shape[2] / 2.))
x = LCN(x , self.gaussian3, mid3)
#Dimensions in accordance to paper
y = x.view(-1, 12600)
y = F.relu(self.FC1(y))
y = self.FC2(y)
return F.log_softmax(y)