-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxception.py
683 lines (634 loc) · 29.3 KB
/
xception.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
# Copyright 2018 The TensorFlow Authors 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.
# ==============================================================================
r"""Xception model.
"Xception: Deep Learning with Depthwise Separable Convolutions"
Fran{\c{c}}ois Chollet
https://arxiv.org/abs/1610.02357
We implement the modified version by Jifeng Dai et al. for their COCO 2017
detection challenge submission, where the model is made deeper and has aligned
features for dense prediction tasks. See their slides for details:
"Deformable Convolutional Networks -- COCO Detection and Segmentation Challenge
2017 Entry"
Haozhi Qi, Zheng Zhang, Bin Xiao, Han Hu, Bowen Cheng, Yichen Wei and Jifeng Dai
ICCV 2017 COCO Challenge workshop
http://presentations.cocodataset.org/COCO17-Detect-MSRA.pdf
We made a few more changes on top of MSRA's modifications:
1. Fully convolutional: All the max-pooling layers are replaced with separable
conv2d with stride = 2. This allows us to use atrous convolution to extract
feature maps at any resolution.
2. We support adding ReLU and BatchNorm after depthwise convolution, motivated
by the design of MobileNetv1.
"MobileNets: Efficient Convolutional Neural Networks for Mobile Vision
Applications"
Andrew G. Howard, Menglong Zhu, Bo Chen, Dmitry Kalenichenko, Weijun Wang,
Tobias Weyand, Marco Andreetto, Hartwig Adam
https://arxiv.org/abs/1704.04861
"""
import collections
import tensorflow as tf
# from tensorflow.contrib import layers as layers_lib
from tensorflow.python.ops import array_ops
slim = tf.contrib.slim
_DEFAULT_MULTI_GRID = [1, 1, 1]
class Block(collections.namedtuple('Block', ['scope', 'unit_fn', 'args'])):
"""A named tuple describing an Xception block.
Its parts are:
scope: The scope of the block.
unit_fn: The Xception unit function which takes as input a tensor and
returns another tensor with the output of the Xception unit.
args: A list of length equal to the number of units in the block. The list
contains one dictionary for each unit in the block to serve as argument to
unit_fn.
"""
def fixed_padding(inputs, kernel_size, rate=1):
"""Pads the input along the spatial dimensions independently of input size.
Args:
inputs: A tensor of size [batch, height_in, width_in, channels].
kernel_size: The kernel to be used in the conv2d or max_pool2d operation.
Should be a positive integer.
rate: An integer, rate for atrous convolution.
Returns:
output: A tensor of size [batch, height_out, width_out, channels] with the
input, either intact (if kernel_size == 1) or padded (if kernel_size > 1).
"""
kernel_size_effective = kernel_size[0] + (kernel_size[0] - 1) * (rate - 1)
pad_total = kernel_size_effective - 1
pad_beg = pad_total // 2
pad_end = pad_total - pad_beg
padded_inputs = tf.pad(inputs, [[0, 0], [pad_beg, pad_end],
[0, 0], [0, 0]])
return padded_inputs
def conv2d_same(inputs, num_outputs, kernel_size, stride, dilation_rate=(1, 1), scope=None):
"""Strided 2-D convolution with 'SAME' padding.
When stride > 1, then we do explicit zero-padding, followed by conv2d with
'VALID' padding.
Note that
net = conv2d_same(inputs, num_outputs, 3, stride=stride)
is equivalent to
net = tf.contrib.layers.conv2d(inputs, num_outputs, 3, stride=1,
padding='SAME')
net = subsample(net, factor=stride)
whereas
net = tf.contrib.layers.conv2d(inputs, num_outputs, 3, stride=stride,
padding='SAME')
is different when the input's height or width is even, which is why we add the
current function. For more details, see ResnetUtilsTest.testConv2DSameEven().
Args:
inputs: A 4-D tensor of size [batch, height_in, width_in, channels].
num_outputs: An integer, the number of output filters.
kernel_size: An int with the kernel_size of the filters.
stride: An integer, the output stride.
rate: An integer, rate for atrous convolution.
scope: Scope.
Returns:
output: A 4-D tensor of size [batch, height_out, width_out, channels] with
the convolution output.
"""
if stride == 1:
return tf.layers.conv2d(
inputs,
num_outputs,
kernel_size,
strides=1,
dilation_rate=dilation_rate,
padding='SAME')
else:
kernel_size_effective = kernel_size[0] + (kernel_size[0] - 1) * (dilation_rate[0] - 1)
pad_total = kernel_size_effective - 1
pad_beg = pad_total // 2
pad_end = pad_total - pad_beg
inputs = array_ops.pad(
inputs, [[0, 0], [pad_beg, pad_end], [0, 0], [0, 0]])
return tf.layers.conv2d(
inputs,
num_outputs,
kernel_size,
strides=stride,
dilation_rate=dilation_rate,
padding='VALID')
@slim.add_arg_scope
def separable_conv2d_same(inputs,
num_outputs,
kernel_size,
depth_multiplier,
stride,
rate=1,
use_explicit_padding=True,
regularize_depthwise=False,
scope=None,
**kwargs):
"""Strided 2-D separable convolution with 'SAME' padding.
If stride > 1 and use_explicit_padding is True, then we do explicit zero-
padding, followed by conv2d with 'VALID' padding.
Note that
net = separable_conv2d_same(inputs, num_outputs, 3,
depth_multiplier=1, stride=stride)
is equivalent to
net = slim.separable_conv2d(inputs, num_outputs, 3,
depth_multiplier=1, stride=1, padding='SAME')
net = resnet_utils.subsample(net, factor=stride)
whereas
net = slim.separable_conv2d(inputs, num_outputs, 3, stride=stride,
depth_multiplier=1, padding='SAME')
is different when the input's height or width is even, which is why we add the
current function.
Consequently, if the input feature map has even height or width, setting
`use_explicit_padding=False` will result in feature misalignment by one pixel
along the corresponding dimension.
Args:
inputs: A 4-D tensor of size [batch, height_in, width_in, channels].
num_outputs: An integer, the number of output filters.
kernel_size: An int with the kernel_size of the filters.
depth_multiplier: The number of depthwise convolution output channels for
each input channel. The total number of depthwise convolution output
channels will be equal to `num_filters_in * depth_multiplier`.
stride: An integer, the output stride.
rate: An integer, rate for atrous convolution.
use_explicit_padding: If True, use explicit padding to make the model fully
compatible with the open source version, otherwise use the native
Tensorflow 'SAME' padding.
regularize_depthwise: Whether or not apply L2-norm regularization on the
depthwise convolution weights.
scope: Scope.
**kwargs: additional keyword arguments to pass to slim.conv2d
Returns:
output: A 4-D tensor of size [batch, height_out, width_out, channels] with
the convolution output.
"""
def _separable_conv2d(padding):
"""Wrapper for separable conv2d."""
return slim.separable_conv2d(inputs,
num_outputs,
kernel_size,
depth_multiplier=depth_multiplier,
stride=stride,
rate=rate,
padding=padding,
scope=scope,
**kwargs)
def _split_separable_conv2d(padding):
"""Splits separable conv2d into depthwise and pointwise conv2d."""
outputs = slim.separable_conv2d(inputs,
None,
kernel_size,
depth_multiplier=depth_multiplier,
stride=stride,
rate=rate,
padding=padding,
scope=scope + '_depthwise',
**kwargs)
return slim.conv2d(outputs,
num_outputs,
1,
scope=scope + '_pointwise',
**kwargs)
if stride == 1 or not use_explicit_padding:
if regularize_depthwise:
outputs = _separable_conv2d(padding='SAME')
else:
outputs = _split_separable_conv2d(padding='SAME')
else:
inputs = fixed_padding(inputs, kernel_size, rate)
if regularize_depthwise:
outputs = _separable_conv2d(padding='VALID')
else:
outputs = _split_separable_conv2d(padding='VALID')
return outputs
@slim.add_arg_scope
def xception_module(inputs,
depth_list,
skip_connection_type,
stride,
unit_rate_list=None,
rate=1,
activation_fn_in_separable_conv=False,
regularize_depthwise=False,
outputs_collections=None,
scope=None):
"""An Xception module.
The output of one Xception module is equal to the sum of `residual` and
`shortcut`, where `residual` is the feature computed by three separable
convolution. The `shortcut` is the feature computed by 1x1 convolution with
or without striding. In some cases, the `shortcut` path could be a simple
identity function or none (i.e, no shortcut).
Note that we replace the max pooling operations in the Xception module with
another separable convolution with striding, since atrous rate is not properly
supported in current TensorFlow max pooling implementation.
Args:
inputs: A tensor of size [batch, height, width, channels].
depth_list: A list of three integers specifying the depth values of one
Xception module.
skip_connection_type: Skip connection type for the residual path. Only
supports 'conv', 'sum', or 'none'.
stride: The block unit's stride. Determines the amount of downsampling of
the units output compared to its input.
unit_rate_list: A list of three integers, determining the unit rate for
each separable convolution in the xception module.
rate: An integer, rate for atrous convolution.
activation_fn_in_separable_conv: Includes activation function in the
separable convolution or not.
regularize_depthwise: Whether or not apply L2-norm regularization on the
depthwise convolution weights.
outputs_collections: Collection to add the Xception unit output.
scope: Optional variable_scope.
Returns:
The Xception module's output.
Raises:
ValueError: If depth_list and unit_rate_list do not contain three elements,
or if stride != 1 for the third separable convolution operation in the
residual path, or unsupported skip connection type.
"""
if len(depth_list) != 3:
raise ValueError('Expect three elements in depth_list.')
if unit_rate_list:
if len(unit_rate_list) != 3:
raise ValueError('Expect three elements in unit_rate_list.')
with tf.variable_scope(scope, 'xception_module', [inputs]) as sc:
residual = inputs
def _separable_conv(features, depth, kernel_size, depth_multiplier,
regularize_depthwise, rate, stride, scope):
if activation_fn_in_separable_conv:
activation_fn = tf.nn.relu
else:
activation_fn = None
features = tf.nn.relu(features)
return separable_conv2d_same(features,
depth,
kernel_size,
depth_multiplier=depth_multiplier,
stride=stride,
rate=rate,
activation_fn=activation_fn,
regularize_depthwise=regularize_depthwise,
scope=scope)
for i in range(3):
residual = _separable_conv(residual,
depth_list[i],
kernel_size=[3, 1],
depth_multiplier=1,
regularize_depthwise=regularize_depthwise,
rate=rate*unit_rate_list[i],
stride=stride if i == 2 else 1,
scope='separable_conv' + str(i+1))
if skip_connection_type == 'conv':
shortcut = slim.conv2d(inputs,
depth_list[-1],
[1, 1],
stride=stride,
activation_fn=None,
scope='shortcut')
outputs = residual + shortcut
elif skip_connection_type == 'sum':
outputs = residual + inputs
elif skip_connection_type == 'none':
outputs = residual
else:
raise ValueError('Unsupported skip connection type.')
return slim.utils.collect_named_outputs(outputs_collections,
sc.name,
outputs)
@slim.add_arg_scope
def stack_blocks_dense(net,
blocks,
output_stride=None,
outputs_collections=None):
"""Stacks Xception blocks and controls output feature density.
First, this function creates scopes for the Xception in the form of
'block_name/unit_1', 'block_name/unit_2', etc.
Second, this function allows the user to explicitly control the output
stride, which is the ratio of the input to output spatial resolution. This
is useful for dense prediction tasks such as semantic segmentation or
object detection.
Control of the output feature density is implemented by atrous convolution.
Args:
net: A tensor of size [batch, height, width, channels].
blocks: A list of length equal to the number of Xception blocks. Each
element is an Xception Block object describing the units in the block.
output_stride: If None, then the output will be computed at the nominal
network stride. If output_stride is not None, it specifies the requested
ratio of input to output spatial resolution, which needs to be equal to
the product of unit strides from the start up to some level of Xception.
For example, if the Xception employs units with strides 1, 2, 1, 3, 4, 1,
then valid values for the output_stride are 1, 2, 6, 24 or None (which
is equivalent to output_stride=24).
outputs_collections: Collection to add the Xception block outputs.
Returns:
net: Output tensor with stride equal to the specified output_stride.
Raises:
ValueError: If the target output_stride is not valid.
"""
# The current_stride variable keeps track of the effective stride of the
# activations. This allows us to invoke atrous convolution whenever applying
# the next residual unit would result in the activations having stride larger
# than the target output_stride.
current_stride = 1
# The atrous convolution rate parameter.
rate = 1
for block in blocks:
with tf.variable_scope(block.scope, 'block', [net]) as sc:
for i, unit in enumerate(block.args):
if output_stride is not None and current_stride > output_stride:
raise ValueError('The target output_stride cannot be reached.')
with tf.variable_scope('unit_%d' % (i + 1), values=[net]):
# If we have reached the target output_stride, then we need to employ
# atrous convolution with stride=1 and multiply the atrous rate by the
# current unit's stride for use in subsequent layers.
if output_stride is not None and current_stride == output_stride:
net = block.unit_fn(net, rate=rate, **dict(unit, stride=1))
rate *= unit.get('stride', 1)
else:
net = block.unit_fn(net, rate=1, **unit)
current_stride *= unit.get('stride', 1)
# Collect activations at the block's end before performing subsampling.
net = slim.utils.collect_named_outputs(outputs_collections, sc.name, net)
if output_stride is not None and current_stride != output_stride:
raise ValueError('The target output_stride cannot be reached.')
return net
def xception(inputs,
blocks,
num_classes=None,
is_training=True,
global_pool=True,
keep_prob=0.5,
output_stride=None,
reuse=None,
scope=None):
"""Generator for Xception models.
This function generates a family of Xception models. See the xception_*()
methods for specific model instantiations, obtained by selecting different
block instantiations that produce Xception of various depths.
Args:
inputs: A tensor of size [batch, height_in, width_in, channels]. Must be
floating point. If a pretrained checkpoint is used, pixel values should be
the same as during training (see go/slim-classification-models for
specifics).
blocks: A list of length equal to the number of Xception blocks. Each
element is an Xception Block object describing the units in the block.
num_classes: Number of predicted classes for classification tasks.
If 0 or None, we return the features before the logit layer.
is_training: whether batch_norm layers are in training mode.
global_pool: If True, we perform global average pooling before computing the
logits. Set to True for image classification, False for dense prediction.
keep_prob: Keep probability used in the pre-logits dropout layer.
output_stride: If None, then the output will be computed at the nominal
network stride. If output_stride is not None, it specifies the requested
ratio of input to output spatial resolution.
reuse: whether or not the network and its variables should be reused. To be
able to reuse 'scope' must be given.
scope: Optional variable_scope.
Returns:
net: A rank-4 tensor of size [batch, height_out, width_out, channels_out].
If global_pool is False, then height_out and width_out are reduced by a
factor of output_stride compared to the respective height_in and width_in,
else both height_out and width_out equal one. If num_classes is 0 or None,
then net is the output of the last Xception block, potentially after
global average pooling. If num_classes is a non-zero integer, net contains
the pre-softmax activations.
end_points: A dictionary from components of the network to the corresponding
activation.
Raises:
ValueError: If the target output_stride is not valid.
"""
with tf.variable_scope(
scope, 'xception', [inputs], reuse=reuse) as sc:
end_points_collection = sc.original_name_scope + 'end_points'
with slim.arg_scope([slim.conv2d,
slim.separable_conv2d,
xception_module,
stack_blocks_dense],
outputs_collections=end_points_collection):
with slim.arg_scope([slim.batch_norm], is_training=is_training):
net = inputs
if output_stride is not None:
if output_stride % 2 != 0:
raise ValueError('The output_stride needs to be a multiple of 2.')
output_stride /= 2
# Root block function operated on inputs.
net = conv2d_same(net, 32, [3, 1], stride=2,
scope='entry_flow/conv1_1')
net = conv2d_same(net, 64, [3, 1], stride=1,
scope='entry_flow/conv1_2')
# Extract features for entry_flow, middle_flow, and exit_flow.
net = stack_blocks_dense(net, blocks, output_stride)
# Convert end_points_collection into a dictionary of end_points.
end_points = slim.utils.convert_collection_to_dict(
end_points_collection, clear_collection=True)
if global_pool:
# Global average pooling.
net = tf.reduce_mean(net, [1, 2], name='global_pool', keepdims=True)
end_points['global_pool'] = net
if num_classes:
net = slim.dropout(net, keep_prob=keep_prob, is_training=is_training,
scope='prelogits_dropout')
net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None,
normalizer_fn=None, scope='logits')
end_points[sc.name + '/logits'] = net
end_points['predictions'] = slim.softmax(net, scope='predictions')
return net, end_points
def xception_block(scope,
depth_list,
skip_connection_type,
activation_fn_in_separable_conv,
regularize_depthwise,
num_units,
stride,
unit_rate_list=None):
"""Helper function for creating a Xception block.
Args:
scope: The scope of the block.
depth_list: The depth of the bottleneck layer for each unit.
skip_connection_type: Skip connection type for the residual path. Only
supports 'conv', 'sum', or 'none'.
activation_fn_in_separable_conv: Includes activation function in the
separable convolution or not.
regularize_depthwise: Whether or not apply L2-norm regularization on the
depthwise convolution weights.
num_units: The number of units in the block.
stride: The stride of the block, implemented as a stride in the last unit.
All other units have stride=1.
unit_rate_list: A list of three integers, determining the unit rate in the
corresponding xception block.
Returns:
An Xception block.
"""
if unit_rate_list is None:
unit_rate_list = _DEFAULT_MULTI_GRID
return Block(scope, xception_module, [{
'depth_list': depth_list,
'skip_connection_type': skip_connection_type,
'activation_fn_in_separable_conv': activation_fn_in_separable_conv,
'regularize_depthwise': regularize_depthwise,
'stride': stride,
'unit_rate_list': unit_rate_list,
}] * num_units)
def xception_65(inputs,
num_classes=None,
is_training=True,
global_pool=True,
keep_prob=0.5,
output_stride=None,
regularize_depthwise=False,
multi_grid=None,
reuse=None,
scope='xception_65'):
"""Xception-65 model."""
# blocks = [
# xception_block('entry_flow/block1',
# depth_list=[128, 128, 128],
# skip_connection_type='conv',
# activation_fn_in_separable_conv=False,
# regularize_depthwise=regularize_depthwise,
# num_units=1,
# stride=2),
# xception_block('entry_flow/block2',
# depth_list=[256, 256, 256],
# skip_connection_type='conv',
# activation_fn_in_separable_conv=False,
# regularize_depthwise=regularize_depthwise,
# num_units=1,
# stride=2),
# xception_block('entry_flow/block3',
# depth_list=[728, 728, 728],
# skip_connection_type='conv',
# activation_fn_in_separable_conv=False,
# regularize_depthwise=regularize_depthwise,
# num_units=1,
# stride=2),
# xception_block('middle_flow/block1',
# depth_list=[728, 728, 728],
# skip_connection_type='sum',
# activation_fn_in_separable_conv=False,
# regularize_depthwise=regularize_depthwise,
# num_units=16,
# stride=1),
# xception_block('exit_flow/block1',
# depth_list=[728, 1024, 1024],
# skip_connection_type='conv',
# activation_fn_in_separable_conv=False,
# regularize_depthwise=regularize_depthwise,
# num_units=1,
# stride=2),
# xception_block('exit_flow/block2',
# depth_list=[1536, 1536, 2048],
# skip_connection_type='none',
# activation_fn_in_separable_conv=True,
# regularize_depthwise=regularize_depthwise,
# num_units=1,
# stride=1,
# unit_rate_list=multi_grid),
# ]
blocks = [
xception_block('entry_flow/block1',
depth_list=[64, 64, 64],
skip_connection_type='conv',
activation_fn_in_separable_conv=False,
regularize_depthwise=regularize_depthwise,
num_units=1,
stride=2),
xception_block('entry_flow/block2',
depth_list=[128, 128, 128],
skip_connection_type='conv',
activation_fn_in_separable_conv=False,
regularize_depthwise=regularize_depthwise,
num_units=1,
stride=2),
xception_block('entry_flow/block3',
depth_list=[256, 256, 256],
skip_connection_type='conv',
activation_fn_in_separable_conv=False,
regularize_depthwise=regularize_depthwise,
num_units=1,
stride=2),
xception_block('middle_flow/block1',
depth_list=[256, 256, 256],
skip_connection_type='sum',
activation_fn_in_separable_conv=False,
regularize_depthwise=regularize_depthwise,
num_units=16,
stride=1),
xception_block('exit_flow/block1',
depth_list=[256, 256, 256],
skip_connection_type='conv',
activation_fn_in_separable_conv=False,
regularize_depthwise=regularize_depthwise,
num_units=1,
stride=2),
xception_block('exit_flow/block2',
depth_list=[512, 512, 512],
skip_connection_type='none',
activation_fn_in_separable_conv=True,
regularize_depthwise=regularize_depthwise,
num_units=1,
stride=1,
unit_rate_list=multi_grid),
]
return xception(inputs,
blocks=blocks,
num_classes=num_classes,
is_training=is_training,
global_pool=global_pool,
keep_prob=keep_prob,
output_stride=output_stride,
reuse=reuse,
scope=scope)
def xception_arg_scope(weight_decay=0.00004,
batch_norm_decay=0.9997,
batch_norm_epsilon=0.001,
batch_norm_scale=True,
weights_initializer_stddev=0.09,
activation_fn=tf.nn.relu,
regularize_depthwise=False,
use_batch_norm=True):
"""Defines the default Xception arg scope.
Args:
weight_decay: The weight decay to use for regularizing the model.
batch_norm_decay: The moving average decay when estimating layer activation
statistics in batch normalization.
batch_norm_epsilon: Small constant to prevent division by zero when
normalizing activations by their variance in batch normalization.
batch_norm_scale: If True, uses an explicit `gamma` multiplier to scale the
activations in the batch normalization layer.
weights_initializer_stddev: The standard deviation of the trunctated normal
weight initializer.
activation_fn: The activation function in Xception.
regularize_depthwise: Whether or not apply L2-norm regularization on the
depthwise convolution weights.
use_batch_norm: Whether or not to use batch normalization.
Returns:
An `arg_scope` to use for the Xception models.
"""
batch_norm_params = {
'decay': batch_norm_decay,
'epsilon': batch_norm_epsilon,
'scale': batch_norm_scale,
}
if regularize_depthwise:
depthwise_regularizer = slim.l2_regularizer(weight_decay)
else:
depthwise_regularizer = None
with slim.arg_scope(
[slim.conv2d, slim.separable_conv2d],
weights_initializer=tf.truncated_normal_initializer(
stddev=weights_initializer_stddev),
activation_fn=activation_fn,
normalizer_fn=slim.batch_norm if use_batch_norm else None):
with slim.arg_scope([slim.batch_norm], **batch_norm_params):
with slim.arg_scope(
[slim.conv2d],
weights_regularizer=slim.l2_regularizer(weight_decay)):
with slim.arg_scope(
[slim.separable_conv2d],
weights_regularizer=depthwise_regularizer) as arg_sc:
return arg_sc