From c1945ba44daf37c2e57c1c6ad9c7b1eb6ce7525d Mon Sep 17 00:00:00 2001 From: Blake Bodycote Date: Thu, 7 Dec 2023 21:43:24 +1100 Subject: [PATCH] Adds a stand-alone python script that visualises image augmentation, along with an example config file of augmentation settings --- example/augmentation_visualisation.py | 40 +++++++++++++++++++++++++++ example/augmentations_example.yaml | 17 ++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 example/augmentation_visualisation.py create mode 100644 example/augmentations_example.yaml diff --git a/example/augmentation_visualisation.py b/example/augmentation_visualisation.py new file mode 100644 index 0000000..1e9f794 --- /dev/null +++ b/example/augmentation_visualisation.py @@ -0,0 +1,40 @@ +import tensorflow as tf +import matplotlib.pyplot as plt +import yaml + +image_path = "images/image0000002.jpg" +config_path = "augmentations_example.yaml" + +# Decode image into tensor +image = open(image_path, 'rb').read() +image_tensor = tf.image.decode_jpeg(image) + +# Get augmentations +with open(config_path, "r") as file: + data = yaml.safe_load(file) +augmentations = data["example"]["config"]["augmentations"] + +# Apply the augmentations that were listed +if "brightness" in augmentations: + v = augmentations["brightness"] + image_tensor = tf.image.adjust_brightness(image_tensor, tf.random.truncated_normal(shape=(), **v)) +if "contrast" in augmentations: + v = augmentations["contrast"] + image_tensor = tf.image.adjust_contrast(image_tensor, tf.random.truncated_normal(shape=(), **v)) +if "hue" in augmentations: + v = augmentations["hue"] + image_tensor = tf.image.adjust_hue(image_tensor, tf.random.truncated_normal(shape=(), **v)) +if "saturation" in augmentations: + v = augmentations["saturation"] + image_tensor = tf.image.adjust_saturation(image_tensor, tf.random.truncated_normal(shape=(), **v)) +if "gamma" in augmentations: + v_gamma = augmentations["gamma"]["gamma"] + v_gain = augmentations["gamma"]["gain"] + image_tensor = tf.image.adjust_gamma( + image_tensor, tf.random.truncated_normal(shape=(), **v_gamma), tf.random.truncated_normal(shape=(), **v_gain) + ) + +# Display image using matplotlib +plt.imshow(image_tensor) +plt.axis('off') +plt.show() diff --git a/example/augmentations_example.yaml b/example/augmentations_example.yaml new file mode 100644 index 0000000..7dbc7a7 --- /dev/null +++ b/example/augmentations_example.yaml @@ -0,0 +1,17 @@ + +example: + type: Image + config: + augmentations: + # Adjust the brightness `x + delta` + brightness: { mean: 0, stddev: 0.05 } + # Adjust the contrast `(x - mean) * delta + mean` + contrast: { mean: 1, stddev: 0.05 } + # Convert to hsv, adjust the hue by a value from [-1 -> 1] and back to rgb + hue: { mean: 0, stddev: 0.05 } + # Convert to hsv, multiply saturation by value and convert back to rgb + saturation: { mean: 1, stddev: 0.05 } + # Adjust the gamma `gain * x**gamma` + gamma: + gamma: { mean: 1, stddev: 0.05 } + gain: { mean: 1, stddev: 0.05 }