I have the following lines of code that reads a csv file and prepares a data set to be fed into a tensorflow model using the Image Data Generator Class. How can I integrate KeraCV’s CutMix and MixUp here?
import pandas as pd
from collections import defaultdict
import os
def find_duplicate_filenames(root_dir):
files_dict = defaultdict(list)
for subdir, dirs, files in os.walk(root_dir):
for file in files:
file_path = os.path.join(subdir, file)
files_dict[file].append(file_path)
duplicates = {file: paths for file, paths in files_dict.items() if len(paths) > 1}
return duplicates
# Path to the directory with training images
root_directory = 'train_images/'
# Identifying duplicates
duplicates_by_name = find_duplicate_filenames(root_directory)
# Create a set of paths to exclude
exclude_paths = {path for paths in duplicates_by_name.values() for path in paths}
# Collect all valid image paths and their labels
valid_files = []
labels = []
for subdir, dirs, files in os.walk(root_directory):
for file in files:
file_path = os.path.join(subdir, file)
if file_path not in exclude_paths:
valid_files.append(file_path)
labels.append(subdir.split('/')[-1]) # assuming folder names are class labels
# Create DataFrame for the image paths and labels
data = pd.DataFrame({'filename': valid_files, 'class': labels})
data = data[data['class']!='']
print(len(data['class'].unique()))
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import numpy as np
def custom_preprocessing(image):
# Example of custom preprocessing logic
# Adjust the image as required, this is just a placeholder
image = np.clip(image, a_min=0, a_max=1) # Ensuring the image is within the range [0, 1]
return image
# ImageDataGenerator with custom preprocessing
train_datagen = ImageDataGenerator(
preprocessing_function=custom_preprocessing, # Using the custom preprocessing function
width_shift_range=0.5, # Randomly shifts images horizontally
horizontal_flip=False, # Randomly flips images horizontally
vertical_flip=False # Randomly flips images vertically
)
# Continue with your model training setup
# Setup train and validation generators
train_generator = train_datagen.flow_from_dataframe(
dataframe=train_data,
x_col='filename',
y_col='class',
target_size=(224, 224),
batch_size=64,
class_mode='categorical',
color_mode='rgb'
)
validation_generator = train_datagen.flow_from_dataframe(
dataframe=valid_data,
x_col='filename',
y_col='class',
target_size=(224, 224),
batch_size=64,
class_mode='categorical',
color_mode='rgb'
)
How can I add the KerasCV CutMix and MixUp layers here to add to the augmentation? I tried adding keras_cv.layers.CutMix()
and keras_cv.layers.MixUp()
into the model but it did not work. Thanks in advance!