def _preprocess_fn(self, features, labels, mode): """The preprocessing function which will be executed prior to the model_fn. Args: features: The input features extracted from a single example in our in_feature_specification format. labels: (Optional) The input labels extracted from a single example in our in_label_specification format. mode: (ModeKeys) Specifies if this is training, evaluation or prediction. Returns: features: The preprocessed features, potentially adding additional tensors derived from the input features. labels: (Optional) The preprocessed labels, potentially adding additional tensors derived from the input features and labels. """ if mode == TRAIN: image = image_transformations.RandomCropImages( [features.state.image], INPUT_SHAPE, TARGET_SHAPE)[0] else: image = image_transformations.CenterCropImages( [features.state.image], INPUT_SHAPE, TARGET_SHAPE)[0] image = tf.image.convert_image_dtype(image, tf.float32) if mode == TRAIN: image = (image_transformations.ApplyPhotometricImageDistortions( [image])[0]) features.state.image = image return features, labels
def testDeterminism(self): """Get deterministic distortion by setting global seed and op-level seed.""" input_shape = [32, 32, 3] gin.bind_parameter('tf.random.uniform.seed', 0) gin.bind_parameter('tf.random.normal.seed', 0) with tf.Graph().as_default(): tf.set_random_seed(123) batch_size = 4 images = self._CreateRampTestImages(batch_size, input_shape[0], input_shape[1]) tensor_list = [] for i in range(batch_size): tensor_list.append(images[i]) distorted_1 = image_transformations.ApplyPhotometricImageDistortions( tensor_list, random_noise_apply_probability=1.0) distorted_2 = image_transformations.ApplyPhotometricImageDistortions( tensor_list, random_noise_apply_probability=1.0) with tf.Session() as sess: images_1 = sess.run(distorted_1) images_2 = sess.run(distorted_2) for image_1, image_2 in zip(images_1, images_2): self.assertAllClose(image_1, image_2)
def maybe_distort_image_batch(images, mode): """Applies data augmentation to given images. Args: images: 4D Tensor (batch images) or 5D Tensor (batch of image sequences). mode: (ModeKeys) Specifies if this is training, evaluation or prediction. Returns: Distorted images. Image distortion is identical for every image in the batch. """ if mode == tf.estimator.ModeKeys.TRAIN: images = image_transformations.ApplyPhotometricImageDistortions( [images])[0] return images
def testPhotometricImageDistortions(self, input_shape): input_shape = input_shape + [3] with tf.Graph().as_default(): batch_size = 4 images = self._CreateRampTestImages(batch_size, input_shape[0], input_shape[1]) tensor_list = [] for i in range(batch_size): tensor_list.append(images[i]) distorted = image_transformations.ApplyPhotometricImageDistortions( tensor_list, random_noise_apply_probability=1.0) delta = tf.reduce_sum(tf.square(images - distorted)) with tf.Session() as sess: images_delta = sess.run(delta) # Check if any distortion applied. self.assertGreater(images_delta, 0)