本文整理汇总了Python中tensorflow.contrib.layers.python.layers.utils.last_dimension方法的典型用法代码示例。如果您正苦于以下问题:Python utils.last_dimension方法的具体用法?Python utils.last_dimension怎么用?Python utils.last_dimension使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.contrib.layers.python.layers.utils
的用法示例。
在下文中一共展示了utils.last_dimension方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: softmax
# 需要导入模块: from tensorflow.contrib.layers.python.layers import utils [as 别名]
# 或者: from tensorflow.contrib.layers.python.layers.utils import last_dimension [as 别名]
def softmax(logits, scope=None):
"""Performs softmax on Nth dimension of N-dimensional logit tensor.
For two-dimensional logits this reduces to tf.nn.softmax. The N-th dimension
needs to have a specified number of elements (number of classes).
Args:
logits: N-dimensional `Tensor` with logits, where N > 1.
scope: Optional scope for variable_scope.
Returns:
A `Tensor` with same shape and type as logits.
"""
# TODO(jrru): Add axis argument which defaults to last dimension.
with variable_scope.variable_scope(scope, 'softmax', [logits]):
num_logits = utils.last_dimension(logits.get_shape(), min_rank=2)
logits_2d = array_ops.reshape(logits, [-1, num_logits])
predictions = nn.softmax(logits_2d)
predictions = array_ops.reshape(predictions, array_ops.shape(logits))
predictions.set_shape(logits.get_shape())
return predictions
示例2: softmax
# 需要导入模块: from tensorflow.contrib.layers.python.layers import utils [as 别名]
# 或者: from tensorflow.contrib.layers.python.layers.utils import last_dimension [as 别名]
def softmax(logits, scope=None):
"""Performs softmax on Nth dimension of N-dimensional logit tensor.
For two-dimensional logits this reduces to tf.nn.softmax. The N-th dimension
needs to have a specified number of elements (number of classes).
Args:
logits: N-dimensional `Tensor` with logits, where N > 1.
scope: Optional scope for variable_scope.
Returns:
a `Tensor` with same shape and type as logits.
"""
# TODO(jrru): Add axis argument which defaults to last dimension.
with variable_scope.variable_scope(scope, 'softmax', [logits]):
num_logits = utils.last_dimension(logits.get_shape(), min_rank=2)
logits_2d = array_ops.reshape(logits, [-1, num_logits])
predictions = nn.softmax(logits_2d)
predictions = array_ops.reshape(predictions, array_ops.shape(logits))
predictions.set_shape(logits.get_shape())
return predictions
示例3: bottleneck_IR
# 需要导入模块: from tensorflow.contrib.layers.python.layers import utils [as 别名]
# 或者: from tensorflow.contrib.layers.python.layers.utils import last_dimension [as 别名]
def bottleneck_IR(inputs, depth, depth_bottleneck, stride, rate=1, w_init=None, scope=None, trainable=None):
with tf.variable_scope(scope, 'bottleneck_v1') as sc:
depth_in = utils.last_dimension(inputs.outputs.get_shape(), min_rank=4)
if depth == depth_in:
shortcut = subsample(inputs, stride, 'shortcut')
else:
shortcut = Conv2d(inputs, depth, filter_size=(1, 1), strides=(stride, stride), act=None,
W_init=w_init, b_init=None, name='shortcut_conv', use_cudnn_on_gpu=True)
shortcut = BatchNormLayer(shortcut, act=tf.identity, is_train=True, trainable=trainable, name='shortcut_bn/BatchNorm')
# bottleneck layer 1
residual = BatchNormLayer(inputs, act=tf.identity, is_train=True, trainable=trainable, name='conv1_bn1')
residual = Conv2d(residual, depth_bottleneck, filter_size=(3, 3), strides=(1, 1), act=None, b_init=None,
W_init=w_init, name='conv1', use_cudnn_on_gpu=True)
residual = BatchNormLayer(residual, act=tf.identity, is_train=True, trainable=trainable, name='conv1_bn2')
# bottleneck prelu
residual = PReluLayer(residual)
# bottleneck layer 2
residual = conv2d_same(residual, depth, kernel_size=3, strides=stride, rate=rate, w_init=w_init, scope='conv2', trainable=trainable)
output = ElementwiseLayer(layer=[shortcut, residual],
combine_fn=tf.add,
name='combine_layer',
act=None)
return output
示例4: bottleneck_IR
# 需要导入模块: from tensorflow.contrib.layers.python.layers import utils [as 别名]
# 或者: from tensorflow.contrib.layers.python.layers.utils import last_dimension [as 别名]
def bottleneck_IR(inputs, depth, depth_bottleneck, stride, rate=1, w_init=None, scope=None, trainable=None):
with tf.variable_scope(scope, 'bottleneck_v1') as sc:
depth_in = utils.last_dimension(inputs.outputs.get_shape(), min_rank=4)
if depth == depth_in:
shortcut = subsample(inputs, stride, 'shortcut')
else:
shortcut = tl.layers.Conv2d(inputs, depth, filter_size=(1, 1), strides=(stride, stride), act=None,
W_init=w_init, b_init=None, name='shortcut_conv', use_cudnn_on_gpu=True)
shortcut = GroupNormLayer(layer=shortcut, act=tf.identity, name='shortcut_bn/BatchNorm')
# bottleneck layer 1
residual = GroupNormLayer(layer=inputs, act=tf.identity, name='conv1_bn1')
residual = tl.layers.Conv2d(residual, depth_bottleneck, filter_size=(3, 3), strides=(1, 1), act=None, b_init=None,
W_init=w_init, name='conv1', use_cudnn_on_gpu=True)
residual = GroupNormLayer(layer=residual, act=tf.identity, name='conv1_bn2')
# bottleneck prelu
residual = tl.layers.PReluLayer(residual)
# bottleneck layer 2
residual = conv2d_same(residual, depth, kernel_size=3, strides=stride, rate=rate, w_init=w_init, scope='conv2', trainable=trainable)
output = ElementwiseLayer(layer=[shortcut, residual],
combine_fn=tf.add,
name='combine_layer',
act=None)
return output
示例5: bottleneck_IR
# 需要导入模块: from tensorflow.contrib.layers.python.layers import utils [as 别名]
# 或者: from tensorflow.contrib.layers.python.layers.utils import last_dimension [as 别名]
def bottleneck_IR(inputs, depth, depth_bottleneck, stride, rate=1, w_init=None, scope=None, trainable=None):
with tf.variable_scope(scope, 'bottleneck_v1') as sc:
depth_in = utils.last_dimension(inputs.outputs.get_shape(), min_rank=4)
if depth == depth_in:
shortcut = subsample(inputs, stride, 'shortcut')
else:
shortcut = tl.layers.Conv2d(inputs, depth, filter_size=(1, 1), strides=(stride, stride), act=None,
W_init=w_init, b_init=None, name='shortcut_conv', use_cudnn_on_gpu=True)
shortcut = BatchNormLayer(shortcut, act=tf.identity, is_train=True, trainable=trainable, name='shortcut_bn/BatchNorm')
# bottleneck layer 1
residual = BatchNormLayer(inputs, act=tf.identity, is_train=True, trainable=trainable, name='conv1_bn1')
residual = tl.layers.Conv2d(residual, depth_bottleneck, filter_size=(3, 3), strides=(1, 1), act=None, b_init=None,
W_init=w_init, name='conv1', use_cudnn_on_gpu=True)
residual = BatchNormLayer(residual, act=tf.identity, is_train=True, trainable=trainable, name='conv1_bn2')
# bottleneck prelu
residual = tl.layers.PReluLayer(residual)
# bottleneck layer 2
residual = conv2d_same(residual, depth, kernel_size=3, strides=stride, rate=rate, w_init=w_init, scope='conv2', trainable=trainable)
output = ElementwiseLayer(layer=[shortcut, residual],
combine_fn=tf.add,
name='combine_layer',
act=None)
return output
示例6: bottleneck_IR_SE
# 需要导入模块: from tensorflow.contrib.layers.python.layers import utils [as 别名]
# 或者: from tensorflow.contrib.layers.python.layers.utils import last_dimension [as 别名]
def bottleneck_IR_SE(inputs, depth, depth_bottleneck, stride, rate=1, w_init=None, scope=None, trainable=None):
with tf.variable_scope(scope, 'bottleneck_v1') as sc:
depth_in = utils.last_dimension(inputs.outputs.get_shape(), min_rank=4)
if depth == depth_in:
shortcut = subsample(inputs, stride, 'shortcut')
else:
shortcut = Conv2d(inputs, depth, filter_size=(1, 1), strides=(stride, stride), act=None,
W_init=w_init, b_init=None, name='shortcut_conv', use_cudnn_on_gpu=True)
shortcut = BatchNormLayer(shortcut, act=tf.identity, is_train=True, trainable=trainable, name='shortcut_bn/BatchNorm')
# bottleneck layer 1
residual = BatchNormLayer(inputs, act=tf.identity, is_train=True, trainable=trainable, name='conv1_bn1')
residual = Conv2d(residual, depth_bottleneck, filter_size=(3, 3), strides=(1, 1), act=None, b_init=None,
W_init=w_init, name='conv1', use_cudnn_on_gpu=True)
residual = BatchNormLayer(residual, act=tf.identity, is_train=True, trainable=trainable, name='conv1_bn2')
# bottleneck prelu
residual = PReluLayer(residual)
# bottleneck layer 2
residual = conv2d_same(residual, depth, kernel_size=3, strides=stride, rate=rate, w_init=w_init, scope='conv2', trainable=trainable)
# squeeze
squeeze = tl.layers.InputLayer(tf.reduce_mean(residual.outputs, axis=[1, 2]), name='squeeze_layer')
# excitation
excitation1 = DenseLayer(squeeze, n_units=int(depth/16.0), act=tf.nn.relu,
W_init=w_init, name='excitation_1')
# excitation1 = tl.layers.PReluLayer(excitation1, name='excitation_prelu')
excitation2 = DenseLayer(excitation1, n_units=depth, act=tf.nn.sigmoid,
W_init=w_init, name='excitation_2')
# scale
scale = tl.layers.ReshapeLayer(excitation2, shape=[tf.shape(excitation2.outputs)[0], 1, 1, depth], name='excitation_reshape')
residual_se = ElementwiseLayer(layer=[residual, scale],
combine_fn=tf.multiply,
name='scale_layer',
act=None)
output = ElementwiseLayer(layer=[shortcut, residual_se],
combine_fn=tf.add,
name='combine_layer',
act=tf.nn.relu)
return output
示例7: bottleneck
# 需要导入模块: from tensorflow.contrib.layers.python.layers import utils [as 别名]
# 或者: from tensorflow.contrib.layers.python.layers.utils import last_dimension [as 别名]
def bottleneck(inputs, depth, depth_bottleneck, stride, rate=1, scope=None):
with tf.variable_scope(scope, 'bottleneck_v1') as sc:
depth_in = utils.last_dimension(inputs.outputs.get_shape(), min_rank=4)
if depth == depth_in:
shortcut = subsample(inputs, stride, 'shortcut')
else:
shortcut = tl.layers.Conv2d(inputs, depth, filter_size=(1, 1), strides=(stride, stride), act=None,
b_init=None, name='shortcut_conv')
shortcut = tl.layers.BatchNormLayer(shortcut, act=tf.identity, is_train=True, name='shortcut_bn/BatchNorm')
# bottleneck layer 1
residual = tl.layers.Conv2d(inputs, depth_bottleneck, filter_size=(1, 1), strides=(1, 1), act=None, b_init=None,
name='conv1')
residual = tl.layers.BatchNormLayer(residual, act=tf.nn.relu, is_train=True, name='conv1_bn/BatchNorm')
# bottleneck layer 2
residual = conv2d_same(residual, depth_bottleneck, kernel_size=3, strides= stride, rate=rate, scope='conv2')
# bottleneck layer 3
residual = tl.layers.Conv2d(residual, depth, filter_size=(1, 1), strides=(1, 1), act=None, b_init=None,
name='conv3')
residual = tl.layers.BatchNormLayer(residual, act=tf.identity, is_train=True, name='conv3_bn/BatchNorm')
output = ElementwiseLayer(layer=[shortcut, residual],
combine_fn=tf.add,
name='combine_layer',
act=tf.nn.relu)
return output
示例8: bottleneck_SE
# 需要导入模块: from tensorflow.contrib.layers.python.layers import utils [as 别名]
# 或者: from tensorflow.contrib.layers.python.layers.utils import last_dimension [as 别名]
def bottleneck_SE(inputs, depth, depth_bottleneck, stride, rate=1, scope=None):
with tf.variable_scope(scope, 'bottleneck_v1') as sc:
depth_in = utils.last_dimension(inputs.outputs.get_shape(), min_rank=4)
if depth == depth_in:
shortcut = subsample(inputs, stride, 'shortcut')
else:
shortcut = tl.layers.Conv2d(inputs, depth, filter_size=(1, 1), strides=(stride, stride), act=None,
b_init=None, name='shortcut_conv')
shortcut = tl.layers.BatchNormLayer(shortcut, act=tf.identity, is_train=True, name='shortcut_bn/BatchNorm')
# bottleneck layer 1
residual = tl.layers.Conv2d(inputs, depth_bottleneck, filter_size=(1, 1), strides=(1, 1), act=None, b_init=None,
name='conv1')
residual = tl.layers.BatchNormLayer(residual, act=tf.nn.relu, is_train=True, name='conv1_bn/BatchNorm')
# bottleneck layer 2
residual = conv2d_same(residual, depth_bottleneck, kernel_size=3, strides= stride, rate=rate, scope='conv2')
# bottleneck layer 3
residual = tl.layers.Conv2d(residual, depth, filter_size=(1, 1), strides=(1, 1), act=None, b_init=None,
name='conv3')
residual = tl.layers.BatchNormLayer(residual, act=tf.identity, is_train=True, name='conv3_bn/BatchNorm')
# squeeze
squeeze = tl.layers.InputLayer(tf.reduce_mean(residual.outputs, axis=[1, 2]), name='squeeze_layer')
# excitation
excitation1 = tl.layers.DenseLayer(squeeze, n_units=int(depth/16.0), act=tf.nn.relu, name='excitation_1')
excitation2 = tl.layers.DenseLayer(excitation1, n_units=depth, act=tf.nn.sigmoid, name='excitation_2')
# scale
scale = tl.layers.ReshapeLayer(excitation2, shape=[tf.shape(excitation2.outputs)[0], 1, 1, depth], name='excitation_reshape')
residual_se = ElementwiseLayer(layer=[residual, scale],
combine_fn=tf.multiply,
name='scale_layer',
act=None)
output = ElementwiseLayer(layer=[shortcut, residual_se],
combine_fn=tf.add,
name='combine_layer',
act=tf.nn.relu)
return output
示例9: bottleneck
# 需要导入模块: from tensorflow.contrib.layers.python.layers import utils [as 别名]
# 或者: from tensorflow.contrib.layers.python.layers.utils import last_dimension [as 别名]
def bottleneck(inputs, depth, depth_bottleneck, stride, rate=1, scope=None):
with tf.variable_scope(scope, 'bottleneck_v1') as sc:
depth_in = utils.last_dimension(inputs.outputs.get_shape(), min_rank=4)
if depth == depth_in:
shortcut = subsample(inputs, stride, 'shortcut')
else:
shortcut = tl.layers.Conv2d(inputs, depth, filter_size=(1, 1), strides=(stride, stride), act=None,
b_init=None, name='shortcut_conv')
shortcut = GroupNormLayer(layer=shortcut, act=tf.identity, name='shortcut_bn/BatchNorm')
# bottleneck layer 1
residual = tl.layers.Conv2d(inputs, depth_bottleneck, filter_size=(1, 1), strides=(1, 1), act=None, b_init=None,
name='conv1')
residual = GroupNormLayer(layer=residual, act=tf.nn.relu, name='conv1_bn/BatchNorm')
# bottleneck layer 2
residual = conv2d_same(residual, depth_bottleneck, kernel_size=3, strides= stride, rate=rate, scope='conv2')
# bottleneck layer 3
residual = tl.layers.Conv2d(residual, depth, filter_size=(1, 1), strides=(1, 1), act=None, b_init=None,
name='conv3')
residual = GroupNormLayer(layer=residual, act=tf.identity, name='conv3_bn/BatchNorm',
scale_init=tf.constant_initializer(0.0))
output = ElementwiseLayer(layer=[shortcut, residual],
combine_fn=tf.add,
name='combine_layer',
act=tf.nn.relu)
return output
示例10: bottleneck_IR_SE
# 需要导入模块: from tensorflow.contrib.layers.python.layers import utils [as 别名]
# 或者: from tensorflow.contrib.layers.python.layers.utils import last_dimension [as 别名]
def bottleneck_IR_SE(inputs, depth, depth_bottleneck, stride, rate=1, w_init=None, scope=None, trainable=None):
with tf.variable_scope(scope, 'bottleneck_v1') as sc:
depth_in = utils.last_dimension(inputs.outputs.get_shape(), min_rank=4)
if depth == depth_in:
shortcut = subsample(inputs, stride, 'shortcut')
else:
shortcut = tl.layers.Conv2d(inputs, depth, filter_size=(1, 1), strides=(stride, stride), act=None,
W_init=w_init, b_init=None, name='shortcut_conv', use_cudnn_on_gpu=True)
shortcut = GroupNormLayer(layer=shortcut, act=tf.identity, name='shortcut_bn/BatchNorm')
residual = GroupNormLayer(layer=inputs, act=tf.identity, name='conv1_bn1')
residual = tl.layers.Conv2d(residual, depth_bottleneck, filter_size=(3, 3), strides=(1, 1), act=None, b_init=None,
W_init=w_init, name='conv1', use_cudnn_on_gpu=True)
residual = GroupNormLayer(layer=residual, act=tf.identity, name='conv1_bn2')
# bottleneck prelu
residual = tl.layers.PReluLayer(residual)
# bottleneck layer 2
residual = conv2d_same(residual, depth, kernel_size=3, strides=stride, rate=rate, w_init=w_init, scope='conv2', trainable=trainable)
# squeeze
squeeze = tl.layers.InputLayer(tf.reduce_mean(residual.outputs, axis=[1, 2]), name='squeeze_layer')
# excitation
excitation1 = tl.layers.DenseLayer(squeeze, n_units=int(depth/16.0), act=tf.nn.relu,
W_init=w_init, name='excitation_1')
# excitation1 = tl.layers.PReluLayer(excitation1, name='excitation_prelu')
excitation2 = tl.layers.DenseLayer(excitation1, n_units=depth, act=tf.nn.sigmoid,
W_init=w_init, name='excitation_2')
# scale
scale = tl.layers.ReshapeLayer(excitation2, shape=[tf.shape(excitation2.outputs)[0], 1, 1, depth], name='excitation_reshape')
residual_se = ElementwiseLayer(layer=[residual, scale],
combine_fn=tf.multiply,
name='scale_layer',
act=None)
output = ElementwiseLayer(layer=[shortcut, residual_se],
combine_fn=tf.add,
name='combine_layer',
act=tf.nn.relu)
return output
示例11: bottleneck_IR_SE
# 需要导入模块: from tensorflow.contrib.layers.python.layers import utils [as 别名]
# 或者: from tensorflow.contrib.layers.python.layers.utils import last_dimension [as 别名]
def bottleneck_IR_SE(inputs, depth, depth_bottleneck, stride, rate=1, w_init=None, scope=None, trainable=None):
with tf.variable_scope(scope, 'bottleneck_v1') as sc:
depth_in = utils.last_dimension(inputs.outputs.get_shape(), min_rank=4)
if depth == depth_in:
shortcut = subsample(inputs, stride, 'shortcut')
else:
shortcut = tl.layers.Conv2d(inputs, depth, filter_size=(1, 1), strides=(stride, stride), act=None,
W_init=w_init, b_init=None, name='shortcut_conv', use_cudnn_on_gpu=True)
shortcut = BatchNormLayer(shortcut, act=tf.identity, is_train=True, trainable=trainable, name='shortcut_bn/BatchNorm')
# bottleneck layer 1
residual = BatchNormLayer(inputs, act=tf.identity, is_train=True, trainable=trainable, name='conv1_bn1')
residual = tl.layers.Conv2d(residual, depth_bottleneck, filter_size=(3, 3), strides=(1, 1), act=None, b_init=None,
W_init=w_init, name='conv1', use_cudnn_on_gpu=True)
residual = BatchNormLayer(residual, act=tf.identity, is_train=True, trainable=trainable, name='conv1_bn2')
# bottleneck prelu
residual = tl.layers.PReluLayer(residual)
# bottleneck layer 2
residual = conv2d_same(residual, depth, kernel_size=3, strides=stride, rate=rate, w_init=w_init, scope='conv2', trainable=trainable)
# squeeze
squeeze = tl.layers.InputLayer(tf.reduce_mean(residual.outputs, axis=[1, 2]), name='squeeze_layer')
# excitation
excitation1 = tl.layers.DenseLayer(squeeze, n_units=int(depth/16.0), act=tf.nn.relu,
W_init=w_init, name='excitation_1')
# excitation1 = tl.layers.PReluLayer(excitation1, name='excitation_prelu')
excitation2 = tl.layers.DenseLayer(excitation1, n_units=depth, act=tf.nn.sigmoid,
W_init=w_init, name='excitation_2')
# scale
scale = tl.layers.ReshapeLayer(excitation2, shape=[tf.shape(excitation2.outputs)[0], 1, 1, depth], name='excitation_reshape')
residual_se = ElementwiseLayer(layer=[residual, scale],
combine_fn=tf.multiply,
name='scale_layer',
act=None)
output = ElementwiseLayer(layer=[shortcut, residual_se],
combine_fn=tf.add,
name='combine_layer',
act=tf.nn.relu)
return output
示例12: preact_conv2d
# 需要导入模块: from tensorflow.contrib.layers.python.layers import utils [as 别名]
# 或者: from tensorflow.contrib.layers.python.layers.utils import last_dimension [as 别名]
def preact_conv2d(
inputs,
num_outputs,
kernel_size,
stride=1,
padding='SAME',
activation_fn=nn.relu,
normalizer_fn=None,
normalizer_params=None,
weights_initializer=initializers.xavier_initializer(),
weights_regularizer=None,
reuse=None,
variables_collections=None,
outputs_collections=None,
trainable=True,
scope=None):
"""Adds a 2D convolution preceded by batch normalization and activation.
"""
with variable_scope.variable_scope(scope, 'Conv', values=[inputs], reuse=reuse) as sc:
inputs = ops.convert_to_tensor(inputs)
dtype = inputs.dtype.base_dtype
if normalizer_fn:
normalizer_params = normalizer_params or {}
inputs = normalizer_fn(inputs, activation_fn=activation_fn, **normalizer_params)
kernel_h, kernel_w = utils.two_element_tuple(kernel_size)
stride_h, stride_w = utils.two_element_tuple(stride)
num_filters_in = utils.last_dimension(inputs.get_shape(), min_rank=4)
weights_shape = [kernel_h, kernel_w, num_filters_in, num_outputs]
weights_collections = utils.get_variable_collections(variables_collections, 'weights')
weights = variables.model_variable('weights',
shape=weights_shape,
dtype=dtype,
initializer=weights_initializer,
regularizer=weights_regularizer,
collections=weights_collections,
trainable=trainable)
outputs = nn.conv2d(inputs, weights, [1, stride_h, stride_w, 1], padding=padding)
return utils.collect_named_outputs(outputs_collections, sc.name, outputs)
示例13: bottleneck
# 需要导入模块: from tensorflow.contrib.layers.python.layers import utils [as 别名]
# 或者: from tensorflow.contrib.layers.python.layers.utils import last_dimension [as 别名]
def bottleneck(inputs,
depth,
depth_bottleneck,
stride,
rate=1,
outputs_collections=None,
scope=None):
"""Bottleneck residual unit variant with BN after convolutions.
This is the original residual unit proposed in [1]. See Fig. 1(a) of [2] for
its definition. Note that we use here the bottleneck variant which has an
extra bottleneck layer.
When putting together two consecutive ResNet blocks that use this unit, one
should use stride = 2 in the last unit of the first block.
Args:
inputs: A tensor of size [batch, height, width, channels].
depth: The depth of the ResNet unit output.
depth_bottleneck: The depth of the bottleneck layers.
stride: The ResNet unit's stride. Determines the amount of downsampling of
the units output compared to its input.
rate: An integer, rate for atrous convolution.
outputs_collections: Collection to add the ResNet unit output.
scope: Optional variable_scope.
Returns:
The ResNet unit's output.
"""
with variable_scope.variable_scope(scope, 'bottleneck_v1', [inputs]) as sc:
depth_in = utils.last_dimension(inputs.get_shape(), min_rank=4)
if depth == depth_in:
shortcut = resnet_utils.subsample(inputs, stride, 'shortcut')
else:
shortcut = layers.conv2d(
inputs,
depth, [1, 1],
stride=stride,
activation_fn=None,
scope='shortcut')
residual = layers.conv2d(
inputs, depth_bottleneck, [1, 1], stride=1, scope='conv1')
residual = resnet_utils.conv2d_same(
residual, depth_bottleneck, 3, stride, rate=rate, scope='conv2')
residual = layers.conv2d(
residual, depth, [1, 1], stride=1, activation_fn=None, scope='conv3')
output = nn_ops.relu(shortcut + residual)
return utils.collect_named_outputs(outputs_collections, sc.name, output)
示例14: bottleneck
# 需要导入模块: from tensorflow.contrib.layers.python.layers import utils [as 别名]
# 或者: from tensorflow.contrib.layers.python.layers.utils import last_dimension [as 别名]
def bottleneck(inputs,
depth,
depth_bottleneck,
stride,
rate=1,
outputs_collections=None,
scope=None):
"""Bottleneck residual unit variant with BN before convolutions.
This is the full preactivation residual unit variant proposed in [2]. See
Fig. 1(b) of [2] for its definition. Note that we use here the bottleneck
variant which has an extra bottleneck layer.
When putting together two consecutive ResNet blocks that use this unit, one
should use stride = 2 in the last unit of the first block.
Args:
inputs: A tensor of size [batch, height, width, channels].
depth: The depth of the ResNet unit output.
depth_bottleneck: The depth of the bottleneck layers.
stride: The ResNet unit's stride. Determines the amount of downsampling of
the units output compared to its input.
rate: An integer, rate for atrous convolution.
outputs_collections: Collection to add the ResNet unit output.
scope: Optional variable_scope.
Returns:
The ResNet unit's output.
"""
with variable_scope.variable_scope(scope, 'bottleneck_v2', [inputs]) as sc:
depth_in = utils.last_dimension(inputs.get_shape(), min_rank=4)
preact = layers.batch_norm(
inputs, activation_fn=nn_ops.relu, scope='preact')
if depth == depth_in:
shortcut = resnet_utils.subsample(inputs, stride, 'shortcut')
else:
shortcut = layers_lib.conv2d(
preact,
depth, [1, 1],
stride=stride,
normalizer_fn=None,
activation_fn=None,
scope='shortcut')
residual = layers_lib.conv2d(
preact, depth_bottleneck, [1, 1], stride=1, scope='conv1')
residual = resnet_utils.conv2d_same(
residual, depth_bottleneck, 3, stride, rate=rate, scope='conv2')
residual = layers_lib.conv2d(
residual,
depth, [1, 1],
stride=1,
normalizer_fn=None,
activation_fn=None,
scope='conv3')
output = shortcut + residual
return utils.collect_named_outputs(outputs_collections, sc.name, output)
示例15: bottleneck_IR
# 需要导入模块: from tensorflow.contrib.layers.python.layers import utils [as 别名]
# 或者: from tensorflow.contrib.layers.python.layers.utils import last_dimension [as 别名]
def bottleneck_IR(inputs, depth, depth_bottleneck, stride, rate=1, w_init=None, scope=None, trainable=None):
with tf.variable_scope(scope, 'bottleneck_v1') as sc:
depth_in = utils.last_dimension(inputs.outputs.get_shape(), min_rank=4)
if depth == depth_in:
shortcut = subsample(inputs, stride, 'shortcut')
else:
shortcut = tl.layers.Conv2d(inputs, depth, filter_size=(1, 1), strides=(stride, stride), act=None,
W_init=w_init, b_init=None, name='shortcut_conv', use_cudnn_on_gpu=True)
shortcut.outputs = tf.layers.batch_normalization(inputs=shortcut.outputs,
momentum=0.9,
training=trainable,
renorm=True,
renorm_clipping={'rmax': 3, 'rmin': 0.3333,
'dmax': 5},
renorm_momentum=0.9,
name='shortcut_bn/BatchNorm')
# bottleneck layer 1
inputs.outputs = tf.layers.batch_normalization(inputs=inputs.outputs,
momentum=0.9,
training=trainable,
renorm=True,
renorm_clipping={'rmax': 3, 'rmin': 0.3333,
'dmax': 5},
renorm_momentum=0.9,
name='conv1_bn1')
residual = tl.layers.Conv2d(inputs, depth_bottleneck, filter_size=(3, 3), strides=(1, 1), act=None, b_init=None,
W_init=w_init, name='conv1', use_cudnn_on_gpu=True)
residual.outputs = tf.layers.batch_normalization(inputs=residual.outputs,
momentum=0.9,
training=trainable,
renorm=True,
renorm_clipping={'rmax': 3, 'rmin': 0.3333,
'dmax': 5},
renorm_momentum=0.9,
name='conv1_bn2')
# bottleneck prelu
residual = tl.layers.PReluLayer(residual)
# bottleneck layer 2
residual = conv2d_same(residual, depth, kernel_size=3, strides=stride, rate=rate, w_init=w_init, scope='conv2', trainable=trainable)
output = ElementwiseLayer(layer=[shortcut, residual],
combine_fn=tf.add,
name='combine_layer',
act=None)
return output