-
Notifications
You must be signed in to change notification settings - Fork 5
/
pt_models.py
207 lines (173 loc) · 8.73 KB
/
pt_models.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
import torch
import torch.utils.model_zoo as model_zoo
from torch import nn as nn
import torch.nn.init as init
from sa_energy_model import FixHWConv2d, conv2d_out_dim
model_urls = {
'alexnet': 'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth',
'squeezenet1_0': 'https://download.pytorch.org/models/squeezenet1_0-a815701f.pth',
}
################################################################
######################### Alex NET ##########################
################################################################
class MyAlexNet(nn.Module):
def __init__(self, h=224, w=224, conv_class=FixHWConv2d, num_classes=1000, dropout=True):
super(MyAlexNet, self).__init__()
feature_layers = []
# conv
feature_layers.append(conv_class(h, w, 3, 64, kernel_size=11, stride=4, padding=2))
h = conv2d_out_dim(h, kernel_size=11, stride=4, padding=2)
w = conv2d_out_dim(w, kernel_size=11, stride=4, padding=2)
feature_layers.append(nn.ReLU(inplace=True))
# pooling
feature_layers.append(nn.MaxPool2d(kernel_size=3, stride=2))
h = conv2d_out_dim(h, kernel_size=3, stride=2)
w = conv2d_out_dim(w, kernel_size=3, stride=2)
# conv
feature_layers.append(conv_class(h, w, 64, 192, kernel_size=5, padding=2))
h = conv2d_out_dim(h, kernel_size=5, padding=2)
w = conv2d_out_dim(w, kernel_size=5, padding=2)
feature_layers.append(nn.ReLU(inplace=True))
# pooling
feature_layers.append(nn.MaxPool2d(kernel_size=3, stride=2))
h = conv2d_out_dim(h, kernel_size=3, stride=2)
w = conv2d_out_dim(w, kernel_size=3, stride=2)
# conv
feature_layers.append(conv_class(h, w, 192, 384, kernel_size=3, padding=1))
h = conv2d_out_dim(h, kernel_size=3, padding=1)
w = conv2d_out_dim(w, kernel_size=3, padding=1)
feature_layers.append(nn.ReLU(inplace=True))
# conv
feature_layers.append(conv_class(h, w, 384, 256, kernel_size=3, padding=1))
h = conv2d_out_dim(h, kernel_size=3, padding=1)
w = conv2d_out_dim(w, kernel_size=3, padding=1)
feature_layers.append(nn.ReLU(inplace=True))
# conv
feature_layers.append(conv_class(h, w, 256, 256, kernel_size=3, padding=1))
h = conv2d_out_dim(h, kernel_size=3, padding=1)
w = conv2d_out_dim(w, kernel_size=3, padding=1)
feature_layers.append(nn.ReLU(inplace=True))
# pooling
feature_layers.append(nn.MaxPool2d(kernel_size=3, stride=2))
h = conv2d_out_dim(h, kernel_size=3, stride=2)
w = conv2d_out_dim(w, kernel_size=3, stride=2)
self.features = nn.Sequential(*feature_layers)
fc_layers = [nn.Dropout(p=0.5 if dropout else 0.0),
nn.Linear(256 * 6 * 6, 4096),
nn.ReLU(inplace=True),
nn.Dropout(p=0.5 if dropout else 0.0),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Linear(4096, num_classes)]
self.classifier = nn.Sequential(*fc_layers)
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), 256 * 6 * 6)
x = self.classifier(x)
return x
def get_inhw(self, x):
res = []
for module in self.features._modules.values():
if isinstance(module, nn.Conv2d):
res.append((x.size(2), x.size(3)))
x = module(x)
for module in self.classifier._modules.values():
if isinstance(module, nn.Linear):
res.append((1, 1))
return res
def myalexnet(pretrained=False, model_root=None, **kwargs):
model = MyAlexNet(**kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['alexnet'], model_root), strict=False)
return model
################################################################
######################## Squeeze NET ########################
################################################################
class MySqueezeNet(nn.Module):
class MyFire(nn.Module):
def __init__(self, inplanes, squeeze_planes,
expand1x1_planes, expand3x3_planes, h_in, w_in, conv_class=FixHWConv2d):
super(MySqueezeNet.MyFire, self).__init__()
h = h_in
w = w_in
self.inplanes = inplanes
self.squeeze = conv_class(h, w, inplanes, squeeze_planes, kernel_size=1)
self.squeeze_activation = nn.ReLU(inplace=True)
h = conv2d_out_dim(h, kernel_size=1)
w = conv2d_out_dim(w, kernel_size=1)
self.expand1x1 = conv_class(h, w, squeeze_planes, expand1x1_planes, kernel_size=1)
self.expand1x1_activation = nn.ReLU(inplace=True)
self.expand3x3 = conv_class(h, w, squeeze_planes, expand3x3_planes, kernel_size=3, padding=1)
self.expand3x3_activation = nn.ReLU(inplace=True)
h = conv2d_out_dim(h, kernel_size=3, padding=1)
w = conv2d_out_dim(w, kernel_size=3, padding=1)
def forward(self, x):
x = self.squeeze_activation(self.squeeze(x))
return torch.cat([
self.expand1x1_activation(self.expand1x1(x)),
self.expand3x3_activation(self.expand3x3(x))
], 1)
def __init__(self, version=1.0, h=224, w=224, conv_class=FixHWConv2d, num_classes=1000, dropout=True):
MyFire = self.MyFire
super(MySqueezeNet, self).__init__()
if version not in [1.0]:
raise ValueError("Unsupported SqueezeNet version {version}:"
"1.0".format(version=version))
self.num_classes = num_classes
feature_layers = []
# conv
feature_layers.append(conv_class(h, w, 3, 96, kernel_size=7, stride=2))
h = conv2d_out_dim(h, kernel_size=7, stride=2)
w = conv2d_out_dim(w, kernel_size=7, stride=2)
feature_layers.append(nn.ReLU(inplace=True))
# pooling
feature_layers.append(nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True))
h = conv2d_out_dim(h, kernel_size=3, stride=2, ceil_mode=True)
w = conv2d_out_dim(w, kernel_size=3, stride=2, ceil_mode=True)
# fire block
feature_layers.append(MyFire(96, 16, 64, 64, h_in=h, w_in=w, conv_class=conv_class))
feature_layers.append(MyFire(128, 16, 64, 64, h_in=h, w_in=w, conv_class=conv_class))
feature_layers.append(MyFire(128, 32, 128, 128, h_in=h, w_in=w, conv_class=conv_class))
feature_layers.append(nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True))
h = conv2d_out_dim(h, kernel_size=3, stride=2, ceil_mode=True)
w = conv2d_out_dim(w, kernel_size=3, stride=2, ceil_mode=True)
feature_layers.append(MyFire(256, 32, 128, 128, h_in=h, w_in=w, conv_class=conv_class))
feature_layers.append(MyFire(256, 48, 192, 192, h_in=h, w_in=w, conv_class=conv_class))
feature_layers.append(MyFire(384, 48, 192, 192, h_in=h, w_in=w, conv_class=conv_class))
feature_layers.append(MyFire(384, 64, 256, 256, h_in=h, w_in=w, conv_class=conv_class))
feature_layers.append(nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True))
h = conv2d_out_dim(h, kernel_size=3, stride=2, ceil_mode=True)
w = conv2d_out_dim(w, kernel_size=3, stride=2, ceil_mode=True)
feature_layers.append(MyFire(512, 64, 256, 256, h_in=h, w_in=w, conv_class=conv_class))
self.features = nn.Sequential(*feature_layers)
# Final convolution is initialized differently form the rest
final_conv = conv_class(h, w, 512, self.num_classes, kernel_size=1)
self.classifier = nn.Sequential(
nn.Dropout(p=0.5 if dropout else 0.0),
final_conv,
nn.ReLU(inplace=True),
nn.AvgPool2d(13, stride=1)
)
for m in self.modules():
if isinstance(m, nn.Conv2d):
if m is final_conv:
init.normal(m.weight.data, mean=0.0, std=0.01)
else:
init.kaiming_uniform(m.weight.data)
if m.bias is not None:
m.bias.data.zero_()
def forward(self, x):
x = self.features(x)
x = self.classifier(x)
return x.view(x.size(0), self.num_classes)
def mysqueezenet1_0(pretrained=False, **kwargs):
r"""SqueezeNet model architecture from the `"SqueezeNet: AlexNet-level
accuracy with 50x fewer parameters and <0.5MB model size"
<https://arxiv.org/abs/1602.07360>`_ paper.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = MySqueezeNet(version=1.0, **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['squeezenet1_0']), strict=False)
return model