本文整理汇总了Python中tensorflow.contrib.slim.variable方法的典型用法代码示例。如果您正苦于以下问题:Python slim.variable方法的具体用法?Python slim.variable怎么用?Python slim.variable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.contrib.slim
的用法示例。
在下文中一共展示了slim.variable方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convert_varname
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import variable [as 别名]
def convert_varname(self, vname, to_namespace=None):
"""
Utility function that converts variable names to the input namespace.
Parameters
----------
vname: string
The variable name.
to_namespace: string
The target namespace.
Returns
-------
"""
namespace = vname.split("/")[0]
if to_namespace is None:
to_namespace = self.name
return vname.replace(namespace, to_namespace)
示例2: mat_transform
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import variable [as 别名]
def mat_transform(input, caps_num_c, regularizer, tag=False):
batch_size = int(input.get_shape()[0])
caps_num_i = int(input.get_shape()[1])
output = tf.reshape(input, shape=[batch_size, caps_num_i, 1, 4, 4])
# the output of capsule is miu, the mean of a Gaussian, and activation, the sum of probabilities
# it has no relationship with the absolute values of w and votes
# using weights with bigger stddev helps numerical stability
w = slim.variable('w', shape=[1, caps_num_i, caps_num_c, 4, 4], dtype=tf.float32,
initializer=tf.truncated_normal_initializer(mean=0.0, stddev=1.0),
regularizer=regularizer)
w = tf.tile(w, [batch_size, 1, 1, 1, 1])
output = tf.tile(output, [1, 1, caps_num_c, 1, 1])
votes = tf.reshape(tf.matmul(output, w), [batch_size, caps_num_i, caps_num_c, 16])
return votes
示例3: vec_transform
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import variable [as 别名]
def vec_transform(input, caps_num_out, channel_num_out):
batch_size = int(input.get_shape()[0])
caps_num_in = int(input.get_shape()[1])
channel_num_in = int(input.get_shape()[-1])
w = slim.variable('w', shape=[1, caps_num_out, caps_num_in, channel_num_in, channel_num_out], dtype=tf.float32,
initializer=tf.random_normal_initializer(mean=0.0, stddev=0.01)) #
w = tf.tile(w, [batch_size, 1, 1, 1, 1])
output = tf.reshape(input, shape=[batch_size, 1, caps_num_in, 1, channel_num_in])
output = tf.tile(output, [1, caps_num_out, 1, 1, 1])
output = tf.reshape(tf.matmul(output, w), [batch_size, caps_num_out, caps_num_in, channel_num_out])
return output
# input should be a tensor with size as [batch_size, caps_num_out, channel_num]
示例4: average_gradients
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import variable [as 别名]
def average_gradients(tower_grads):
"""Calculate the average gradient for each shared variable across all towers.
Note that this function provides a synchronization point across all towers.
Args:
tower_grads: List of lists of (gradient, variable) tuples. The outer list
is over individual gradients. The inner list is over the gradient
calculation for each tower.
Returns:
List of pairs of (gradient, variable) where the gradient has been averaged
across all towers.
"""
average_grads = []
for grad_and_vars in zip(*tower_grads):
# Note that each grad_and_vars looks like the following:
# ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN))
grads = []
for g, _ in grad_and_vars:
# Add 0 dimension to the gradients to represent the tower.
expanded_g = tf.expand_dims(g, 0)
# Append on a 'tower' dimension which we will average over below.
grads.append(expanded_g)
# Average over the 'tower' dimension.
grad = tf.concat(axis=0, values=grads)
grad = tf.reduce_mean(grad, 0)
# Keep in mind that the Variables are redundant because they are shared
# across towers. So .. we will just return the first tower's pointer to
# the Variable.
v = grad_and_vars[0][1]
grad_and_var = (grad, v)
average_grads.append(grad_and_var)
return average_grads
示例5: sum_gradients
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import variable [as 别名]
def sum_gradients(tower_grads):
"""Calculate the average gradient for each shared variable across all towers.
Note that this function provides a synchronization point across all towers.
Args:
tower_grads: List of lists of (gradient, variable) tuples. The outer list
is over individual gradients. The inner list is over the gradient
calculation for each tower.
Returns:
List of pairs of (gradient, variable) where the gradient has been averaged
across all towers.
"""
sum_grads = []
for grad_and_vars in zip(*tower_grads):
# Note that each grad_and_vars looks like the following:
# ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN))
grads = []
for g, _ in grad_and_vars:
# Add 0 dimension to the gradients to represent the tower.
expanded_g = tf.expand_dims(g, 0)
# Append on a 'tower' dimension which we will average over below.
grads.append(expanded_g)
# Average over the 'tower' dimension.
grad = tf.concat(axis=0, values=grads)
grad = tf.reduce_sum(grad, 0)
# Keep in mind that the Variables are redundant because they are shared
# across towers. So .. we will just return the first tower's pointer to
# the Variable.
v = grad_and_vars[0][1]
grad_and_var = (grad, v)
sum_grads.append(grad_and_var)
return sum_grads
示例6: _make_graph
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import variable [as 别名]
def _make_graph(self):
self.logger.info("Generating testing graph on {} GPUs ...".format(self.cfg.nr_gpus))
with tf.variable_scope(tf.get_variable_scope()):
for i in range(self.cfg.nr_gpus):
with tf.device('/gpu:%d' % i):
with tf.name_scope('tower_%d' % i) as name_scope:
with slim.arg_scope([slim.model_variable, slim.variable], device='/device:CPU:0'):
self.net.make_network(is_train=False)
self._input_list.append(self.net.get_inputs())
self._output_list.append(self.net.get_outputs())
tf.get_variable_scope().reuse_variables()
self._outputs = aggregate_batch(self._output_list)
# run_meta = tf.RunMetadata()
# opts = tf.profiler.ProfileOptionBuilder.float_operation()
# flops = tf.profiler.profile(self.sess.graph, run_meta=run_meta, cmd='op', options=opts)
#
# opts = tf.profiler.ProfileOptionBuilder.trainable_variables_parameter()
# params = tf.profiler.profile(self.sess.graph, run_meta=run_meta, cmd='op', options=opts)
# print("{:,} --- {:,}".format(flops.total_float_ops, params.total_parameters))
# from IPython import embed; embed()
return self._outputs
示例7: tiny
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import variable [as 别名]
def tiny(net, classes, num_anchors, training=False, center=True):
def batch_norm(net):
net = slim.batch_norm(net, center=center, scale=True, epsilon=1e-5, is_training=training)
if not center:
net = tf.nn.bias_add(net, slim.variable('biases', shape=[tf.shape(net)[-1]], initializer=tf.zeros_initializer()))
return net
scope = __name__.split('.')[-2] + '_' + inspect.stack()[0][3]
net = tf.identity(net, name='%s/input' % scope)
with slim.arg_scope([slim.layers.conv2d], kernel_size=[3, 3], weights_initializer=tf.truncated_normal_initializer(stddev=0.1), normalizer_fn=batch_norm, activation_fn=leaky_relu), slim.arg_scope([slim.layers.max_pool2d], kernel_size=[2, 2], padding='SAME'):
index = 0
channels = 16
for _ in range(5):
net = slim.layers.conv2d(net, channels, scope='%s/conv%d' % (scope, index))
net = slim.layers.max_pool2d(net, scope='%s/max_pool%d' % (scope, index))
index += 1
channels *= 2
net = slim.layers.conv2d(net, channels, scope='%s/conv%d' % (scope, index))
net = slim.layers.max_pool2d(net, stride=1, scope='%s/max_pool%d' % (scope, index))
index += 1
channels *= 2
net = slim.layers.conv2d(net, channels, scope='%s/conv%d' % (scope, index))
index += 1
net = slim.layers.conv2d(net, channels, scope='%s/conv%d' % (scope, index))
net = slim.layers.conv2d(net, num_anchors * (5 + classes), kernel_size=[1, 1], activation_fn=None, scope='%s/conv' % scope)
net = tf.identity(net, name='%s/output' % scope)
return scope, net
示例8: _make_graph
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import variable [as 别名]
def _make_graph(self):
self.logger.info("Generating testing graph on {} GPUs ...".format(self.cfg.num_gpus))
with tf.variable_scope(tf.get_variable_scope()):
for i in range(self.cfg.num_gpus):
with tf.device('/gpu:%d' % i):
with tf.name_scope('tower_%d' % i) as name_scope:
with slim.arg_scope([slim.model_variable, slim.variable], device='/device:CPU:0'):
self.net.make_network(is_train=False)
self._input_list.append(self.net.get_inputs())
self._output_list.append(self.net.get_outputs())
tf.get_variable_scope().reuse_variables()
self._outputs = aggregate_batch(self._output_list)
# run_meta = tf.RunMetadata()
# opts = tf.profiler.ProfileOptionBuilder.float_operation()
# flops = tf.profiler.profile(self.sess.graph, run_meta=run_meta, cmd='op', options=opts)
#
# opts = tf.profiler.ProfileOptionBuilder.trainable_variables_parameter()
# params = tf.profiler.profile(self.sess.graph, run_meta=run_meta, cmd='op', options=opts)
# print("{:,} --- {:,}".format(flops.total_float_ops, params.total_parameters))
# from IPython import embed; embed()
return self._outputs
示例9: average_gradients
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import variable [as 别名]
def average_gradients(tower_grads):
"""Calculate the average gradient for each shared variable across all towers.
Note that this function provides a synchronization point across all towers.
Args:
tower_grads: List of lists of (gradient, variable) tuples. The outer list
is over individual gradients. The inner list is over the gradient
calculation for each tower.
Returns:
List of pairs of (gradient, variable) where the gradient has been averaged
across all towers.
"""
average_grads = []
for grad_and_vars in zip(*tower_grads):
# Note that each grad_and_vars looks like the following:
# ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN))
grads = []
for g, _ in grad_and_vars:
# Add 0 dimension to the gradients to represent the tower.
expanded_g = tf.expand_dims(g, 0)
# Append on a 'tower' dimension which we will average over below.
grads.append(expanded_g)
# Average over the 'tower' dimension.
grad = tf.concat(axis=0, values=grads)
grad = tf.reduce_mean(grad, 0)
# Keep in mind that the Variables are redundant because they are shared
# across towers. So .. we will just return the first tower's pointer to
# the Variable.
v = grad_and_vars[0][1]
grad_and_var = (grad, v)
average_grads.append(grad_and_var)
return average_grads
示例10: __init__
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import variable [as 别名]
def __init__(self, name, inputs, tower_setup, for_imagenet_classification=False):
super(ResNet50, self).__init__()
#for now always freeze the batch norm of the resnet
inp, n_features_inp = prepare_input(inputs)
#for grayscale
if n_features_inp == 1:
inp = tf.concat([inp, inp, inp], axis=-1)
else:
assert n_features_inp == 3
#to keep the preprocessing consistent with our usual preprocessing, revert the std normalization
from ReID_net.datasets.Util.Normalization import IMAGENET_RGB_STD
#I double checked it, this seems to be the right preprocessing
inp = inp * IMAGENET_RGB_STD * 255
num_classes = 1000 if for_imagenet_classification else None
#note that we do not add the name to the variable scope at the moment, so that if we would use multiple resnets
#in the same network, this will throw an error.
#but if we add the name, the loading of pretrained weights will be difficult
with slim.arg_scope(slim.nets.resnet_v1.resnet_arg_scope()):
with slim.arg_scope([slim.model_variable, slim.variable], device=tower_setup.variable_device):
logits, end_points = slim.nets.resnet_v1.resnet_v1_50(inp, num_classes=num_classes, is_training=False)
#mapping from https://github.com/wuzheng-sjtu/FastFPN/blob/master/libs/nets/pyramid_network.py
mapping = {"C1": "resnet_v1_50/conv1/Relu:0",
"C2": "resnet_v1_50/block1/unit_2/bottleneck_v1",
"C3": "resnet_v1_50/block2/unit_3/bottleneck_v1",
"C4": "resnet_v1_50/block3/unit_5/bottleneck_v1",
"C5": "resnet_v1_50/block4/unit_3/bottleneck_v1"}
if for_imagenet_classification:
self.outputs = [tf.nn.softmax(logits)]
else:
# use C3 up to C5
self.outputs = [end_points[mapping[c]] for c in ["C3", "C4", "C5"]]
self.n_params = 25600000 # roughly 25.6M
示例11: darknet
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import variable [as 别名]
def darknet(net, classes, num_anchors, training=False, center=True):
def batch_norm(net):
net = slim.batch_norm(net, center=center, scale=True, epsilon=1e-5, is_training=training)
if not center:
net = tf.nn.bias_add(net, slim.variable('biases', shape=[tf.shape(net)[-1]], initializer=tf.zeros_initializer()))
return net
scope = __name__.split('.')[-2] + '_' + inspect.stack()[0][3]
net = tf.identity(net, name='%s/input' % scope)
with slim.arg_scope([slim.layers.conv2d], kernel_size=[3, 3], normalizer_fn=batch_norm, activation_fn=leaky_relu), slim.arg_scope([slim.layers.max_pool2d], kernel_size=[2, 2], padding='SAME'):
index = 0
channels = 32
for _ in range(2):
net = slim.layers.conv2d(net, channels, scope='%s/conv%d' % (scope, index))
net = slim.layers.max_pool2d(net, scope='%s/max_pool%d' % (scope, index))
index += 1
channels *= 2
for _ in range(2):
net = slim.layers.conv2d(net, channels, scope='%s/conv%d' % (scope, index))
index += 1
net = slim.layers.conv2d(net, channels / 2, kernel_size=[1, 1], scope='%s/conv%d' % (scope, index))
index += 1
net = slim.layers.conv2d(net, channels, scope='%s/conv%d' % (scope, index))
net = slim.layers.max_pool2d(net, scope='%s/max_pool%d' % (scope, index))
index += 1
channels *= 2
net = slim.layers.conv2d(net, channels, scope='%s/conv%d' % (scope, index))
index += 1
net = slim.layers.conv2d(net, channels / 2, kernel_size=[1, 1], scope='%s/conv%d' % (scope, index))
index += 1
net = slim.layers.conv2d(net, channels, scope='%s/conv%d' % (scope, index))
index += 1
net = slim.layers.conv2d(net, channels / 2, kernel_size=[1, 1], scope='%s/conv%d' % (scope, index))
index += 1
net = slim.layers.conv2d(net, channels, scope='%s/conv%d' % (scope, index))
passthrough = tf.identity(net, name=scope + '/passthrough')
net = slim.layers.max_pool2d(net, scope='%s/max_pool%d' % (scope, index))
index += 1
channels *= 2
# downsampling finished
net = slim.layers.conv2d(net, channels, scope='%s/conv%d' % (scope, index))
index += 1
net = slim.layers.conv2d(net, channels / 2, kernel_size=[1, 1], scope='%s/conv%d' % (scope, index))
index += 1
net = slim.layers.conv2d(net, channels, scope='%s/conv%d' % (scope, index))
index += 1
net = slim.layers.conv2d(net, channels / 2, kernel_size=[1, 1], scope='%s/conv%d' % (scope, index))
index += 1
net = slim.layers.conv2d(net, channels, scope='%s/conv%d' % (scope, index))
index += 1
net = slim.layers.conv2d(net, channels, scope='%s/conv%d' % (scope, index))
index += 1
net = slim.layers.conv2d(net, channels, scope='%s/conv%d' % (scope, index))
index += 1
with tf.name_scope(scope):
_net = reorg(passthrough)
net = tf.concat([_net, net], 3, name='%s/concat%d' % (scope, index))
net = slim.layers.conv2d(net, channels, scope='%s/conv%d' % (scope, index))
net = slim.layers.conv2d(net, num_anchors * (5 + classes), kernel_size=[1, 1], activation_fn=None, scope='%s/conv' % scope)
net = tf.identity(net, name='%s/output' % scope)
return scope, net
示例12: average_gradients
# 需要导入模块: from tensorflow.contrib import slim [as 别名]
# 或者: from tensorflow.contrib.slim import variable [as 别名]
def average_gradients(tower_grads):
"""Compute average gradients across all towers.
Calculate the average gradient for each shared variable across all towers.
Note that this function provides a synchronization point across all towers.
Credit:
https://github.com/naturomics/CapsNet-
Tensorflow/blob/master/dist_version/distributed_train.py
Args:
tower_grads:
List of lists of (gradient, variable) tuples. The outer list is over
individual gradients. The inner list is over the gradient calculation for each tower.
Returns:
average_grads:
List of pairs of (gradient, variable) where the gradient has been
averaged across all towers.
"""
average_grads = []
for grad_and_vars in zip(*tower_grads):
# Note that each grad_and_vars looks like the following:
# ((grad0_gpu0, var0_gpu0), ... , (grad0_gpuN, var0_gpuN))
grads = []
for g, _ in grad_and_vars:
# Add 0 dimension to the gradients to represent the tower.
expanded_g = tf.expand_dims(g, 0)
# Append on a 'tower' dimension which we will average over below.
grads.append(expanded_g)
# Average over the 'tower' dimension.
grad = tf.concat(axis=0, values=grads)
grad = tf.reduce_mean(grad, 0)
# Keep in mind that the Variables are redundant because they are shared
# across towers. So .. we will just return the first tower's pointer to
# the Variable.
v = grad_and_vars[0][1]
grad_and_var = (grad, v)
average_grads.append(grad_and_var)
return average_grads