def distort_randomly_image_color(image_tensor,annotation_tensor,image_shape): # Accepts image tensor(w,h,3) and returns color distorted image. # Performs random brightness, saturation, hue, contrast change as it is performed for inception model training in TF-slim. # All the parameters of random variables are originally preserved. # Works in slow and fast mode. Slow mode performs only saturation and brightness random change. # returns img_float_distorted_original_range: Tensor of size(width,height,3) of type tf.float # Image tensor with distorted color in [0,255] intensity range fast_mode = False # Normalizing the image img_float_zero_one_range = tf.to_float(image_tensor)/255 # Randomly distort the color of image. There are 4 ways to do it. # Credit: TF-Slim # https://github.com/tensorflow/models/blob/master/slim/preprocessing/inception_preprocessing.py#L224 # Most probably the inception models were trainined using this color augmentation: # https://github.com/tensorflow/models/tree/master/slim#pre-trained-models distorted_image = apply_with_random_selector(img_float_zero_one_range, lambda x,ordering: distort_color(x,ordering,fast_mode=fast_mode),num_cases=4) img_float_distorted_original_range = distorted_image * 255 return img_float_distorted_original_range,annotation_tensor,image_shape
def distort_randomly_image_color(image_tensor, fast_mode=False): """Accepts image tensor of (width, height, 3) and returns color distorted image. The function performs random brightness, saturation, hue, contrast change as it is performed for inception model training in TF-Slim (you can find the link below in comments). All the parameters of random variables were originally preserved. There are two regimes for the function to work: fast and slow. Slow one performs only saturation and brightness random change is performed. Parameters ---------- image_tensor : Tensor of size (width, height, 3) of tf.int32 or tf.float Tensor with image with range [0,255] fast_mode : boolean Boolean value representing whether to use fast or slow mode Returns ------- img_float_distorted_original_range : Tensor of size (width, height, 3) of type tf.float. Image Tensor with distorted color in [0,255] intensity range """ # Make the range to be in [0,1] img_float_zero_one_range = tf.to_float(image_tensor) / 255 # Randomly distort the color of image. There are 4 ways to do it. # Credit: TF-Slim # https://github.com/tensorflow/models/blob/master/slim/preprocessing/inception_preprocessing.py#L224 # Most probably the inception models were trainined using this color augmentation: # https://github.com/tensorflow/models/tree/master/slim#pre-trained-models distorted_image = apply_with_random_selector(img_float_zero_one_range, lambda x, ordering: distort_color(x, ordering, fast_mode=fast_mode), num_cases=4) img_float_distorted_original_range = distorted_image * 255 return img_float_distorted_original_range
def distort_randomly_image_color(image_tensor, annotation_tensor, image_shape): """Accepts image tensor of (width, height, 3) and returns color distorted image. The function performs random brightness, saturation, hue, contrast change as it is performed for inception model training in TF-Slim (you can find the link below in comments). All the parameters of random variables were originally preserved. There are two regimes for the function to work: fast and slow. Slow one performs only saturation and brightness random change is performed. Parameters ---------- image_tensor : Tensor of size (width, height, 3) of tf.int32 or tf.float Tensor with image with range [0,255] fast_mode : boolean Boolean value representing whether to use fast or slow mode Returns ------- img_float_distorted_original_range : Tensor of size (width, height, 3) of type tf.float. Image Tensor with distorted color in [0,255] intensity range """ fast_mode=False # Make the range to be in [0,1] img_float_zero_one_range = tf.to_float(image_tensor) / 255 # Randomly distort the color of image. There are 4 ways to do it. # Credit: TF-Slim # https://github.com/tensorflow/models/blob/master/slim/preprocessing/inception_preprocessing.py#L224 # Most probably the inception models were trainined using this color augmentation: # https://github.com/tensorflow/models/tree/master/slim#pre-trained-models distorted_image = apply_with_random_selector(img_float_zero_one_range, lambda x, ordering: distort_color(x, ordering, fast_mode=fast_mode), num_cases=4) img_float_distorted_original_range = distorted_image * 255 return img_float_distorted_original_range, annotation_tensor, image_shape
def distort_image(im, fast_mode=False): # All images in the same batch are transformed the same way, but over # iterations you see different distortions. # im should be float with values between 0 and 1. im_ = tf.reshape(im, shape=(-1,1,3)) im_ = ip.apply_with_random_selector( im_, lambda x, ordering: ip.distort_color(x, ordering, fast_mode), num_cases=4) im_ = tf.reshape(im_, tf.shape(im)) return im_
def distort_image(im, fast_mode=False): # All images in the same batch are transformed the same way, but over # iterations you see different distortions. # im should be float with values between 0 and 1. im_ = tf.reshape(im, shape=(-1,1,3)) im_ = ip.apply_with_random_selector( im_, lambda x, ordering: ip.distort_color(x, ordering, fast_mode), num_cases=4) im_ = tf.reshape(im_, tf.shape(im)) return im_
def distort_randomly_image_color(image, annotation, image_shape): fast_mode = False img_float_zero_one_range = tf.to_float(image) / 255 distorted_image = apply_with_random_selector( img_float_zero_one_range, lambda x, ordering: distort_color(x, ordering, fast_mode=fast_mode), num_cases=4) img_float_distorted_original_range = distorted_image * 255 return img_float_distorted_original_range, annotation, image_shape
def preprocess_image(image, height, width, is_training=False): fast_mode=True image = common_preprocess(image, height, width) if is_training: bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4]) image_with_box = tf.image.draw_bounding_boxes(tf.expand_dims(image, 0), bbox) tf.image_summary('image_with_bounding_boxes', image_with_box, 3) distorted_image, _ = distorted_bounding_box_crop(image, bbox, area_range=(0.4,1.0)) distorted_image.set_shape([None, None, 3]) image_with_distorted_box = tf.image.draw_bounding_boxes( tf.expand_dims(image, 0), distorted_bbox) tf.image_summary('images_with_distorted_bounding_box', image_with_distorted_box) # This resizing operation may distort the images because the aspect # ratio is not respected. We select a resize method in a round robin # fashion based on the thread number. # Note that ResizeMethod contains 4 enumerated resizing methods. # We select only 1 case for fast_mode bilinear. num_resize_cases = 1 if fast_mode else 4 distorted_image = apply_with_random_selector( distorted_image, lambda x, method: tf.image.resize_images(x, [height, width], method=method), num_cases=num_resize_cases) distorted_image = tf.image.random_flip_left_right(distorted_image) distorted_image = apply_with_random_selector( distorted_image, lambda x, ordering: distort_color(x, ordering, fast_mode), num_cases=4) tf.image_summary('final_distorted_image', tf.expand_dims(distorted_image, 0), 3) return distorted_image else: return image