Sure! Here's an updated code snippet that uses tf.data to load and preprocess the image data instead of ImageDataGenerator:
import tensorflow as tf
from tensorflow.keras.applications import EfficientNetB7
from tensorflow.keras.layers import Input, Dense, Flatten, Conv2D, MaxPooling2D, Dropout, concatenate
from tensorflow.keras.models import Model
# Define input shapes for each branch
rgb_input = Input(shape=(224, 224, 3))
dft_input = Input(shape=(224, 224, 3))
hsv_input = Input(shape=(224, 224, 3))
# Define data augmentation for all branches
data_augmentation = tf.keras.Sequential([
tf.keras.layers.RandomFlip("horizontal"),
tf.keras.layers.RandomRotation(0.1),
tf.keras.layers.RandomZoom(0.1),
])
# Define the function to load and preprocess the image data using tf.data
def load_and_preprocess_image(filename):
# Read the image from the file
image_string = tf.io.read_file(filename)
# Decode the image
image = tf.image.decode_jpeg(image_string, channels=3)
# Resize the image
image = tf.image.resize(image, [224, 224])
# Normalize the pixel values to be between 0 and 1
image = tf.cast(image, tf.float32) / 255.0
return image
# Define the function to get the DFT transformed and shifted frequency spectrum image
def get_dft_transformed_image(image):
# Compute the 2D DFT of the image
dft = tf.signal.fft2d(tf.cast(image, tf.complex64))
# Shift the zero-frequency component to the center of the spectrum
shifted_dft = tf.signal.fftshift(dft)
# Get the magnitude of the spectrum
magnitude = tf.abs(shifted_dft)
# Normalize the magnitude to be between 0 and 1
magnitude = magnitude / tf.reduce_max(magnitude)
# Convert the magnitude to a 3-channel image
magnitude = tf.repeat(tf.expand_dims(magnitude, axis=-1), 3, axis=-1)
return magnitude
# Define the function to get the HSV image
def get_hsv_image(image):
# Convert the RGB image to HSV
hsv = tf.image.rgb_to_hsv(image)
return hsv
# Define the function to load and preprocess the image data for each branch
def load_and_preprocess_data(rgb_filename, dft_filename, hsv_filename, label):
# Load and preprocess the RGB image
rgb_image = load_and_preprocess_image(rgb_filename)
rgb_image = data_augmentation(rgb_image)
# Load and preprocess the DFT transformed and shifted frequency spectrum image
dft_image = load_and_preprocess_image(dft_filename)
dft_image = get_dft_transformed_image(dft_image)
dft_image = data_augmentation(dft_image)
# Load and preprocess the HSV image
hsv_image = load_and_preprocess_image(hsv_filename)
hsv_image = get_hsv_image(hsv_image)
hsv_image = data_augmentation(hsv_image)
# Return the preprocessed data and the label
return (rgb_image, dft_image, hsv_image), label
# Define the batch size and number of epochs
batch_size = 32
num_epochs = 10
# Define the training and validation directories
train_dir = 'path/to/training/data'
valid_dir = 'path/to/validation/data'
# Define the training and validation datasets using tf.data
train_dataset = tf.data.Dataset.list_files(train_dir + '/*/*')