-
Notifications
You must be signed in to change notification settings - Fork 8
/
densenet161.py
375 lines (292 loc) · 14.7 KB
/
densenet161.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# Copyright 2016 pudae. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Contains the definition of the DenseNet architecture.
As described in https://arxiv.org/abs/1608.06993.
Densely Connected Convolutional Networks
Gao Huang, Zhuang Liu, Kilian Q. Weinberger, Laurens van der Maaten
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import os
slim = tf.contrib.slim
import constants as const
import nets.nn_utils as nn_utils
import utils.os_utils as os_utils
from nets import attention_filter
@slim.add_arg_scope
def _global_avg_pool2d(inputs, data_format='NHWC', scope=None, outputs_collections=None):
with tf.variable_scope(scope, 'xx', [inputs]) as sc:
axis = [1, 2] if data_format == 'NHWC' else [2, 3]
net = tf.reduce_mean(inputs, axis=axis, keep_dims=True)
net = slim.utils.collect_named_outputs(outputs_collections, sc.name, net)
return net
@slim.add_arg_scope
def _conv(inputs, num_filters, kernel_size, stride=1, dropout_rate=None,
scope=None, outputs_collections=None):
with tf.variable_scope(scope, 'xx', [inputs]) as sc:
net = slim.batch_norm(inputs)
net = tf.nn.relu(net)
net = slim.conv2d(net, num_filters, kernel_size)
if dropout_rate:
net = tf.nn.dropout(net)
net = slim.utils.collect_named_outputs(outputs_collections, sc.name, net)
return net
@slim.add_arg_scope
def _conv_block(inputs, num_filters, data_format='NHWC', scope=None, outputs_collections=None):
with tf.variable_scope(scope, 'conv_blockx', [inputs]) as sc:
net = inputs
net = _conv(net, num_filters*4, 1, scope='x1')
net = _conv(net, num_filters, 3, scope='x2')
if data_format == 'NHWC':
net = tf.concat([inputs, net], axis=3)
else: # "NCHW"
net = tf.concat([inputs, net], axis=1)
net = slim.utils.collect_named_outputs(outputs_collections, sc.name, net)
return net
@slim.add_arg_scope
def _dense_block(inputs, num_layers, num_filters, growth_rate,
grow_num_filters=True, scope=None, outputs_collections=None,filter_type=None,verbose=None):
with tf.variable_scope(scope, 'dense_blockx', [inputs]) as sc:
net = inputs
for i in range(num_layers):
branch = i + 1
net = _conv_block(net, growth_rate, scope='conv_block'+str(branch))
end_point = 'conv_block'+str(branch)
net = attention_filter.add_attention_filter(net, end_point,verbose=verbose,filter_type=filter_type)
if grow_num_filters:
num_filters += growth_rate
net = slim.utils.collect_named_outputs(outputs_collections, sc.name, net)
return net, num_filters
@slim.add_arg_scope
def _transition_block(inputs, num_filters, compression=1.0,
scope=None, outputs_collections=None):
num_filters = int(num_filters * compression)
with tf.variable_scope(scope, 'transition_blockx', [inputs]) as sc:
net = inputs
net = _conv(net, num_filters, 1, scope='blk')
net = slim.avg_pool2d(net, 2)
net = slim.utils.collect_named_outputs(outputs_collections, sc.name, net)
return net, num_filters
def densenet(inputs,
num_classes=1000,
reduction=None,
growth_rate=None,
num_filters=None,
num_layers=None,
dropout_rate=None,
data_format='NHWC',
is_training=True,
reuse=None,
filter_type=None,
verbose=None,
scope=None):
assert reduction is not None
assert growth_rate is not None
assert num_filters is not None
assert num_layers is not None
compression = 1.0 - reduction
num_dense_blocks = len(num_layers)
if data_format == 'NCHW':
inputs = tf.transpose(inputs, [0, 3, 1, 2])
with tf.variable_scope(scope, 'densenetxxx', [inputs, num_classes],
reuse=reuse) as sc:
end_points_collection = sc.name + '_end_points'
with slim.arg_scope([slim.batch_norm, slim.dropout],
is_training=is_training), \
slim.arg_scope([slim.conv2d, _conv, _conv_block,
_dense_block, _transition_block],
outputs_collections=end_points_collection), \
slim.arg_scope([_conv], dropout_rate=dropout_rate):
net = inputs
# initial convolution
net = slim.conv2d(net, num_filters, 7, stride=2, scope='conv1')
net = slim.batch_norm(net)
net = tf.nn.relu(net)
net = slim.max_pool2d(net, 3, stride=2, padding='SAME')
# blocks
for i in range(num_dense_blocks - 1):
# dense blocks
net, num_filters = _dense_block(net, num_layers[i], num_filters,
growth_rate,
scope='dense_block' + str(i+1),filter_type=filter_type,verbose=verbose)
# Add transition_block
net, num_filters = _transition_block(net, num_filters,
compression=compression,
scope='transition_block' + str(i+1))
net, num_filters = _dense_block(
net, num_layers[-1], num_filters,
growth_rate,
scope='dense_block' + str(num_dense_blocks),filter_type=filter_type,verbose=verbose)
# final blocks
with tf.variable_scope('final_block', [inputs]):
net = slim.batch_norm(net)
net = tf.nn.relu(net)
net = _global_avg_pool2d(net, scope='global_avg_pool')
net = slim.conv2d(net, num_classes, 1,
biases_initializer=tf.zeros_initializer(),
scope='logits')
end_points = slim.utils.convert_collection_to_dict(
end_points_collection)
if num_classes is not None:
end_points['predictions'] = slim.softmax(net, scope='predictions')
return net, end_points
def densenet121(inputs, num_classes=1000, data_format='NHWC', is_training=True, reuse=None):
return densenet(inputs,
num_classes=num_classes,
reduction=0.5,
growth_rate=32,
num_filters=64,
num_layers=[6,12,24,16],
data_format=data_format,
is_training=is_training,
reuse=reuse,
scope='densenet121')
densenet121.default_image_size = 224
def densenet161(inputs, num_classes=1000, data_format='NHWC', is_training=True, reuse=None):
return densenet(inputs,
num_classes=num_classes,
reduction=0.5,
growth_rate=48,
num_filters=96,
num_layers=[6,12,36,24],
data_format=data_format,
is_training=is_training,
reuse=reuse,
scope='densenet161')
densenet161.default_image_size = 224
def densenet169(inputs, num_classes=1000, data_format='NHWC', is_training=True, reuse=None):
return densenet(inputs,
num_classes=num_classes,
reduction=0.5,
growth_rate=32,
num_filters=64,
num_layers=[6,12,32,32],
data_format=data_format,
is_training=is_training,
reuse=reuse,
scope='densenet169')
densenet169.default_image_size = 224
def densenet_arg_scope(weight_decay=1e-4,
batch_norm_decay=0.999,
batch_norm_epsilon=1e-5,
data_format='NHWC'):
with slim.arg_scope([slim.conv2d, slim.batch_norm, slim.avg_pool2d, slim.max_pool2d,
_conv_block, _global_avg_pool2d],
data_format=data_format):
with slim.arg_scope([slim.conv2d],
weights_regularizer=slim.l2_regularizer(weight_decay),
activation_fn=None,
biases_initializer=None):
with slim.arg_scope([slim.batch_norm],
scale=True,
decay=batch_norm_decay,
epsilon=batch_norm_epsilon) as scope:
return scope
class DenseNet161:
def var_2_train(self):
scopes = [scope.strip() for scope in 'densenet161/logits'.split(',')]
variables_to_train = []
for scope in scopes:
variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope)
variables_to_train.extend(variables)
# print(variables_to_train)
return variables_to_train
def load_model(self,save_model_dir,ckpt_file,sess,saver,load_logits=False):
# Try to initialize the network from a custom model
if (os.path.exists(save_model_dir) and os_utils.chkpt_exists(save_model_dir)):
saver.restore(sess, ckpt_file)
return 'Model weights initialized from {}'.format(ckpt_file)
else: # if custom model is not provided, initialize the network weights using imageNet weights
if(load_logits):
exclusions = [scope.strip() for scope in '**'.split(',')]
else:
exclusions = [scope.strip() for scope in 'global_step,densenet161/logits'.split(',')]
variables_to_restore = []
for var in tf.contrib.slim.get_model_variables():
for exclusion in exclusions:
if var.op.name.startswith(exclusion):
break
else:
variables_to_restore.append(var)
init_fn = tf.contrib.framework.assign_from_checkpoint_fn(self.cfg.imagenet_weights_filepath, variables_to_restore,ignore_missing_vars=False)
init_fn(sess)
return 'Model weights initialized from imageNet'
def __init__(self,cfg, weight_decay=0.0001, data_format='NHWC',reuse=None,
images_ph = None,
lbls_ph = None,
weights_ph=None):
self.cfg = cfg
filter_type = cfg.filter_type
verbose = cfg.print_filter_name
num_classes = cfg.num_classes
batch_size = None
if lbls_ph is not None:
self.gt_lbls = tf.reshape(lbls_ph,[-1,num_classes])
else:
self.gt_lbls = tf.placeholder(tf.int32, shape=(batch_size, num_classes), name='class_lbls')
self.do_augmentation = tf.placeholder(tf.bool, name='do_augmentation')
self.loss_class_weight = tf.placeholder(tf.float32, shape=(num_classes, num_classes), name='weights')
self.input = tf.placeholder(tf.float32, shape=(batch_size, const.max_frame_size, const.max_frame_size,
const.num_channels), name='context_input')
# if is_training:
if images_ph is not None:
self.input = images_ph
_,w,h,c = self.input.shape
aug_imgs = tf.reshape(self.input, [-1, w, h, c])
print('No nnutils Augmentation')
else:
aug_imgs = tf.cond(self.do_augmentation,
lambda: nn_utils.augment(self.input,cfg.preprocess_func, horizontal_flip=True, vertical_flip=False,
rotate=0, crop_probability=0, color_aug_probability=0)
, lambda: nn_utils.center_crop(self.input,cfg.preprocess_func))
# aug_imgs = self.input ## Already augmented
with tf.contrib.slim.arg_scope(densenet_arg_scope(weight_decay=weight_decay, data_format=data_format)):
val_nets, val_end_points = densenet(aug_imgs,
num_classes=num_classes,
reduction=0.5,
growth_rate=48,
num_filters=96,
num_layers=[6, 12, 36, 24],
data_format=data_format,
is_training=False, ## Set is always to false
reuse=None,
filter_type=filter_type,
verbose=verbose,
scope='densenet161')
def cal_metrics(end_points):
gt = tf.argmax(self.gt_lbls, 1)
logits = tf.reshape(end_points['densenet161/logits'], [-1, num_classes])
pre_logits = end_points['densenet161/dense_block4']
center_supervised_cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=self.gt_lbls,
logits=logits,
name='xentropy_center')
loss = tf.reduce_mean(center_supervised_cross_entropy, name='xentropy_mean')
predictions = tf.reshape(end_points['predictions'], [-1, num_classes])
class_prediction = tf.argmax(predictions, 1)
supervised_correct_prediction = tf.equal(gt, class_prediction)
supervised_correct_prediction_cast = tf.cast(supervised_correct_prediction, tf.float32)
accuracy = tf.reduce_mean(supervised_correct_prediction_cast)
confusion_mat = tf.math.confusion_matrix(gt, class_prediction, num_classes=num_classes)
_, accumulated_accuracy = tf.compat.v1.metrics.accuracy(gt, class_prediction)
_, per_class_acc_acc = tf.compat.v1.metrics.mean_per_class_accuracy(gt, class_prediction, num_classes=num_classes)
per_class_acc_acc = tf.reduce_mean(per_class_acc_acc)
class_prediction = tf.nn.softmax(logits)
return loss,pre_logits,accuracy,confusion_mat,accumulated_accuracy,per_class_acc_acc,class_prediction,logits
# self.train_loss,self.train_pre_logits,self.train_accuracy,self.train_confusion_mat,\
# self.train_accumulated_accuracy,self.train_per_class_acc_acc ,self.train_class_prediction,self.train_logits = cal_metrics(train_end_points);
self.val_loss,self.val_pre_logits,self.val_accuracy, self.val_confusion_mat, self.val_accumulated_accuracy \
, self.val_per_class_acc_acc ,self.val_class_prediction,self.val_logits = cal_metrics(val_end_points);