fruit_project.utils.datasets.det_dataset

Classes

DET_DS

A custom dataset class for object detection tasks.

Module Contents

class fruit_project.utils.datasets.det_dataset.DET_DS(root_dir: str, type: str, image_dir: str, label_dir: str, config_file: str, transforms: albumentations.Compose = None, input_size: int = 224)[source]

Bases: torch.utils.data.Dataset

A custom dataset class for object detection tasks.

This dataset class loads images and their corresponding labels from specified directories, applies transformations if provided, and returns the processed image along with target annotations.

root_dir[source]

The root directory containing the dataset.

Type:

str

type[source]

The type of dataset (e.g., ‘train’, ‘val’, ‘test’).

Type:

str

image_dir[source]

The subdirectory containing the images.

Type:

str

label_dir[source]

The subdirectory containing the label files.

Type:

str

config_file

The path to the configuration file containing class names.

Type:

str

transforms[source]

A function or object to apply transformations to the images and annotations.

Type:

Albumentations Compose, optional

input_size[source]

The input size for the images (default is 224).

Type:

int

image_paths[source]

A list of valid image file paths.

Type:

list

labels[source]

A list of class names.

Type:

list

id2lbl[source]

A mapping from class IDs to class names.

Type:

dict

lbl2id[source]

A mapping from class names to class IDs.

Type:

dict

__len__()[source]

Returns the number of valid images in the dataset.

__getitem__(idx)[source]

Returns the processed image and target annotations for the given index.

Parameters:
  • root_dir (str) – The root directory containing the dataset.

  • type (str) – The type of dataset (e.g., ‘train’, ‘val’, ‘test’).

  • image_dir (str) – The subdirectory containing the images.

  • label_dir (str) – The subdirectory containing the label files.

  • config_file (str) – The path to the configuration file containing class names.

  • transforms (Albumentations Compose, optional) – A function or object to apply transformations to the images and annotations.

  • input_size (int, optional) – The input size for the images (default is 224).

Raises:
  • FileNotFoundError – If the configuration file or label files are not found.

  • ValueError – If an image cannot be loaded or is invalid.

root_dir[source]
type[source]
image_dir[source]
label_dir[source]
transforms = None[source]
input_size = 224[source]
config_dir[source]
image_paths = [][source]
labels[source]
id2lbl[source]
lbl2id[source]
__len__()[source]
Returns:

The number of valid images in the dataset.

Return type:

int

__getitem__(idx)[source]

Retrieves the processed image and target annotations for the given index.

Parameters:

idx (int) – The index of the image to retrieve.

Returns:

A tuple containing:
  • img (numpy.ndarray): The processed image.

  • target (dict): A dictionary containing target annotations, including:
    • image_id (int): The index of the image.

    • annotations (list): A list of dictionaries with bounding box, category ID, area, and iscrowd flag.

    • orig_size (torch.Tensor): The original size of the image (height, width).

Return type:

tuple