-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
206 lines (158 loc) · 6.64 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
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
import torch
import torch.nn as nn
import torch.nn.init as init
class ZipLayer(nn.Module):
"""Zip Module"""
def __init__(self,
in_channels,
squeeze_channels,
e1x1_channels,
e3x3_channels):
super(ZipLayer, self).__init__()
self.in_channels = in_channels
self.squeeze_channels = squeeze_channels
self.e1x1_channels = e1x1_channels
self.e3x3_channels = e3x3_channels
self.squeeze_layer = self.get_squeeze_layer()
self.expand_1x1_layer = self.get_expand_1x1_layer()
self.expand_3x3_layer = self.get_expand_3x3_layer()
def get_squeeze_layer(self):
layers = []
layers.append(nn.Conv2d(self.in_channels,
self.squeeze_channels,
kernel_size=1))
layers.append(nn.BatchNorm2d(num_features=self.squeeze_channels))
layers.append(nn.ReLU(inplace=True))
return nn.Sequential(*layers)
def get_expand_1x1_layer(self):
layers = []
layers.append(nn.Conv2d(self.squeeze_channels,
self.e1x1_channels,
kernel_size=1))
layers.append(nn.BatchNorm2d(num_features=self.e1x1_channels))
layers.append(nn.ReLU(inplace=True))
return nn.Sequential(*layers)
def get_expand_3x3_layer(self):
layers = []
layers.append(nn.Conv2d(self.squeeze_channels,
self.e3x3_channels,
kernel_size=3,
padding=1))
layers.append(nn.BatchNorm2d(num_features=self.e3x3_channels))
layers.append(nn.ReLU(inplace=True))
return nn.Sequential(*layers)
def forward(self, x):
y = self.squeeze_layer(x)
return torch.cat([
self.expand_1x1_layer(y),
self.expand_3x3_layer(y)
], 1)
class ZipBlock(nn.Module):
def __init__(self,
in_channels,
num_layers,
compress_factor,
expand_factor,
expand_interval):
super(ZipBlock, self).__init__()
self.in_channels = in_channels
self.num_layers = num_layers
self.compress_factor = compress_factor
self.expand_factor = expand_factor
self.expand_interval = expand_interval
self.net = self.get_network()
def get_network(self):
layers = []
in_channels = self.in_channels
for i in range(self.num_layers):
squeeze_channels = in_channels // self.compress_factor
out_channels = in_channels
if (i + 1) % self.expand_interval == 0:
out_channels *= self.expand_factor
layers.append(ZipLayer(in_channels=in_channels,
squeeze_channels=squeeze_channels,
e1x1_channels=out_channels // 2,
e3x3_channels=out_channels // 2))
in_channels = out_channels
return nn.Sequential(*layers)
def forward(self, x):
return self.net(x)
"""
different configurations of ZipNet
"""
configs = {
'A': [64, [64, 4, 4, 2, 2], [256, 2, 8, 1, 1], [256, 4, 8, 2, 3]],
'B': [64, [64, 4, 4, 2, 2], [256, 2, 8, 1, 1], [256, 4, 8, 2, 2]]
}
class ZipNet(nn.Module):
"""ZipNet"""
def __init__(self,
config,
channels,
class_count):
super(ZipNet, self).__init__()
self.config = configs[config]
self.channels = channels
self.class_count = class_count
self.conv_net = self.get_conv_net()
def get_conv_net(self):
layers = []
layers.append(nn.Conv2d(in_channels=self.channels,
out_channels=self.config[0],
kernel_size=3,
stride=2))
layers.append(nn.BatchNorm2d(num_features=self.config[0]))
layers.append(nn.ReLU(inplace=True))
layers.append(nn.MaxPool2d(kernel_size=2,
stride=2,
ceil_mode=True))
layers.append(ZipBlock(in_channels=self.config[1][0],
num_layers=self.config[1][1],
compress_factor=self.config[1][2],
expand_factor=self.config[1][3],
expand_interval=self.config[1][4]))
layers.append(nn.MaxPool2d(kernel_size=2,
stride=2,
ceil_mode=True))
layers.append(ZipBlock(in_channels=self.config[2][0],
num_layers=self.config[2][1],
compress_factor=self.config[2][2],
expand_factor=self.config[2][3],
expand_interval=self.config[2][4]))
layers.append(nn.MaxPool2d(kernel_size=2,
stride=2,
ceil_mode=True))
layers.append(ZipBlock(in_channels=self.config[3][0],
num_layers=self.config[3][1],
compress_factor=self.config[3][2],
expand_factor=self.config[3][3],
expand_interval=self.config[3][4]))
expand_count = self.config[3][1] // self.config[3][4]
final_channels = self.config[3][0]
for i in range(expand_count):
final_channels *= self.config[3][3]
self.final_conv = nn.Conv2d(in_channels=final_channels,
out_channels=self.class_count,
kernel_size=1)
layers.append(nn.Dropout())
layers.append(self.final_conv)
layers.append(nn.BatchNorm2d(num_features=self.class_count))
layers.append(nn.ReLU(inplace=True))
return nn.Sequential(*layers)
def init_weights(self):
"""
initializes weights for each layer
"""
for module in self.modules():
if isinstance(module, nn.Conv2d):
if module is self.final_conv:
init.normal_(module.weight, mean=0.0, std=0.01)
else:
init.kaiming_uniform_(module.weight)
if module.bias is not None:
init.constant_(module.bias, 0)
def forward(self, x):
y = self.conv_net(x)
pool = nn.AvgPool2d(y.size(2), stride=1)
y = pool(y)
return y.view(y.size(0), self.class_count)