birdfsd_yolov5.augmentation package

Submodules

birdfsd_yolov5.augmentation.image_aug module

aug_pipelines() Sequential[source]

A simple and common augmentation sequence

This function returns a sequence of augmentations to be applied to the images. The augmentations are:

  1. Flipping the image horizontally with a probability of 0.5

  2. Random cropping of the image with a probability of 0.1

  3. Blurring the image with a probability of 0.5

  4. Changing the contrast of the image with a probability of 1

  5. Adding gaussian noise to the image with a probability of 0.5

  6. Changing the brightness of the image with a probability of 0.2

  7. Applying affine transformations to the image with a probability of 1

Notes

Ref: https://imgaug.readthedocs.io/en/latest/source/examples_basics.html # noqa

Returns

iaa.Sequential: A sequence of augmentations to be applied to the images.

check_classes_preserved(classes_file: str, output_dir: str) None[source]

Checks if the dataset classes were preserved after augmentation.

Parameters
  • classes_file – The path to the classes file.

  • output_dir – The path to the output directory.

Returns

None

create_batch(images_dir: str, labels_dir: str, output_dir: str, batch_size: int = 128) list[source]

Creates batches of images and labels from the given directories.

Parameters
  • images_dir (str) – The directory containing the images.

  • labels_dir (str) – The directory containing the labels.

  • output_dir (str) – The directory to save the augmented images and labels.

  • batch_size (int) – The number of images to be augmented in a single batch.

Returns

A list of images that were skipped due to errors.

Return type

list

export_augs_as_files(image_aug: list, bbs_aug: list, output_dir: str, _skipped: list) None[source]

Exports augmented images and bounding boxes to a directory.

Parameters
  • image_aug (list) – List of augmented images.

  • bbs_aug (list) – List of augmented bounding boxes.

  • output_dir (str) – Path to the output directory.

  • _skipped (list) – List of skipped images.

Returns

None

run_aug_pipeline(dataset_path: str, batch_size: int = 128) None[source]

Runs the images’ augmentation pipeline

This function takes a dataset path and augments the dataset by applying random transformations to the images and labels.

Parameters
  • dataset_path (str) – The path to the dataset.

  • batch_size (int) – The number of images to process at once.

Returns

None

split_data(images_dir: str, labels_dir: str, _output_dir: str) None[source]

Split the data into train and validation sets.

Parameters
  • images_dir (str) – The path to the directory containing the images.

  • labels_dir (str) – The path to the directory containing the labels.

  • _output_dir (str) – The path to the output directory.

Returns

None

xywh_to_xyxy(x: float, y: float, w: float, h: float, image_width: float, image_height: float) tuple[source]

Converts a bounding box from xywh format to xyxy format.

Parameters
  • x (float) – The x coordinate of the center of the bounding box.

  • y (float) – The y coordinate of the center of the bounding box.

  • w (float) – The width of the bounding box.

  • h (float) – The height of the bounding box.

  • image_width (float) – The width of the image.

  • image_height (float) – The height of the image.

Returns

A tuple containing the x1, y1, x2, and y2 coordinates of the bounding box.

Return type

tuple

birdfsd_yolov5.augmentation.transfer_color_aug module

class Transfer(n: int = 300, eps: float = 1e-06, m: int = 6, c: int = 3, file_path: Optional[str] = None, ref_path: Optional[str] = None)[source]

Bases: ColorTransfer, Regrain, Rotations

Uses three color transfer algorithms to augment the dataset.

This class is used to transfer the color distribution of the reference image to the source image while preserving the details and structure of the source image.

static img2arr(file_path: str) ndarray[source]

Converts an image file to a numpy array. :param file_path: The path to the image file. :type file_path: str

Returns

The image as a numpy array.

Return type

np.ndarray

insert(file_path: str, ref_path: str) None[source]

Inserts data into the Transfer class instance.

Inserts a source and target image to the class to be used by any of the transformer methods.

Parameters
  • file_path (str) – The path to the source image.

  • ref_path (str) – The path to the reference image.

Returns

None

lm_algo() ndarray[source]

This method adapts the source mean and std to the reference image’s

Returns

Transformed image as a numpy array.

Return type

np.ndarray

mstd_algo() ndarray[source]

This method uses algorithms proposed by Reinhard, E., Adhikhmin, M., Gooch, B., & Shirley, P. (2001).

Note

Original paper: https://doi.org/10.1109/38.946629

Returns

Transformed image as a numpy array.

Return type

np.ndarray

pdf_algo() ndarray[source]

Method proposed by Pitié, F., Kokaram, A. C., & Dahyot, R. (2007).

Returns

Transformed image as a numpy array.

Return type

np.ndarray

static save(arr: ndarray, output: str) None[source]

Saves an image array to disk.

Parameters
  • arr (np.ndarray) – The image to save.

  • output (str) – The path to save the image to.

Returns

The path to the saved image.

Return type

str

run(dataset_dir: str, ref_images_dir: str) None[source]

Runs the color transfer pipeline

Runs the color tansfer augmentation from a dataset with a structure that conforms to yolov5 dataset structure. Reference images are randomly selected from a pool of images that are placed in ref_images_dir.

Parameters
  • dataset_dir (str) – The directory containing the dataset.

  • ref_images_dir (str) – The directory containing the reference images.

Returns

None

Module contents