-
Notifications
You must be signed in to change notification settings - Fork 20
/
resnet.py
397 lines (347 loc) · 15.9 KB
/
resnet.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2020 Apple Inc. All Rights Reserved.
#
"""
ResNet model.
See `Deep Residual Learning for Image Recognition`_ for more details.
.. _Deep Residual Learning for Image Recognition: https://arxiv.org/abs/1512.03385
"""
from typing import Callable, Dict, List, Optional, Union
import torch
import torch.nn as nn
from quant.binary.binary_conv import QuantConv2d
non_linearity_map = {
'relu': nn.ReLU,
'prelu': nn.PReLU,
'identity': nn.Identity,
}
class RegularBasicBlock(nn.Module):
"""ResNet regular basic block."""
def __init__(
self, in_planes: int, planes: int, x_quant: str, w_quant: str,
nonlins: List[str], stride: int = 1,
clamp: Optional[Dict] = None,
moving_average_mode: str = 'off',
moving_average_momentum: float = 0.99,
) -> None:
"""
Build ResNet regular basic block.
Args:
in_planes: the number of in-channels for the block
planes: the number of out-channels for the block
x_quant: quantization scheme for activations,
see :mod:`~quant.binary.binary_conv`.
w_quant: quantization scheme for weights,
see :mod:`~quant.binary.binary_conv`.
nonlins: non-linearities for the black. It should be a list of two
strings, where each string is in {'relu', 'prelu', 'identity'}.
stride: stride size
clamp: clamping scheme for activations.
It should have a key named "kind" indicating the kind of clamping function
and other keys indicating other potential arguments.
See :mod:`~quant.binary.binary_conv`.
moving_average_mode: moving average mode to use,
see :class:`~quant.binary.activation_quantization.ActivationQuantizer`.
moving_average_momentum: momentum for moving average update,
see :class:`~quant.binary.activation_quantization.ActivationQuantizer`.
"""
super(RegularBasicBlock, self).__init__()
if len(nonlins) != 2:
raise ValueError('There should be 2 non-linearities.')
self.conv1 = QuantConv2d(
x_quant, w_quant, in_planes, planes, 3, clamp,
moving_average_mode, moving_average_momentum, stride=stride, padding=1, bias=False
)
self.bn1 = nn.BatchNorm2d(planes)
self.nonlin1 = non_linearity_map[nonlins[0]]()
self.conv2 = QuantConv2d(
x_quant, w_quant, planes, planes, 3, clamp,
moving_average_mode, moving_average_momentum, stride=1, padding=1, bias=False
)
self.bn2 = nn.BatchNorm2d(planes)
self.nonlin2 = non_linearity_map[nonlins[1]]()
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != planes:
self.shortcut = nn.Sequential(
nn.Conv2d(
in_planes,
planes,
kernel_size=1,
stride=stride,
bias=False,
),
nn.BatchNorm2d(planes),
)
def forward(self, x: torch.Tensor) -> torch.Tensor: # type: ignore
"""Forward pass of RegularBasicBlock."""
out = self.nonlin1(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out = out + self.shortcut(x)
out = self.nonlin2(out)
return out
class XnorBasicBlock(nn.Module):
"""
ResNet XNOR regular basic block.
Block structure (BN -> Quant -> Conv -> NonLin):
Rastegari, Mohammad, et al.
"Xnor-net: Imagenet classification using binary convolutional neural networks."
European conference on computer vision. Springer, Cham, 2016.
Using double shortcuts:
Zechun Liu, Baoyuan Wu, Wenhan Luo, Xin Yang, Wei Liu, and Kwang-Ting Cheng.
"Bi-real net: Enhancing the performance of 1-bit CNNs with improved representational
capability and advanced training algorithm."
In Proceedings of the European conference on computer vision (ECCV), pages 722–737, 2018.
"""
def __init__(
self, in_planes: int, planes: int, x_quant: str, w_quant: str,
nonlins: List[str], stride: int = 1, double_shortcut: bool = False,
clamp: Optional[Dict] = None,
moving_average_mode: str = 'off',
moving_average_momentum: float = 0.99,
) -> None:
"""
Build ResNet XNOR basic block.
Args:
in_planes: the number of in-channels for the block
planes: the number of out-channels for the block
x_quant: quantization scheme for activations,
see :mod:`~quant.binary.binary_conv`.
w_quant: quantization scheme for weights,
see :mod:`~quant.binary.binary_conv`.
nonlins: non-linearities for the block. It should be a list of two
strings, where each string is in {'relu', 'prelu', 'identity'}.
stride: stride size
double_shortcut: whether to use double shortcuts.
clamp: clamping scheme for activations.
It should have a key named "kind" indicating the kind of clamping function
and other keys indicating other potential arguments.
See :mod:`~quant.binary.binary_conv`.
moving_average_mode: moving average mode to use,
see :class:`~quant.binary.activation_quantization.ActivationQuantizer`.
moving_average_momentum: momentum for moving average update,
see :class:`~quant.binary.activation_quantization.ActivationQuantizer`.
"""
super(XnorBasicBlock, self).__init__()
if len(nonlins) != 2:
raise ValueError('There should be 2 non-linearities.')
self.double_shortcut = double_shortcut
self.bn1 = nn.BatchNorm2d(in_planes)
self.conv1 = QuantConv2d(
x_quant, w_quant, in_planes, planes, 3, clamp,
moving_average_mode, moving_average_momentum, stride=stride, padding=1, bias=True
)
self.nonlin1 = non_linearity_map[nonlins[0]]()
self.bn2 = nn.BatchNorm2d(planes)
self.conv2 = QuantConv2d(
x_quant, w_quant, planes, planes, 3, clamp,
moving_average_mode, moving_average_momentum, stride=1, padding=1, bias=True
)
self.nonlin2 = non_linearity_map[nonlins[1]]()
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != planes:
self.shortcut = nn.Sequential(
nn.Conv2d(
in_planes,
planes,
kernel_size=1,
stride=stride,
bias=True,
),
nn.BatchNorm2d(planes),
)
def forward(self, x: torch.Tensor) -> torch.Tensor: # type: ignore
"""Forward pass of XnorBasicBlock."""
out1 = self.nonlin1(self.conv1(self.bn1(x)))
if self.double_shortcut:
out1 = out1 + self.shortcut(x)
out2 = self.conv2(self.bn2(out1))
if self.double_shortcut:
out2 = self.nonlin2(out2)
return out2 + out1
out2 = out2 + self.shortcut(x)
return self.nonlin2(out2)
class QResNet(nn.Module):
"""
ResNet implementation supporting full precision and quantized schemes.
Note we use full-precision down-sampling. See:
Zechun Liu, Baoyuan Wu, Wenhan Luo, Xin Yang, Wei Liu, and Kwang-Ting Cheng.
Bi-real net: Enhancing the performance of 1-bit CNNs with improved representational
capability and advanced training algorithm.
In Proceedings of the European conference on computer vision (ECCV), pages 722–737, 2018.
Two types of blocks can be used, either
:class:`~quant.models.resnet.RegularBasicBlock` (regular) or
:class:`~quant.models.resnet.XnorBasicBlock` (xnor).
ResNet consists of the following layers:
layer0 (first layer), layer1, layer2, layer3, layer4 (optional), layer5 (last layer).
`layer0` is the feature extractor layer (conv1).
Its config dictionary contains keys: `n_in_channels`, `kernel_size`, `stride`, `padding`,
`bias`, and `maxpool`.
It is important to note that `n_in_channels` does not refer to the number of channels of the
image (3), but rather the number of input channels to `layer1`.
All arguments except for `maxpool` are passed to PyTorch ``nn.Conv2d``.
`maxpool` is another dictionary with keys `type`, `kernel_size`, `stride`, and `padding`.
If the type is `identity`, there is no pooling.
If the type is `maxpool2d`, then the other keys are passed to construct ``nn.MaxPool2d``.
`layer1`, `layer2`, `layer3`, `layer4` are all dictionaries used to
configure the corresponding layers.
Usually they can all be the same dictionary.
The keys and values here are used to construct either
:class:`~quant.models.resnet.RegularBasicBlock` or :class:`~quant.models.resnet.XnorBasicBlock`
depending on what is specified in `block`.
`nonlins` is a list of two strings specifying the non-linearity to use inside each layer.
Each string value can be `relu`, `prelu`, or `identity`.
"""
def __init__(
self,
loss_fn: Callable[..., torch.Tensor],
block: str,
layer0: dict,
layer1: dict,
layer2: dict,
layer3: dict,
layer4: Optional[dict],
nonlins: List[str],
num_blocks: List[int],
output_classes: int,
moving_average_mode: str = 'off',
moving_average_momentum: float = 0.99,
) -> None:
"""
Construct QResNet.
Args:
loss_fn: loss function of the model
block: name of the block to use ('regular' or 'xnor')
layer0: configuration for conv1 layer of the model
layer1: configuration for layer1 layer of the model
layer2: configuration for layer2 layer of the model
layer3: configuration for layer3 layer of the model
layer4: configuration for layer4 layer of the model
nonlins: non-linearities to use for each layer. It should be a list of two
strings, where each string is in {'relu', 'prelu', 'identity'}.
num_blocks: a list representing the number of blocks in each layer
output_classes: number of output classes
moving_average_mode: moving average mode to use
see :class:`~quant.binary.activation_quantization.ActivationQuantizer`.
moving_average_momentum: momentum for moving average
update, see :class:`~quant.binary.activation_quantization.ActivationQuantizer`.
"""
super(QResNet, self).__init__()
# loss_fn is a loss function in torch.nn.functional
setattr(self, 'loss_fn', loss_fn)
blocks = {
'regular': RegularBasicBlock,
'xnor': XnorBasicBlock,
}
try:
block_cls: Union[RegularBasicBlock, XnorBasicBlock] \
= blocks[block] # type: ignore
except KeyError:
raise ValueError(f'Block {block} is not supported.')
n_in_channels = layer0['n_in_channels']
self.conv1 = nn.Conv2d(
3,
n_in_channels,
kernel_size=layer0['kernel_size'],
stride=layer0['stride'],
padding=layer0['padding'],
bias=layer0['bias'],
)
if layer0['maxpool']['type'] == 'identity':
self.maxpool = nn.Identity()
elif layer0['maxpool']['type'] == 'maxpool2d': # pragma: no cover (coverage does not report it even though it's covered) # noqa: E501
self.maxpool = nn.MaxPool2d( # type: ignore
kernel_size=layer0['maxpool']['kernel_size'],
stride=layer0['maxpool']['stride'],
padding=layer0['maxpool']['padding'],
)
else:
raise ValueError(
f"maxpool type {layer0['maxpool']['type']} is not supported."
)
self.bn1 = nn.BatchNorm2d(n_in_channels)
self.blocks = nn.ModuleList(
[nn.Sequential(self.conv1, self.bn1, nn.ReLU(inplace=True), self.maxpool)]
)
n_planes = self._make_layer(
block_cls, layer1,
n_in_channels, n_in_channels, num_blocks[0], nonlins, stride=1,
moving_average_mode=moving_average_mode,
moving_average_momentum=moving_average_momentum
)
n_planes = self._make_layer(
block_cls, layer2,
n_planes, 2 * n_in_channels, num_blocks[1], nonlins, stride=2,
moving_average_mode=moving_average_mode,
moving_average_momentum=moving_average_momentum
)
n_planes = self._make_layer(
block_cls, layer3,
n_planes, 4 * n_in_channels, num_blocks[2], nonlins, stride=2,
moving_average_mode=moving_average_mode,
moving_average_momentum=moving_average_momentum
)
if layer4 is not None: # pragma: no cover (coverage does not report it even though it's covered) # noqa: E501
n_planes = self._make_layer(
block_cls, layer4,
n_planes, 8 * n_in_channels, num_blocks[3], nonlins, stride=2,
moving_average_mode=moving_average_mode,
moving_average_momentum=moving_average_momentum
)
self.linear_classifier = nn.Sequential(
nn.AdaptiveAvgPool2d((1, 1)),
nn.Flatten(), # type: ignore
nn.Linear(n_planes, output_classes)
)
def _make_layer(
self,
block: Union[RegularBasicBlock, XnorBasicBlock],
layer_config: dict,
in_planes: int,
out_planes: int,
num_blocks: int,
nonlins: List[str],
stride: int,
moving_average_mode: str = 'off',
moving_average_momentum: float = 0.99,
) -> int:
"""
Make a layer (layer1, layer2, layer3, layer4).
Args:
block:
block to user in the layer
layer_config: a dictionary containing the config for the layer.
It should have the following keys:
* x_quant: quantization scheme for activations
* w_quant: quantization scheme for weights
* clamp: clamping scheme for activations.
It should have a key named "kind" indicating the kind of clamping function
and other keys indicating other potential arguments.
* other optional keys such as double_shortcut
in_planes: the number of in-channels for the layer
out_planes: the number of out-channels for the layer
num_blocks: the number of blocks for the layer
nonlins: non-linearities for the current layer. It should be a list of two
strings, where each string is in {'relu', 'prelu', 'identity'}.
stride: stride size
moving_average_mode: moving average mode to use
see :class:`~quant.binary.activation_quantization.ActivationQuantizer`.
moving_average_momentum: momentum for moving average
update, see :class:`~quant.binary.activation_quantization.ActivationQuantizer`.
Returns:
the number of planes of the layer
"""
strides = [stride] + [1] * (num_blocks - 1)
for stride in strides:
self.blocks.append(
block(in_planes, out_planes, nonlins=nonlins, stride=stride,
moving_average_mode=moving_average_mode,
moving_average_momentum=moving_average_momentum, **layer_config)
)
in_planes = out_planes
return in_planes
def forward(self, x: torch.Tensor) -> torch.Tensor: # type: ignore
"""Forward pass of XnorBasicBlock."""
for block in self.blocks:
x = block(x)
return self.linear_classifier(x)