-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
322 lines (266 loc) · 11.8 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
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
#!/usr/bin/env python
# coding: utf-8
# # Python program to create a CNN model with Normalization to detect MNIST digits
# ### Import the required libraries
# In[1]:
from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
import os
# # Model Architecture
# ## Class for Model with Batch Normalization
# In[2]:
# Set the dropout probability to be used in the network
DROPOUT_VALUE = 0.04
# In[3]:
# In Batch Normalization, each channel in a layer is picked up across the batch on images and normalized.
# Here, we use nn.BatchNorm2d(num_channels) after each Conv2d layer to implement Batch Normalization.
class BatchNormalization(nn.Module):
def __init__(self):
super(BatchNormalization, self).__init__()
# Input Block
self.conv1 = nn.Sequential(
#input - RF:1x1, Channel Size: 28x28; Output RF: 3x3, Channel Size: 26x26
nn.Conv2d(in_channels=1, out_channels=8, kernel_size=(3, 3), padding=0, bias=False),
nn.ReLU(),
nn.BatchNorm2d(8)
)
# CONVOLUTION BLOCK 1
self.conv2 = nn.Sequential(
#input - RF:3x3, Channel Size: 26x26; Output RF: 5x5, Channel Size: 24x24
nn.Conv2d(in_channels=8, out_channels=16, kernel_size=(3, 3), padding=0, bias=False),
nn.ReLU(),
nn.BatchNorm2d(16)
)
# TRANSITION BLOCK 1
self.trans = nn.Sequential(
#input - RF:5x5, Channel Size: 24x24; Output RF: 5x5, Channel Size: 24x24
nn.Conv2d(in_channels=16, out_channels=8, kernel_size=(1, 1), padding=0, bias=False),
#input - RF:5x5, Channel Size: 24x24; Output RF: 6x6, Channel Size: 12x12
nn.MaxPool2d(2, 2)
)
# CONVOLUTION BLOCK 2
self.conv3 = nn.Sequential(
#input - RF:6x6, Channel Size: 12x12; Output RF: 10x10, Channel Size: 10x10
nn.Conv2d(in_channels=8, out_channels=12, kernel_size=(3, 3), padding=0, bias=False),
nn.ReLU(),
nn.BatchNorm2d(12),
#input - RF:10x10, Channel Size: 10x10; Output RF: 14x14, Channel Size: 8x8
nn.Conv2d(in_channels=12, out_channels=16, kernel_size=(3, 3), padding=0, bias=False),
nn.ReLU(),
nn.BatchNorm2d(16),
nn.Dropout(DROPOUT_VALUE)
)
# CONVOLUTION BLOCK 3
self.conv4 = nn.Sequential(
#input - RF:14x14, Channel Size: 8x8; Output RF: 18x18, Channel Size: 6x6
nn.Conv2d(in_channels=16, out_channels=16, kernel_size=(3, 3), padding=0, bias=False),
nn.ReLU(),
nn.BatchNorm2d(16),
nn.Dropout(DROPOUT_VALUE)
)
# CONVOLUTION BLOCK 4
self.conv5 = nn.Sequential(
#input - RF:18x18, Channel Size: 6x6; Output RF: 22x22, Channel Size: 6x6
nn.Conv2d(in_channels=16, out_channels=16, kernel_size=(3, 3), padding=1, bias=False),
nn.ReLU(),
nn.BatchNorm2d(16),
)
# OUTPUT BLOCK
self.gap = nn.Sequential(
#input - RF:22x22, Channel Size: 6x6; Output RF: 32x32, Channel Size: 1x1
nn.AvgPool2d(kernel_size=6)
)
self.conv6 = nn.Sequential(
#input - RF:32x32, Channel Size: 1x1; Output RF: 32x32, Channel Size: 1x1
nn.Conv2d(in_channels=16, out_channels=10, kernel_size=(1, 1), padding=0, bias=False),
)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.trans(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
x = self.gap(x)
x = self.conv6(x)
x = x.view(-1, 10)
return F.log_softmax(x, dim=-1)
# ## Class for Model with Group Normalization
# In[4]:
# In Group Normalization, we forms groups of channels within each layer and normalize the element values in these channel groups.
# Here, we use nn.GroupNorm(num_groups,num_channels) after each Conv2d layer to implement Group Normalization.
class GroupNormalization(nn.Module):
def __init__(self):
super(GroupNormalization, self).__init__()
# Input Block
self.conv1 = nn.Sequential(
#input - RF:1x1, Channel Size: 28x28; Output RF: 3x3, Channel Size: 26x26
nn.Conv2d(in_channels=1, out_channels=8, kernel_size=(3, 3), padding=0, bias=False),
nn.ReLU(),
nn.GroupNorm(num_groups=2, num_channels=8)
)
# CONVOLUTION BLOCK 1
self.conv2 = nn.Sequential(
#input - RF:3x3, Channel Size: 26x26; Output RF: 5x5, Channel Size: 24x24
nn.Conv2d(in_channels=8, out_channels=16, kernel_size=(3, 3), padding=0, bias=False),
nn.ReLU(),
nn.GroupNorm(num_groups=4, num_channels=16)
)
# TRANSITION BLOCK 1
self.trans = nn.Sequential(
#input - RF:5x5, Channel Size: 24x24; Output RF: 5x5, Channel Size: 24x24
nn.Conv2d(in_channels=16, out_channels=8, kernel_size=(1, 1), padding=0, bias=False),
#input - RF:5x5, Channel Size: 24x24; Output RF: 6x6, Channel Size: 12x12
nn.MaxPool2d(2, 2)
)
# CONVOLUTION BLOCK 2
self.conv3 = nn.Sequential(
#input - RF:6x6, Channel Size: 12x12; Output RF: 10x10, Channel Size: 10x10
nn.Conv2d(in_channels=8, out_channels=12, kernel_size=(3, 3), padding=0, bias=False),
nn.ReLU(),
nn.GroupNorm(num_groups=3, num_channels=12),
#input - RF:10x10, Channel Size: 10x10; Output RF: 14x14, Channel Size: 8x8
nn.Conv2d(in_channels=12, out_channels=16, kernel_size=(3, 3), padding=0, bias=False),
nn.ReLU(),
nn.GroupNorm(num_groups=4, num_channels=16),
nn.Dropout(DROPOUT_VALUE)
)
# CONVOLUTION BLOCK 3
self.conv4 = nn.Sequential(
#input - RF:14x14, Channel Size: 8x8; Output RF: 18x18, Channel Size: 6x6
nn.Conv2d(in_channels=16, out_channels=16, kernel_size=(3, 3), padding=0, bias=False),
nn.ReLU(),
nn.GroupNorm(num_groups=4, num_channels=16),
nn.Dropout(DROPOUT_VALUE)
)
# CONVOLUTION BLOCK 4
self.conv5 = nn.Sequential(
#input - RF:18x18, Channel Size: 6x6; Output RF: 22x22, Channel Size: 6x6
nn.Conv2d(in_channels=16, out_channels=16, kernel_size=(3, 3), padding=1, bias=False),
nn.ReLU(),
nn.GroupNorm(num_groups=4, num_channels=16),
)
# OUTPUT BLOCK
self.gap = nn.Sequential(
#input - RF:22x22, Channel Size: 6x6; Output RF: 32x32, Channel Size: 1x1
nn.AvgPool2d(kernel_size=6)
)
self.conv6 = nn.Sequential(
#input - RF:32x32, Channel Size: 1x1; Output RF: 32x32, Channel Size: 1x1
nn.Conv2d(in_channels=16, out_channels=10, kernel_size=(1, 1), padding=0, bias=False),
)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.trans(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
x = self.gap(x)
x = self.conv6(x)
x = x.view(-1, 10)
return F.log_softmax(x, dim=-1)
# ## Class for Model with Layer Normalization
# In[5]:
# Layer Normalization is a special case of Group Normalization wherein we selct the group count as 1.
# This will result in the entire channels in the layer to be normalized at once.
# Here, we use nn.GroupNorm(1,num_channels) after each Conv2d layer to implement Layer Normalization.
class LayerNormalization(nn.Module):
def __init__(self):
super(LayerNormalization, self).__init__()
# Input Block
self.conv1 = nn.Sequential(
#input - RF:1x1, Channel Size: 28x28; Output RF: 3x3, Channel Size: 26x26
nn.Conv2d(in_channels=1, out_channels=8, kernel_size=(3, 3), padding=0, bias=False),
nn.ReLU(),
# Layer Normalization
nn.GroupNorm(1,8)
)
# CONVOLUTION BLOCK 1
self.conv2 = nn.Sequential(
#input - RF:3x3, Channel Size: 26x26; Output RF: 5x5, Channel Size: 24x24
nn.Conv2d(in_channels=8, out_channels=16, kernel_size=(3, 3), padding=0, bias=False),
nn.ReLU(),
# Layer Normalization
nn.GroupNorm(1,16)
)
# TRANSITION BLOCK 1
self.trans = nn.Sequential(
#input - RF:5x5, Channel Size: 24x24; Output RF: 5x5, Channel Size: 24x24
nn.Conv2d(in_channels=16, out_channels=8, kernel_size=(1, 1), padding=0, bias=False),
#input - RF:5x5, Channel Size: 24x24; Output RF: 6x6, Channel Size: 12x12
nn.MaxPool2d(2, 2)
)
# CONVOLUTION BLOCK 2
self.conv3 = nn.Sequential(
#input - RF:6x6, Channel Size: 12x12; Output RF: 10x10, Channel Size: 10x10
nn.Conv2d(in_channels=8, out_channels=12, kernel_size=(3, 3), padding=0, bias=False),
nn.ReLU(),
# Layer Normalization
nn.GroupNorm(1,12),
#input - RF:10x10, Channel Size: 10x10; Output RF: 14x14, Channel Size: 8x8
nn.Conv2d(in_channels=12, out_channels=16, kernel_size=(3, 3), padding=0, bias=False),
nn.ReLU(),
# Layer Normalization
nn.GroupNorm(1,16),
nn.Dropout(DROPOUT_VALUE)
)
# CONVOLUTION BLOCK 3
self.conv4 = nn.Sequential(
#input - RF:14x14, Channel Size: 8x8; Output RF: 18x18, Channel Size: 6x6
nn.Conv2d(in_channels=16, out_channels=16, kernel_size=(3, 3), padding=0, bias=False),
nn.ReLU(),
# Layer Normalization
nn.GroupNorm(1,16),
nn.Dropout(DROPOUT_VALUE)
)
# CONVOLUTION BLOCK 4
self.conv5 = nn.Sequential(
#input - RF:18x18, Channel Size: 6x6; Output RF: 22x22, Channel Size: 6x6
nn.Conv2d(in_channels=16, out_channels=16, kernel_size=(3, 3), padding=1, bias=False),
nn.ReLU(),
# Layer Normalization
nn.GroupNorm(1,16),
)
# OUTPUT BLOCK
self.gap = nn.Sequential(
#input - RF:22x22, Channel Size: 6x6; Output RF: 32x32, Channel Size: 1x1
nn.AvgPool2d(kernel_size=6)
)
self.conv6 = nn.Sequential(
#input - RF:32x32, Channel Size: 1x1; Output RF: 32x32, Channel Size: 1x1
nn.Conv2d(in_channels=16, out_channels=10, kernel_size=(1, 1), padding=0, bias=False),
)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.trans(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
x = self.gap(x)
x = self.conv6(x)
x = x.view(-1, 10)
return F.log_softmax(x, dim=-1)
# ## build_model: This function accepts type of Normalization as input and returns the model object with corresponding normalization applied
# ## Raises an exception in case of an incorrect Normalization type is given in input
# In[18]:
def build_model(model_type):
# Check if the input is Batch Normalization
if model_type == 'BN':
model = BatchNormalization()
# Check if the input is Group Normalization
elif model_type == 'GN':
model = GroupNormalization()
# Check if the input is Layer Normalization
elif model_type == 'LN':
model = LayerNormalization()
# Raise an exception in case the input is invalid
else:
raise Exception("Invalid Normalization: model_type should be 'BN' or 'GN' or 'LN'")
# Return the model object to the calling program
return model