我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用tensorflow.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 _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 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): """ 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 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 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 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 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(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 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 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 __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(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 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)