-
Notifications
You must be signed in to change notification settings - Fork 0
/
reactnet.py
207 lines (174 loc) · 7.75 KB
/
reactnet.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
from typing import Optional, Sequence, Union
import larq as lq
import tensorflow as tf
from zookeeper import Field, factory
from larq_zoo.core import utils
from larq_zoo.core.model_factory import ModelFactory
from classification_models.tfkeras import Classifiers
class LearnableBias(tf.keras.layers.Layer):
def __init__(self, out_chn):
super().__init__()
self.learnable_bias = tf.Variable(tf.zeros([1, 1, 1, out_chn]), name="learnable_bias_"+str(tf.keras.backend.get_uid("learnable_bias")))
def call(self, inputs):
return tf.add(inputs, self.learnable_bias)
def get_config(self):
return {**super().get_config(), "learnable_bias": self.learnable_bias.numpy()}
@factory
class ReActNetFactory(ModelFactory):
"""Implementation of [ReActNet]"""
filters: int = Field(32)
kernel_initializer: Union[tf.keras.initializers.Initializer, str] = Field(
"glorot_normal"
)
def block(
self, x, use_lab: bool, double_filters: bool = False, override_stride: bool = False
) -> tf.Tensor:
# Compute dimensions
self.input_quantizer_1 = lq.quantizers.LAB() if use_lab else lq.quantizers.SteSign()
in_filters = x.get_shape().as_list()[-1]
out_filters = in_filters if not double_filters else 2 * in_filters
stride = 1 if (override_stride or not double_filters) else 2
shortcut = x
if stride == 2:
shortcut = tf.keras.layers.AvgPool2D(2, strides=2, padding="valid")(shortcut)
x1 = LearnableBias(in_filters)(x)
x1 = lq.layers.QuantConv2D(
in_filters,
(3, 3),
strides=stride,
padding="same",
pad_values=1,
kernel_quantizer=self.kernel_quantizer,
input_quantizer=self.input_quantizer_1,
kernel_constraint=self.kernel_constraint,
use_bias=False,
)(x1)
x1 = tf.keras.layers.BatchNormalization(momentum=0.8)(x1)
x1 = tf.keras.layers.add([x1, shortcut])
x1 = LearnableBias(in_filters)(x1)
x1 = tf.keras.layers.PReLU(alpha_initializer=tf.keras.initializers.Constant(0.25), shared_axes=[1,2])(x1)
x1 = LearnableBias(in_filters)(x1)
x2 = LearnableBias(in_filters)(x1)
if in_filters == out_filters:
self.input_quantizer_21 = lq.quantizers.ConvBinarizerDepthwise() if use_lab else lq.quantizers.SteSign()
x2 = lq.layers.QuantConv2D(
out_filters,
(1, 1),
kernel_quantizer=self.kernel_quantizer, # Comment for ReActNet-C
input_quantizer=self.input_quantizer_21, # Comment for ReActNet-C
kernel_constraint=self.kernel_constraint,
use_bias=False,
)(x2)
x2 = tf.keras.layers.BatchNormalization(momentum=0.8)(x2)
x2 = tf.keras.layers.add([x1, x2]) # forgotten to add this line
else:
assert out_filters == in_filters * 2
self.input_quantizer_22 = lq.quantizers.ConvBinarizerDepthwise() if use_lab else lq.quantizers.SteSign()
x2 = self.input_quantizer_22(x2)
x21 = lq.layers.QuantConv2D(
in_filters,
(1, 1),
kernel_quantizer=self.kernel_quantizer, # Comment for ReActNet-C
kernel_constraint=self.kernel_constraint,
use_bias=False,
)(x2)
x22 = lq.layers.QuantConv2D(
in_filters,
(1, 1),
kernel_quantizer=self.kernel_quantizer, # Comment for ReActNet-C
kernel_constraint=self.kernel_constraint,
use_bias=False,
)(x2)
x21 = tf.keras.layers.BatchNormalization(momentum=0.8)(x21)
x22 = tf.keras.layers.BatchNormalization(momentum=0.8)(x22)
x21 = tf.keras.layers.add([x21, x1])
x22 = tf.keras.layers.add([x22, x1])
x2 = tf.concat([x21, x22], axis=-1)
x2 = LearnableBias(out_filters)(x2)
x2 = tf.keras.layers.PReLU(alpha_initializer=tf.keras.initializers.Constant(0.25), shared_axes=[1,2])(x2)
x2 = LearnableBias(out_filters)(x2)
return x2
def build(self) -> tf.keras.models.Model:
# Layer 1
out = tf.keras.layers.Conv2D(
self.filters,
(3, 3),
strides=2,
kernel_initializer=self.kernel_initializer,
padding="same",
use_bias=False,
)(self.image_input)
out = tf.keras.layers.BatchNormalization(momentum=0.8)(out)
out = self.block(out, self.lab_blocks[0], double_filters=True, override_stride=True)
for _ in range(2):
out = self.block(out, self.lab_blocks[1], double_filters=True)
out = self.block(out, self.lab_blocks[1])
out = self.block(out, self.lab_blocks[2], double_filters=True)
for _ in range(5):
out = self.block(out, self.lab_blocks[2])
out = self.block(out, self.lab_blocks[3], double_filters=True)
out = self.block(out, self.lab_blocks[3])
if self.include_top:
out = utils.global_pool(out)
out = tf.keras.layers.Dense(self.num_classes, name=f"{self.model_name}_logits")(out)
out = tf.keras.layers.Activation("softmax", dtype="float32")(out)
model = tf.keras.Model(inputs=self.image_input, outputs=out, name=self.model_name)
return model
@factory
class ResNet34Factory():
model_name: str = Field("resnet_34")
def build(self) -> tf.keras.models.Model:
ResNet34, _ = Classifiers.get('resnet34')
return ResNet34(input_shape=(224,224,3), weights='imagenet', include_top=True)
@factory
class ReActNetBANFactory(ReActNetFactory):
model_name: str = Field("reactnet_ban")
kernel_quantizer = None
kernel_constraint = None
@property
def kernel_regularizer(self):
return tf.keras.regularizers.l2(1e-5)
@factory
class ReActNetBNNFactory(ReActNetFactory):
model_name: str = Field("reactnet_bnn")
kernel_quantizer = "magnitude_aware_sign"
kernel_constraint = "weight_clip"
lab_blocks: Sequence[bool] = Field()
def ReActNet(
*, # Keyword arguments only
input_shape: Optional[Sequence[Optional[int]]] = None,
input_tensor: Optional[utils.TensorType] = None,
include_top: bool = True,
num_classes: int = 1000,
lab_blocks: Sequence[int],
) -> tf.keras.models.Model:
"""Instantiates the ReActNet architecture.
Ported from Pytorch to Tensorflow by the authors of LAB-BNN, however not able to
reproduce the same results.
# Arguments
input_shape: Optional shape tuple, to be specified if you would like to use a
model with an input image resolution that is not (224, 224, 3).
It should have exactly 3 inputs channels.
input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as
image input for the model.
include_top: whether to include the fully-connected layer at the top of the
network.
num_classes: optional number of classes to classify images into, only to be
specified if `include_top` is True, and if no `weights` argument is
specified.
# Returns
A Keras model instance.
# Raises
ValueError: in case of invalid argument for `weights`, or invalid input shape.
# References
- [Bi-Real Net: Enhancing the Performance of 1-bit CNNs With Improved
Representational Capability and Advanced Training
Algorithm](https://arxiv.org/abs/1808.00278)
"""
return ReActNetBNNFactory(
include_top=include_top,
input_tensor=input_tensor,
input_shape=input_shape,
num_classes=num_classes,
lab_blocks=lab_blocks,
).build()