Commit 5f71cb15 authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira
Browse files

Implemented random rotate for images

parent 9c27182d
Pipeline #20591 failed with stage
in 64 minutes and 48 seconds
......@@ -44,6 +44,7 @@ def append_image_augmentation(image,
random_brightness=False,
random_contrast=False,
random_saturation=False,
random_rotate=False,
per_image_normalization=True):
"""
Append to the current tensor some random image augmentation operation
......@@ -67,6 +68,9 @@ def append_image_augmentation(image,
random_saturation:
Adjust the saturation of an RGB image by a random factor (https://www.tensorflow.org/api_docs/python/tf/image/random_saturation)
random_rotate:
Randomly rotate face images between -10 and 10 degrees
per_image_normalization:
Linearly scales image to have zero mean and unit norm.
......@@ -92,6 +96,10 @@ def append_image_augmentation(image,
if random_saturation:
image = tf.image.random_saturation(image, lower=0, upper=0.5)
if random_rotate:
image = tf.contrib.image.rotate(image, angles=numpy.random.randint(-5,5),
interpolation="BILINEAR")
if gray_scale:
image = tf.image.rgb_to_grayscale(image, name="rgb_to_gray")
#self.output_shape[3] = 1
......
......@@ -20,6 +20,7 @@ def shuffle_data_and_labels_image_augmentation(filenames,
random_brightness=False,
random_contrast=False,
random_saturation=False,
random_rotate=False,
per_image_normalization=True,
extension=None):
"""
......@@ -70,6 +71,9 @@ def shuffle_data_and_labels_image_augmentation(filenames,
random_saturation:
Adjust the saturation of an RGB image by a random factor (https://www.tensorflow.org/api_docs/python/tf/image/random_saturation)
random_rotate:
Randomly rotate face images between -10 and 10 degrees
per_image_normalization:
Linearly scales image to have zero mean and unit norm.
......@@ -89,6 +93,7 @@ def shuffle_data_and_labels_image_augmentation(filenames,
random_brightness=random_brightness,
random_contrast=random_contrast,
random_saturation=random_saturation,
random_rotate=random_rotate,
per_image_normalization=per_image_normalization,
extension=extension)
......@@ -108,6 +113,7 @@ def create_dataset_from_path_augmentation(filenames,
random_brightness=False,
random_contrast=False,
random_saturation=False,
random_rotate=False,
per_image_normalization=True,
extension=None):
"""
......@@ -141,6 +147,7 @@ def create_dataset_from_path_augmentation(filenames,
random_brightness=random_brightness,
random_contrast=random_contrast,
random_saturation=random_saturation,
random_rotate=random_rotate,
per_image_normalization=per_image_normalization,
extension=extension)
......@@ -159,6 +166,7 @@ def image_augmentation_parser(filename,
random_brightness=False,
random_contrast=False,
random_saturation=False,
random_rotate=False,
per_image_normalization=True,
extension=None):
"""
......@@ -180,6 +188,7 @@ def image_augmentation_parser(filename,
random_brightness=random_brightness,
random_contrast=random_contrast,
random_saturation=random_saturation,
random_rotate=random_rotate,
per_image_normalization=per_image_normalization)
label = tf.cast(label, tf.int64)
......
......@@ -20,6 +20,7 @@ def shuffle_data_and_labels_image_augmentation(filenames,
random_brightness=False,
random_contrast=False,
random_saturation=False,
random_rotate=False,
per_image_normalization=True,
extension=None):
"""
......@@ -76,6 +77,9 @@ def shuffle_data_and_labels_image_augmentation(filenames,
random_saturation:
Adjust the saturation of an RGB image by a random factor (https://www.tensorflow.org/api_docs/python/tf/image/random_saturation)
random_rotate:
Randomly rotate face images between -10 and 10 degrees
per_image_normalization:
Linearly scales image to have zero mean and unit norm.
......@@ -94,6 +98,7 @@ def shuffle_data_and_labels_image_augmentation(filenames,
random_brightness=random_brightness,
random_contrast=random_contrast,
random_saturation=random_saturation,
random_rotate=random_rotate,
per_image_normalization=per_image_normalization,
extension=extension)
......@@ -112,6 +117,7 @@ def create_dataset_from_path_augmentation(filenames,
random_brightness=False,
random_contrast=False,
random_saturation=False,
random_rotate=False,
per_image_normalization=True,
extension=None):
"""
......@@ -158,6 +164,9 @@ def create_dataset_from_path_augmentation(filenames,
random_saturation:
Adjust the saturation of an RGB image by a random factor (https://www.tensorflow.org/api_docs/python/tf/image/random_saturation)
random_rotate:
Randomly rotate face images between -10 and 10 degrees
per_image_normalization:
Linearly scales image to have zero mean and unit norm.
......@@ -176,6 +185,7 @@ def create_dataset_from_path_augmentation(filenames,
random_brightness=random_brightness,
random_contrast=random_contrast,
random_saturation=random_saturation,
random_rotate=random_rotate,
per_image_normalization=per_image_normalization,
extension=extension)
......@@ -198,6 +208,7 @@ def image_augmentation_parser(filename_left,
random_brightness=False,
random_contrast=False,
random_saturation=False,
random_rotate=False,
per_image_normalization=True,
extension=None):
"""
......@@ -221,6 +232,7 @@ def image_augmentation_parser(filename_left,
random_brightness=random_brightness,
random_contrast=random_contrast,
random_saturation=random_saturation,
random_rotate=random_rotate,
per_image_normalization=per_image_normalization)
image_right = append_image_augmentation(
......@@ -231,6 +243,7 @@ def image_augmentation_parser(filename_left,
random_brightness=random_brightness,
random_contrast=random_contrast,
random_saturation=random_saturation,
random_rotate=random_rotate,
per_image_normalization=per_image_normalization)
image = dict()
......
......@@ -30,6 +30,7 @@ def image_augmentation_parser(serialized_example,
random_brightness=False,
random_contrast=False,
random_saturation=False,
random_rotate=False,
per_image_normalization=True):
"""
Parses a single tf.Example into image and label tensors.
......@@ -52,6 +53,7 @@ def image_augmentation_parser(serialized_example,
random_brightness=random_brightness,
random_contrast=random_contrast,
random_saturation=random_saturation,
random_rotate=random_rotate,
per_image_normalization=per_image_normalization)
# Cast label data into int64
......@@ -123,6 +125,7 @@ def create_dataset_from_records_with_augmentation(
random_brightness=False,
random_contrast=False,
random_saturation=False,
random_rotate=False,
per_image_normalization=True):
"""
Create dataset from a list of tf-record files
......@@ -156,6 +159,7 @@ def create_dataset_from_records_with_augmentation(
random_brightness=random_brightness,
random_contrast=random_contrast,
random_saturation=random_saturation,
random_rotate=random_rotate,
per_image_normalization=per_image_normalization)
dataset = dataset.map(parser)
return dataset
......@@ -173,6 +177,7 @@ def shuffle_data_and_labels_image_augmentation(tfrecord_filenames,
random_brightness=False,
random_contrast=False,
random_saturation=False,
random_rotate=False,
per_image_normalization=True):
"""
Dump random batches from a list of tf-record files and applies some image augmentation
......@@ -215,7 +220,10 @@ def shuffle_data_and_labels_image_augmentation(tfrecord_filenames,
random_saturation:
Adjust the saturation of an RGB image by a random factor (https://www.tensorflow.org/api_docs/python/tf/image/random_saturation)
per_image_normalization:
random_rotate:
Randomly rotate face images between -10 and 10 degrees
per_image_normalization:
Linearly scales image to have zero mean and unit norm.
"""
......@@ -230,6 +238,7 @@ def shuffle_data_and_labels_image_augmentation(tfrecord_filenames,
random_brightness=random_brightness,
random_contrast=random_contrast,
random_saturation=random_saturation,
random_rotate=random_rotate,
per_image_normalization=per_image_normalization)
dataset = dataset.shuffle(buffer_size).batch(batch_size).repeat(epochs)
......@@ -335,6 +344,7 @@ def batch_data_and_labels_image_augmentation(tfrecord_filenames,
random_brightness=False,
random_contrast=False,
random_saturation=False,
random_rotate=False,
per_image_normalization=True):
"""
Dump in order batches from a list of tf-record files
......@@ -368,6 +378,7 @@ def batch_data_and_labels_image_augmentation(tfrecord_filenames,
random_brightness=random_brightness,
random_contrast=random_contrast,
random_saturation=random_saturation,
random_rotate=random_rotate,
per_image_normalization=per_image_normalization)
dataset = dataset.batch(batch_size).repeat(epochs)
......
......@@ -20,6 +20,7 @@ def shuffle_data_and_labels_image_augmentation(filenames,
random_brightness=False,
random_contrast=False,
random_saturation=False,
random_rotate=False,
per_image_normalization=True,
extension=None):
"""
......@@ -74,6 +75,9 @@ def shuffle_data_and_labels_image_augmentation(filenames,
random_saturation:
Adjust the saturation of an RGB image by a random factor (https://www.tensorflow.org/api_docs/python/tf/image/random_saturation)
random_rotate:
Randomly rotate face images between -10 and 10 degrees
per_image_normalization:
Linearly scales image to have zero mean and unit norm.
......@@ -93,6 +97,7 @@ def shuffle_data_and_labels_image_augmentation(filenames,
random_brightness=random_brightness,
random_contrast=random_contrast,
random_saturation=random_saturation,
random_rotate=random_rotate,
per_image_normalization=per_image_normalization,
extension=extension)
......@@ -113,6 +118,7 @@ def create_dataset_from_path_augmentation(filenames,
random_brightness=False,
random_contrast=False,
random_saturation=False,
random_rotate=False,
per_image_normalization=True,
extension=None):
"""
......@@ -146,6 +152,7 @@ def create_dataset_from_path_augmentation(filenames,
random_brightness=random_brightness,
random_contrast=random_contrast,
random_saturation=random_saturation,
random_rotate=random_rotate,
per_image_normalization=per_image_normalization,
extension=extension)
......@@ -169,6 +176,7 @@ def image_augmentation_parser(anchor,
random_brightness=False,
random_contrast=False,
random_saturation=False,
random_rotate=False,
per_image_normalization=True,
extension=None):
"""
......@@ -194,6 +202,7 @@ def image_augmentation_parser(anchor,
random_brightness=random_brightness,
random_contrast=random_contrast,
random_saturation=random_saturation,
random_rotate=random_rotate,
per_image_normalization=per_image_normalization)
triplet[n] = image
......
......@@ -13,6 +13,7 @@ from bob.learn.tensorflow.dataset.tfrecords import shuffle_data_and_labels, batc
from bob.learn.tensorflow.utils import load_mnist, create_mnist_tfrecord
from bob.learn.tensorflow.utils.hooks import LoggerHookEstimator
from bob.learn.tensorflow.loss import mean_cross_entropy_loss
from bob.learn.tensorflow.utils import reproducible
import numpy
......@@ -26,11 +27,11 @@ model_dir = "./temp"
learning_rate = 0.1
data_shape = (28, 28, 1) # size of atnt images
data_type = tf.float32
batch_size = 16
batch_size = 32
validation_batch_size = 250
epochs = 1
epochs = 6
steps = 5000
reproducible.set_seed()
def test_logitstrainer():
# Trainer logits
......@@ -167,6 +168,8 @@ def run_logitstrainer_mnist(trainer, augmentation=False):
data_shape,
data_type,
batch_size,
random_flip=True,
random_rotate=True,
epochs=epochs)
else:
return shuffle_data_and_labels(
......@@ -196,10 +199,10 @@ def run_logitstrainer_mnist(trainer, augmentation=False):
trainer.train(input_fn, steps=steps, hooks=hooks)
if not trainer.embedding_validation:
acc = trainer.evaluate(input_fn_validation)
assert acc['accuracy'] > 0.20
assert acc['accuracy'] > 0.10
else:
acc = trainer.evaluate(input_fn_validation)
assert acc['accuracy'] > 0.20
assert acc['accuracy'] > 0.10
# Cleaning up
tf.reset_default_graph()
......
......@@ -21,9 +21,9 @@ model_dir_adapted = "./temp2"
learning_rate = 0.1
data_shape = (28, 28, 1) # size of atnt images
data_type = tf.float32
batch_size = 16
batch_size = 32
validation_batch_size = 250
epochs = 2
epochs = 6
steps = 5000
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment