-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimproved_network.py
146 lines (128 loc) · 3.97 KB
/
improved_network.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
import tensorflow as tf
FLAGS = tf.app.flags.FLAGS
'''
An alternative architecture, with a different combination of sub sampling layers
'''
def deepnn_v2(x_image, output=43):
activation = tf.nn.relu
if FLAGS.crelu:
activation = tf.nn.crelu
weight_decay = tf.contrib.layers.l2_regularizer(scale=0.0001)
kernel_initialiser = tf.random_uniform_initializer(-0.05, 0.05)
# First convolutional layer - maps one RGB image to 32 feature maps.
conv1 = tf.layers.conv2d(
inputs=x_image,
filters=32,
kernel_initializer=kernel_initialiser,
kernel_size=[5, 5],
padding='same',
use_bias=False,
kernel_regularizer=weight_decay,
name='conv1',
activation=activation,
)
conv1_bn = conv1
if FLAGS.norm_layer:
conv1_bn = tf.layers.batch_normalization(conv1)
# conv1_bn_pad = tf.pad(conv1_bn, padding_pooling, "CONSTANT")
pool1 = tf.layers.average_pooling2d(
inputs=conv1_bn,
pool_size=[3, 3],
strides=2,
padding='same',
name='pool1'
)
conv2 = tf.layers.conv2d(
inputs=pool1,
filters=32,
kernel_initializer=kernel_initialiser,
kernel_size=[5, 5],
padding='same',
activation=activation,
use_bias=False,
kernel_regularizer=weight_decay,
name='conv2'
)
conv2_bn = conv2
if FLAGS.norm_layer:
conv2_bn = tf.layers.batch_normalization(conv2)
pool2 = tf.layers.max_pooling2d(
inputs=conv2_bn,
pool_size=[3, 3],
strides=2,
padding='same',
name='pool2'
)
conv3 = tf.layers.conv2d(
inputs=pool2,
kernel_initializer=kernel_initialiser,
filters=64,
kernel_size=[5, 5],
padding='same',
activation=activation,
use_bias=False,
kernel_regularizer=weight_decay,
name='conv3'
)
conv3_bn = conv3
if FLAGS.norm_layer:
conv3_bn = tf.layers.batch_normalization(conv3)
pool3 = tf.layers.max_pooling2d(
inputs=conv3_bn,
pool_size=[3, 3],
strides=2,
padding='same',
name='pool3'
)
conv4 = tf.layers.conv2d(
inputs=pool3,
filters=64,
kernel_initializer=kernel_initialiser,
kernel_size=[4, 4],
padding='same',
activation=activation,
kernel_regularizer=weight_decay,
use_bias=False,
name='conv4'
)
conv4_bn = conv4
if FLAGS.norm_layer:
conv4_bn = tf.layers.batch_normalization(conv4)
pool1_multiscale = tf.layers.max_pooling2d(
inputs=conv4_bn,
pool_size=[3, 3],
strides=1,
padding='same',
name='pool1_multiscale'
)
pool2_multiscale = tf.layers.max_pooling2d(
inputs=conv2_bn,
pool_size=[3, 3],
strides=(4, 2),
padding='same',
name='pool2_multiscale'
)
pool3_multiscale = tf.layers.max_pooling2d(
inputs=conv3_bn,
pool_size=[3, 3],
strides=(2, 2),
padding='same',
name='pool3_multiscale'
)
# Multi-Scale features - fast forward earlier layer results
pool1_flat = tf.contrib.layers.flatten(pool1_multiscale)
pool2_flat = tf.contrib.layers.flatten(pool2_multiscale)
pool3_flat = tf.contrib.layers.flatten(pool3_multiscale)
conv4_flat = tf.contrib.layers.flatten(conv4_bn)
if FLAGS.multi_scale:
full_pool = tf.concat([pool1_flat, pool2_flat, pool3_flat, conv4_flat], axis=1)
else:
full_pool = conv4_flat
full_pool = tf.nn.dropout(full_pool , FLAGS.dropout_keep_rate, seed=FLAGS.seed)
logits = tf.layers.dense(inputs=full_pool,
units=output,
kernel_regularizer=weight_decay,
kernel_initializer=tf.truncated_normal_initializer(mean=0, stddev=0.01, seed=FLAGS.seed),
name='fc1',
)
return logits