Data Augmentation of 3D Image and Label Pairs

Hello everyone,

I’m seeking assistance with a data augmentation task involving pairs of 3D image and label data. My dataset consists of 3D volumes, where each volume is represented as a stack of 2D slices. I’m looking for guidance on how to apply augmentation techniques like rotation, shifting, and flipping to both the image and corresponding label slices while ensuring their spatial correspondence.

Thank you in advance for your help!

Hi @samo_timy ,

Here’s a basic code template that can help you get started with your data augmentation process:

import numpy as np
import tensorflow as tf

def apply_augmentation(image, label):
    # Apply random rotation
    angle = np.random.uniform(-15, 15)
    image = tf.image.rot90(image, k=angle // 90)
    label = tf.image.rot90(label, k=angle // 90)

    # Apply random shifting
    shift = np.random.randint(-10, 10, size=3)
    image = tf.roll(image, shift, axis=(0, 1, 2))
    label = tf.roll(label, shift, axis=(0, 1, 2))

    # Apply random flipping
    if np.random.rand() < 0.5:
        image = tf.reverse(image, axis=[0])
        label = tf.reverse(label, axis=[0])

    return image, label

# Iterate through your dataset 
for image, label in dataset:
    # Apply augmentation
    aug_image, aug_label = apply_augmentation(image, label)

For more details you can refer the Tensorflow Image augmentation documentation

I hope this helps!


Hi @Laxma_Reddy_Patlolla ,

thank you so much.