Ecto-Trigger 1.0
Loading...
Searching...
No Matches
CustomDataGenerator Class Reference

Inherits Sequence.

Public Member Functions

 __init__ (self, data_directory, batch_size, input_shape, stop_training_flag={"stop":False}, shuffle=True)
 
 load_image_and_label_paths (self)
 
 __len__ (self)
 
 read_img (self, path)
 
 __getitem__ (self, index)
 
 read_binary_label (self, label_path)
 
 on_epoch_end (self)
 

Public Attributes

 data_directory = data_directory
 
 batch_size = batch_size
 
 input_shape = input_shape[:2]
 
int nr_channels = input_shape[2]
 
 shuffle = shuffle
 
 stop_training_flag = stop_training_flag
 
 image_paths
 
 label_paths = self.load_image_and_label_paths()
 
 indexes = np.arange(len(self.image_paths))
 
 augmenter = get_augmenter(input_shape[:2])
 

Detailed Description

Custom data generator for Keras models.

This generator deals with YOLO-format object detection data annotations and loads them into a Keras Sequence suitable for training binary (presence/absence) mobilenet_v2 models. 
This is done by: 1) loading images and their corresponding labels, 2) resizing them to fit model input shape, 3) application of pre-processing and image augmentation.
Preprocessing is facilitated by the mobilenet_v2 preprocess_input function, accessed through Keras and augmentation makes use of the imgaug PyPI package.
If the input shape contains only 1 colour channel, then preprocessing loads the images as greyscale using opencv function cvtColor.
Each Generator yields batches for training or evaluation, batch size is selected at instantation. 

CustomDataGenerator is iterable, the method __get_item__() can be broken out for debugging purposes by setting the stop_training_flag.

Attributes:
    data_directory (str or list): Directory or list of directories containing images and labels.
    batch_size (int): Number of samples per batch.
    input_shape (tuple): Shape of the input images (height, width, channels).
    stop_training_flag (dict): Dictionary containing a 'stop' flag for early stopping.
    shuffle (bool): Whether to shuffle the dataset at the end of each epoch.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
data_directory,
batch_size,
input_shape,
stop_training_flag = {"stop":False},
shuffle = True )
Initialize the data generator.

Args:
    data_directory (str or list): Directory or list of directories with image and label files.
    batch_size (int): Number of samples per batch.
    input_shape (tuple): Shape of the input images (height, width, channels).
    stop_training_flag (dict): Dictionary with a 'stop' key to signal early stopping, a useful way to stop the generator hanging for test runs.
    shuffle (bool): Whether to shuffle data after each epoch. Defaults to True.

Member Function Documentation

◆ __getitem__()

__getitem__ ( self,
index )
Generate one batch of data. This method will raise a StopIteration Exception if self.stop_training_flag is set.

Args:
    index (int): Index of the batch.

Returns:
    tuple: Batch of images (X) and labels (y).

◆ __len__()

__len__ ( self)
Compute the number of batches per epoch.

Returns:
    int: Number of batches.

◆ load_image_and_label_paths()

load_image_and_label_paths ( self)
Load paths to image and label files.

Returns:
    tuple: Lists of image paths and corresponding label paths.

◆ on_epoch_end()

on_epoch_end ( self)
Shuffle the dataset at the end of each epoch, if enabled.

◆ read_binary_label()

read_binary_label ( self,
label_path )
Read a binary label from a label file.

Args:
    label_path (str): Path to the label file.

Returns:
    float: Binary label (1.0 for presence, 0.0 for absence).

◆ read_img()

read_img ( self,
path )
Read and preprocess an image.

Args:
    path (str): Path to the image file.

Returns:
    np.ndarray: Preprocessed image.

Member Data Documentation

◆ augmenter

augmenter = get_augmenter(input_shape[:2])

◆ batch_size

batch_size = batch_size

◆ data_directory

data_directory = data_directory

◆ image_paths

image_paths

◆ indexes

indexes = np.arange(len(self.image_paths))

◆ input_shape

input_shape = input_shape[:2]

◆ label_paths

label_paths = self.load_image_and_label_paths()

◆ nr_channels

int nr_channels = input_shape[2]

◆ shuffle

shuffle = shuffle

◆ stop_training_flag

stop_training_flag = stop_training_flag

The documentation for this class was generated from the following file: