-
Notifications
You must be signed in to change notification settings - Fork 0
/
resnet.py
243 lines (152 loc) · 8.44 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
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
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 26 15:44:12 2017
@author: Florian
Deep residual networks to classify MNIST dataset
"""
# to be included in all python tensorflow files
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
def conv_2D(x, w, b, stride=1, padding='SAME', activation=None):
'''
2D convolution
x: tensor of shape (batch, height, width, channel) ->
w: tensor of shape (f_width, f_height, channels_in, channels_out) -> weights
b: tensor of shape (channels_out) -> biases
'''
# convolution
x = tf.nn.conv2d(x, w, strides=[1, stride, stride, 1], padding=padding)
# add biases
x = tf.nn.bias_add(x, b)
if activation is not None:
x = activation(x)
return x
def print_tensor_shape(x, msg=''):
print(msg, x.get_shape().as_list())
class RepBlock(object):
def __init__(self, num_repeats, num_filters, bottleneck_size, name_scope):
self.num_repeats = num_repeats
self.num_filters = num_filters
self.bottleneck_size = bottleneck_size
self.name_scope = name_scope
def apply_block(self, net):
print_tensor_shape(net, 'entering apply_block')
# loop over repeats
for i_repeat in range(self.num_repeats):
print_tensor_shape(net, 'layer %i' % i_repeat)
# subsampling is performed by a convolution with stride=2, only
# for the first convolution of the first repetition
if i_repeat == 0:
stride = 2
else:
stride = 1
name = self.name_scope+'/%i/conv_in' % i_repeat
with tf.variable_scope(name):
w = tf.get_variable('w', initializer=tf.truncated_normal([1, 1, net.get_shape().as_list()[-1], self.bottleneck_size], stddev=0.1))
b = tf.get_variable('b', initializer=tf.truncated_normal([self.bottleneck_size], stddev=0.1))
conv = conv_2D(net, w, b, stride=stride, padding='VALID', activation=tf.nn.relu)
print_tensor_shape(conv, name)
name = self.name_scope+'/%i/conv_bottleneck' % i_repeat
with tf.variable_scope(name):
w = tf.get_variable('w', initializer=tf.truncated_normal([3, 3, conv.get_shape().as_list()[-1], self.bottleneck_size], stddev=0.1))
b = tf.get_variable('b', initializer=tf.truncated_normal([self.bottleneck_size], stddev=0.1))
conv = conv_2D(conv, w, b, stride=1, padding='SAME', activation=tf.nn.relu)
print_tensor_shape(conv, name)
name = self.name_scope+'/%i/conv_out' % i_repeat
with tf.variable_scope(name):
w = tf.get_variable('w', initializer=tf.truncated_normal([1, 1, conv.get_shape().as_list()[-1], self.num_filters], stddev=0.1))
b = tf.get_variable('b', initializer=tf.truncated_normal([self.num_filters], stddev=0.1))
conv = conv_2D(conv, w, b, stride=1, padding='VALID', activation=None)
print_tensor_shape(conv, name)
if i_repeat == 0:
net = conv + tf.nn.max_pool(net, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
else:
net = conv + net
net = tf.nn.relu(net)
return net
def resnet(x):
# reshape input
x = tf.reshape(x, shape=[-1, 28, 28, 1])
# init block for each layer
layer_1 = RepBlock(num_repeats=3, num_filters=128, bottleneck_size=32, name_scope='layer_1')
layer_2 = RepBlock(num_repeats=3, num_filters=256, bottleneck_size=64, name_scope='layer_2')
# layer_3 = RepBlock(num_repeats=3, num_filters=512, bottleneck_size=128, name_scope='layer_3')
# layer_4 = RepBlock(num_repeats=3, num_filters=1024, bottleneck_size=256, name_scope='layer_4')
layers = [layer_1, layer_2]
# first layer
name = 'conv_1'
with tf.variable_scope(name):
w = tf.get_variable('w', initializer=tf.truncated_normal([7, 7, x.get_shape().as_list()[-1], 64], stddev=0.1))
b = tf.get_variable('b', initializer=tf.truncated_normal([64], stddev=0.1))
net = conv_2D(x, w, b, stride=1, padding='SAME', activation=tf.nn.relu)
print_tensor_shape(net)
net = tf.nn.max_pool(
net, [1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME')
print_tensor_shape(net)
with tf.variable_scope('conv_2'):
w = tf.get_variable('w', initializer=tf.truncated_normal([1, 1, net.get_shape().as_list()[-1], layers[0].num_filters], stddev=0.1))
b = tf.get_variable('b', initializer=tf.truncated_normal([layers[0].num_filters], stddev=0.1))
net = conv_2D(net, w, b, stride=1, padding='SAME', activation=tf.nn.relu)
print_tensor_shape(net)
for i_layer, layer in enumerate(layers):
# pass the net through all blocks of the layer
net = layer.apply_block(net)
print_tensor_shape(net, 'After block')
try:
# upscale (depth) to the next block size
next_block = layers[i_layer+1]
with tf.variable_scope('upscale_%i' % i_layer):
w = tf.get_variable('w', initializer=tf.truncated_normal([1, 1, net.get_shape().as_list()[-1], next_block.num_filters], stddev=0.1))
b = tf.get_variable('b', initializer=tf.truncated_normal([next_block.num_filters], stddev=0.1))
net = conv_2D(net, w, b, stride=1, padding='SAME', activation=tf.nn.relu)
print_tensor_shape(net)
except IndexError:
pass
# apply average pooling
net = tf.nn.avg_pool(net, ksize=[1, net.get_shape().as_list()[1], net.get_shape().as_list()[2], 1],
strides=[1, 1, 1, 1], padding='VALID')
print_tensor_shape(net, msg='after average pooling')
# fully connected layer
with tf.variable_scope('fc'):
w = tf.get_variable('w', initializer=tf.truncated_normal([256, 10], stddev=0.1))
b = tf.get_variable('b', initializer=tf.truncated_normal([10], stddev=0.1))
net = tf.reshape(net, shape=[-1, 256])
net = tf.add(tf.matmul(net, w), b)
print_tensor_shape(net, 'after fc')
return net
if __name__ == '__main__':
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
X = tf.placeholder(tf.float32, [None, 784])
Y = tf.placeholder(tf.float32, [None, 10])
Y_pred = resnet(X)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Y_pred, labels=Y))
optim = tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost)
correct_pred = tf.equal(tf.argmax(Y_pred, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
session = tf.InteractiveSession()
init_op = tf.initialize_all_variables()
session.run(init_op)
nb_epochs = 2
batch_size = 128
training_size = mnist.train.num_examples
nb_mini_batches = training_size // batch_size
cumul_acc = 0
# loop over epochs
for i_epoch in range(nb_epochs):
# loop over mini-batches
for i_batch in range(nb_mini_batches):
# get mini-batch
batch_x, batch_y = mnist.train.next_batch(batch_size)
[_, cost_val, acc] = session.run([optim, cost, accuracy], feed_dict={X: batch_x, Y:batch_y})
cumul_acc += acc
if i_batch % 10 == 0:
print('epoch %i - batch %i - cost=%f - cumul_accuracy=%f' % (i_epoch, i_batch, cost_val, cumul_acc/10))
cumul_acc = 0
# test set
acc = session.run(accuracy, feed_dict={X: mnist.test.images[:1024], Y: mnist.test.labels[:1024]})
print('Accuracy on test set: %f' % acc)