本文整理汇总了Python中tensorflow.compat.v1.random_crop方法的典型用法代码示例。如果您正苦于以下问题:Python v1.random_crop方法的具体用法?Python v1.random_crop怎么用?Python v1.random_crop使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.compat.v1
的用法示例。
在下文中一共展示了v1.random_crop方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _distort_image
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import random_crop [as 别名]
def _distort_image(self, image):
"""Distort one image for training a network.
Adopted the standard data augmentation scheme that is widely used for
this dataset: the images are first zero-padded with 4 pixels on each side,
then randomly cropped to again produce distorted images; half of the images
are then horizontally mirrored.
Args:
image: input image.
Returns:
distorted image.
"""
image = tf.image.resize_image_with_crop_or_pad(
image, self.height + 8, self.width + 8)
distorted_image = tf.random_crop(image,
[self.height, self.width, self.depth])
# Randomly flip the image horizontally.
distorted_image = tf.image.random_flip_left_right(distorted_image)
if self.summary_verbosity >= 3:
tf.summary.image('distorted_image', tf.expand_dims(distorted_image, 0))
return distorted_image
示例2: patch_discriminator
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import random_crop [as 别名]
def patch_discriminator(x, filters=64, filter_size=5, n=4,
name="patch_discrim"):
"""Patch descriminator."""
with tf.variable_scope(name):
x_shape = shape_list(x)
spatial_dims = [x_shape[1] // 4, x_shape[2] // 4]
x = tf.random_crop(x, [x_shape[0]] + spatial_dims + [x_shape[3]])
for i in range(n):
x = general_conv(
x=x,
num_filters=filters * 2**i,
filter_size=filter_size,
stride=2 if i != n - 1 else 1,
stddev=0.02,
padding="SAME",
name="c%d" % i,
do_norm="instance" if i != 0 else False,
do_relu=i != n - 1,
relufactor=0.2)
x = tf.reduce_mean(x, [1, 2])
return x
示例3: randomize
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import random_crop [as 别名]
def randomize(images, init_shape, expand_shape=None, crop_shape=None,
vertical_flip=False):
"""Returns a function that randomly translates and flips images."""
def random_image(image):
"""Randmly translates and flips images."""
image = tf.reshape(image, init_shape)
current_shape = init_shape
if expand_shape is not None and expand_shape != current_shape:
if expand_shape[-1] != current_shape[-1]:
raise ValueError('Number channels is not specified correctly.')
image = tf.image.resize_image_with_crop_or_pad(
image, expand_shape[0], expand_shape[1])
current_shape = expand_shape
if crop_shape is not None and crop_shape != current_shape:
image = tf.random_crop(image, crop_shape)
if vertical_flip:
image = tf.image.random_flip_left_right(image)
return image
return tf.map_fn(random_image, images)
示例4: should_distort_images
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import random_crop [as 别名]
def should_distort_images(flip_left_right, random_crop, random_scale,
random_brightness):
"""Whether any distortions are enabled, from the input flags.
Args:
flip_left_right: Boolean whether to randomly mirror images horizontally.
random_crop: Integer percentage setting the total margin used around the
crop box.
random_scale: Integer percentage of how much to vary the scale by.
random_brightness: Integer range to randomly multiply the pixel values by.
Returns:
Boolean value indicating whether any distortions should be applied.
"""
return (flip_left_right or (random_crop != 0) or (random_scale != 0) or
(random_brightness != 0))
示例5: image_augmentation
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import random_crop [as 别名]
def image_augmentation(images, do_colors=False, crop_size=None):
"""Image augmentation: cropping, flipping, and color transforms."""
if crop_size is None:
crop_size = [299, 299]
images = tf.random_crop(images, crop_size + [3])
images = tf.image.random_flip_left_right(images)
if do_colors: # More augmentation, but might be slow.
images = tf.image.random_brightness(images, max_delta=32. / 255.)
images = tf.image.random_saturation(images, lower=0.5, upper=1.5)
images = tf.image.random_hue(images, max_delta=0.2)
images = tf.image.random_contrast(images, lower=0.5, upper=1.5)
return images
示例6: cifar_image_augmentation
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import random_crop [as 别名]
def cifar_image_augmentation(images):
"""Image augmentation suitable for CIFAR-10/100.
As described in https://arxiv.org/pdf/1608.06993v3.pdf (page 5).
Args:
images: a Tensor.
Returns:
Tensor of the same shape as images.
"""
images = tf.image.resize_image_with_crop_or_pad(images, 40, 40)
images = tf.random_crop(images, [32, 32, 3])
images = tf.image.random_flip_left_right(images)
return images
示例7: vqa_v2_preprocess_image
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import random_crop [as 别名]
def vqa_v2_preprocess_image(
image,
height,
width,
mode,
resize_side=512,
distort=True,
image_model_fn="resnet_v1_152",
):
"""vqa v2 preprocess image."""
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
assert resize_side > 0
if resize_side:
image = _aspect_preserving_resize(image, resize_side)
if mode == tf.estimator.ModeKeys.TRAIN:
image = tf.random_crop(image, [height, width, 3])
else:
# Central crop, assuming resize_height > height, resize_width > width.
image = tf.image.resize_image_with_crop_or_pad(image, height, width)
image = tf.clip_by_value(image, 0.0, 1.0)
if mode == tf.estimator.ModeKeys.TRAIN and distort:
image = _flip(image)
num_distort_cases = 4
# pylint: disable=unnecessary-lambda
image = _apply_with_random_selector(
image, lambda x, ordering: _distort_color(x, ordering),
num_cases=num_distort_cases)
if image_model_fn.startswith("resnet_v1"):
# resnet_v1 uses vgg preprocessing
image = image * 255.
image = _mean_image_subtraction(image, [_R_MEAN, _G_MEAN, _B_MEAN])
elif image_model_fn.startswith("resnet_v2"):
# resnet v2 uses inception preprocessing
image = tf.subtract(image, 0.5)
image = tf.multiply(image, 2.0)
return image
示例8: preprocess_example
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import random_crop [as 别名]
def preprocess_example(self, example, mode, hparams):
# Crop to target shape instead of down-sampling target, leaving target
# of maximum available resolution.
target_shape = (self.output_dim, self.output_dim, self.num_channels)
example["targets"] = tf.random_crop(example["targets"], target_shape)
example["inputs"] = image_utils.resize_by_area(example["targets"],
self.input_dim)
if self.inpaint_fraction is not None and self.inpaint_fraction > 0:
mask = random_square_mask((self.input_dim,
self.input_dim,
self.num_channels),
self.inpaint_fraction)
example["inputs"] = tf.multiply(
tf.convert_to_tensor(mask, dtype=tf.int64),
example["inputs"])
if self.input_dim is None:
raise ValueError("Cannot train in-painting for examples with "
"only targets (i.e. input_dim is None, "
"implying there are only targets to be "
"generated).")
return example
示例9: get_wavenet_batch
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import random_crop [as 别名]
def get_wavenet_batch(self, batch_size, length=64000):
"""Get the Tensor expressions from the reader.
Args:
batch_size: The integer batch size.
length: Number of timesteps of a cropped sample to produce.
Returns:
A dict of key:tensor pairs. This includes "pitch", "wav", and "key".
"""
example = self.get_example(batch_size)
wav = example["audio"]
wav = tf.slice(wav, [0], [64000])
pitch = tf.squeeze(example["pitch"])
key = tf.squeeze(example["note_str"])
if self.is_training:
# random crop
crop = tf.random_crop(wav, [length])
crop = tf.reshape(crop, [1, length])
key, crop, pitch = tf.train.shuffle_batch(
[key, crop, pitch],
batch_size,
num_threads=4,
capacity=500 * batch_size,
min_after_dequeue=200 * batch_size)
else:
# fixed center crop
offset = (64000 - length) // 2 # 24320
crop = tf.slice(wav, [offset], [length])
crop = tf.reshape(crop, [1, length])
key, crop, pitch = tf.train.shuffle_batch(
[key, crop, pitch],
batch_size,
num_threads=4,
capacity=500 * batch_size,
min_after_dequeue=200 * batch_size)
crop = tf.reshape(tf.cast(crop, tf.float32), [batch_size, length])
pitch = tf.cast(pitch, tf.int32)
return {"pitch": pitch, "wav": crop, "key": key}
示例10: crop
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import random_crop [as 别名]
def crop(image, is_training, crop_size):
h, w, c = crop_size[0], crop_size[1], image.shape[-1]
if is_training:
return tf.random_crop(image, [h, w, c])
else:
# Central crop for now. (See Table 5 in Appendix of
# https://arxiv.org/pdf/1703.07737.pdf for why)
dy = (tf.shape(image)[0] - h)//2
dx = (tf.shape(image)[1] - w)//2
return tf.image.crop_to_bounding_box(image, dy, dx, h, w)
示例11: preprocess_train
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import random_crop [as 别名]
def preprocess_train(x, width, height):
"""Pre-processing applied to training data set.
Args:
x: Input image float32 tensor.
width: int specifying intended width in pixels of image after preprocessing.
height: int specifying intended height in pixels of image after
preprocessing.
Returns:
x: transformed input with random crops, flips and reflection.
"""
x = pad_input(x, crop_dim=4)
x = tf.random_crop(x, [width, height, 3])
x = tf.image.random_flip_left_right(x)
return x
示例12: preprocess_for_train
# 需要导入模块: from tensorflow.compat import v1 [as 别名]
# 或者: from tensorflow.compat.v1 import random_crop [as 别名]
def preprocess_for_train(image,
output_height,
output_width,
padding=_PADDING,
add_image_summaries=True,
use_grayscale=False):
"""Preprocesses the given image for training.
Note that the actual resizing scale is sampled from
[`resize_size_min`, `resize_size_max`].
Args:
image: A `Tensor` representing an image of arbitrary size.
output_height: The height of the image after preprocessing.
output_width: The width of the image after preprocessing.
padding: The amound of padding before and after each dimension of the image.
add_image_summaries: Enable image summaries.
use_grayscale: Whether to convert the image from RGB to grayscale.
Returns:
A preprocessed image.
"""
if add_image_summaries:
tf.summary.image('image', tf.expand_dims(image, 0))
# Transform the image to floats.
image = tf.to_float(image)
if use_grayscale:
image = tf.image.rgb_to_grayscale(image)
if padding > 0:
image = tf.pad(image, [[padding, padding], [padding, padding], [0, 0]])
# Randomly crop a [height, width] section of the image.
distorted_image = tf.random_crop(image,
[output_height, output_width, 3])
# Randomly flip the image horizontally.
distorted_image = tf.image.random_flip_left_right(distorted_image)
if add_image_summaries:
tf.summary.image('distorted_image', tf.expand_dims(distorted_image, 0))
# Because these operations are not commutative, consider randomizing
# the order their operation.
distorted_image = tf.image.random_brightness(distorted_image,
max_delta=63)
distorted_image = tf.image.random_contrast(distorted_image,
lower=0.2, upper=1.8)
# Subtract off the mean and divide by the variance of the pixels.
return tf.image.per_image_standardization(distorted_image)