def distort_image(image, height, width):
# Image processing for training the network. Note the many random
# distortions applied to the image.
distorted_image = tf.random_crop(image, [height, width, 3])
#distorted_image = tf.image.resize_images(image, [height, width])
# Randomly flip the image horizontally.
distorted_image = tf.image.random_flip_left_right(distorted_image)
# 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)
return distorted_image
python类random_crop()的实例源码
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))
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))
def preprocess(self, image):
if self.mods and self.mods['random_flip']:
image = tf.image.random_flip_left_right(image)
if self.mods and self.mods['random_saturation']:
image = tf.image.random_saturation(image, .95, 1.05)
if self.mods and self.mods['random_brightness']:
image = tf.image.random_brightness(image, .05)
if self.mods and self.mods['random_contrast']:
image = tf.image.random_contrast(image, .95, 1.05)
if self.mods and self.mods['crop_size'] > 0:
crop_size = self.mods['crop_size']
wiggle = 8
off_x, off_y = 25 - wiggle, 60 - wiggle
crop_size_plus = crop_size + 2 * wiggle
image = tf.image.resize_image_with_crop_or_pad(image, off_y + crop_size_plus, off_x + crop_size_plus)
image = tf.image.crop_to_bounding_box(image, off_y, off_x, crop_size_plus, crop_size_plus)
image = tf.random_crop(image, [crop_size, crop_size, 3])
image = tf.image.resize_images(image, size=(self.image_size, self.image_size))
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
image = (image / 127.5) - 1.
image.set_shape([self.image_size, self.image_size, 3])
return image
def cifar_tf_preprocess(random_crop=True, random_flip=True, whiten=True):
image_size = 32
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 + 4,
image_size + 4)
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.
# 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
def preprocess_training(img, height=None, width=None, normalize=None):
img_size = img.get_shape().as_list()
height = height or img_size[0]
width = width or img_size[1]
# Image processing for training the network. Note the many random
# distortions applied to the image.
# Randomly crop a [height, width] section of the image.
distorted_image = tf.random_crop(img, [height, width, 3])
# Randomly flip the image horizontally.
distorted_image = tf.image.random_flip_left_right(distorted_image)
# 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)
if normalize:
# Subtract off the mean and divide by the variance of the pixels.
distorted_image = tf.image.per_image_whitening(distorted_image)
return distorted_image
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))
def __build_generic_data_tensor(self, raw_images, raw_targets, shuffle, augmentation):
""" Creates the input pipeline and performs some preprocessing. """
images = ops.convert_to_tensor(raw_images)
targets = ops.convert_to_tensor(raw_targets)
set_size = raw_images.shape[0]
images = tf.reshape(images, [set_size, 28, 28, 1])
image, label = tf.train.slice_input_producer([images, targets], shuffle=shuffle)
# Data Augmentation
if augmentation:
image = tf.image.resize_image_with_crop_or_pad(image, self.IMAGE_HEIGHT+4, self.IMAGE_WIDTH+4)
image = tf.random_crop(image, [self.IMAGE_HEIGHT, self.IMAGE_WIDTH, self.NUM_OF_CHANNELS])
image = tf.image.random_flip_left_right(image)
image = tf.image.per_image_standardization(image)
images_batch, labels_batch = tf.train.batch([image, label], batch_size=self.batch_size, num_threads=self.NUM_THREADS)
return images_batch, labels_batch
def random_crop(images, height, width):
"""Randomly crops an image/images to a given size.
Args:
images: 4-D Tensor of shape `[batch, height, width, channels]` or
3-D Tensor of shape `[height, width, channels]`.
height: `float`. The height to crop to.
width: `float`. The width to crop to.
Returns:
If `images` was 4-D, a 4-D float Tensor of shape
`[batch, new_height, new_width, channels]`.
If `images` was 3-D, a 3-D float Tensor of shape
`[new_height, new_width, channels]`.
"""
images_shape = get_shape(images)
if len(images_shape) > 4:
ValueError("'image' must have either 3 or 4 dimensions, "
"received `{}`.".format(images_shape))
if len(images_shape) == 4:
return tf.map_fn(lambda img: tf.random_crop(img, [height, width, images_shape[-1]]), images)
return tf.random_crop(images, [height, width, images_shape[-1]])
def _train_preprocess(reshaped_image, args):
# 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, [args.crop_size[0], args.crop_size[1], args.num_channels])
# 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)
# Subtract off the mean and divide by the variance of the pixels.
reshaped_image = tf.image.per_image_standardization(reshaped_image)
# Set the shapes of tensors.
reshaped_image.set_shape([args.crop_size[0], args.crop_size[1], args.num_channels])
#read_input.label.set_shape([1])
return reshaped_image
def random_crop_and_pad_image_and_labels(self, image, labels, size):
"""Randomly crops `image` together with `labels`.
Args:
image: A Tensor with shape [D_1, ..., D_K, N]
labels: A Tensor with shape [D_1, ..., D_K, M]
size: A Tensor with shape [K] indicating the crop size.
Returns:
A tuple of (cropped_image, cropped_label).
"""
combined = tf.concat([image, labels], axis=2)
image_shape = tf.shape(image)
combined_pad = tf.image.pad_to_bounding_box(
combined, 0, 0,
tf.maximum(size[0], image_shape[0]),
tf.maximum(size[1], image_shape[1]))
last_label_dim = tf.shape(labels)[-1]
last_image_dim = tf.shape(image)[-1]
combined_crop = tf.random_crop(
combined_pad,
size=tf.concat([size, [last_label_dim + last_image_dim]],
axis=0))
return (combined_crop[:, :, :last_image_dim],
combined_crop[:, :, last_image_dim:])
def random_crop_and_pad_image_and_labels(self, image, labels, size):
"""Randomly crops `image` together with `labels`.
Args:
image: A Tensor with shape [D_1, ..., D_K, N]
labels: A Tensor with shape [D_1, ..., D_K, M]
size: A Tensor with shape [K] indicating the crop size.
Returns:
A tuple of (cropped_image, cropped_label).
"""
combined = tf.concat([image, labels], axis=2)
image_shape = tf.shape(image)
combined_pad = tf.image.pad_to_bounding_box(
combined, 0, 0,
tf.maximum(size[0], image_shape[0]),
tf.maximum(size[1], image_shape[1]))
last_label_dim = tf.shape(labels)[-1]
last_image_dim = tf.shape(image)[-1]
combined_crop = tf.random_crop(
combined_pad,
size=tf.concat([size, [last_label_dim + last_image_dim]],
axis=0))
return (combined_crop[:, :, :last_image_dim],
combined_crop[:, :, last_image_dim:])
retrain.py 文件源码
项目:transfer_learning_sound_classification
作者: lukeinator42
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
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))
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))
image_reader_classfc.py 文件源码
项目:dcsp_segmentation
作者: arslan-chaudhry
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def random_crop_and_pad_image_and_labels(image, crop_h, crop_w, seed):
"""
Randomly crop and pads the input images.
Args:
image: Training image to crop/ pad.
crop_h: Height of cropped segment.
crop_w: Width of cropped segment.
seed: Random seed.
"""
image_shape = tf.shape(image)
img_pad = tf.image.pad_to_bounding_box(image, 0, 0, tf.maximum(crop_h, image_shape[0]), tf.maximum(crop_w, image_shape[1]))
img_crop = tf.random_crop(img_pad, [crop_h,crop_w,3], seed=seed)
# Set static shape so that tensorflow knows shape at compile time.
img_crop.set_shape((crop_h, crop_w, 3))
return img_crop
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))
def read_and_augment_data(image_list, label_list, image_size, batch_size, max_nrof_epochs,
random_crop, random_flip, random_rotate, nrof_preprocess_threads, shuffle=True):
images = ops.convert_to_tensor(image_list, dtype=tf.string)
labels = ops.convert_to_tensor(label_list, dtype=tf.int32)
# Makes an input queue
input_queue = tf.train.slice_input_producer([images, labels],
num_epochs=max_nrof_epochs, shuffle=shuffle)
images_and_labels = []
for _ in range(nrof_preprocess_threads):
image, label = read_images_from_disk(input_queue)
if random_rotate:
image = tf.py_func(random_rotate_image, [image], tf.uint8)
if random_crop:
image = tf.random_crop(image, [image_size, image_size, 3])
else:
image = tf.image.resize_image_with_crop_or_pad(image, image_size, image_size)
if random_flip:
image = tf.image.random_flip_left_right(image)
#pylint: disable=no-member
image.set_shape((image_size, image_size, 3))
image = tf.image.per_image_standardization(image)
images_and_labels.append([image, label])
image_batch, label_batch = tf.train.batch_join(
images_and_labels, batch_size=batch_size,
capacity=4 * nrof_preprocess_threads * batch_size,
allow_smaller_final_batch=True)
return image_batch, label_batch
def crop(image, random_crop, image_size):
if image.shape[1]>image_size:
sz1 = int(image.shape[1]//2)
sz2 = int(image_size//2)
if random_crop:
diff = sz1-sz2
(h, v) = (np.random.randint(-diff, diff+1), np.random.randint(-diff, diff+1))
else:
(h, v) = (0,0)
image = image[(sz1-sz2+v):(sz1+sz2+v),(sz1-sz2+h):(sz1+sz2+h),:]
return image
def __init__(self, path, batch_size=32,
capacity=16,
min_after_dequeue=4,
output_resolution=[1080, 1920],
shuffle=False,
fliplr=False,
flipud=False,
rotate=False,
random_crop=False,
params=None,
nthreads=1,
num_epochs=None):
self.path = path
self.batch_size = batch_size
self.capacity = capacity
self.min_after_dequeue = min_after_dequeue
self.shuffle = shuffle
self.nthreads = nthreads
self.num_epochs = num_epochs
self.params = params
self.output_resolution = output_resolution
# Data augmentation
self.fliplr = fliplr
self.flipud = flipud
self.rotate = rotate
self.random_crop = random_crop
sample = self._produce_one_sample()
self.samples = self._batch_samples(sample)
def postprocess_images(self, ims):
def _postprocess_images(im):
im = tf.decode_raw(im, np.uint8)
im = tf.image.convert_image_dtype(im, dtype=tf.float32)
im = tf.reshape(im, [256, 256, 3])
im = tf.random_crop(im, [self.crop_size, self.crop_size, 3])
return im
return tf.map_fn(lambda im: _postprocess_images(im), ims, dtype=tf.float32)