Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add augmentation examples #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions example/augmentation_visualisation.py
Original file line number Diff line number Diff line change
@@ -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()
17 changes: 17 additions & 0 deletions example/augmentations_example.yaml
Original file line number Diff line number Diff line change
@@ -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 }