本文整理汇总了Python中tensorflow.random_crop方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.random_crop方法的具体用法?Python tensorflow.random_crop怎么用?Python tensorflow.random_crop使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.random_crop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cifar_tf_preprocess
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_crop [as 别名]
def cifar_tf_preprocess(inp, random_crop=True, random_flip=True, whiten=True,
br_sat_con=False):
image_size = 32
image = inp
if random_crop:
image = tf.image.resize_image_with_crop_or_pad(inp, image_size + 4,
image_size + 4)
image = tf.random_crop(image, [image_size, image_size, 3])
if random_flip:
image = tf.image.random_flip_left_right(image)
# Brightness/saturation/constrast provides small gains .2%~.5% on cifar.
if br_sat_con:
image = tf.image.random_brightness(image, max_delta=63. / 255.)
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
image = tf.image.random_contrast(image, lower=0.2, upper=1.8)
if whiten:
image = tf.image.per_image_standardization(image)
return image
示例2: _train_preprocess
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_crop [as 别名]
def _train_preprocess(self, reshaped_image):
# Image processing for training the network. Note the many random
# distortions applied to the image.
# Randomly crop a [height, width] section of the image.
reshaped_image = tf.random_crop(reshaped_image, self.processed_size)
# Randomly flip the image horizontally.
reshaped_image = tf.image.random_flip_left_right(reshaped_image)
# Because these operations are not commutative, consider randomizing
# the order their operation.
reshaped_image = tf.image.random_brightness(reshaped_image,
max_delta=63)
# Randomly changing contrast of the image
reshaped_image = tf.image.random_contrast(reshaped_image,
lower=0.2, upper=1.8)
return reshaped_image
示例3: should_distort_images
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow 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))
示例4: parse_tfrecord_tf
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_crop [as 别名]
def parse_tfrecord_tf(record, res, rnd_crop):
features = tf.parse_single_example(record, features={
'shape': tf.FixedLenFeature([3], tf.int64),
'data': tf.FixedLenFeature([], tf.string),
'label': tf.FixedLenFeature([1], tf.int64)})
# label is always 0 if uncondtional
# to get CelebA attr, add 'attr': tf.FixedLenFeature([40], tf.int64)
data, label, shape = features['data'], features['label'], features['shape']
label = tf.cast(tf.reshape(label, shape=[]), dtype=tf.int32)
img = tf.decode_raw(data, tf.uint8)
if rnd_crop:
# For LSUN Realnvp only - random crop
img = tf.reshape(img, shape)
img = tf.random_crop(img, [res, res, 3])
img = tf.reshape(img, [res, res, 3])
return img, label # to get CelebA attr, also return attr
示例5: patch_discriminator
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow 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
示例6: __init__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_crop [as 别名]
def __init__(self, raw_cifar10data, sess, model):
assert isinstance(raw_cifar10data, CIFAR10Data)
self.image_size = 32
# create augmentation computational graph
self.x_input_placeholder = tf.placeholder(tf.float32, shape=[None, 32, 32, 3])
padded = tf.map_fn(lambda img: tf.image.resize_image_with_crop_or_pad(
img, self.image_size + 4, self.image_size + 4),
self.x_input_placeholder)
cropped = tf.map_fn(lambda img: tf.random_crop(img, [self.image_size,
self.image_size,
3]), padded)
flipped = tf.map_fn(lambda img: tf.image.random_flip_left_right(img), cropped)
self.augmented = flipped
self.train_data = AugmentedDataSubset(raw_cifar10data.train_data, sess,
self.x_input_placeholder,
self.augmented)
self.eval_data = AugmentedDataSubset(raw_cifar10data.eval_data, sess,
self.x_input_placeholder,
self.augmented)
self.label_names = raw_cifar10data.label_names
示例7: tf_preprocess
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_crop [as 别名]
def tf_preprocess(random_crop=True,
random_flip=True,
random_color=True,
whiten=False):
image_size = 84
inp = tf.placeholder(tf.float32, [image_size, image_size, 3])
image = inp
# image = tf.cast(inp, tf.float32)
if random_crop:
log.info("Apply random cropping")
image = tf.image.resize_image_with_crop_or_pad(inp, image_size + 8,
image_size + 8)
image = tf.random_crop(image, [image_size, image_size, 3])
if random_flip:
log.info("Apply random flipping")
image = tf.image.random_flip_left_right(image)
# Brightness/saturation/constrast provides small gains .2%~.5% on cifar.
if random_color:
image = tf.image.random_brightness(image, max_delta=63. / 255.)
image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
image = tf.image.random_contrast(image, lower=0.2, upper=1.8)
if whiten:
log.info("Apply whitening")
image = tf.image.per_image_whitening(image)
return inp, image
示例8: random_crop_image
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_crop [as 别名]
def random_crop_image(img, size, offset=None):
# adapted from code from tf.random_crop
shape = tf.shape(img)
#remove the assertion for now since it makes the queue filling slow for some reason
#check = tf.Assert(
# tf.reduce_all(shape[:2] >= size),
# ["Need value.shape >= size, got ", shape, size])
#with tf.control_dependencies([check]):
# img = tf.identity(img)
limit = shape[:2] - size + 1
dtype = tf.int32
if offset is None:
offset = tf.random_uniform(shape=(2,), dtype=dtype, maxval=dtype.max, seed=None) % limit
offset = tf.stack([offset[0], offset[1], 0])
size0 = size[0] if isinstance(size[0], int) else None
size1 = size[1] if isinstance(size[1], int) else None
size_im = tf.stack([size[0], size[1], img.get_shape().as_list()[2]])
img_cropped = tf.slice(img, offset, size_im)
out_shape_img = [size0, size1, img.get_shape()[2]]
img_cropped.set_shape(out_shape_img)
return img_cropped, offset
示例9: preprocess_image
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_crop [as 别名]
def preprocess_image(image, is_training):
"""Preprocess a single image of layout [height, width, depth]."""
if is_training:
# Resize the image to add four extra pixels on each side.
image = tf.image.resize_image_with_crop_or_pad(
image, _HEIGHT + 8, _WIDTH + 8)
# Randomly crop a [_HEIGHT, _WIDTH] section of the image.
image = tf.random_crop(image, [_HEIGHT, _WIDTH, _DEPTH])
# Randomly flip the image horizontally.
image = tf.image.random_flip_left_right(image)
# Subtract off the mean and divide by the variance of the pixels.
image = tf.image.per_image_standardization(image)
return image
示例10: svhn_tf_preprocess
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_crop [as 别名]
def svhn_tf_preprocess(inp, random_crop=True):
image_size = 32
image = inp
if random_crop:
print("Apply random cropping")
image = tf.image.resize_image_with_crop_or_pad(inp, image_size + 4,
image_size + 4)
image = tf.random_crop(image, [image_size, image_size, 3])
return inp, image
示例11: preprocess_for_train
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_crop [as 别名]
def preprocess_for_train(image,
output_height,
output_width,
padding=_PADDING):
"""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.
Returns:
A preprocessed image.
"""
tf.summary.image('image', tf.expand_dims(image, 0))
# Transform the image to floats.
image = tf.to_float(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)
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)
示例12: preprocess
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_crop [as 别名]
def preprocess(self, image):
"""Preprocess a single image in [height, width, depth] layout."""
if self.subset == 'train' and self.use_distortion:
# Pad 4 pixels on each dimension of feature map, done in mini-batch
image = tf.image.resize_image_with_crop_or_pad(image, 40, 40)
image = tf.random_crop(image, [HEIGHT, WIDTH, DEPTH])
image = tf.image.random_flip_left_right(image)
return image
示例13: decode_jpg
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_crop [as 别名]
def decode_jpg(filename, directory, center=False, crop=None, flip=False, resize=None, ratio=False, filename_offset='',
normalize='imagenet'):
# Read image
im_content = tf.read_file(directory + filename_offset + filename)
example = tf.image.decode_jpeg(im_content, channels=3)
# preprocessing
example = tf.cast(example[:, :, ::-1], tf.float32)
if normalize is 'imagenet':
example = example - IMAGENET_MEAN
elif normalize:
example = example / 127.5 - 1.
# cropping
if crop:
shape = tf.shape(example)
if ratio:
assert isinstance(crop, list)
crop_h = crop[0]
crop_w = crop[1]
else:
assert isinstance(crop, int)
shortest = tf.cond(tf.less(shape[0], shape[1]), lambda: shape[0], lambda: shape[1])
crop_h = tf.cond(tf.less_equal(shortest, tf.constant(crop)), lambda: shortest, lambda: tf.constant(crop))
crop_w = crop_h
if center:
example = tf.image.resize_image_with_crop_or_pad(example, crop_h, crop_w)
else:
example = tf.random_crop(example, [crop_h, crop_w, 3])
# resize
if resize:
assert isinstance(resize, (int, float))
new_size = tf.stack([resize, resize])
example = tf.image.resize_images(example, new_size)
# random horizontal flip
if flip:
example = tf.image.random_flip_left_right(example)
return tf.transpose(example, [2, 0, 1])
示例14: image_augmentation
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow 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
示例15: cifar_image_augmentation
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow 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