-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,228 additions
and
1,895 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2016 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. | ||
# ============================================================================== | ||
|
||
# modified from tensorflow/contrib/layers/python/layers/layers.py | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
from tensorflow.contrib.framework.python.ops import variables | ||
from tensorflow.contrib.layers.python.layers import initializers | ||
from tensorflow.contrib.layers.python.layers import utils | ||
from tensorflow.python.framework import ops | ||
from tensorflow.python.ops import init_ops | ||
from tensorflow.python.ops import nn | ||
from tensorflow.python.ops import variable_scope | ||
|
||
DATA_FORMAT_NCHW = 'NCHW' | ||
DATA_FORMAT_NHWC = 'NHWC' | ||
DATA_FORMAT_NCDHW = 'NCDHW' | ||
DATA_FORMAT_NDHWC = 'NDHWC' | ||
|
||
def _model_variable_getter(getter, | ||
name, | ||
shape=None, | ||
dtype=None, | ||
initializer=None, | ||
regularizer=None, | ||
trainable=True, | ||
collections=None, | ||
caching_device=None, | ||
partitioner=None, | ||
rename=None, | ||
use_resource=None, | ||
**_): | ||
"""Getter that uses model_variable for compatibility with core layers.""" | ||
short_name = name.split('/')[-1] | ||
if rename and short_name in rename: | ||
name_components = name.split('/') | ||
name_components[-1] = rename[short_name] | ||
name = '/'.join(name_components) | ||
return variables.model_variable( | ||
name, | ||
shape=shape, | ||
dtype=dtype, | ||
initializer=initializer, | ||
regularizer=regularizer, | ||
collections=collections, | ||
trainable=trainable, | ||
caching_device=caching_device, | ||
partitioner=partitioner, | ||
custom_getter=getter, | ||
use_resource=use_resource) | ||
|
||
|
||
def _build_variable_getter(rename=None): | ||
"""Build a model variable getter that respects scope getter and renames.""" | ||
|
||
# VariableScope will nest the getters | ||
def layer_variable_getter(getter, *args, **kwargs): | ||
kwargs['rename'] = rename | ||
return _model_variable_getter(getter, *args, **kwargs) | ||
|
||
return layer_variable_getter | ||
|
||
def depth_conv2d( | ||
inputs, | ||
kernel_size, | ||
stride=1, | ||
channel_multiplier=1, | ||
padding='SAME', | ||
data_format=DATA_FORMAT_NHWC, | ||
rate=1, | ||
activation_fn=nn.relu, | ||
normalizer_fn=None, | ||
normalizer_params=None, | ||
weights_initializer=initializers.xavier_initializer(), | ||
weights_regularizer=None, | ||
biases_initializer=init_ops.zeros_initializer(), | ||
biases_regularizer=None, | ||
reuse=None, | ||
variables_collections=None, | ||
outputs_collections=None, | ||
trainable=True, | ||
scope=None): | ||
|
||
if data_format not in (DATA_FORMAT_NCHW, DATA_FORMAT_NHWC): | ||
raise ValueError('data_format has to be either NCHW or NHWC.') | ||
layer_variable_getter = _build_variable_getter({ | ||
'bias': 'biases', | ||
'depthwise_kernel': 'depthwise_weights' | ||
}) | ||
|
||
with variable_scope.variable_scope( | ||
scope, | ||
'SeparableConv2d', [inputs], | ||
reuse=reuse, | ||
custom_getter=layer_variable_getter) as sc: | ||
inputs = ops.convert_to_tensor(inputs) | ||
|
||
df = ('channels_first' | ||
if data_format and data_format.startswith('NC') else 'channels_last') | ||
|
||
# Actually apply depthwise conv instead of separable conv. | ||
dtype = inputs.dtype.base_dtype | ||
kernel_h, kernel_w = utils.two_element_tuple(kernel_size) | ||
stride_h, stride_w = utils.two_element_tuple(stride) | ||
num_filters_in = utils.channel_dimension( | ||
inputs.get_shape(), df, min_rank=4) | ||
weights_collections = utils.get_variable_collections( | ||
variables_collections, 'weights') | ||
|
||
depthwise_shape = [kernel_h, kernel_w, num_filters_in, channel_multiplier] | ||
depthwise_weights = variables.model_variable( | ||
'depthwise_weights', | ||
shape=depthwise_shape, | ||
dtype=dtype, | ||
initializer=weights_initializer, | ||
regularizer=weights_regularizer, | ||
trainable=trainable, | ||
collections=weights_collections) | ||
strides = [1, 1, stride_h, stride_w] if data_format.startswith('NC') else [1, stride_h, stride_w, 1] | ||
|
||
outputs = nn.depthwise_conv2d( | ||
inputs, | ||
depthwise_weights, | ||
strides, | ||
padding, | ||
rate=utils.two_element_tuple(rate), | ||
data_format=data_format) | ||
num_outputs = num_filters_in | ||
|
||
if normalizer_fn is not None: | ||
normalizer_params = normalizer_params or {} | ||
outputs = normalizer_fn(outputs, **normalizer_params) | ||
else: | ||
if biases_initializer is not None: | ||
biases_collections = utils.get_variable_collections( | ||
variables_collections, 'biases') | ||
biases = variables.model_variable( | ||
'biases', | ||
shape=[ | ||
num_outputs, | ||
], | ||
dtype=dtype, | ||
initializer=biases_initializer, | ||
regularizer=biases_regularizer, | ||
trainable=trainable, | ||
collections=biases_collections) | ||
outputs = nn.bias_add(outputs, biases, data_format=data_format) | ||
|
||
if activation_fn is not None: | ||
outputs = activation_fn(outputs) | ||
return utils.collect_named_outputs(outputs_collections, sc.name, outputs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.