-
Notifications
You must be signed in to change notification settings - Fork 0
/
resnet_cifar10_v2.py
182 lines (152 loc) · 6.64 KB
/
resnet_cifar10_v2.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
# 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, Flatten, BatchNormalization, ReLU, Add
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.regularizers import l2
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(0.0005))(inputs)
x = BatchNormalization(gamma_regularizer=l2(0.0005), beta_regularizer=l2(0.0005))(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(gamma_regularizer=l2(0.0005), beta_regularizer=l2(0.0005))(x)
x = ReLU()(x)
x = Conv2D(n_filters, (1, 1), strides=(1, 1), use_bias=False,
kernel_regularizer=l2(0.0005))(x)
# Bottleneck layer
x = BatchNormalization(gamma_regularizer=l2(0.0005), beta_regularizer=l2(0.0005))(x)
x = ReLU()(x)
x = Conv2D(n_filters, (3, 3), strides=(1, 1), padding="same",
use_bias=False, kernel_regularizer=l2(0.0005))(x)
# Dimensionality restoration - increase the number of output filters by 2X or 4X
x = BatchNormalization(gamma_regularizer=l2(0.0005), beta_regularizer=l2(0.0005),
gamma_initializer="zeros")(x)
x = ReLU()(x)
x = Conv2D(n_filters * n, (1, 1), strides=(1, 1),
use_bias=False, kernel_regularizer=l2(0.0005))(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(0.0005))(x)
## Construct the 1x1, 3x3, 1x1 convolution block
# Dimensionality reduction
x = BatchNormalization(gamma_regularizer=l2(0.0005), beta_regularizer=l2(0.0005))(x)
x = ReLU()(x)
x = Conv2D(n_filters, (1, 1), strides=(1,1), use_bias=False, kernel_regularizer=l2(0.0005))(x)
# Bottleneck layer - feature pooling when strides=(2, 2)
x = BatchNormalization(gamma_regularizer=l2(0.0005), beta_regularizer=l2(0.0005))(x)
x = ReLU()(x)
x = Conv2D(n_filters, (3, 3), strides=strides, padding='same',
use_bias=False, kernel_regularizer=l2(0.0005))(x)
# Dimensionality restoration - increase the number of filters by 2X (or 4X)
x = BatchNormalization(gamma_regularizer=l2(0.0005), beta_regularizer=l2(0.0005))(x)
x = ReLU()(x)
x = Conv2D(n_filters * n, (1, 1), strides=(1, 1), use_bias=False, kernel_regularizer=l2(0.0005))(x)
# Add the projection shortcut to the output of the residual block
x = Add()([shortcut, x])
return x
def classifier(x, n_classes):
''' Construct a Classifier
x : input into the classifier
n_classes : number of classes
'''
# Pool the feature maps after the end of all the residual blocks
x = BatchNormalization()(x)
x = ReLU()(x)
x = AveragePooling2D(pool_size=8)(x)
# Flatten into 1D vector
x = Flatten()(x)
# Final Dense Outputting Layer
outputs = Dense(n_classes, activation='softmax', kernel_initializer='he_normal')(x)
return outputs
#-------------------
# Model | n |
# ResNet20 | 2 |
# ResNet56 | 6 |
# ResNet110 | 12 |
# ResNet164 | 18 |
# ResNet1001 | 111 |
#
n = 18
depth = n * 9 + 2
n_blocks = ((depth - 2) // 9) - 1
# The input tensor
inputs = Input(shape=(32, 32, 3))
# The Stem Convolution Group
x = stem(inputs)
# The learner
x = learner(x, n_blocks)
# The Classifier for 10 classes
outputs = classifier(x, 10)
# Instantiate the Model
model = Model(inputs, outputs)