Repository navigation
Expand file tree
/
Copy pathMyMobileNet_v2.py
More file actions
172 lines (147 loc) · 5.84 KB
/
Copy pathMyMobileNet_v2.py
File metadata and controls
172 lines (147 loc) · 5.84 KB
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
import torch
import torch.nn as nn
from torchinfo import summary
from collections import OrderedDict
# Depthwise Convolution Module
class DepthWise_Conv(nn.Module):
def __init__(self, in_fts, stride=(1,1)) -> None:
super().__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_fts, in_fts, kernel_size=(3,3), stride=stride, padding=(1,1), groups=in_fts, bias=False),
nn.BatchNorm2d(in_fts),
nn.ReLU6(inplace=True)
)
def forward(self, input_image):
x = self.conv(input_image)
return x
# PointWise Convolution Module
class Pointwise_Conv(nn.Module):
def __init__(self, in_fts, out_fts) -> None:
super().__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_fts, out_fts, kernel_size=(1,1), bias=False),
nn.BatchNorm2d(out_fts)
)
def forward(self, input_image):
x = self.conv(input_image)
return x
# Bottleneck Layer when Stride is 1
class NetForStrideOne(nn.Module):
def __init__(self, in_fts, out_fts, expansion) -> None:
super().__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(in_fts, expansion*in_fts, kernel_size=(1,1), bias=False),
nn.BatchNorm2d(expansion*in_fts),
nn.ReLU6(inplace=True)
)
self.dw = DepthWise_Conv(expansion*in_fts)
self.pw = Pointwise_Conv(expansion*in_fts, out_fts)
self.in_fts = in_fts
self.out_fts = out_fts
self.expansion = expansion
def forward(self, input_image):
if self.expansion == 1:
x = self.dw(input_image)
x = self.pw(x)
else:
x = self.conv1(input_image)
x = self.dw(x)
x = self.pw(x)
# If input channel and output channel are same, then perform add
# residual part
if self.in_fts == self.out_fts:
x = input_image + x
return x
# Bottleneck layer when Stride is 2
class NetForStrideTwo(nn.Module):
def __init__(self, in_fts, out_fts, expansion) -> None:
super().__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(in_fts, expansion*in_fts, kernel_size=(1,1), bias=False),
nn.BatchNorm2d(expansion*in_fts),
nn.ReLU6(inplace=True)
)
self.dw = DepthWise_Conv(expansion*in_fts, stride=(2,2))
self.pw = Pointwise_Conv(expansion*in_fts, out_fts)
self.expansion = expansion
def forward(self, input_image):
if self.expansion == 1:
x = self.dw(input_image)
x = self.pw(x)
else:
x = self.conv1(input_image)
x = self.dw(x)
x = self.pw(x)
return x
# MobileNetV2 architecture
class MyMobileNet_v2(nn.Module):
def __init__(self, bottleneckLayerDetails, in_fts=3, numClasses=1000, width_multiplier=1) -> None:
super().__init__()
self.bottleneckLayerDetails = bottleneckLayerDetails
self.width_multiplier = width_multiplier
self.conv1 = nn.Sequential(
nn.Conv2d(in_fts, round(width_multiplier*32), kernel_size=(3,3), stride=(2,2), padding=(1,1), bias=False),
nn.BatchNorm2d(round(width_multiplier*32)),
nn.ReLU6(inplace=True)
)
self.in_fts = round(width_multiplier*32)
# Defined bottleneck layer as per Table 2
self.layerConstructed = self.constructLayer()
# Top layers after bottleneck
self.feature = nn.Sequential(
nn.Conv2d(self.in_fts, round(width_multiplier*1280), kernel_size=(1,1), bias=False),
nn.BatchNorm2d(round(width_multiplier*1280)),
nn.ReLU6(inplace=True)
)
self.avgpool = nn.AdaptiveAvgPool2d(output_size=(1,1))
self.outputLayer = nn.Sequential(
nn.Dropout2d(),
nn.Conv2d(round(width_multiplier*1280), numClasses, kernel_size=(1,1)),
)
def forward(self, input_image):
x = self.conv1(input_image)
x = self.layerConstructed(x)
x = self.feature(x)
x = self.avgpool(x)
x = self.outputLayer(x)
return x
# Defined function to construct the layer based on bottleneck layer defined in Table 2
def constructLayer(self):
itemIndex = 0
block = OrderedDict()
# iterating the defined layer details
for lItem in self.bottleneckLayerDetails:
# each items assigned corresponding values
t, out_fts, n, stride = lItem
# If width multipler is mentioned then perform this line
out_fts = round(self.width_multiplier*out_fts)
# for stride value 1
if stride == 1:
# constructedd the NetForStrideOne module by n times
for nItem in range(n):
block[str(itemIndex)+"_"+str(nItem)] = NetForStrideOne(self.in_fts, out_fts, t)
self.in_fts = out_fts
# for stride value 2
elif stride == 2:
# First layer constructed for NetForStrideTwo module once only
block[str(itemIndex)+"_"+str(0)] = NetForStrideTwo(self.in_fts, out_fts, t)
self.in_fts = out_fts
# Remaining will be NetForStrideOne module (n-1) times
for nItem in range(1,n):
block[str(itemIndex)+"_"+str(nItem)] = NetForStrideOne(self.in_fts, out_fts, t)
itemIndex += 1
return nn.Sequential(block)
if __name__ == "__main__":
# as per research paper
bottleneckLayerDetails = [
# (expansion, out_dimension, number_of_times, stride)
(1,16,1,1),
(6,24,2,2),
(6,32,3,2),
(6,64,4,2),
(6,96,3,1),
(6,160,3,2),
(6,320,1,1)
]
model = MyMobileNet_v2(bottleneckLayerDetails, width_multiplier=1)
summary(model, (1,3,224,224))