-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathresnet20.py
272 lines (230 loc) · 7.63 KB
/
resnet20.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
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ResNet20, 56, 110, 164, 1001 version 2 for CIFAR-10
# Paper: https://arxiv.org/pdf/1603.05027.pdf
# Modified from:
# https://github.com/GoogleCloudPlatform/keras-idiomatic-programmer/blob/master/zoo/resnet/resnet_cifar10_v2.py
import tensorflow as tf
from tensorflow.keras import Model, Input
from tensorflow.keras.layers import (
Conv2D,
Dense,
BatchNormalization,
ReLU,
Add,
Activation,
)
from tensorflow.keras.layers import AveragePooling2D, GlobalAvgPool2D, Dropout
from tensorflow.keras.layers import experimental
from tensorflow.keras.regularizers import l2
WEIGHT_DECAY = 5e-4
def stem(inputs):
"""Construct Stem Convolutional Group
inputs : the input vector
"""
x = Conv2D(
16,
(3, 3),
strides=(1, 1),
padding="same",
use_bias=False,
kernel_regularizer=l2(WEIGHT_DECAY),
)(inputs)
x = BatchNormalization()(x)
x = ReLU()(x)
return x
def learner(x, n_blocks):
"""Construct the Learner
x : input to the learner
n_blocks : number of blocks in a group
"""
# First Residual Block Group of 16 filters (Stage 1)
# Quadruple (4X) the size of filters to fit the next Residual Group
x = residual_group(x, 16, n_blocks, strides=(1, 1), n=4)
# Second Residual Block Group of 64 filters (Stage 2)
# Double (2X) the size of filters and reduce feature maps by 75% (strides=2) to fit the next Residual Group
x = residual_group(x, 64, n_blocks, n=2)
# Third Residual Block Group of 64 filters (Stage 3)
# Double (2X) the size of filters and reduce feature maps by 75% (strides=2) to fit the next Residual Group
x = residual_group(x, 128, n_blocks, n=2)
return x
def residual_group(x, n_filters, n_blocks, strides=(2, 2), n=2):
"""Construct a Residual Group
x : input into the group
n_filters : number of filters for the group
n_blocks : number of residual blocks with identity link
strides : whether the projection block is a strided convolution
n : multiplier for the number of filters out
"""
# Double the size of filters to fit the first Residual Group
x = projection_block(x, n_filters, strides=strides, n=n)
# Identity residual blocks
for _ in range(n_blocks):
x = identity_block(x, n_filters, n)
return x
def identity_block(x, n_filters, n=2):
"""Construct a Bottleneck Residual Block of Convolutions
x : input into the block
n_filters: number of filters
n : multiplier for filters out
"""
# Save input vector (feature maps) for the identity link
shortcut = x
## Construct the 1x1, 3x3, 1x1 residual block (fig 3c)
# Dimensionality reduction
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(
n_filters,
(1, 1),
strides=(1, 1),
use_bias=False,
kernel_regularizer=l2(WEIGHT_DECAY),
)(x)
# Bottleneck layer
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(
n_filters,
(3, 3),
strides=(1, 1),
padding="same",
use_bias=False,
kernel_regularizer=l2(WEIGHT_DECAY),
)(x)
# Dimensionality restoration - increase the number of output filters by 2X or 4X
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(
n_filters * n,
(1, 1),
strides=(1, 1),
use_bias=False,
kernel_regularizer=l2(WEIGHT_DECAY),
)(x)
# Add the identity link (input) to the output of the residual block
x = Add()([x, shortcut])
return x
def projection_block(x, n_filters, strides=(2, 2), n=2):
"""Construct a Bottleneck Residual Block with Projection Shortcut
Increase the number of filters by 2X (or 4X on first stage)
x : input into the block
n_filters: number of filters
strides : whether the first convolution is strided
n : multiplier for number of filters out
"""
# Construct the projection shortcut
# Increase filters by 2X (or 4X) to match shape when added to output of block
shortcut = Conv2D(
n_filters * n,
(1, 1),
strides=strides,
use_bias=False,
kernel_regularizer=l2(WEIGHT_DECAY),
)(x)
## Construct the 1x1, 3x3, 1x1 convolution block
# Dimensionality reduction
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(
n_filters,
(1, 1),
strides=(1, 1),
use_bias=False,
kernel_regularizer=l2(WEIGHT_DECAY),
)(x)
# Bottleneck layer - feature pooling when strides=(2, 2)
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(
n_filters,
(3, 3),
strides=strides,
padding="same",
use_bias=False,
kernel_regularizer=l2(WEIGHT_DECAY),
)(x)
# Dimensionality restoration - increase the number of filters by 2X (or 4X)
x = BatchNormalization()(x)
x = ReLU()(x)
x = Conv2D(
n_filters * n,
(1, 1),
strides=(1, 1),
use_bias=False,
kernel_regularizer=l2(WEIGHT_DECAY),
)(x)
# Add the projection shortcut to the output of the residual block
x = Add()([shortcut, x])
return x
def projection_head(x, hidden_dim=128):
"""Constructs the projection head."""
for i in range(2):
x = Dense(
hidden_dim,
name=f"projection_layer_{i}",
kernel_regularizer=l2(WEIGHT_DECAY),
)(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
outputs = Dense(hidden_dim, name="projection_output")(x)
return outputs
def prediction_head(x, hidden_dim=128, mx=4):
"""Constructs the prediction head."""
x = BatchNormalization()(x)
x = Dense(
hidden_dim // mx,
name=f"prediction_layer_0",
kernel_regularizer=l2(WEIGHT_DECAY),
)(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)
x = Dense(
hidden_dim,
name="prediction_output",
kernel_regularizer=l2(WEIGHT_DECAY),
)(x)
return x
# -------------------
# Model | n |
# ResNet20 | 2 |
# ResNet56 | 6 |
# ResNet110 | 12 |
# ResNet164 | 18 |
# ResNet1001 | 111 |
def get_network(n=2, hidden_dim=128, use_pred=False, return_before_head=True):
depth = n * 9 + 2
n_blocks = ((depth - 2) // 9) - 1
# The input tensor
inputs = Input(shape=(32, 32, 3))
x = experimental.preprocessing.Rescaling(scale=1.0 / 127.5, offset=-1)(inputs)
# The Stem Convolution Group
x = stem(x)
# The learner
x = learner(x, n_blocks)
# Projections
trunk_output = GlobalAvgPool2D()(x)
projection_outputs = projection_head(trunk_output, hidden_dim=hidden_dim)
if return_before_head:
model = Model(inputs, [trunk_output, projection_outputs])
else:
model = Model(inputs, projection_outputs)
# Predictions
if use_pred:
prediction_outputs = prediction_head(projection_outputs)
if return_before_head:
model = Model(inputs, [projection_outputs, prediction_outputs])
else:
model = Model(inputs, prediction_outputs)
return model