forked from kuangliu/pytorch-cifar
-
Notifications
You must be signed in to change notification settings - Fork 2
/
project1_model.py
210 lines (185 loc) · 7.68 KB
/
project1_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
'''ResNet in PyTorch.
For Pre-activation ResNet, see 'preact_resnet.py'.
Reference:
[1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
Deep Residual Learning for Image Recognition. arXiv:1512.03385
'''
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_planes, planes, stride=1, conv_kernel_size=3, shortcut_kernel_size=1, drop=0.4):
"""
Convolutional Layer kernel size Fi
Skip connection (shortcut) kernel size Ki
"""
super(BasicBlock, self).__init__()
self.drop = drop
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=conv_kernel_size, stride=stride, padding=int(conv_kernel_size/2), bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=conv_kernel_size,stride=1, padding=int(conv_kernel_size/2), bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != self.expansion*planes:
self.shortcut = nn.Sequential(
nn.Conv2d(in_planes, self.expansion*planes,kernel_size=shortcut_kernel_size, stride=stride, padding=int(shortcut_kernel_size/2), bias=False),
nn.BatchNorm2d(self.expansion*planes)
)
if self.drop: self.dropout = nn.Dropout(self.drop)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out += self.shortcut(x)
out = F.relu(out)
if self.drop: out = self.dropout(out)
return out
def conv1x1(in_channels,
out_channels,
stride=1,
groups=1,
bias=False):
"""
Convolution 1x1 layer.
Parameters:
----------
in_channels : int
Number of input channels.
out_channels : int
Number of output channels.
stride : int or tuple/list of 2 int, default 1
Strides of the convolution.
groups : int, default 1
Number of groups.
bias : bool, default False
Whether the layer uses a bias vector.
"""
return nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=1,
stride=stride,
groups=groups,
bias=bias)
class SEBlock(nn.Module):
"""
Squeeze-and-Excitation block from 'Squeeze-and-Excitation Networks,' https://arxiv.org/abs/1709.01507.
Parameters:
----------
channels : int
Number of channels.
reduction : int, default 16
Squeeze reduction value.
"""
def __init__(self,
channels,
reduction=16):
super(SEBlock, self).__init__()
mid_cannels = channels // reduction
self.pool = nn.AdaptiveAvgPool2d(output_size=1)
self.conv1 = conv1x1(
in_channels=channels,
out_channels=mid_cannels,
bias=True)
self.activ = nn.ReLU(inplace=True)
self.conv2 = conv1x1(
in_channels=mid_cannels,
out_channels=channels,
bias=True)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
w = self.pool(x)
w = self.conv1(w)
w = self.activ(w)
w = self.conv2(w)
w = self.sigmoid(w)
x = x * w
return x
class ResNet(nn.Module):
def __init__(
self,
block,
num_blocks,
conv_kernel_sizes=None,
shortcut_kernel_sizes=None,
num_classes=10,
num_channels=32,
avg_pool_kernel_size=4,
drop=None,
squeeze_and_excitation=None):
super(ResNet, self).__init__()
self.in_planes = num_channels
# self.avg_pool_kernel_size = avg_pool_kernel_size
self.avg_pool_kernel_size = int(32 / (2**(len(num_blocks)-1)))
"""
# of channels Ci
"""
self.num_channels = num_channels
self.conv1 = nn.Conv2d(3, self.num_channels, kernel_size=3,stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(self.num_channels)
self.drop = drop
self.squeeze_and_excitation = squeeze_and_excitation
if self.squeeze_and_excitation:
self.seblock = SEBlock(channels=self.num_channels)
"""
# of Residual Layers N
# of Residual Blocks Bi
"""
self.residual_layers = []
for n in range(len(num_blocks)):
stride = 1 if n==0 else 2 # stride=1 for first residual layer, and stride=2 for the remaining layers
conv_kernel_size = conv_kernel_sizes[n] if conv_kernel_sizes else 3 # setting default kernel size of block's convolutional layers
shortcut_kernel_size = shortcut_kernel_sizes[n] if shortcut_kernel_sizes else 1 # setting default kernel size of block's skip connection (shortcut) layers
self.residual_layers.append(self._make_layer(
block,
self.num_channels*(2**n),
num_blocks[n],
stride=stride,
conv_kernel_size=conv_kernel_size,
shortcut_kernel_size=shortcut_kernel_size))
self.residual_layers = nn.ModuleList(self.residual_layers)
self.linear = nn.Linear(self.num_channels*(2**n)*block.expansion, num_classes)
"""
Dropout layer
"""
if self.drop:
self.dropout = nn.Dropout(self.drop) # Define proportion or neurons to dropout
def _make_layer(self, block, planes, num_blocks, stride, conv_kernel_size, shortcut_kernel_size):
strides = [stride] + [1]*(num_blocks-1)
layers = []
for stride in strides:
layers.append(block(self.in_planes, planes, stride, conv_kernel_size, shortcut_kernel_size, drop=self.drop))
self.in_planes = planes * block.expansion
return nn.Sequential(*layers)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
if self.squeeze_and_excitation: out = self.seblock(out)
for layer in self.residual_layers:
out = layer(out)
"""
Average pool kernel size
"""
out = F.avg_pool2d(out, self.avg_pool_kernel_size)
out = out.view(out.size(0), -1)
if self.drop: out = self.dropout(out)
out = self.linear(out)
return out
def project1_model(config=None):
# Best Model
net = ResNet(
block=BasicBlock,
num_blocks=[4, 4, 3], # N: number of Residual Layers | Bi:Residual blocks in Residual Layer i
conv_kernel_sizes=[3, 3, 3], # Fi: Conv. kernel size in Residual Layer i
shortcut_kernel_sizes=[1, 1, 1] , # Ki: Skip connection kernel size in Residual Layer i
num_channels=64, # Ci: # channels in Residual Layer i
avg_pool_kernel_size=8, # P: Average pool kernel size
drop=0, # use dropout with drop proportion
squeeze_and_excitation=1 # Enable/disable Squeeze-and-Excitation Block
)
total_params = 0
for x in filter(lambda p: p.requires_grad, net.parameters()):
total_params += np.prod(x.data.numpy().shape)
# print("Total number of params", total_params)
# print("Total layers", len(list(filter(lambda p: p.requires_grad and len(p.data.size())>1, net.parameters()))))
return net, total_params