-
Notifications
You must be signed in to change notification settings - Fork 11
/
resnet.py
163 lines (108 loc) · 5.52 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
# from https://github.com/taki0112/ResNet-Tensorflow
import tensorflow as tf
import tensorflow.contrib as tf_contrib
tf.random.set_random_seed(0)
# Xavier : tf_contrib.layers.xavier_initializer()
# He : tf_contrib.layers.variance_scaling_initializer()
# Normal : tf.random_normal_initializer(mean=0.0, stddev=0.02)
# l2_decay : tf_contrib.layers.l2_regularizer(0.0001)
weight_init = tf_contrib.layers.variance_scaling_initializer()
weight_regularizer = tf_contrib.layers.l2_regularizer(0.0001)
##################################################################################
# Layer
##################################################################################
def conv(x, channels, kernel=4, stride=2, padding='SAME', use_bias=True, scope='conv_0'):
with tf.variable_scope(scope):
x = tf.layers.conv2d(inputs=x, filters=channels,
kernel_size=kernel, kernel_initializer=weight_init,
kernel_regularizer=weight_regularizer,
strides=stride, use_bias=use_bias, padding=padding)
return x
def fully_conneted(x, units, use_bias=True, scope='fully_0'):
with tf.variable_scope(scope):
x = flatten(x)
x = tf.layers.dense(x, units=units, kernel_initializer=weight_init, kernel_regularizer=weight_regularizer, use_bias=use_bias)
return x
def resblock(x_init, channels, is_training=True, use_bias=True, downsample=False, scope='resblock') :
with tf.variable_scope(scope) :
x = batch_norm(x_init, is_training, scope='batch_norm_0')
x = relu(x)
if downsample :
x = conv(x, channels, kernel=3, stride=2, use_bias=use_bias, scope='conv_0')
x_init = conv(x_init, channels, kernel=1, stride=2, use_bias=use_bias, scope='conv_init')
else :
x = conv(x, channels, kernel=3, stride=1, use_bias=use_bias, scope='conv_0')
x = batch_norm(x, is_training, scope='batch_norm_1')
x = relu(x)
x = conv(x, channels, kernel=3, stride=1, use_bias=use_bias, scope='conv_1')
return x + x_init
def non_resblock(x_init, channels, is_training=True, use_bias=True, downsample=False, scope='non_resblock') :
with tf.variable_scope(scope) :
x = batch_norm(x_init, is_training, scope='batch_norm_0')
x = relu(x)
if downsample :
x = conv(x, channels, kernel=3, stride=2, use_bias=use_bias, scope='conv_0')
else :
x = conv(x, channels, kernel=3, stride=1, use_bias=use_bias, scope='conv_0')
return x
def bottle_resblock(x_init, channels, is_training=True, use_bias=True, downsample=False, scope='bottle_resblock') :
with tf.variable_scope(scope) :
x = batch_norm(x_init, is_training, scope='batch_norm_1x1_front')
shortcut = relu(x)
x = conv(shortcut, channels, kernel=1, stride=1, use_bias=use_bias, scope='conv_1x1_front')
x = batch_norm(x, is_training, scope='batch_norm_3x3')
x = relu(x)
if downsample :
x = conv(x, channels, kernel=3, stride=2, use_bias=use_bias, scope='conv_0')
shortcut = conv(shortcut, channels*4, kernel=1, stride=2, use_bias=use_bias, scope='conv_init')
else :
x = conv(x, channels, kernel=3, stride=1, use_bias=use_bias, scope='conv_0')
shortcut = conv(shortcut, channels * 4, kernel=1, stride=1, use_bias=use_bias, scope='conv_init')
x = batch_norm(x, is_training, scope='batch_norm_1x1_back')
x = relu(x)
x = conv(x, channels*4, kernel=1, stride=1, use_bias=use_bias, scope='conv_1x1_back')
return x + shortcut
def get_residual_layer(res_n) :
x = []
if res_n == 18 :
x = [2, 2, 2, 2]
if res_n == 34 :
x = [3, 4, 6, 3]
if res_n == 50 :
x = [3, 4, 6, 3]
if res_n == 101 :
x = [3, 4, 23, 3]
if res_n == 152 :
x = [3, 8, 36, 3]
return x
##################################################################################
# Sampling
##################################################################################
def flatten(x) :
return tf.layers.flatten(x)
def global_avg_pooling(x):
gap = tf.reduce_mean(x, axis=[1, 2], keepdims=True)
return gap
def avg_pooling(x) :
return tf.layers.average_pooling2d(x, pool_size=2, strides=2, padding='SAME')
##################################################################################
# Activation function
##################################################################################
def relu(x):
return tf.nn.relu(x)
##################################################################################
# Normalization function
##################################################################################
def batch_norm(x, is_training=True, scope='batch_norm'):
return tf_contrib.layers.batch_norm(x,
decay=0.9, epsilon=1e-05,
center=True, scale=True, updates_collections=None,
is_training=is_training, scope=scope)
##################################################################################
# Loss function
##################################################################################
def classification_loss(logit, label) :
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=label, logits=logit))
prediction = tf.equal(tf.argmax(logit, -1), tf.argmax(label, -1))
accuracy = tf.reduce_mean(tf.cast(prediction, tf.float32))
return loss, accuracy