-
Notifications
You must be signed in to change notification settings - Fork 10
/
resnet.py
212 lines (155 loc) · 9.58 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
from __future__ import division, print_function, absolute_import
import logging
import tensorflow as tf
from src.conv_net import ops
logging.basicConfig(level=logging.DEBUG, filename="logfile.log", filemode="w",
format="%(asctime)-15s %(levelname)-8s %(message)s")
# Courtesy #
# https://github.com/dalgu90/resnet-18-tensorflow
def conv_1(conf, X, filters, scope_name):
f = filters
with tf.variable_scope(scope_name):
X = ops.conv_layer(conf, X, k_shape=[7, 7, 3, f], stride=2, padding='SAME', w_init='tn', scope_name='conv_1',
add_smry=False)
X = ops.batch_norm(conf, X, scope_name='bn_1')
X = ops.activation(X, "relu")
X = tf.layers.max_pooling2d(X, pool_size=3, padding='SAME', strides=2)
logging.info('%s : conv_1 shape: %s', str(scope_name), str(X.shape))
return X
def residual_block(conf, X, filters, block_num, dropout, scope_name):
f0 = X.get_shape().as_list()[-1]
f1, f2 = filters
X_shortcut = X
with tf.variable_scope(scope_name):
X = ops.conv_layer(conf, X, k_shape=[3, 3, f0, f1], stride=1, padding='SAME', w_init='tn', scope_name='conv_1',
add_smry=False)
X = ops.batch_norm(conf, X, scope_name='bn_1')
X = ops.activation(X, "relu")
logging.info('%s : conv_1 shape: %s', str(scope_name), str(X.shape))
if dropout is not None:
logging.info('%s : dropout = %s shape: %s', str(scope_name), str(dropout), str(X.shape))
X = tf.nn.dropout(X, dropout)
X = ops.conv_layer(conf, X, k_shape=[3, 3, f1, f2], stride=1, padding='SAME', w_init='tn', scope_name='conv_2',
add_smry=False)
X = ops.batch_norm(conf, X, scope_name='bn_2')
logging.info('%s : conv_2 shape: %s', str(scope_name), str(X.shape))
# Add skip connection
X = X + X_shortcut
X = ops.activation(X, 'relu')
logging.info('%s : Skip add shape: %s', str(scope_name), str(X.shape))
return X
def residual_block_first(conf, X, filters, block_num, dropout, scope_name):
'''
Why need this? Normally we have skip connections between 2 layers in one residual block.
When going from 1 residual block to another we decrease in the image size, In-order to maintain skip connection
between the layers, we need to have the same dimension for input and output.
'''
f0 = X.get_shape().as_list()[-1]
f1, f2 = filters
with tf.variable_scope(scope_name):
# We perform a 1x1 conv increasing the num_out channels to equal the number of dimensions. We also perform
# down sampling of convolutional layer by using a stride of 2
X_shortcut = ops.conv_layer(conf, X, [1, 1, f0, f1], stride=2, padding='SAME', w_init='tn', scope_name='X_Shortcut',
add_smry=False)
logging.info('%s : conv_shortcut shape: %s', str(scope_name), str(X_shortcut.shape))
X = ops.conv_layer(conf, X, [3, 3, f0, f1], stride=2, padding='SAME', w_init='tn', scope_name='conv_1',
add_smry=False)
X = ops.batch_norm(conf, X, scope_name='bn_1')
X = ops.activation(X, 'relu', scope_name='relu_1')
logging.info('%s : conv_1 shape: %s', str(scope_name), str(X.shape))
#if dropout is not None:
# logging.info('%s : dropout = %s shape: %s', str(scope_name), str(dropout), str(X.shape))
# X = tf.nn.dropout(X, dropout)
X = ops.conv_layer(conf, X, [3, 3, f1, f2], stride=1, padding='SAME', w_init='tn', scope_name='conv_2',
add_smry=False)
X = ops.batch_norm(conf, X, scope_name='bn_2')
logging.info('%s : conv_2 shape: %s', str(scope_name), str(X.shape))
X = X + X_shortcut
X = ops.activation(X, 'relu', scope_name='relu_2')
logging.info('%s : Skip add shape: %s', str(scope_name), str(X.shape))
return X
def embeddings(conf, inpX, use_dropout):
filters = [64, 64, 128, 256, 512]
if use_dropout:
dropout_prob = [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
else:
dropout_prob = [None, None, None, None, None, None, None, None]
# Convolution Layer
X = conv_1(conf, inpX, filters[0], scope_name='conv_layer')
logging.info('conv_layer : conv shape: %s', str(X.get_shape().as_list()))
# Residual Block 1,2
X = residual_block(conf, X, [filters[1], filters[1]], block_num=1, dropout=dropout_prob[0],
scope_name='residual_block_1_1')
X = residual_block(conf, X, [filters[1], filters[1]], block_num=2, dropout=dropout_prob[1],
scope_name='residual_block_1_2')
# Residual Block 3,4
X = residual_block_first(conf, X, [filters[2], filters[2]], block_num=3, dropout=dropout_prob[2], scope_name='residual_block_2_1')
X = residual_block(conf, X, [filters[2], filters[2]], block_num=4, dropout=dropout_prob[3],
scope_name='residual_block_2_2')
# Residual block 5,6
X = residual_block_first(conf, X, [filters[3], filters[3]], block_num=5, dropout=dropout_prob[4], scope_name='residual_block_3_1')
X = residual_block(conf, X, [filters[3], filters[3]], block_num=6, dropout=dropout_prob[5],
scope_name='residual_block_3_2')
# Residual block 7,8
X = residual_block_first(conf, X, [filters[4], filters[4]], block_num=7, dropout=dropout_prob[6], scope_name='residual_block_4_1')
X = residual_block(conf, X, [filters[4], filters[4]], block_num=8, dropout=dropout_prob[7],
scope_name='residual_block_4_2')
# Flatten (dropout?)
X = tf.contrib.layers.flatten(X, scope='flatten')
logging.info('X - flattened: %s', str(X.get_shape().as_list()))
# if use_dropout:
# X = tf.nn.dropout(X, 0.7)
# logging.info('Flattened : dropout = %s shape: %s', str(0.7), str(X.shape))
# FC-Layer : Get a good 512 encoding to build ensemble
embeddings = ops.fc_layers(conf, X, [X.get_shape().as_list()[-1], 512], w_init='tn', scope_name='fc_layer1', add_smry=False)
return embeddings
def resnet(conf, img_shape, device_type, use_dropout):
inpX = tf.placeholder(dtype=tf.float32,
shape=[None, img_shape[0], img_shape[1], img_shape[2]],
name='X')
inpY = tf.placeholder(dtype=tf.float32,
shape=[None, conf['myNet']['num_labels']],
name='Y')
with tf.device(device_type):
X_embeddings = embeddings(conf, inpX, use_dropout)
X_embeddings = ops.activation(X_embeddings, 'relu', scope_name='relu_fc')
logging.info('X - FC Layer (RELU): %s', str(X_embeddings.get_shape().as_list()))
# SOFTMAX Layer
X_logits = ops.fc_layers(conf, X_embeddings, [512, 2], w_init='tn', scope_name='fc_layer2', add_smry=False)
logging.info('LOGITS - Softmax Layer: %s', str(X_logits.get_shape().as_list()))
Y_probs = tf.nn.softmax(X_logits)
logging.info('Softmax Y-Prob shape: shape %s', str(Y_probs.shape))
loss = ops.get_loss(y_true=inpY, y_logits=X_logits, which_loss='sigmoid_cross_entropy', lamda=None)
optimizer, l_rate = ops.optimize(conf, loss=loss, learning_rate_decay=True, add_smry=False)
acc = ops.accuracy(labels=inpY, logits=X_logits, type='training', add_smry=False)
return dict(inpX=inpX, inpY=inpY, outProbs=Y_probs, accuracy=acc, loss=loss, optimizer=optimizer, l_rate=l_rate)
def mixture_of_experts(conf, img_shape, device_type, use_dropout):
inpX1 = tf.placeholder(dtype=tf.float32,
shape=[None, img_shape[0], img_shape[1], img_shape[2]],
name='expert1')
inpX2 = tf.placeholder(dtype=tf.float32,
shape=[None, img_shape[0], img_shape[1], img_shape[2]],
name='expert2')
inpY = tf.placeholder(dtype=tf.float32,
shape=[None, conf['myNet']['num_labels']],
name='Y')
with tf.device(device_type):
logging.info('Expert 1: Creating Computation graph for Expert 1 ............... ')
with tf.variable_scope('Expert1'):
embeddings_m1 = embeddings(inpX1, use_dropout)
logging.info('Expert 2: Creating Computation graph for Expert 2 ............... ')
with tf.variable_scope('Expert2'):
embeddings_m2 = embeddings(inpX2, use_dropout)
expert_embeddings = tf.concat(values=[embeddings_m1, embeddings_m2], axis=-1)
expert_embeddings = ops.activation(expert_embeddings, type='sigmoid', scope_name='sigmoid')
logging.info('EMBEDDINGS: Stacked (sigmoid Gate) %s', str(expert_embeddings.get_shape().as_list()))
# SOFTMAX Layer
X_logits = ops.fc_layers(conf, expert_embeddings, [1024, 2], w_init='tn', scope_name='softmax', add_smry=False)
logging.info('LOGITS - Softmax Layer: %s', str(X_logits.get_shape().as_list()))
Y_probs = tf.nn.softmax(X_logits)
logging.info('Softmax Y-Prob shape: shape %s', str(Y_probs.shape))
loss = ops.get_loss(y_true=inpY, y_logits=X_logits, which_loss='sigmoid_cross_entropy', lamda=None)
optimizer, l_rate = ops.optimize(loss=loss, learning_rate_decay=True, add_smry=False)
acc = ops.accuracy(labels=inpY, logits=X_logits, type='training', add_smry=False)
return dict(inpX1=inpX1, inpX2=inpX2, inpY=inpY, outProbs=Y_probs, accuracy=acc, loss=loss,
optimizer=optimizer, l_rate=l_rate)