-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresnet.py
60 lines (45 loc) · 1.99 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
import numpy as np
import tensorflow as tf
def weight_variable(shape, name=None):
initial = tf.truncated_normal(shape, mean=0.0, stddev=0.01)
return tf.Variable(initial, name=name)
def weight_variable2(shape, name=None):
initial = tf.truncated_normal(shape, mean=0.0, stddev=0.01)
return tf.Variable(initial, name=name)
def softmax_layer(inpt, shape):
fc_w = weight_variable2(shape)
fc_b = tf.Variable(tf.zeros([shape[1]]))
fc_h = tf.nn.softmax(tf.matmul(inpt, fc_w) + fc_b)
return fc_h
def conv_layer(inpt, filter_shape, stride):
out_channels = filter_shape[3]
filter_ = weight_variable2(filter_shape)
b=tf.Variable(tf.zeros([out_channels])+0.1)
print "Conv Filter size=" + str(out_channels)
conv = tf.nn.bias_add(tf.nn.conv2d(inpt, filter=filter_, strides=[1, stride, stride, 1], padding="SAME"), b)
mean, var = tf.nn.moments(conv, axes=[0,1,2])
beta = tf.Variable(tf.zeros([out_channels]), name="beta")
gamma = weight_variable([out_channels], name="gamma")
batch_norm = tf.nn.batch_norm_with_global_normalization(
conv, mean, var, beta, gamma, 0.001,
scale_after_normalization=True)
out = tf.nn.relu(batch_norm)
return out
def residual_block(inpt, output_depth, down_sample, projection=False):
input_depth = inpt.get_shape().as_list()[3]
if down_sample:
filter_ = [1,2,2,1]
inpt = tf.nn.max_pool(inpt, ksize=filter_, strides=filter_, padding='SAME')
conv1 = conv_layer(inpt, [3, 3, input_depth, output_depth], 1)
conv2 = conv_layer(conv1, [3, 3, output_depth, output_depth], 1)
if input_depth != output_depth:
if projection:
# Option B: Projection shortcut
input_layer = conv_layer(inpt, [1, 1, input_depth, output_depth], 2)
else:
# Option A: Zero-padding
input_layer = tf.pad(inpt, [[0,0], [0,0], [0,0], [0, output_depth - input_depth]])
else:
input_layer = inpt
res = conv2 + input_layer
return res