本文整理汇总了Python中tensorflow.extract_image_patches方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.extract_image_patches方法的具体用法?Python tensorflow.extract_image_patches怎么用?Python tensorflow.extract_image_patches使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.extract_image_patches方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _VerifyValues
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import extract_image_patches [as 别名]
def _VerifyValues(self, image, ksizes, strides, rates, padding, patches):
"""Tests input-output pairs for the ExtractImagePatches op.
Args:
image: Input tensor with shape: [batch, in_rows, in_cols, depth].
ksizes: Patch size specified as: [ksize_rows, ksize_cols].
strides: Output strides, specified as [stride_rows, stride_cols].
rates: Atrous rates, specified as [rate_rows, rate_cols].
padding: Padding type.
patches: Expected output.
"""
ksizes = [1] + ksizes + [1]
strides = [1] + strides + [1]
rates = [1] + rates + [1]
with self.test_session(use_gpu=True):
out_tensor = tf.extract_image_patches(
tf.constant(image),
ksizes=ksizes,
strides=strides,
rates=rates,
padding=padding,
name="im2col")
self.assertAllClose(patches, out_tensor.eval())
示例2: patch_decomposition
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import extract_image_patches [as 别名]
def patch_decomposition(self, T_features):
# patch decomposition
patch_size = 1
patches_as_depth_vectors = tf.extract_image_patches(
images=T_features, ksizes=[1, patch_size, patch_size, 1],
strides=[1, 1, 1, 1], rates=[1, 1, 1, 1], padding='VALID',
name='patches_as_depth_vectors')
self.patches_NHWC = tf.reshape(
patches_as_depth_vectors,
shape=[-1, patch_size, patch_size, patches_as_depth_vectors.shape[3].value],
name='patches_PHWC')
self.patches_HWCN = tf.transpose(
self.patches_NHWC,
perm=[1, 2, 3, 0],
name='patches_HWCP') # tf.conv2 ready format
return self.patches_HWCN
示例3: compute_cost_volume
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import extract_image_patches [as 别名]
def compute_cost_volume(x1, x2, H, W, channel, d=9):
x1 = tf.nn.l2_normalize(x1, axis=3)
x2 = tf.nn.l2_normalize(x2, axis=3)
# choice 1: use tf.extract_image_patches, may not work for some tensorflow versions
x2_patches = tf.extract_image_patches(x2, [1, d, d, 1], strides=[1, 1, 1, 1], rates=[1, 1, 1, 1], padding='SAME')
# choice 2: use convolution, but is slower than choice 1
# out_channels = d * d
# w = tf.eye(out_channels*channel, dtype=tf.float32)
# w = tf.reshape(w, (d, d, channel, out_channels*channel))
# x2_patches = tf.nn.conv2d(x2, w, strides=[1, 1, 1, 1], padding='SAME')
x2_patches = tf.reshape(x2_patches, [-1, H, W, d, d, channel])
x1_reshape = tf.reshape(x1, [-1, H, W, 1, 1, channel])
x1_dot_x2 = tf.multiply(x1_reshape, x2_patches)
cost_volume = tf.reduce_sum(x1_dot_x2, axis=-1)
#cost_volume = tf.reduce_mean(x1_dot_x2, axis=-1)
cost_volume = tf.reshape(cost_volume, [-1, H, W, d*d])
return cost_volume
示例4: extract_patches_fn
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import extract_image_patches [as 别名]
def extract_patches_fn(image: tf.Tensor, patch_shape: Tuple[int, int], offsets: Tuple[int, int]) -> tf.Tensor:
"""Will cut a given image into patches.
:param image: tf.Tensor
:param patch_shape: shape of the extracted patches [h, w]
:param offsets: offset to add to the origin of first patch top-right coordinate, useful during data augmentation \
to have slighlty different patches each time. This value will be multiplied by [h/2, w/2] (range values [0,1])
:return: patches [batch_patches, h, w, c]
"""
with tf.name_scope('patch_extraction'):
h, w = patch_shape
c = image.get_shape()[-1]
offset_h = tf.cast(tf.round(offsets[0] * h // 2), dtype=tf.int32)
offset_w = tf.cast(tf.round(offsets[1] * w // 2), dtype=tf.int32)
offset_img = image[offset_h:, offset_w:, :]
offset_img = offset_img[None, :, :, :]
patches = tf.extract_image_patches(offset_img, ksizes=[1, h, w, 1], strides=[1, h // 2, w // 2, 1],
rates=[1, 1, 1, 1], padding='VALID')
patches_shape = tf.shape(patches)
return tf.reshape(patches, [tf.reduce_prod(patches_shape[:3]), h, w, int(c)])
示例5: kernel_tile
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import extract_image_patches [as 别名]
def kernel_tile(input, kernel, stride):
# output = tf.extract_image_patches(input, ksizes=[1, kernel, kernel, 1], strides=[1, stride, stride, 1], rates=[1, 1, 1, 1], padding='VALID')
input_shape = input.get_shape()
tile_filter = np.zeros(shape=[kernel, kernel, input_shape[3],
kernel * kernel], dtype=np.float32)
for i in range(kernel):
for j in range(kernel):
tile_filter[i, j, :, i * kernel + j] = 1.0
tile_filter_op = tf.constant(tile_filter, dtype=tf.float32)
output = tf.nn.depthwise_conv2d(input, tile_filter_op, strides=[
1, stride, stride, 1], padding='VALID')
output_shape = output.get_shape()
output = tf.reshape(output, shape=[int(output_shape[0]), int(
output_shape[1]), int(output_shape[2]), int(input_shape[3]), kernel * kernel])
output = tf.transpose(output, perm=[0, 1, 2, 4, 3])
return output
# input should be a tensor with size as [batch_size, caps_num_i, 16]
示例6: create_conv_hessian_computing_tf_graph
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import extract_image_patches [as 别名]
def create_conv_hessian_computing_tf_graph(input_shape, layer_kernel, layer_stride):
"""
This function create the TensorFlow graph for computing hessian matrix for fully-connected layer.
Compared with create_res_hessian_computing_tf_graph, it append extract one for bias term.
"""
input_holder = tf.placeholder(dtype=tf.float32, shape=input_shape)
patches = tf.extract_image_patches(images = input_holder,
ksizes = [1,layer_kernel, layer_kernel,1],
strides = [1, layer_stride, layer_stride, 1],
rates = [1, 1, 1, 1],
padding = 'SAME')
print 'Patches shape: %s' %patches.get_shape()
vect_w_b = tf.concat([patches, tf.ones([tf.shape(patches)[0], \
tf.shape(patches)[1], tf.shape(patches)[2], 1])], axis=3)
a = tf.expand_dims(vect_w_b, axis=-1)
b = tf.expand_dims(vect_w_b, axis=3)
outprod = tf.multiply(a, b)
# print 'outprod shape: %s' %outprod.get_shape()
get_hessian_op = tf.reduce_mean(outprod, axis=[0, 1, 2])
print 'Hessian shape: %s' % get_hessian_op.get_shape()
return input_holder, get_hessian_op
# Construct hessian inverse computing graph for Woodbury
示例7: forward
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import extract_image_patches [as 别名]
def forward(self):
inp = self.inp.out
s = self.lay.stride
self.out = tf.extract_image_patches(
inp, [1,s,s,1], [1,s,s,1], [1,1,1,1], 'VALID')
示例8: _forward
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import extract_image_patches [as 别名]
def _forward(self, vs):
if self.local: # expand input patches and split by filters
input_local_expanded = tf.extract_image_patches(self.inpt,
pad_shape(self.ksize),
self.strides,
[1, 1, 1, 1],
padding=self.padding)
values = []
for filt in tf.split(axis=3, num_or_size_splits=self.n_filters, value=self.filters):
channel_i = tf.reduce_sum(tf.multiply(filt, input_local_expanded), 3,
keep_dims=True)
values.append(channel_i)
self.output = tf.concat(axis=3, values=values)
else: # split by images in batch and map to regular conv2d function
inpt = tf.expand_dims(self.inpt, 1)
filt_shape = [-1, self.ksize[0], self.ksize[1], self.n_cin, self.n_filters]
filt = tf.reshape(self.filters, filt_shape)
elems = (inpt, filt)
result = tf.map_fn(lambda x: tf.nn.conv2d(x[0], x[1],
self.strides,
self.padding), elems,
dtype=tf.float32, infer_shape=False)
result = tf.squeeze(result, [1])
result.set_shape(self.inpt.get_shape()[:-1].concatenate([self.n_filters]))
self.output = result
示例9: make_covariance_update_op
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import extract_image_patches [as 别名]
def make_covariance_update_op(self, ema_decay, ema_weight):
filter_height, filter_width, _, _ = self._filter_shape
# TODO(b/64144716): there is potential here for a big savings in terms
# of memory use.
if self._dilations is None:
rates = (1, 1, 1, 1)
else:
rates = tuple(self._dilations)
self._patches = []
for tower in range(self._num_towers):
with maybe_place_on_device(self._get_data_device(tower)):
patches = tf.extract_image_patches(
self._inputs[tower],
ksizes=[1, filter_height, filter_width, 1],
strides=self._strides,
rates=rates,
padding=self._padding)
if self._patch_mask is not None:
assert self._patch_mask.shape == self._filter_shape[0:-1]
# This should work as intended due to broadcasting.
patches *= self._patch_mask
if self._has_bias:
patches = append_homog(patches)
self._patches.append(patches)
return super(ConvDiagonalFactor, self).make_covariance_update_op(
ema_decay, ema_weight)
示例10: reorg_layer
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import extract_image_patches [as 别名]
def reorg_layer(net, stride=2, name='reorg'):
with tf.name_scope(name):
net = tf.extract_image_patches(net,
ksizes=[1, stride, stride, 1],
strides=[1, stride, stride, 1],
rates=[1, 1, 1, 1],
padding='VALID')
return net
示例11: extract_image_patches
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import extract_image_patches [as 别名]
def extract_image_patches(x, ksizes, ssizes, padding='same',
data_format='channels_last'):
'''
Extract the patches from an image
# Parameters
x : The input image
ksizes : 2-d tuple with the kernel size
ssizes : 2-d tuple with the strides size
padding : 'same' or 'valid'
data_format : 'channels_last' or 'channels_first'
# Returns
The (k_w,k_h) patches extracted
TF ==> (batch_size,w,h,k_w,k_h,c)
TH ==> (batch_size,w,h,c,k_w,k_h)
'''
kernel = [1, ksizes[0], ksizes[1], 1]
strides = [1, ssizes[0], ssizes[1], 1]
padding = _preprocess_padding(padding)
if data_format == 'channels_first':
x = KTF.permute_dimensions(x, (0, 2, 3, 1))
bs_i, w_i, h_i, ch_i = KTF.int_shape(x)
patches = tf.extract_image_patches(x, kernel, strides, [1, 1, 1, 1],
padding)
# Reshaping to fit Theano
bs, w, h, ch = KTF.int_shape(patches)
patches = tf.reshape(tf.transpose(tf.reshape(patches, [-1, w, h, tf.floordiv(ch, ch_i), ch_i]), [0, 1, 2, 4, 3]),
[-1, w, h, ch_i, ksizes[0], ksizes[1]])
if data_format == 'channels_last':
patches = KTF.permute_dimensions(patches, [0, 1, 2, 4, 5, 3])
return patches
示例12: _reorg
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import extract_image_patches [as 别名]
def _reorg(self, name, inputs, stride, data_format, use_space_to_depth=True, darknet_original=False):
with tf.name_scope(name):
# darknet original reorg layer is weird.
# As default we don't use darknet original reorg.
# See detail: https://github.com/LeapMind/lmnet/issues/16
if darknet_original:
# TODO(wakisaka): You can use only data_format == "NHWC", yet.
assert data_format == "NHWC"
input_shape = tf.shape(inputs)
# channel shape use static.
b, h, w, c = input_shape[0], input_shape[1], input_shape[2], inputs.get_shape().as_list()[3]
output_h = h // stride
output_w = w // stride
output_c = c * stride * stride
transpose_tensor = tf.transpose(inputs, [0, 3, 1, 2])
reshape_tensor = tf.reshape(transpose_tensor, [b, (c // (stride * stride)), h, stride, w, stride])
transpose_tensor = tf.transpose(reshape_tensor, [0, 3, 5, 1, 2, 4])
reshape_tensor = tf.reshape(transpose_tensor, [b, output_c, output_h, output_w])
transpose_tensor = tf.transpose(reshape_tensor, [0, 2, 3, 1])
outputs = tf.reshape(transpose_tensor, [b, output_h, output_w, output_c])
return outputs
else:
# tf.extract_image_patches() raise error with images_placeholder `None` shape as dynamic image.
# Github issue: https://github.com/leapmindadmin/lmnet/issues/17
# Currently, I didn't try to space_to_depth with images_placeholder `None` shape as dynamic image.
if use_space_to_depth:
outputs = tf.space_to_depth(inputs, stride, data_format=data_format)
return outputs
else:
# TODO(wakisaka): You can use only data_format == "NHWC", yet.
assert data_format == "NHWC"
ksize = [1, stride, stride, 1]
strides = [1, stride, stride, 1]
rates = [1, 1, 1, 1]
outputs = tf.extract_image_patches(inputs, ksize, strides, rates, "VALID")
return outputs
示例13: testGradient
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import extract_image_patches [as 别名]
def testGradient(self):
# Set graph seed for determinism.
random_seed = 42
tf.set_random_seed(random_seed)
with self.test_session():
for test_case in self._TEST_CASES:
np.random.seed(random_seed)
in_shape = test_case['in_shape']
in_val = tf.constant(np.random.random(in_shape),
dtype=tf.float32)
for padding in ['VALID', 'SAME']:
out_val = tf.extract_image_patches(in_val,
test_case['ksizes'],
test_case['strides'],
test_case['rates'],
padding)
out_shape = out_val.get_shape().as_list()
err = tf.test.compute_gradient_error(
in_val, in_shape, out_val, out_shape
)
print('extract_image_patches gradient err: %.4e' % err)
self.assertLess(err, 1e-4)
示例14: im2col
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import extract_image_patches [as 别名]
def im2col(
x: Union[tf.Tensor, np.ndarray],
h_filter: int,
w_filter: int,
padding: str,
stride: int,
) -> tf.Tensor:
"""Generic implementation of im2col on tf.Tensors."""
with tf.name_scope("im2col"):
# we need NHWC because tf.extract_image_patches expects this
nhwc_tensor = tf.transpose(x, [0, 2, 3, 1])
channels = int(nhwc_tensor.shape[3])
# extract patches
patch_tensor = tf.extract_image_patches(
nhwc_tensor,
ksizes=[1, h_filter, w_filter, 1],
strides=[1, stride, stride, 1],
rates=[1, 1, 1, 1],
padding=padding,
)
# change back to NCHW
patch_tensor_nchw = tf.reshape(
tf.transpose(patch_tensor, [3, 1, 2, 0]), (h_filter, w_filter, channels, -1)
)
# reshape to x_col
x_col_tensor = tf.reshape(
tf.transpose(patch_tensor_nchw, [2, 0, 1, 3]),
(channels * h_filter * w_filter, -1),
)
return x_col_tensor
示例15: ternary_loss
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import extract_image_patches [as 别名]
def ternary_loss(im1, im2_warped, mask, max_distance=1):
patch_size = 2 * max_distance + 1
with tf.variable_scope('ternary_loss'):
def _ternary_transform(image):
intensities = tf.image.rgb_to_grayscale(image) * 255
#patches = tf.extract_image_patches( # fix rows_in is None
# intensities,
# ksizes=[1, patch_size, patch_size, 1],
# strides=[1, 1, 1, 1],
# rates=[1, 1, 1, 1],
# padding='SAME')
out_channels = patch_size * patch_size
w = np.eye(out_channels).reshape((patch_size, patch_size, 1, out_channels))
weights = tf.constant(w, dtype=tf.float32)
patches = tf.nn.conv2d(intensities, weights, strides=[1, 1, 1, 1], padding='SAME')
transf = patches - intensities
transf_norm = transf / tf.sqrt(0.81 + tf.square(transf))
return transf_norm
def _hamming_distance(t1, t2):
dist = tf.square(t1 - t2)
dist_norm = dist / (0.1 + dist)
dist_sum = tf.reduce_sum(dist_norm, 3, keepdims=True)
return dist_sum
t1 = _ternary_transform(im1)
t2 = _ternary_transform(im2_warped)
dist = _hamming_distance(t1, t2)
transform_mask = create_mask(mask, [[max_distance, max_distance],
[max_distance, max_distance]])
return charbonnier_loss(dist, mask * transform_mask)