class DataGenerator(Sequence):
def __init__(self, all_filenames, input_size=(128, 128), batch_size=8, shuffle=True, seed=123, encode: dict = None, color_mode='hsv', function=None) -> None:
super(DataGenerator, self).__init__()
# Check if the encoding dictionary is provided
assert encode != None, 'Not empty !'
# Check if the color mode is valid
assert color_mode == 'hsv' or color_mode == 'rgb' or color_mode == 'gray'
# Initialize instance variables
self.all_filenames = all_filenames
self.input_size = input_size
self.batch_size = batch_size
self.shuffle = shuffle
self.color_mode = color_mode
self.encode = encode
self.function = function
# Set random seed for shuffling
np.random.seed(seed)
# Shuffle the data at the start
self.on_epoch_end()
def processing(self, mask):
# Encode mask based on the provided dictionary
d = list(map(lambda x: self.encode[tuple(x)], mask.reshape(-1, 3)))
return np.array(d).reshape(*self.input_size, 1)
def __len__(self):
# Calculate the number of batches per epoch
return int(np.floor(len(self.all_filenames) / self.batch_size))
def __getitem__(self, index):
# Generate one batch of data
indexes = self.indexes[index * self.batch_size : (index + 1) * self.batch_size]
all_filenames_temp = [self.all_filenames[k] for k in indexes]
X, Y = self.__data_generation(all_filenames_temp)
return X, Y
def on_epoch_end(self):
# Update indexes after each epoch
self.indexes = np.arange(len(self.all_filenames))
if self.shuffle == True:
np.random.shuffle(self.indexes)
def __data_generation(self, all_filenames_temp):
# Generates data containing batch_size samples
# Initialize arrays for images and masks
batch = len(all_filenames_temp)
if self.color_mode == 'gray':
X = np.empty(shape=(batch, *self.input_size, 1))
else:
X = np.empty(shape=(batch, *self.input_size, 3))
Y = np.empty(shape=(batch, *self.input_size, 1))
# Iterate over the filenames in the current batch
for i, (fn, label_fn) in enumerate(all_filenames_temp):
# Load and preprocess image
img = cv2.imread(fn)
if self.color_mode == 'hsv':
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
elif self.color_mode == 'rgb':
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
elif self.color_mode == 'gray':
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = tf.expand_dims(img, axis=2)
img = tf.image.resize(img, self.input_size, method='nearest')
img = tf.cast(img, tf.float32)
img /= 255.
# Load and preprocess mask
mask = cv2.imread(label_fn, 0)
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)
mask = tf.image.resize(mask, self.input_size, method='nearest')
mask = np.array(mask)
if self.function:
mask = self.function(mask)
mask = self.processing(mask)
mask = tf.cast(mask, tf.float32)
# Assign images and masks to the arrays
X[i,] = img
Y[i,] = mask
return X, Y