-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrueControl.py
384 lines (336 loc) · 13.2 KB
/
trueControl.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
from typing import Optional, Sequence
# The point of this one is to remove ALL the batchnorms.
import larq as lq
import numpy as np
import tensorflow as tf
from zookeeper import Field, factory
from larq_zoo.core import utils
from larq_zoo.core.model_factory import ModelFactory
# The point of this one is to run the control with a normal maxpool, without the maxblurpool
# This is for a fair comparison; if this is worse than the "fullest" version, I will find a principled way
# to renormalize the maxblurpool
@lq.utils.register_keras_custom_object
def blurpool_initializer(shape, dtype=None):
"""Initializer for anti-aliased pooling.
# References
- [Making Convolutional Networks Shift-Invariant Again](https://arxiv.org/abs/1904.11486)
"""
ksize, filters = shape[0], shape[2]
if ksize == 2:
k = np.array([1, 1])
elif ksize == 3:
k = np.array([1, 2, 1])
elif ksize == 5:
k = np.array([1, 4, 6, 4, 1])
else:
raise ValueError("filter size should be in 2, 3, 5")
k = np.outer(k, k)
k = k / np.sum(k)
k = np.expand_dims(k, axis=-1)
k = np.repeat(k, filters, axis=-1)
return np.reshape(k, shape)
@factory
class TrueControlQuickNetFactory(ModelFactory):
name = "quicknet"
section_blocks: Sequence[int] = Field((4, 4, 4, 4))
section_filters: Sequence[int] = Field((64, 128, 256, 512))
@property
def imagenet_weights_path(self):
return utils.download_pretrained_model(
model="quicknet",
version="v1.0",
file="quicknet_weights.h5",
file_hash="8aba9e4e5f8d342faef04a0b2ae8e562da57dbb7d15162e8b3e091c951ba756c",# Not correct filepath; from original QuickNet
)
@property
def imagenet_no_top_weights_path(self):
return utils.download_pretrained_model(
model="quicknet",
version="v1.0",
file="quicknet_weights_notop.h5",
file_hash="204414e438373f14f6056a1c098249f505a87dd238e18d3a47a9bd8b66227881",# Not correct filepath; from original QuickNet
)
@property
def input_quantizer(self):
return lq.quantizers.SteSign(clip_value=1.25)
@property
def kernel_quantizer(self):
return lq.quantizers.SteSign(clip_value=1.25)
@property
def kernel_constraint(self):
return lq.constraints.WeightClip(clip_value=1.25)
def __post_configure__(self):
assert len(self.section_blocks) == len(self.section_filters)
def stem_module(self, filters: int, x: tf.Tensor) -> tf.Tensor:
"""Start of network."""
assert filters % 4 == 0
x = lq.layers.QuantConv2D(
filters // 4,
(3, 3),
kernel_initializer="he_normal",
padding="same",
strides=2,
use_bias=False,
)(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Activation("relu")(x)
x = lq.layers.QuantDepthwiseConv2D(
(3, 3),
padding="same",
strides=2,
use_bias=False,
)(x)
x = tf.keras.layers.BatchNormalization(scale=False, center=False)(x)
x = lq.layers.QuantConv2D(
filters,
1,
kernel_initializer="he_normal",
use_bias=False,
)(x)
return tf.keras.layers.BatchNormalization()(x)
def residual_block(self, x: tf.Tensor) -> tf.Tensor:
"""Standard residual block, without strides or filter changes."""
residual = x
x = lq.layers.QuantConv2D(
int(x.shape[-1]),
(3, 3),
activation="relu",
input_quantizer=self.input_quantizer,
kernel_constraint=self.kernel_constraint,
kernel_quantizer=self.kernel_quantizer,
kernel_initializer="glorot_normal",
padding="same",
pad_values=1.0,
use_bias=False,
)(x)
x = tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5)(x)
return x + residual
def transition_block(
self,
x: tf.Tensor,
filters: int,
strides: int,
) -> tf.Tensor:
"""Pointwise transition block."""
x = tf.keras.layers.Activation("relu")(x)
x = tf.keras.layers.MaxPool2D(pool_size=strides, strides=strides)(x) # Only change from control
x = lq.layers.QuantConv2D(
filters,
(1, 1),
kernel_initializer="glorot_normal",
use_bias=False,
)(x)
return tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5)(x)
def build(self) -> tf.keras.models.Model:
x = self.stem_module(self.section_filters[0], self.image_input)
for block, (layers, filters) in enumerate(
zip(self.section_blocks, self.section_filters)
):
for layer in range(layers):
if filters != x.shape[-1]:
x = self.transition_block(x, filters, strides=2)
x = self.residual_block(x)
if self.include_top:
x = tf.keras.layers.Activation("relu")(x)
x = utils.global_pool(x)
x = lq.layers.QuantDense(
self.num_classes,
kernel_initializer="glorot_normal",
)(x)
x = tf.keras.layers.Activation("softmax", dtype="float32")(x)
model = tf.keras.Model(inputs=self.image_input, outputs=x, name=self.name)
# Load weights.
if self.weights == "imagenet":
weights_path = (
self.imagenet_weights_path
if self.include_top
else self.imagenet_no_top_weights_path
)
model.load_weights(weights_path)
elif self.weights is not None:
model.load_weights(self.weights)
return model
@factory
class TrueControlQuickNetSmallFactory(TrueControlQuickNetFactory):
name = "quicknet_small"
section_filters = Field((32, 64, 256, 512))
@property
def imagenet_weights_path(self):
return utils.download_pretrained_model(
model="quicknet",
version="v1.0",
file="quicknet_small_weights.h5",
file_hash="1ac3b07df7f5a911dd0b49febb2486428ddf1ca130297c403815dfae5a1c71a2",# Not correct filepath; from original QuickNet
)
@property
def imagenet_no_top_weights_path(self):
return utils.download_pretrained_model(
model="quicknet",
version="v1.0",
file="quicknet_small_weights_notop.h5",
file_hash="be8ba657155846be355c5580d1ea56eaf8282616de065ffc39257202f9f164ea",# Not correct filepath; from original QuickNet
)
@factory
class TrueControlQuickNetLargeFactory(TrueControlQuickNetFactory):
name = "quicknet_large"
section_blocks = Field((6, 8, 12, 6))
@property
def imagenet_weights_path(self):
return utils.download_pretrained_model(
model="quicknet",
version="v1.0",
file="quicknet_large_weights.h5",
file_hash="c5158e8a59147b31370becd937825f4db8a5cdf308314874f678f596629be45c",# Not correct filepath; from original QuickNet
)
@property
def imagenet_no_top_weights_path(self):
return utils.download_pretrained_model(
model="quicknet",
version="v1.0",
file="quicknet_large_weights_notop.h5",
file_hash="adcf154a2a8007e81bd6af77c035ffbf54cd6413b89a0ba294e23e76a82a1b78",# Not correct filepath; from original QuickNet
)
def TrueControlQuickNet(
*, # Keyword arguments only
input_shape: Optional[Sequence[Optional[int]]] = None,
input_tensor: Optional[utils.TensorType] = None,
weights: Optional[str] = "imagenet",
include_top: bool = True,
num_classes: int = 1000,
) -> tf.keras.models.Model:
"""Instantiates the TrueControlQuickNet architecture.
Optionally loads weights pre-trained on ImageNet.
```netron
quicknet-v1.0/quicknet.json
```
```summary
sota.TrueControlQuickNet
```
```plot-altair
/plots/quicknet.vg.json
```
# ImageNet Metrics
| Top-1 Accuracy | Top-5 Accuracy | Parameters | Memory |
| -------------- | -------------- | ---------- | ------- |
| 63.3 % | 84.6 % | 13 234 088 | 4.17 MB |
# 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.
weights: one of `None` (random initialization), "imagenet" (pre-training on
ImageNet), or the path to the weights file to be loaded.
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.
"""
return TrueControlQuickNetFactory(
input_shape=input_shape,
input_tensor=input_tensor,
weights=weights,
include_top=include_top,
num_classes=num_classes,
).build()
def TrueControlQuickNetLarge(
*, # Keyword arguments only
input_shape: Optional[Sequence[Optional[int]]] = None,
input_tensor: Optional[utils.TensorType] = None,
weights: Optional[str] = "imagenet",
include_top: bool = True,
num_classes: int = 1000,
) -> tf.keras.models.Model:
"""Instantiates the TrueControlQuickNetLarge architecture.
Optionally loads weights pre-trained on ImageNet.
```netron
quicknet-v1.0/quicknet_large.json
```
```summary
sota.TrueControlQuickNetLarge
```
```plot-altair
/plots/quicknet_large.vg.json
```
# ImageNet Metrics
| Top-1 Accuracy | Top-5 Accuracy | Parameters | Memory |
| -------------- | -------------- | ---------- | ------- |
| 66.9 % | 87.0 % | 23 342 248 | 5.40 MB |
# 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.
weights: one of `None` (random initialization), "imagenet" (pre-training on
ImageNet), or the path to the weights file to be loaded.
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.
"""
return TrueControlQuickNetLargeFactory(
input_shape=input_shape,
input_tensor=input_tensor,
weights=weights,
include_top=include_top,
num_classes=num_classes,
).build()
def TrueControlQuickNetSmall(
*, # Keyword arguments only
input_shape: Optional[Sequence[Optional[int]]] = None,
input_tensor: Optional[utils.TensorType] = None,
weights: Optional[str] = "imagenet",
include_top: bool = True,
num_classes: int = 1000,
) -> tf.keras.models.Model:
"""Instantiates the TrueControlQuickNetSmall architecture.
Optionally loads weights pre-trained on ImageNet.
```netron
quicknet-v1.0/quicknet_small.json
```
```summary
sota.TrueControlQuickNetSmall
```
```plot-altair
/plots/quicknet_small.vg.json
```
# ImageNet Metrics
| Top-1 Accuracy | Top-5 Accuracy | Parameters | Memory |
| -------------- | -------------- | ---------- | ------- |
| 59.4 % | 81.8 % | 12 655 688 | 4.00 MB |
# 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.
weights: one of `None` (random initialization), "imagenet" (pre-training on
ImageNet), or the path to the weights file to be loaded.
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.
"""
return TrueControlQuickNetSmallFactory(
input_shape=input_shape,
input_tensor=input_tensor,
weights=weights,
include_top=include_top,
num_classes=num_classes,
).build()