-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinceptionresnetv2.py
211 lines (190 loc) · 8.44 KB
/
inceptionresnetv2.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
import torch
import torch.nn as nn
__all__ = ['inceptionresnetv2']
class Conv2d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, padding, stride=1, bias=True):
super(Conv2d, self).__init__()
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, bias=bias)
self.bn = nn.BatchNorm2d(out_channels, eps=0.001, momentum=0.1)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = self.relu(x)
return x
class Reduction_A(nn.Module):
# 35 -> 17
def __init__(self, in_channels, k, l, m, n):
super(Reduction_A, self).__init__()
self.branch_0 = Conv2d(in_channels, n, 3, stride=2, padding=0, bias=False)
self.branch_1 = nn.Sequential(
Conv2d(in_channels, k, 1, stride=1, padding=0, bias=False),
Conv2d(k, l, 3, stride=1, padding=1, bias=False),
Conv2d(l, m, 3, stride=2, padding=0, bias=False),
)
self.branch_2 = nn.MaxPool2d(3, stride=2, padding=0)
def forward(self, x):
x0 = self.branch_0(x)
x1 = self.branch_1(x)
x2 = self.branch_2(x)
return torch.cat((x0, x1, x2), dim=1) # 17 x 17 x 1024
class Stem(nn.Module):
def __init__(self, in_channels):
super(Stem, self).__init__()
self.features = nn.Sequential(
Conv2d(in_channels, 32, 3, stride=2, padding=0, bias=False), # 149 x 149 x 32
Conv2d(32, 32, 3, stride=1, padding=0, bias=False), # 147 x 147 x 32
Conv2d(32, 64, 3, stride=1, padding=1, bias=False), # 147 x 147 x 64
nn.MaxPool2d(3, stride=2, padding=0), # 73 x 73 x 64
Conv2d(64, 80, 1, stride=1, padding=0, bias=False), # 73 x 73 x 80
Conv2d(80, 192, 3, stride=1, padding=0, bias=False), # 71 x 71 x 192
nn.MaxPool2d(3, stride=2, padding=0), # 35 x 35 x 192
)
self.branch_0 = Conv2d(192, 96, 1, stride=1, padding=0, bias=False)
self.branch_1 = nn.Sequential(
Conv2d(192, 48, 1, stride=1, padding=0, bias=False),
Conv2d(48, 64, 5, stride=1, padding=2, bias=False),
)
self.branch_2 = nn.Sequential(
Conv2d(192, 64, 1, stride=1, padding=0, bias=False),
Conv2d(64, 96, 3, stride=1, padding=1, bias=False),
Conv2d(96, 96, 3, stride=1, padding=1, bias=False),
)
self.branch_3 = nn.Sequential(
nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False),
Conv2d(192, 64, 1, stride=1, padding=0, bias=False)
)
def forward(self, x):
x = self.features(x)
x0 = self.branch_0(x)
x1 = self.branch_1(x)
x2 = self.branch_2(x)
x3 = self.branch_3(x)
return torch.cat((x0, x1, x2, x3), dim=1)
class Inception_ResNet_A(nn.Module):
def __init__(self, in_channels, scale=1.0):
super(Inception_ResNet_A, self).__init__()
self.scale = scale
self.branch_0 = Conv2d(in_channels, 32, 1, stride=1, padding=0, bias=False)
self.branch_1 = nn.Sequential(
Conv2d(in_channels, 32, 1, stride=1, padding=0, bias=False),
Conv2d(32, 32, 3, stride=1, padding=1, bias=False)
)
self.branch_2 = nn.Sequential(
Conv2d(in_channels, 32, 1, stride=1, padding=0, bias=False),
Conv2d(32, 48, 3, stride=1, padding=1, bias=False),
Conv2d(48, 64, 3, stride=1, padding=1, bias=False)
)
self.conv = nn.Conv2d(128, 320, 1, stride=1, padding=0, bias=True)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x0 = self.branch_0(x)
x1 = self.branch_1(x)
x2 = self.branch_2(x)
x_res = torch.cat((x0, x1, x2), dim=1)
x_res = self.conv(x_res)
return self.relu(x + self.scale * x_res)
class Inception_ResNet_B(nn.Module):
def __init__(self, in_channels, scale=1.0):
super(Inception_ResNet_B, self).__init__()
self.scale = scale
self.branch_0 = Conv2d(in_channels, 192, 1, stride=1, padding=0, bias=False)
self.branch_1 = nn.Sequential(
Conv2d(in_channels, 128, 1, stride=1, padding=0, bias=False),
Conv2d(128, 160, (1, 7), stride=1, padding=(0, 3), bias=False),
Conv2d(160, 192, (7, 1), stride=1, padding=(3, 0), bias=False)
)
self.conv = nn.Conv2d(384, 1088, 1, stride=1, padding=0, bias=True)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x0 = self.branch_0(x)
x1 = self.branch_1(x)
x_res = torch.cat((x0, x1), dim=1)
x_res = self.conv(x_res)
return self.relu(x + self.scale * x_res)
class Reduciton_B(nn.Module):
def __init__(self, in_channels):
super(Reduciton_B, self).__init__()
self.branch_0 = nn.Sequential(
Conv2d(in_channels, 256, 1, stride=1, padding=0, bias=False),
Conv2d(256, 384, 3, stride=2, padding=0, bias=False)
)
self.branch_1 = nn.Sequential(
Conv2d(in_channels, 256, 1, stride=1, padding=0, bias=False),
Conv2d(256, 288, 3, stride=2, padding=0, bias=False),
)
self.branch_2 = nn.Sequential(
Conv2d(in_channels, 256, 1, stride=1, padding=0, bias=False),
Conv2d(256, 288, 3, stride=1, padding=1, bias=False),
Conv2d(288, 320, 3, stride=2, padding=0, bias=False)
)
self.branch_3 = nn.MaxPool2d(3, stride=2, padding=0)
def forward(self, x):
x0 = self.branch_0(x)
x1 = self.branch_1(x)
x2 = self.branch_2(x)
x3 = self.branch_3(x)
return torch.cat((x0, x1, x2, x3), dim=1)
class Inception_ResNet_C(nn.Module):
def __init__(self, in_channels, scale=1.0, activation=True):
super(Inception_ResNet_C, self).__init__()
self.scale = scale
self.activation = activation
self.branch_0 = Conv2d(in_channels, 192, 1, stride=1, padding=0, bias=False)
self.branch_1 = nn.Sequential(
Conv2d(in_channels, 192, 1, stride=1, padding=0, bias=False),
Conv2d(192, 224, (1, 3), stride=1, padding=(0, 1), bias=False),
Conv2d(224, 256, (3, 1), stride=1, padding=(1, 0), bias=False)
)
self.conv = nn.Conv2d(448, 2080, 1, stride=1, padding=0, bias=True)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
x0 = self.branch_0(x)
x1 = self.branch_1(x)
x_res = torch.cat((x0, x1), dim=1)
x_res = self.conv(x_res)
if self.activation:
return self.relu(x + self.scale * x_res)
return x + self.scale * x_res
class Inception_ResNetv2(nn.Module):
def __init__(self, in_channels=3, classes=365, k=256, l=256, m=384, n=384):
super(Inception_ResNetv2, self).__init__()
blocks = []
blocks.append(Stem(in_channels))
for i in range(10):
blocks.append(Inception_ResNet_A(320, 0.17))
blocks.append(Reduction_A(320, k, l, m, n))
for i in range(20):
blocks.append(Inception_ResNet_B(1088, 0.10))
blocks.append(Reduciton_B(1088))
for i in range(9):
blocks.append(Inception_ResNet_C(2080, 0.20))
blocks.append(Inception_ResNet_C(2080, activation=False))
self.features = nn.Sequential(*blocks)
self.conv = Conv2d(2080, 1536, 1, stride=1, padding=0, bias=False)
self.global_average_pooling = nn.AdaptiveAvgPool2d((1, 1))
self.linear = nn.Linear(1536, classes)
def forward(self, x):
x = self.features(x)
x = self.conv(x)
x = self.global_average_pooling(x)
x = x.view(x.size(0), -1)
x = self.linear(x)
return x
def inceptionresnetv2(pretrained=False):
r"""InceptionResnetV2 model architecture from the
`"InceptionV4, Inception-ResNet..." <https://arxiv.org/abs/1602.07261>`_ paper.
Args:
pretrained ('string'): If True, returns a model pre-trained on ImageNet
"""
model = Inception_ResNetv2()
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['imagenet']))
return model
def demo():
from torchstat import stat
net = inceptionresnetv2()
y = net(torch.randn(1, 3, 299, 299))
print(y.size())
stat(net, (3, 224, 224))
# demo()