本文整理汇总了Python中tensorflow.contrib方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.contrib方法的具体用法?Python tensorflow.contrib怎么用?Python tensorflow.contrib使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.contrib方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __depthwise_conv2d_p
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import contrib [as 别名]
def __depthwise_conv2d_p(name, x, w=None, kernel_size=(3, 3), padding='SAME', stride=(1, 1),
initializer=tf.contrib.layers.xavier_initializer(), l2_strength=0.0, bias=0.0):
with tf.variable_scope(name):
stride = [1, stride[0], stride[1], 1]
kernel_shape = [kernel_size[0], kernel_size[1], x.shape[-1], 1]
with tf.name_scope('layer_weights'):
if w is None:
w = __variable_with_weight_decay(kernel_shape, initializer, l2_strength)
__variable_summaries(w)
with tf.name_scope('layer_biases'):
if isinstance(bias, float):
bias = tf.get_variable('biases', [x.shape[-1]], initializer=tf.constant_initializer(bias))
__variable_summaries(bias)
with tf.name_scope('layer_conv2d'):
conv = tf.nn.depthwise_conv2d(x, w, stride, padding)
out = tf.nn.bias_add(conv, bias)
return out
示例2: depthwise_conv2d
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import contrib [as 别名]
def depthwise_conv2d(name, x, w=None, kernel_size=(3, 3), padding='SAME', stride=(1, 1),
initializer=tf.contrib.layers.xavier_initializer(), l2_strength=0.0, bias=0.0, activation=None,
batchnorm_enabled=False, is_training=True):
"""Implementation of depthwise 2D convolution wrapper"""
with tf.variable_scope(name) as scope:
conv_o_b = __depthwise_conv2d_p(name=scope, x=x, w=w, kernel_size=kernel_size, padding=padding,
stride=stride, initializer=initializer, l2_strength=l2_strength, bias=bias)
if batchnorm_enabled:
conv_o_bn = tf.layers.batch_normalization(conv_o_b, training=is_training)
if not activation:
conv_a = conv_o_bn
else:
conv_a = activation(conv_o_bn)
else:
if not activation:
conv_a = conv_o_b
else:
conv_a = activation(conv_o_b)
return conv_a
示例3: depthwise_separable_conv2d
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import contrib [as 别名]
def depthwise_separable_conv2d(name, x, w_depthwise=None, w_pointwise=None, width_multiplier=1.0, num_filters=16,
kernel_size=(3, 3),
padding='SAME', stride=(1, 1),
initializer=tf.contrib.layers.xavier_initializer(), l2_strength=0.0, biases=(0.0, 0.0),
activation=None, batchnorm_enabled=True,
is_training=True):
"""Implementation of depthwise separable 2D convolution operator as in MobileNet paper"""
total_num_filters = int(round(num_filters * width_multiplier))
with tf.variable_scope(name) as scope:
conv_a = depthwise_conv2d('depthwise', x=x, w=w_depthwise, kernel_size=kernel_size, padding=padding,
stride=stride,
initializer=initializer, l2_strength=l2_strength, bias=biases[0],
activation=activation,
batchnorm_enabled=batchnorm_enabled, is_training=is_training)
conv_o = conv2d('pointwise', x=conv_a, w=w_pointwise, num_filters=total_num_filters, kernel_size=(1, 1),
initializer=initializer, l2_strength=l2_strength, bias=biases[1], activation=activation,
batchnorm_enabled=batchnorm_enabled, is_training=is_training)
return conv_a, conv_o
############################################################################################################
# Fully Connected layer Methods
示例4: dense_p
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import contrib [as 别名]
def dense_p(name, x, w=None, output_dim=128, initializer=tf.contrib.layers.xavier_initializer(), l2_strength=0.0,
bias=0.0):
"""
Fully connected layer
:param name: (string) The name scope provided by the upper tf.name_scope('name') as scope.
:param x: (tf.tensor) The input to the layer (N, D).
:param output_dim: (integer) It specifies H, the output second dimension of the fully connected layer [ie:(N, H)]
:param initializer: (tf.contrib initializer) The initialization scheme, He et al. normal or Xavier normal are recommended.
:param l2_strength:(weight decay) (float) L2 regularization parameter.
:param bias: (float) Amount of bias. (if not float, it means pretrained bias)
:return out: The output of the layer. (N, H)
"""
n_in = x.get_shape()[-1].value
with tf.variable_scope(name):
if w == None:
w = variable_with_weight_decay([n_in, output_dim], initializer, l2_strength)
variable_summaries(w)
if isinstance(bias, float):
bias = tf.get_variable("layer_biases", [output_dim], tf.float32, tf.constant_initializer(bias))
variable_summaries(bias)
output = tf.nn.bias_add(tf.matmul(x, w), bias)
return output
示例5: __depthwise_conv2d_atrous_p
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import contrib [as 别名]
def __depthwise_conv2d_atrous_p(name, x, w=None, kernel_size=(3, 3), padding='SAME', stride=(1, 1),
initializer=tf.contrib.layers.xavier_initializer(), l2_strength=0.0, bias=0.0,
dilation_factor= 1):
with tf.variable_scope(name):
stride = [1, stride[0], stride[1], 1]
kernel_shape = [kernel_size[0], kernel_size[1], x.shape[-1], 1]
with tf.name_scope('layer_weights'):
if w == None:
w = variable_with_weight_decay(kernel_shape, initializer, l2_strength)
variable_summaries(w)
with tf.name_scope('layer_biases'):
if isinstance(bias, float):
bias = tf.get_variable('biases', [x.shape[-1]], initializer=tf.constant_initializer(bias))
variable_summaries(bias)
with tf.name_scope('layer_conv2d'):
conv = tf.nn.depthwise_conv2d(x, w, stride, padding, [dilation_factor, dilation_factor])
out = tf.nn.bias_add(conv, bias)
return out
示例6: depthwise_conv2d
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import contrib [as 别名]
def depthwise_conv2d(name, x, w=None, kernel_size=(3, 3), padding='SAME', stride=(1, 1),dilation_factor=1,
initializer=tf.contrib.layers.xavier_initializer(), l2_strength=0.0, bias=0.0, activation=None,
batchnorm_enabled=False, is_training=True):
with tf.variable_scope(name) as scope:
if dilation_factor>1:
conv_o_b = __depthwise_conv2d_atrous_p(name=scope, x=x, w=w, kernel_size=kernel_size, padding=padding,
stride=stride, initializer=initializer, l2_strength=l2_strength, bias=bias,
dilation_factor= dilation_factor)
else:
conv_o_b = __depthwise_conv2d_p(name=scope, x=x, w=w, kernel_size=kernel_size, padding=padding,
stride=stride, initializer=initializer, l2_strength=l2_strength, bias=bias)
if batchnorm_enabled:
conv_o_bn = tf.layers.batch_normalization(conv_o_b, training=is_training)
if not activation:
conv_a = conv_o_bn
else:
conv_a = activation(conv_o_bn)
else:
if not activation:
conv_a = conv_o_b
else:
conv_a = activation(conv_o_b)
return conv_a
示例7: depthwise_separable_conv2d
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import contrib [as 别名]
def depthwise_separable_conv2d(name, x, w_depthwise=None, w_pointwise=None, width_multiplier=1.0, num_filters=16,
kernel_size=(3, 3),
padding='SAME', stride=(1, 1),
initializer=tf.contrib.layers.xavier_initializer(), l2_strength=0.0, biases=(0.0, 0.0),
activation=None, batchnorm_enabled=True,
is_training=True):
total_num_filters = int(round(num_filters * width_multiplier))
with tf.variable_scope(name) as scope:
conv_a = depthwise_conv2d('depthwise', x=x, w=w_depthwise, kernel_size=kernel_size, padding=padding,
stride=stride,
initializer=initializer, l2_strength=l2_strength, bias=biases[0],
activation=activation,
batchnorm_enabled=batchnorm_enabled, is_training=is_training)
conv_o = conv2d('pointwise', x=conv_a, w=w_pointwise, num_filters=total_num_filters, kernel_size=(1, 1),
initializer=initializer, l2_strength=l2_strength, bias=biases[1], activation=activation,
batchnorm_enabled=batchnorm_enabled, is_training=is_training)
return conv_o
示例8: depthwise_conv2d
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import contrib [as 别名]
def depthwise_conv2d(name, x, w=None, kernel_size=(3, 3), padding='SAME', stride=(1, 1),
initializer=tf.contrib.layers.xavier_initializer(), l2_strength=0.0, bias=0.0, activation=None,
batchnorm_enabled=False, is_training=True):
with tf.variable_scope(name) as scope:
conv_o_b = __depthwise_conv2d_p(name='conv', x=x, w=w, kernel_size=kernel_size, padding=padding,
stride=stride, initializer=initializer, l2_strength=l2_strength, bias=bias)
if batchnorm_enabled:
conv_o_bn = tf.layers.batch_normalization(conv_o_b, training=is_training, epsilon=1e-5)
if not activation:
conv_a = conv_o_bn
else:
conv_a = activation(conv_o_bn)
else:
if not activation:
conv_a = conv_o_b
else:
conv_a = activation(conv_o_b)
return conv_a
############################################################################################################
# ShuffleNet unit methods
示例9: inception_score
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import contrib [as 别名]
def inception_score(images, num_batches=None):
"""IS function from tf.contrib
Args:
images: must be 4-D tensor, ranges from [0, 255]
num_batches: Number of batches to split `generated_images` in to in
order to efficiently run them through the classifier network.
"""
batches = images.shape[0]
if not num_batches:
num_batches = (batches + _INCEPTION_BATCH - 1) // _INCEPTION_BATCH
graph = _TFGAN.get_graph_def_from_url_tarball(
'http://download.tensorflow.org/models/frozen_inception_v1_2015_12_05.tar.gz',
'inceptionv1_for_inception_score.pb',
'/tmp/frozen_inception_v1_2015_12_05.tar.gz')
return _TFGAN.classifier_score(
images=images,
classifier_fn=partial(_run_inception,
layer_name='logits:0',
inception_graph=graph),
num_batches=num_batches)
示例10: __conv2d_p
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import contrib [as 别名]
def __conv2d_p(name, x, w=None, num_filters=16, kernel_size=(3, 3), padding='SAME', stride=(1, 1),
initializer=tf.contrib.layers.xavier_initializer(), l2_strength=0.0, bias=0.0):
"""
Convolution 2D Wrapper
:param name: (string) The name scope provided by the upper tf.name_scope('name') as scope.
:param x: (tf.tensor) The input to the layer (N, H, W, C).
:param w: (tf.tensor) pretrained weights (if None, it means no pretrained weights)
:param num_filters: (integer) No. of filters (This is the output depth)
:param kernel_size: (integer tuple) The size of the convolving kernel.
:param padding: (string) The amount of padding required.
:param stride: (integer tuple) The stride required.
:param initializer: (tf.contrib initializer) The initialization scheme, He et al. normal or Xavier normal are recommended.
:param l2_strength:(weight decay) (float) L2 regularization parameter.
:param bias: (float) Amount of bias. (if not float, it means pretrained bias)
:return out: The output of the layer. (N, H', W', num_filters)
"""
with tf.variable_scope(name):
stride = [1, stride[0], stride[1], 1]
kernel_shape = [kernel_size[0], kernel_size[1], x.shape[-1], num_filters]
with tf.name_scope('layer_weights'):
if w == None:
w = __variable_with_weight_decay(kernel_shape, initializer, l2_strength)
__variable_summaries(w)
with tf.name_scope('layer_biases'):
if isinstance(bias, float):
bias = tf.get_variable('biases', [num_filters], initializer=tf.constant_initializer(bias))
__variable_summaries(bias)
with tf.name_scope('layer_conv2d'):
conv = tf.nn.conv2d(x, w, stride, padding)
out = tf.nn.bias_add(conv, bias)
return out
示例11: test_forward_inception_v1
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import contrib [as 别名]
def test_forward_inception_v1():
'''test inception V1 model'''
with tf.Graph().as_default():
graph_def = nnvm.testing.tf.get_workload("InceptionV1/classify_image_graph_def-with_shapes.pb")
# Call the utility to import the graph definition into default graph.
graph_def = nnvm.testing.tf.ProcessGraphDefParam(graph_def)
# Build an image from random data.
from PIL import Image
from tvm.contrib import util
img_array = np.random.uniform(size=(1, 600, 600, 3)).astype("uint8")
img = Image.frombuffer('RGB', (600, 600), img_array.tostring(), 'raw', 'RGB', 0, 1)
temp = util.tempdir()
img_path = temp.relpath("tf-test.jpg")
img.save(img_path);
import os.path
if not tf.gfile.Exists(os.path.join(img_path)):
tf.logging.fatal('File does not exist %s', image)
data = tf.gfile.FastGFile(os.path.join(img_path), 'rb').read()
temp.remove()
# Extract tensorflow decoded image frame for tvm input
with tf.Session() as sess:
tvm_data = run_tf_graph(sess, data, 'DecodeJpeg/contents:0', 'DecodeJpeg:0')
with tf.Session() as sess:
tf_output = run_tf_graph(sess, data, 'DecodeJpeg/contents:0', 'softmax:0')
tvm_output = run_tvm_graph(graph_def, tvm_data, 'DecodeJpeg/contents')
np.testing.assert_allclose(tf_output, tvm_output, rtol=1e-5, atol=1e-5)
#######################################################################
# Mobilenet
# ---------
示例12: testContrib
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import contrib [as 别名]
def testContrib(self):
# pylint: disable=g-import-not-at-top
import tensorflow as tf
_ = tf.contrib.layers # `tf.contrib` is loaded lazily on first use.
assert inspect.ismodule(tf.contrib)
示例13: testLayers
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import contrib [as 别名]
def testLayers(self):
# pylint: disable=g-import-not-at-top
import tensorflow as tf
assert inspect.ismodule(tf.contrib.layers)
示例14: testLinearOptimizer
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import contrib [as 别名]
def testLinearOptimizer(self):
# pylint: disable=g-import-not-at-top
import tensorflow as tf
assert inspect.ismodule(tf.contrib.linear_optimizer)
示例15: serve
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import contrib [as 别名]
def serve(self, config, model_path, gpuid=0):
v1_export_dir = os.path.join(model_path, _V1_SAVED_MODEL_DIR)
if os.path.exists(v1_export_dir):
raise ValueError('SavedModel exported with OpenNMT-tf 1.x are no longer supported. '
'They include ops from tf.contrib which is not included in '
'TensorFlow 2.x binaries. To upgrade automatically, you can release '
'or serve from a OpenNMT-tf 1.x training checkpoint.')
export_dir = os.path.join(model_path, _SAVED_MODEL_DIR)
translate_fn = tf.saved_model.load(export_dir).signatures['serving_default']
return None, translate_fn