-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresnet_objectoriented.py
331 lines (282 loc) · 14.9 KB
/
resnet_objectoriented.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
"""
File name: resnet_objectoriented.py
Author: Benjamin Planche
Date created: 26.03.2019
Date last modified: 18:56 26.03.2019
Python Version: "3.6"
Copyright = "Copyright (C) 2018-2019 of Packt"
Credits = ["Eliot Andres, Benjamin Planche"]
License = "MIT"
Version = "1.0.0"
Maintainer = "non"
Status = "Prototype" # "Prototype", "Development", or "Production"
"""
#==============================================================================
# Imported Modules
#==============================================================================
import functools
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import (
Input, Activation, Dense, Flatten, Conv2D, MaxPooling2D,
GlobalAveragePooling2D, AveragePooling2D, BatchNormalization, add)
import tensorflow.keras.regularizers as regulizers
#==============================================================================
# Class Definitions
#==============================================================================
class ConvWithBatchNorm(tf.keras.layers.Conv2D):
""" Convolutional layer with batch normalization"""
def __init__(self, activation='relu', name='convbn', **kwargs):
"""
Initialize the layer.
:param activation: Activation function (name or callable)
:param name: Name suffix for the sub-layers.
:param kwargs: Mandatory and optional parameters of tf.keras.layers.Conv2D
"""
self.activation = Activation(
activation, name=name + '_act') if activation is not None else None
super().__init__(activation=None, name=name + '_c', **kwargs)
self.batch_norm = BatchNormalization(axis=-1, name=name + '_bn')
def call(self, inputs, training=None):
"""
Call the layer.
:param inputs: Input tensor to process
:param training: Flag to let TF knows if it is a training iteration or not
(this will affect the behavior of BatchNorm)
:return: Convolved tensor
"""
x = super().call(inputs)
x = self.batch_norm(x, training=training)
if self.activation is not None:
x = self.activation(x)
return x
class ResidualMerge(tf.keras.layers.Layer):
""" Layer to merge the original tensor and the residual one in residual blocks"""
def __init__(self, name='block', **kwargs):
"""
Initialize the layer.
:param activation: Activation function (name or callable)
:param name: Name suffix for the sub-layers.
:param kwargs: Optional parameters of tf.keras.layers.Conv2D
"""
super().__init__(name=name)
self.shortcut = None
self.kwargs = kwargs
def build(self, input_shape):
x_shape = input_shape[0]
x_residual_shape = input_shape[1]
if x_shape[1] == x_residual_shape[1] and \
x_shape[2] == x_residual_shape[2] and \
x_shape[3] == x_residual_shape[3]:
self.shortcut = functools.partial(tf.identity, name=self.name + '_shortcut')
else:
strides = (
int(round(x_shape[1] / x_residual_shape[1])), # vertical stride
int(round(x_shape[2] / x_residual_shape[2])) # horizontal stride
)
x_residual_channels = x_residual_shape[3]
self.shortcut = ConvWithBatchNorm(
filters=x_residual_channels, kernel_size=(1, 1), strides=strides,
activation=None, name=self.name + '_shortcut_c', **self.kwargs)
def call(self, inputs):
"""
Call the layer.
:param inputs: Tuple of two input tensors to merge
:return: Merged tensor
"""
x, x_residual = inputs
x_shortcut = self.shortcut(x)
x_merge = add([x_shortcut, x_residual])
return x_merge
class BasicResidualBlock(tf.keras.Model):
""" Basic residual block"""
def __init__(self, filters=16, kernel_size=1, strides=1, activation='relu',
kernel_initializer='he_normal', kernel_regularizer=regulizers.l2(1e-4),
name='res_basic', **kwargs):
"""
Initialize the layer.
:param filters: Number of filters
:param kernel_size: Kernel size
:param strides: Convolution strides
:param activation: Activation function (name or callable)
:param kernel_initializer: Kernel initialisation method name
:param kernel_regularizer: Kernel regularizer
:param name: Name suffix for the sub-layers.
:param kwargs: Optional parameters of tf.keras.layers.Conv2D
"""
super().__init__(name=name)
self.conv_1 = ConvWithBatchNorm(
filters=filters, kernel_size=kernel_size, activation=activation, padding='same',
kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer,
strides=strides, name=name + '_cb_1', **kwargs)
self.conv_2 = ConvWithBatchNorm(
filters=filters, kernel_size=kernel_size, activation=None, padding='same',
kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer,
strides=1, name=name + '_cb_2', **kwargs)
self.merge = ResidualMerge(
kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer,
name=name)
self.activation = Activation(activation, name=name + '_act')
def call(self, inputs, training=None):
"""
Call the block.
:param inputs: Input tensor to process
:param training: Flag to let TF knows if it is a training iteration or not
(this will affect the behavior of BatchNorm)
:return: Block output tensor
"""
x = inputs
# Residual path:
x_residual = self.conv_1(x, training=training)
x_residual = self.conv_2(x_residual, training=training)
# Merge residual result with original tensor:
x_merge = self.merge([x, x_residual])
x_merge = self.activation(x_merge)
return x_merge
class ResidualBlockWithBottleneck(tf.keras.Model):
""" Residual block with bottleneck, recommended for deep ResNets (depth > 34)"""
def __init__(self, filters=16, kernel_size=1, strides=1, activation='relu',
kernel_initializer='he_normal', kernel_regularizer=regulizers.l2(1e-4),
name='res_basic', **kwargs):
"""
Initialize the block.
:param filters: Number of filters
:param kernel_size: Kernel size
:param strides: Convolution strides
:param activation: Activation function (name or callable)
:param kernel_initializer: Kernel initialisation method name
:param kernel_regularizer: Kernel regularizer
:param name: Name suffix for the sub-layers.
:param kwargs: Optional parameters of tf.keras.layers.Conv2D
"""
super().__init__(name=name)
self.conv_0 = ConvWithBatchNorm(
filters=filters, kernel_size=1, activation=activation, padding='valid',
kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer,
strides=1, name=name + '_cb_0', **kwargs)
self.conv_1 = ConvWithBatchNorm(
filters=filters, kernel_size=kernel_size, activation=activation, padding='same',
kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer,
strides=strides, name=name + '_cb_1', **kwargs)
self.conv_2 = ConvWithBatchNorm(
filters=4 * filters, kernel_size=1, activation=None, padding='valid',
kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer,
strides=1, name=name + '_cb_2', **kwargs)
self.merge = ResidualMerge(
kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer,
name=name)
self.activation = Activation(activation, name=name + '_act')
def call(self, inputs, training=None):
"""
Call the layer.
:param inputs: Input tensor to process
:param training: Flag to let TF knows if it is a training iteration or not
(this will affect the behavior of BatchNorm)
:return: Block output tensor
"""
x = inputs
# Residual path:
x_residual = self.conv_0(x, training=training)
x_residual = self.conv_1(x_residual, training=training)
x_residual = self.conv_2(x_residual, training=training)
# Merge residual result with original tensor:
x_merge = self.merge([x, x_residual])
x_merge = self.activation(x_merge)
return x_merge
class ResidualMacroBlock(tf.keras.models.Sequential):
""" Macro-block, chaining multiple residual blocks (as a Sequential model)"""
def __init__(self, block_class=ResidualBlockWithBottleneck, repetitions=3,
filters=16, kernel_size=1, strides=1, activation='relu',
kernel_initializer='he_normal', kernel_regularizer=regulizers.l2(1e-4),
name='res_macroblock', **kwargs):
"""
Initialize the block.
:param block_class: Block class to be used.
:param repetitions: Number of times the block should be repeated inside.
:param filters: Number of filters
:param kernel_size: Kernel size
:param strides: Convolution strides
:param activation: Activation function (name or callable)
:param kernel_initializer: Kernel initialisation method name
:param kernel_regularizer: Kernel regularizer
:param name: Name suffix for the sub-layers.
:param kwargs: Optional parameters of tf.keras.layers.Conv2D
"""
super().__init__(
[block_class(
filters=filters, kernel_size=kernel_size, activation=activation,
strides=strides if i == 0 else 1, name="{}_{}".format(name, i),
kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer)
for i in range(repetitions)],
name=name)
class ResNet(tf.keras.models.Sequential):
""" ResNet model for classification"""
def __init__(self, input_shape, num_classes=1000,
block_class=ResidualBlockWithBottleneck, repetitions=(2, 2, 2, 2),
kernel_initializer='he_normal', kernel_regularizer=regulizers.l2(1e-4),
name='resnet'):
"""
Initialize a ResNet model for classification.
:param input_shape: Input shape (e.g. (224, 224, 3))
:param num_classes: Number of classes to predict
:param block_class: Block class to be used
:param repetitions: List of repetitions for each macro-blocks the network should contain
:param kernel_initializer: Kernel initialisation method name
:param kernel_regularizer: Kernel regularizer
:param name: Model's name
:return: ResNet model.
"""
filters = 64
strides = 2
super().__init__(
# Initial conv + max-pool layers:
[Input(shape=input_shape, name='input'),
ConvWithBatchNorm(
filters=filters, kernel_size=7, activation='relu', padding='same', strides=strides,
kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer,
name='conv'),
MaxPooling2D(pool_size=3, strides=strides, padding='same', name='max_pool')
] + \
# Residual blocks:
[ResidualMacroBlock(
block_class=block_class, repetitions=repet,
filters=min(filters * (2 ** i), 1024), kernel_size=3, activation='relu',
strides=strides if i != 0 else 1, name='block_{}'.format(i),
kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer)
for i, repet in enumerate(repetitions)
] + \
# Final layers leading to classification output:
[GlobalAveragePooling2D(name='avg_pool'),
Dense(units=num_classes, kernel_initializer=kernel_initializer, activation='softmax')
], name=name)
# Standard ResNet versions:
class ResNet18(ResNet):
def __init__(self, input_shape, num_classes=1000, name='resnet18',
kernel_initializer='he_normal', kernel_regularizer=regulizers.l2(1e-4)):
super().__init__(input_shape, num_classes,
block_class=BasicResidualBlock, repetitions=(2, 2, 2, 2),
kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer)
class ResNet34(ResNet):
def __init__(self, input_shape, num_classes=1000, name='resnet34',
kernel_initializer='he_normal', kernel_regularizer=regulizers.l2(1e-4)):
super().__init__(input_shape, num_classes,
block_class=BasicResidualBlock, repetitions=(3, 4, 6, 3),
kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer)
class ResNet50(ResNet):
def __init__(self, input_shape, num_classes=1000, name='resnet50',
kernel_initializer='he_normal', kernel_regularizer=regulizers.l2(1e-4)):
super().__init__(input_shape, num_classes,
block_class=ResidualBlockWithBottleneck, repetitions=(3, 4, 6, 3),
kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer)
class ResNet101(ResNet):
def __init__(self, input_shape, num_classes=1000, name='resnet101',
kernel_initializer='he_normal', kernel_regularizer=regulizers.l2(1e-4)):
super().__init__(input_shape, num_classes,
block_class=ResidualBlockWithBottleneck, repetitions=(3, 4, 23, 3),
kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer)
class ResNet152(ResNet):
def __init__(self, input_shape, num_classes=1000, name='resnet152',
kernel_initializer='he_normal', kernel_regularizer=regulizers.l2(1e-4)):
super().__init__(input_shape, num_classes,
block_class=ResidualBlockWithBottleneck, repetitions=(3, 8, 36, 3),
kernel_initializer=kernel_initializer, kernel_regularizer=kernel_regularizer)