本文整理汇总了Python中tensorflow.python.ops.array_ops.batch_to_space函数的典型用法代码示例。如果您正苦于以下问题:Python batch_to_space函数的具体用法?Python batch_to_space怎么用?Python batch_to_space使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了batch_to_space函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: pyr_up
def pyr_up(batch): # matches cv2.pyrUp()
s = array_ops.shape(batch)
zeros = array_ops.zeros([3 * s[0], s[1], s[2], s[3]])
res = array_ops.concat([batch, zeros], 0)
res = array_ops.batch_to_space(res, crops=[[0, 0], [0, 0]], block_size=2)
res = spatial_conv(res, 4)
return res
开发者ID:Albert-Z-Guo,项目名称:tensorflow,代码行数:7,代码来源:sliced_wasserstein_impl.py
示例2: testAtrousSequence
def testAtrousSequence(self):
"""Tests optimization of sequence of atrous convolutions.
Verifies that a sequence of `atrous_conv2d` operations with identical `rate`
parameters, 'SAME' `padding`, and `filters` with odd heights/ widths:
net = atrous_conv2d(net, filters1, rate, padding="SAME")
net = atrous_conv2d(net, filters2, rate, padding="SAME")
...
net = atrous_conv2d(net, filtersK, rate, padding="SAME")
is equivalent to:
pad = ... # padding so that the input dims are multiples of rate
net = space_to_batch(net, paddings=pad, block_size=rate)
net = conv2d(net, filters1, strides=[1, 1, 1, 1], padding="SAME")
net = conv2d(net, filters2, strides=[1, 1, 1, 1], padding="SAME")
...
net = conv2d(net, filtersK, strides=[1, 1, 1, 1], padding="SAME")
net = batch_to_space(net, crops=pad, block_size=rate)
"""
padding = "SAME" # The padding needs to be "SAME"
np.random.seed(1) # Make it reproducible.
with self.session(use_gpu=True):
# Input: [batch, height, width, input_depth]
for height in range(15, 17):
for width in range(15, 17):
x_shape = [3, height, width, 2]
x = np.random.random_sample(x_shape).astype(np.float32)
for kernel in [1, 3, 5]: # The kernel size needs to be odd.
# Filter: [kernel_height, kernel_width, input_depth, output_depth]
f_shape = [kernel, kernel, 2, 2]
f = 1e-2 * np.random.random_sample(f_shape).astype(np.float32)
for rate in range(2, 4):
# y1: three atrous_conv2d in a row.
y1 = nn_ops.atrous_conv2d(x, f, rate, padding=padding)
y1 = nn_ops.atrous_conv2d(y1, f, rate, padding=padding)
y1 = nn_ops.atrous_conv2d(y1, f, rate, padding=padding)
# y2: space_to_batch, three conv2d in a row, batch_to_space
pad_bottom = 0 if height % rate == 0 else rate - height % rate
pad_right = 0 if width % rate == 0 else rate - width % rate
pad = [[0, pad_bottom], [0, pad_right]]
y2 = array_ops.space_to_batch(x, paddings=pad, block_size=rate)
y2 = nn_ops.conv2d(y2, f, strides=[1, 1, 1, 1], padding=padding)
y2 = nn_ops.conv2d(y2, f, strides=[1, 1, 1, 1], padding=padding)
y2 = nn_ops.conv2d(y2, f, strides=[1, 1, 1, 1], padding=padding)
y2 = array_ops.batch_to_space(y2, crops=pad, block_size=rate)
self.assertAllClose(
y1.eval(), self.evaluate(y2), rtol=1e-2, atol=1e-2)
开发者ID:adit-chandra,项目名称:tensorflow,代码行数:52,代码来源:atrous_conv2d_test.py
示例3: _SpaceToBatchGrad
def _SpaceToBatchGrad(op, grad):
# Its gradient is the opposite op: BatchToSpace.
block_size = op.get_attr("block_size")
return [array_ops.batch_to_space(grad, op.inputs[1], block_size=block_size),
None]
开发者ID:0ruben,项目名称:tensorflow,代码行数:5,代码来源:array_grad.py
示例4: batch_to_space
def batch_to_space(*args, **kwargs):
return array_ops.batch_to_space(*args, **kwargs)
开发者ID:AndrewTwinz,项目名称:tensorflow,代码行数:2,代码来源:spacetobatch_op_test.py
示例5: atrous_conv2d
def atrous_conv2d(value, filters, rate, padding, name=None):
"""Atrous convolution (a.k.a. convolution with holes or dilated convolution).
Computes a 2-D atrous convolution, also known as convolution with holes or
dilated convolution, given 4-D `value` and `filters` tensors. If the `rate`
parameter is equal to one, it performs regular 2-D convolution. If the `rate`
parameter is greater than one, it performs convolution with holes, sampling
the input values every `rate` pixels in the `height` and `width` dimensions.
This is equivalent to convolving the input with a set of upsampled filters,
produced by inserting `rate - 1` zeros between two consecutive values of the
filters along the `height` and `width` dimensions, hence the name atrous
convolution or convolution with holes (the French word trous means holes in
English).
More specifically:
output[b, i, j, k] = sum_{di, dj, q} filters[di, dj, q, k] *
value[b, i + rate * di, j + rate * dj, q]
Atrous convolution allows us to explicitly control how densely to compute
feature responses in fully convolutional networks. Used in conjunction with
bilinear interpolation, it offers an alternative to `conv2d_transpose` in
dense prediction tasks such as semantic image segmentation, optical flow
computation, or depth estimation. It also allows us to effectively enlarge
the field of view of filters without increasing the number of parameters or
the amount of computation.
For a description of atrous convolution and how it can be used for dense
feature extraction, please see: [Semantic Image Segmentation with Deep
Convolutional Nets and Fully Connected CRFs](http://arxiv.org/abs/1412.7062).
The same operation is investigated further in [Multi-Scale Context Aggregation
by Dilated Convolutions](http://arxiv.org/abs/1511.07122). Previous works
that effectively use atrous convolution in different ways are, among others,
[OverFeat: Integrated Recognition, Localization and Detection using
Convolutional Networks](http://arxiv.org/abs/1312.6229) and [Fast Image
Scanning with Deep Max-Pooling Convolutional Neural Networks]
(http://arxiv.org/abs/1302.1700). Atrous convolution is also closely related
to the so-called noble identities in multi-rate signal processing.
There are many different ways to implement atrous convolution (see the refs
above). The implementation here reduces
atrous_conv2d(value, filters, rate, padding=padding)
to the following three operations:
paddings = ...
net = space_to_batch(value, paddings, block_size=rate)
net = conv2d(net, filters, strides=[1, 1, 1, 1], padding="VALID")
crops = ...
net = batch_to_space(net, crops, block_size=rate)
Advanced usage. Note the following optimization: A sequence of `atrous_conv2d`
operations with identical `rate` parameters, 'SAME' `padding`, and filters
with odd heights/ widths:
net = atrous_conv2d(net, filters1, rate, padding="SAME")
net = atrous_conv2d(net, filters2, rate, padding="SAME")
...
net = atrous_conv2d(net, filtersK, rate, padding="SAME")
can be equivalently performed cheaper in terms of computation and memory as:
pad = ... # padding so that the input dims are multiples of rate
net = space_to_batch(net, paddings=pad, block_size=rate)
net = conv2d(net, filters1, strides=[1, 1, 1, 1], padding="SAME")
net = conv2d(net, filters2, strides=[1, 1, 1, 1], padding="SAME")
...
net = conv2d(net, filtersK, strides=[1, 1, 1, 1], padding="SAME")
net = batch_to_space(net, crops=pad, block_size=rate)
because a pair of consecutive `space_to_batch` and `batch_to_space` ops with
the same `block_size` cancel out when their respective `paddings` and `crops`
inputs are identical.
Args:
value: A 4-D `Tensor` of type `float`. It needs to be in the default "NHWC"
format. Its shape is `[batch, in_height, in_width, in_channels]`.
filters: A 4-D `Tensor` with the same type as `value` and shape
`[filter_height, filter_width, in_channels, out_channels]`. `filters`'
`in_channels` dimension must match that of `value`. Atrous convolution is
equivalent to standard convolution with upsampled filters with effective
height `filter_height + (filter_height - 1) * (rate - 1)` and effective
width `filter_width + (filter_width - 1) * (rate - 1)`, produced by
inserting `rate - 1` zeros along consecutive elements across the
`filters`' spatial dimensions.
rate: A positive int32. The stride with which we sample input values across
the `height` and `width` dimensions. Equivalently, the rate by which we
upsample the filter values by inserting zeros across the `height` and
`width` dimensions. In the literature, the same parameter is sometimes
called `input stride` or `dilation`.
padding: A string, either `'VALID'` or `'SAME'`. The padding algorithm.
name: Optional name for the returned tensor.
Returns:
A `Tensor` with the same type as `value`.
Raises:
ValueError: If input/output depth does not match `filters`' shape, or if
padding is other than `'VALID'` or `'SAME'`.
#.........这里部分代码省略.........
开发者ID:ThomasWollmann,项目名称:tensorflow,代码行数:101,代码来源:nn_ops.py
示例6: atrous_pool2d
def atrous_pool2d(value,ksize, rate, padding, name=None, pooling_type="MAX"):
with ops.name_scope(name, "atrous_pool2d", [value]) as name:
value = ops.convert_to_tensor(value, name="value")
if rate < 1:
raise ValueError("rate {} cannot be less than one".format(rate))
if rate == 1:
if pooling_type == "MAX":
value = nn_ops.max_pool(value=value,
ksize=ksize,
strides=[1, 1, 1, 1],
padding=padding)
return value
elif pooling_type == "AVG":
value = nn_ops.avg_pool(value=value,
ksize=ksize,
strides=[1, 1, 1, 1],
padding=padding)
return value
else:
raise ValueError("Invalid pooling type")
# We have two padding contributions. The first is used for converting "SAME"
# to "VALID". The second is required so that the height and width of the
# zero-padded value tensor are multiples of rate.
# Padding required to reduce to "VALID" convolution
if padding == "SAME":
# Handle filters whose shape is unknown during graph creation.
# if filters.get_shape().is_fully_defined():
# filter_shape = filters.get_shape().as_list()
# else:
# filter_shape = array_ops.shape(filters)
# filter_height, filter_width = filter_shape[0], filter_shape[1]
kernel_height, kernel_width = ksize[1], ksize[2]
# Spatial dimensions of the filters and the upsampled filters in which we
# introduce (rate - 1) zeros between consecutive filter values.
kernel_height_up = kernel_height + (kernel_height - 1) * (rate - 1)
kernel_width_up = kernel_width + (kernel_width - 1) * (rate - 1)
pad_height = kernel_height_up - 1
pad_width = kernel_width_up - 1
# When pad_height (pad_width) is odd, we pad more to bottom (right),
# following the same convention as conv2d().
pad_top = pad_height // 2
pad_bottom = pad_height - pad_top
pad_left = pad_width // 2
pad_right = pad_width - pad_left
elif padding == "VALID":
pad_top = 0
pad_bottom = 0
pad_left = 0
pad_right = 0
else:
raise ValueError("Invalid padding")
# Handle input whose shape is unknown during graph creation.
if value.get_shape().is_fully_defined():
value_shape = value.get_shape().as_list()
else:
value_shape = array_ops.shape(value)
in_height = value_shape[1] + pad_top + pad_bottom
in_width = value_shape[2] + pad_left + pad_right
# More padding so that rate divides the height and width of the input.
pad_bottom_extra = (rate - in_height % rate) % rate
pad_right_extra = (rate - in_width % rate) % rate
# The paddings argument to space_to_batch includes both padding components.
space_to_batch_pad = [[pad_top, pad_bottom + pad_bottom_extra],
[pad_left, pad_right + pad_right_extra]]
value = array_ops.space_to_batch(input=value,
paddings=space_to_batch_pad,
block_size=rate)
if pooling_type == "MAX":
value = nn_ops.max_pool(value=value,
ksize=ksize,
strides=[1, 1, 1, 1],
padding="VALID",
name=name)
elif pooling_type == "AVG":
value = nn_ops.avg_pool(value=value,
ksize=ksize,
strides=[1, 1, 1, 1],
padding="VALID",
name=name)
else:
raise ValueError("Invalid pooling type")
# The crops argument to batch_to_space is just the extra padding component.
batch_to_space_crop = [[0, pad_bottom_extra], [0, pad_right_extra]]
value = array_ops.batch_to_space(input=value,
#.........这里部分代码省略.........
开发者ID:leezqcst,项目名称:regulatory-prediction,代码行数:101,代码来源:custom_ops.py
示例7: atrous_conv2d
def atrous_conv2d(value, filters, rate, padding, name=None):
with ops.op_scope([value, filters], name, "atrous_conv2d") as name:
value = ops.convert_to_tensor(value, name="value")
filters = ops.convert_to_tensor(filters, name="filters")
value_shape = value.get_shape()
filter_shape = filters.get_shape()
if not value_shape[3].is_compatible_with(filter_shape[2]):
raise ValueError(
"value's input channels does not match filters' input channels, "
"{} != {}".format(value_shape[3], filter_shape[2]))
if rate < 1:
raise ValueError("rate {} cannot be less than one".format(rate))
if rate == 1:
value = gen_nn_ops.conv2d(input=value,
filter=filters,
strides=[1, 1, 1, 1],
padding=padding)
return value
# We have two padding contributions. The first is used for converting "SAME"
# to "VALID". The second is required so that the height and width of the
# zero-padded value tensor are multiples of rate.
# Spatial dimensions of original input
value_shape = array_ops.shape(value)
in_height = value_shape[1]
in_width = value_shape[2]
# Spatial dimensions of the filters and the upsampled filters in which we
# introduce (rate - 1) zeros between consecutive filter values.
filter_shape = array_ops.shape(filters)
filter_height = filter_shape[0]
filter_width = filter_shape[1]
filter_height_up = filter_height + (filter_height - 1) * (rate - 1)
filter_width_up = filter_width + (filter_width - 1) * (rate - 1)
# Padding required to reduce to "VALID" convolution
if padding == "SAME":
pad_height = filter_height_up - 1
pad_width = filter_width_up - 1
elif padding == "VALID":
pad_height = 0
pad_width = 0
else:
raise ValueError("Invalid padding")
# When padding is "SAME" and the pad_height (pad_width) is odd, we pad more
# to bottom (right), following the same convention as conv2d().
pad_top = math_ops.floordiv(pad_height, 2)
pad_bottom = pad_height - pad_top
pad_left = math_ops.floordiv(pad_width, 2)
pad_right = pad_width - pad_left
# More padding so that rate divides the height and width of the input value
in_height = in_height + pad_top + pad_bottom
in_width = in_width + pad_left + pad_right
mod_height = math_ops.mod(in_height, rate)
mod_width = math_ops.mod(in_width, rate)
null = constant_op.constant(0)
pad_bottom_extra = control_flow_ops.cond(gen_math_ops.equal(mod_height, 0), lambda: null, lambda: rate - mod_height)
pad_right_extra = control_flow_ops.cond(gen_math_ops.equal(mod_width, 0), lambda: null, lambda: rate - mod_width)
# The paddings argument to space_to_batch includes both padding components
pad_bottom = pad_bottom + pad_bottom_extra
pad_right = pad_right + pad_right_extra
print 'hahahaha'
v = array_ops.expand_dims(array_ops.pack([pad_top, pad_bottom]),1)
h = array_ops.expand_dims(array_ops.pack([pad_left, pad_right]),1)
space_to_batch_pad = array_ops.concat(1, [v,h])
space_to_batch_pad = [[pad_top, pad_bottom],
[pad_left, pad_right]]
value = array_ops.space_to_batch(input=value,
paddings=space_to_batch_pad,
block_size=rate)
value = gen_nn_ops.conv2d(input=value,
filter=filters,
strides=[1, 1, 1, 1],
padding="VALID",
name=name)
# The crops argument to batch_to_space is just the extra padding component
v = array_ops.expand_dims(array_ops.pack([0, pad_bottom_extra]),1)
h = array_ops.expand_dims(array_ops.pack([0, pad_right_extra]),1)
batch_to_space_crop = array_ops.concat(1, [v,h])
batch_to_space_crop = [[0, pad_bottom_extra], [0, pad_right_extra]]
value = array_ops.batch_to_space(input=value,
crops=batch_to_space_crop,
block_size=rate)
return value
开发者ID:ypxie,项目名称:keras-1,代码行数:93,代码来源:numpy_backend.py
注:本文中的tensorflow.python.ops.array_ops.batch_to_space函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论