-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
132 lines (100 loc) · 4.3 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
import torch
import torch.nn as nn
"""
Inverted Residual Block -
"""
class Inverted_Residual_Block(nn.Module):
def __init__(self, in_c, out_c, k=3, s=1, t=6, alpha=1.0):
super(Inverted_Residual_Block, self).__init__()
"""
At groups=1
- all inputs are convolved to all outputs.
At groups=2
- the operation becomes equivalent to having two conv layers side by side, each seeing half the input channels,
and producing half the output channels, and both subsequently concatenated.
At groups= in_channels
- each input channel is convolved with its own set of filters, of size
"""
in_C = int(in_c * alpha)
middle_C = int(in_c * alpha * t)
out_C = int(out_c * alpha)
self.shortcut = True if s == 1 and in_C == out_C else False
layers = []
if t != 1:
layers.extend([
nn.Conv2d(in_C, middle_C, kernel_size=1, stride=1, groups=1, bias=False),
nn.BatchNorm2d(middle_C),
nn.ReLU6(True)
])
layers.extend([
nn.Conv2d(middle_C, middle_C, kernel_size=k, stride=s, padding=(k-1)//2, groups=middle_C, bias=False),
nn.ReLU6(True),
nn.Conv2d(middle_C, out_C, kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(out_C),
])
self.conv = nn.Sequential(*layers)
def forward(self, x):
out = x
out = self.conv(out)
if self.shortcut:
out = out + x
return out
"""
MobileNet_v2
"""
class MobileNet_v2(nn.Module):
def __init__(self, alpha=1.0):
super(MobileNet_v2, self).__init__()
self.feature = self._build_features(alpha)
self.classifier = self._build_classifier(alpha)
def forward(self, x):
out = self.feature(x)
out = self.classifier(out)
return out
def _build_features(self, alpha):
layers = [
nn.Conv2d(in_channels=3, out_channels=int(32*alpha), kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(int(32*alpha)),
nn.ReLU6(True),
Inverted_Residual_Block(32, 16, s=1, t=1, alpha=alpha),
Inverted_Residual_Block(16, 24, s=2, t=6, alpha=alpha),
Inverted_Residual_Block(24, 24, s=1, t=6, alpha=alpha),
Inverted_Residual_Block(24, 32, s=2, t=6, alpha=alpha),
Inverted_Residual_Block(32, 32, s=1, t=6, alpha=alpha),
Inverted_Residual_Block(32, 32, s=1, t=6, alpha=alpha),
Inverted_Residual_Block(32, 64, s=2, t=6, alpha=alpha),
Inverted_Residual_Block(64, 64, s=1, t=6, alpha=alpha),
Inverted_Residual_Block(64, 64, s=1, t=6, alpha=alpha),
Inverted_Residual_Block(64, 64, s=1, t=6, alpha=alpha),
Inverted_Residual_Block(64, 96, s=1, t=6, alpha=alpha),
Inverted_Residual_Block(96, 96, s=1, t=6, alpha=alpha),
Inverted_Residual_Block(96, 96, s=1, t=6, alpha=alpha),
Inverted_Residual_Block(96, 160, s=2, t=6, alpha=alpha),
Inverted_Residual_Block(160, 160, s=1, t=6, alpha=alpha),
Inverted_Residual_Block(160, 160, s=1, t=6, alpha=alpha),
Inverted_Residual_Block(160, 320, s=1, t=6, alpha=alpha),
nn.Conv2d(int(320*alpha), int(alpha*1280), 1, 1, 0, bias=False),
nn.BatchNorm2d(int(alpha*1280)),
nn.ReLU6(True)
]
feature = nn.Sequential(
*layers
)
return feature
def _build_classifier(self, alpha):
layers = [
nn.AdaptiveAvgPool2d(output_size=(1, 1)),
nn.Flatten(start_dim=1),
nn.Linear(in_features=int(alpha*1280), out_features=1000),
]
classifier = nn.Sequential(
*layers
)
return classifier
if __name__ == "__main__":
from torchsummaryM import summary
from torchvision.models.mobilenet import MobileNetV2
model = MobileNetV2()
summary(model, torch.zeros(1, 3, 224, 224))
model = MobileNet_v2(alpha=1.4)
summary(model, torch.zeros(1, 3, 224, 224))