-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathResNet.py
180 lines (157 loc) · 8.12 KB
/
ResNet.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
<<<<<<< HEAD
'''Implements ResNet9,..56 dynamically for CIFAR-10
Description of implementation can be found here: https://arxiv.org/pdf/1512.03385.pdf'''
import tensorflow as
class ResNetBlock(tf.keras.layers.Layer):
'''See official RStudio/Keras documentation here:
https://github.com/rstudio/keras/blob/main/vignettes/examples/cifar10_resnet.py
for implemetation of residual block layers
Implements residual block described for CIFAR 10 in
He et al. (2016): https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/He_Deep_Residual_Learning_CVPR_2016_paper.pdf
'''
def __init__(self, n_filters, kernel_size, stride, l2=5e-4, init_stride=False, first_layer=False):
self.n_filters = n_filters
self.first_layer = first_layer
super(ResNetBlock, self).__init__()
if init_stride:
stride1 = stride + 1
else:
stride1 = stride
self.conv_layer_1 = tf.keras.layers.Conv2D(n_filters, kernel_size, strides=stride1, padding='same',
kernel_regularizer=tf.keras.regularizers.l2(l2),
kernel_initializer='he_normal')
self.conv_layer_2 = tf.keras.layers.Conv2D(n_filters, kernel_size, strides=stride, padding='same',
kernel_regularizer=tf.keras.regularizers.l2(l2),
kernel_initializer='he_normal')
self.bn1 = tf.keras.layers.BatchNormalization()
self.act1 = tf.keras.layers.ReLU()
self.bn2 = tf.keras.layers.BatchNormalization()
self.act2 = tf.keras.layers.ReLU()
self.conv_projection = tf.keras.layers.Conv2D(n_filters, 1, strides=stride1, padding='same',
kernel_regularizer=tf.keras.regularizers.l2(l2),
kernel_initializer='he_normal')
def call(self, inputs):
x = self.conv_layer_1(inputs) # apply without activation since will batch normalize
x = self.bn1(x)
x = self.act1(x) # use ReLU activation as specified by paper
x = self.conv_layer_2(x)
x = self.bn2(x)
if self.first_layer:
inputs = self.conv_projection(inputs)
x = tf.keras.layers.Add()([x, inputs])
x = self.act2(x)
return x
class ResNet56(tf.keras.Model):
def __init__(self, block_depth, base_filters=16, l2=5e-4):
self.block_depth = block_depth
super(ResNet56, self).__init__()
self.conv_1 = tf.keras.layers.Conv2D(base_filters, 3, padding='same')
self.pre_bn = tf.keras.layers.BatchNormalization()
self.stack1 = [ResNetBlock(base_filters, 3, 1, l2=l2) for _ in range(self.block_depth-1)]
self.one_to_two = ResNetBlock(base_filters * 2, 3, 1, init_stride=True, first_layer=True, l2=l2)
self.stack2 = [ResNetBlock(base_filters * 2, 3, 1, l2=l2) for _ in range(self.block_depth - 1)]
self.two_to_three = ResNetBlock(base_filters * 4, 3, 1, init_stride=True, first_layer=True, l2=l2)
self.stack3 = [ResNetBlock(base_filters * 4, 3, 1, l2=l2) for _ in range(self.block_depth - 1)]
self.out_dense = tf.keras.layers.Dense(10, kernel_regularizer=tf.keras.regularizers.l2(l2)) #, activation='softmax')
def call(self, inputs):
x = self.conv_1(inputs)
x = self.pre_bn(x)
x = tf.keras.layers.Activation('relu')(x)
for i in range(self.block_depth-1):
x = self.stack1[i](x)
x = self.one_to_two(x)
for i in range(self.block_depth-1):
x = self.stack2[i](x)
x = self.two_to_three(x)
for i in range(self.block_depth-1):
x = self.stack3[i](x)
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Flatten()(x)
x = self.out_dense(x)
return x
def summary(self):
"""See hack here: https://stackoverflow.com/questions/55235212/model-summary-cant-print-output-shape-while-using-subclass-model
overrides default 'multiple' output shape for debugging, something that is still an open issue on GitHub for TF2.7"""
x = tf.keras.layers.Input(shape=(32,32,3))
m = tf.keras.Model(inputs=x, outputs=self.call(x))
return m.summary()
mod = ResNet56(3, 16)
mod.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True))
=======
'''Implements ResNet9,..56 dynamically for CIFAR-10
Description of implementation can be found here: https://arxiv.org/pdf/1512.03385.pdf'''
import tensorflow as tf
class ResNetBlock(tf.keras.layers.Layer):
'''See official RStudio/Keras documentation here:
https://github.com/rstudio/keras/blob/main/vignettes/examples/cifar10_resnet.py
for implemetation of residual block layers
Implements residual block described for CIFAR 10 in
He et al. (2016): https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/He_Deep_Residual_Learning_CVPR_2016_paper.pdf
'''
def __init__(self, n_filters, kernel_size, stride, init_stride=False, first_layer=False):
self.n_filters = n_filters
self.first_layer = first_layer
super(ResNetBlock, self).__init__()
if init_stride:
stride1 = stride + 1
else:
stride1 = stride
self.conv_layer_1 = tf.keras.layers.Conv2D(n_filters, kernel_size, strides=stride1, padding='same',
kernel_regularizer=tf.keras.regularizers.l2(1e-4),
kernel_initializer='he_normal')
self.conv_layer_2 = tf.keras.layers.Conv2D(n_filters, kernel_size, strides=stride, padding='same',
kernel_regularizer=tf.keras.regularizers.l2(1e-4),
kernel_initializer='he_normal')
self.bn1 = tf.keras.layers.BatchNormalization()
self.act1 = tf.keras.layers.ReLU()
self.bn2 = tf.keras.layers.BatchNormalization()
self.act2 = tf.keras.layers.ReLU()
self.conv_projection = tf.keras.layers.Conv2D(n_filters, 1, strides=stride1, padding='same',
#kernel_regularizer=tf.keras.regularizers.l2(1e-3),
kernel_initializer='he_normal')
def call(self, inputs):
x = self.conv_layer_1(inputs) # apply without activation since will batch normalize
x = self.bn1(x)
x = self.act1(x) # use ReLU activation as specified by paper
x = self.conv_layer_2(x)
x = self.bn2(x)
if self.first_layer:
inputs = self.conv_projection(inputs)
x = tf.keras.layers.Add()([x, inputs])
x = self.act2(x)
return x
class ResNet56(tf.keras.Model):
def __init__(self, block_depth, base_filters=16):
self.block_depth = block_depth
super(ResNet56, self).__init__()
self.conv_1 = tf.keras.layers.Conv2D(base_filters, 3, padding='same')
self.pre_bn = tf.keras.layers.BatchNormalization()
self.stack1 = [ResNetBlock(base_filters, 3, 1) for _ in range(self.block_depth-1)]
self.one_to_two = ResNetBlock(base_filters * 2, 3, 1, init_stride=True, first_layer=True)
self.stack2 = [ResNetBlock(base_filters * 2, 3, 1) for _ in range(self.block_depth - 1)]
self.two_to_three = ResNetBlock(base_filters * 4, 3, 1, init_stride=True, first_layer=True)
self.stack3 = [ResNetBlock(base_filters * 4, 3, 1) for _ in range(self.block_depth - 1)]
self.out_dense = tf.keras.layers.Dense(10, activation='softmax')
def call(self, inputs):
x = self.conv_1(inputs)
x = self.pre_bn(x)
x = tf.keras.layers.Activation('relu')(x)
for i in range(self.block_depth-1):
x = self.stack1[i](x)
x = self.one_to_two(x)
for i in range(self.block_depth-1):
x = self.stack2[i](x)
x = self.two_to_three(x)
for i in range(self.block_depth-1):
x = self.stack3[i](x)
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Flatten()(x)
x = self.out_dense(x)
return x
def summary(self):
"""See hack here: https://stackoverflow.com/questions/55235212/model-summary-cant-print-output-shape-while-using-subclass-model
overrides default 'multiple' output shape for debugging, something that is still an open issue on GitHub for TF2.7"""
x = tf.keras.layers.Input(shape=(32,32,3))
m = tf.keras.Model(inputs=x, outputs=self.call(x))
return m.summary()
>>>>>>> 5c65073e7e9b9d1e712f2f35af09fbe7b3ffc696