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))
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 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 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 _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:
distored 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
def pre_process_data(image,training):
if training:
image = tf.random_crop(image,size=[img_size_cropped,img_size_cropped,cifar10.num_channels])
image = tf.image.flip_left_right(image)
image = tf.image.random_hue(image)
image = tf.image.random_contrast(image)
image = tf.image.random_saturation(image)
image = tf.image.random_brightness(image)
image = tf.maximum(image,1.0)
image = tf.minimum(image,0.0)
else:
#for testing image
image = tf.image.resize_image_with_crop_or_pad(image,img_size_cropped,img_size_cropped);
return 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 distort_op(self, image_tensor):
""" copied from tensorflow cifar10 tutorial"""
# Randomly crop a [height, width] section of the image.
distorted_image = tf.random_crop(image_tensor, [self.shape[0],self.shape[1], self.channels])
# 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
def distort_op(self, image_tensor):
""" copied from tensorflow cifar10 tutorial"""
# Randomly crop a [height, width] section of the image.
distorted_image = tf.random_crop(image_tensor, [self.shape[0],self.shape[1], self.channels])
# 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
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):
"""
Brief:
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 get_batch(image, label, batch_size, crop_size):
#??????
distorted_image=tf.image.central_crop(image,33./37.)
distorted_image = tf.random_crop(distorted_image, [crop_size, crop_size, 3])#????,???
# # distorted_image = tf.image.random_flip_up_down(distorted_image)#??????
# distorted_image = tf.image.random_brightness(distorted_image,max_delta=50)#????
# distorted_image = tf.image.random_contrast(distorted_image,lower=0.2, upper=1.8)#?????
#??batch
#shuffle_batch????capacity????shuttle??????????????????batch???capacity?????
#?????????
images, label_batch = tf.train.shuffle_batch([distorted_image, label],batch_size=batch_size,
num_threads=4,capacity=50000,min_after_dequeue=10000)
# ????
#tf.image_summary('images', images)
return images, tf.reshape(label_batch, [batch_size])
#?????????????get_batch??
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 inputs(lists, image_shape, batch_size):
filename_queue = tf.train.string_input_producer(lists, shuffle=True)
reader = tf.TextLineReader()
_, value = reader.read(filename_queue)
image, label = read_my_file_format(value)
image = tf.image.resize_images(image, [image_shape[0]+3, image_shape[1]+3])
image = tf.random_crop(image, image_shape)
label = tf.cast(label, tf.float32)
image.set_shape(image_shape)
# image = tf.image.random_flip_left_right(image)
float_image = tf.image.per_image_whitening(image)
min_after_dequeue = 1000
capacity = min_after_dequeue+(2+1)*batch_size
image_batch, label_batch = tf.train.shuffle_batch([float_image, label],
batch_size=batch_size,
capacity=capacity,
min_after_dequeue=min_after_dequeue)
return image_batch, label_batch
def inputs_for_test(lists, image_shape, batch_size):
filename_queue = tf.train.string_input_producer(lists, shuffle=True)
reader = tf.TextLineReader()
_, value = reader.read(filename_queue)
image, label = read_my_file_format(value)
image = tf.image.resize_images(image, [image_shape[0], image_shape[1]])
# image = tf.random_crop(image, image_shape)
label = tf.cast(label, tf.float32)
image.set_shape(image_shape)
# image = tf.image.random_flip_left_right(image)
float_image = tf.image.per_image_whitening(image)
min_after_dequeue = 1000
capacity = min_after_dequeue+(2+1)*batch_size
image_batch, label_batch = tf.train.batch([float_image, label],
batch_size=batch_size)
return image_batch, label_batch
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
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 random_crop(image, crop_size, padding=None):
"""Randmly crop a image.
Args:
image: 3-D float Tensor of image
crop_size:int/tuple, output image height, width, for deep network we prefer same width and height
padding: int, padding use to restore original image size, padded with 0's
Returns:
3-D float Tensor of randomly flipped updown image used for training.
"""
if isinstance(crop_size, int):
crop_size = (crop_size, crop_size)
oshape = np.shape(image)
if padding:
oshape = (oshape[0] + 2 * padding, oshape[1] + 2 * padding)
npad = ((padding, padding), (padding, padding), (0, 0))
modified_image = image
if padding:
modified_image = np.lib.pad(
image, pad_width=npad, mode='constant', constant_values=0)
nh = random.randint(0, oshape[0] - crop_size[0])
nw = random.randint(0, oshape[1] - crop_size[1])
modified_image = modified_image[nh:nh + crop_size[0], nw:nw + crop_size[1]]
return modified_image
def read_data(prefix, labels_dic, mixing, files_from_cl):
image_list = sorted(map(lambda x: os.path.join(prefix, x),
filter(lambda x: x.endswith('JPEG'), files_from_cl)))
prefix2 = []
for file_i in image_list:
tmp = file_i.split(prefix+'/')[1].split("_")[0]
prefix2.append(tmp)
prefix2 = np.array(prefix2)
labels_list = np.array([mixing[labels_dic[i]] for i in prefix2])
assert(len(image_list) == len(labels_list))
images = tf.convert_to_tensor(image_list, dtype=tf.string)
labels = tf.convert_to_tensor(labels_list, dtype=tf.int32)
input_queue = tf.train.slice_input_producer([images, labels], shuffle=True, capacity=2000)
image_file_content = tf.read_file(input_queue[0])
label = input_queue[1]
image = tf.image.resize_images(tf.image.decode_jpeg(image_file_content, channels=3), [256, 256])
image = tf.random_crop(image, [224, 224, 3])
image = tf.image.random_flip_left_right(image)
return image, label
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))
retrain.py 文件源码
项目:Embarrassingly-Parallel-Image-Classification
作者: Azure
项目源码
文件源码
阅读 45
收藏 0
点赞 0
评论 0
def get_preprocessing():
def preprocessing_fn(image, output_height=224, output_width=224):
''' Resize the image and subtract "mean" RGB values '''
_R_MEAN = 123.68
_G_MEAN = 116.78
_B_MEAN = 103.94
#image = tf.expand_dims(image, 0)
temp_dim = np.random.randint(175, 223)
distorted_image = tf.random_crop(image, [output_height, output_width, 3])
distorted_image = tf.expand_dims(distorted_image, 0)
resized_image = tf.image.resize_bilinear(distorted_image, [output_height, output_width], align_corners=False)
resized_image = tf.squeeze(resized_image)
resized_image.set_shape([output_height, output_width, 3])
resized_image = tf.image.random_flip_left_right(resized_image)
image = tf.to_float(resized_image)
return(mean_image_subtraction(image, [_R_MEAN, _G_MEAN, _B_MEAN]))
return(preprocessing_fn)
def patch_discriminator(inputdisc, name="discriminator"):
with tf.variable_scope(name):
f = 4
patch_input = tf.random_crop(inputdisc, [1, 70, 70, 3])
o_c1 = layers.general_conv2d(patch_input, ndf, f, f, 2, 2,
0.02, "SAME", "c1", do_norm="False",
relufactor=0.2)
o_c2 = layers.general_conv2d(o_c1, ndf * 2, f, f, 2, 2,
0.02, "SAME", "c2", relufactor=0.2)
o_c3 = layers.general_conv2d(o_c2, ndf * 4, f, f, 2, 2,
0.02, "SAME", "c3", relufactor=0.2)
o_c4 = layers.general_conv2d(o_c3, ndf * 8, f, f, 2, 2,
0.02, "SAME", "c4", relufactor=0.2)
o_c5 = layers.general_conv2d(
o_c4, 1, f, f, 1, 1, 0.02, "SAME", "c5", do_norm=False,
do_relu=False)
return o_c5
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_batch_reader(paths_image, batch_size):
"""
"""
file_name_queue = tf.train.string_input_producer(paths_image)
reader_key, reader_val = tf.WholeFileReader().read(file_name_queue)
# decode a raw input image
image = tf.image.decode_jpeg(reader_val, channels=3)
# to float32 and -1.0 ~ +1.0
image = tf.cast(image, dtype=tf.float32) / 127.5 - 1.0
# scale up to increase training data
image = tf.image.resize_images(image, [264, 264])
# crop to 256 x 256 for the model.
# also, a batch need concreate image size
image = tf.random_crop(image, size=[256, 256, 3])
# random horizontal flipping to increase training data
image = tf.image.random_flip_left_right(image)
# create bacth
return tf.train.batch(tensors=[image], batch_size=batch_size)
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:
distored 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
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 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))