ON THIS PAGE

  • API reference

API reference

0.1.5
package

datadreamer

package

datadreamer.dataset_annotation

module
module
module
module
class
CLIPAnnotator
A class for image annotation using the CLIP model, specializing in image classification.
class
BaseAnnotator
Abstract base class for creating annotators.
class
class
OWLv2Annotator
A class for image annotation using the OWLv2 model, specializing in object detection.
module

datadreamer.dataset_annotation.utils

function
apply_tta(image)
Apply test-time augmentation (TTA) to the given image.

Args:
    image: The image to be augmented.

Returns:
    list: A list of augmented images.

Note:
    Currently, only horizontal flip is enabled. Additional transformations like
    vertical flip and color jitter are commented out but can be enabled as needed.
class

datadreamer.dataset_annotation.CLIPAnnotator(datadreamer.dataset_annotation.BaseAnnotator)

method
__init__(self, seed: float = 42, device: str = 'cuda', size: str = 'base')
Initializes the CLIPAnnotator with a specific seed and device.

Args:
    seed (float): Seed for reproducibility. Defaults to 42.
    device (str): The device to run the model on. Defaults to 'cuda'.
variable
variable
variable
variable
method
annotate_batch(self, images: List [ PIL.Image.Image ], objects: List [ str ], conf_threshold: float = 0.1, synonym_dict: dict [ str , List [ str ] ] | None = None) -> List[np.ndarray]: List[np.ndarray]
Annotates images using the OWLv2 model.

Args:
    images: The images to be annotated.
    objects: A list of objects (text) to test against the images.
    conf_threshold (float, optional): Confidence threshold for the annotations. Defaults to 0.1.
    synonym_dict (dict, optional): Dictionary for handling synonyms in labels. Defaults to None.

Returns:
    List[List[int]]: A list of lists of labels for each image.
method
release(self, empty_cuda_cache: bool = False)
Releases the model and optionally empties the CUDA cache.

Args:
    empty_cuda_cache (bool, optional): Whether to empty the CUDA cache. Defaults to False.
class

datadreamer.dataset_annotation.BaseAnnotator(abc.ABC)

class

datadreamer.dataset_annotation.TaskList(enum.Enum)

class

datadreamer.dataset_annotation.OWLv2Annotator(datadreamer.dataset_annotation.BaseAnnotator)

method
__init__(self, seed: float = 42, device: str = 'cuda', size: str = 'base')
Initializes the OWLv2Annotator with a specific seed and device.

Args:
    seed (float): Seed for reproducibility. Defaults to 42.
    device (str): The device to run the model on. Defaults to 'cuda'.
variable
variable
variable
variable
method
annotate_batch(self, images: List [ PIL.Image.Image ], prompts: List [ str ], conf_threshold: float = 0.1, iou_threshold: float = 0.2, use_tta: bool = False, synonym_dict: dict [ str , List [ str ] ] | None = None) -> Tuple[List[np.ndarray], List[np.ndarray], List[np.ndarray]]: Tuple[List[np.ndarray], List[np.ndarray], List[np.ndarray]]
Annotates images using the OWLv2 model.

Args:
    images: The images to be annotated.
    prompts: Prompts to guide the annotation.
    conf_threshold (float, optional): Confidence threshold for the annotations. Defaults to 0.1.
    iou_threshold (float, optional): Intersection over union threshold for non-maximum suppression. Defaults to 0.2.
    use_tta (bool, optional): Flag to apply test-time augmentation. Defaults to False.
    synonym_dict (dict, optional): Dictionary for handling synonyms in labels. Defaults to None.

Returns:
    tuple: A tuple containing the final bounding boxes, scores, and labels for the annotations.
method
release(self, empty_cuda_cache: bool = False)
Releases the model and optionally empties the CUDA cache.

Args:
    empty_cuda_cache (bool, optional): Whether to empty the CUDA cache. Defaults to False.
package

datadreamer.image_generation

module
module
module
module
module
class
StableDiffusionImageGenerator
A subclass of ImageGenerator that uses the Stable Diffusion model for image generation.
class
StableDiffusionLightningImageGenerator
A subclass of ImageGenerator specifically designed to use the Stable Diffusion Lightning model for faster image generation.
class
StableDiffusionTurboImageGenerator
A subclass of ImageGenerator specifically designed to use the Stable Diffusion Turbo model for faster image generation.
module

datadreamer.image_generation.clip_image_tester

class
ClipImageTester
A class for testing images against a set of textual objects using the CLIP model.
class

datadreamer.image_generation.clip_image_tester.ClipImageTester

method
__init__(self, device: str = 'cuda')
Initializes the ClipImageTester with the CLIP model and processor.
variable
variable
variable
method
test_image(self, image: Image.Image, objects: List [ str ], conf_threshold = 0.05)
Tests the generated image against a set of objects using the CLIP model.

Args:
    image (Image.Image): The image to be tested.
    objects (List[str]): A list of objects (text) to test against the image.
    conf_threshold (float): Confidence threshold for considering an object as present.

Returns:
    tuple: A tuple containing a boolean indicating if the image passes the test,
           the probabilities of the objects, and the number of objects that passed the test.
method
test_images_batch(self, images: List [ Image.Image ], objects: List [ List [ str ] ], conf_threshold = 0.05) -> List[tuple]: List[tuple]
Tests the generated images against a set of objects using the CLIP model.

Args:
    images (List[Image.Image]): The images to be tested.
    objects (List[List[str]]): A list of objects (text) to test against the images.
    conf_threshold (float, optional): Confidence threshold for considering an object as present. Defaults to 0.05.

Returns:
    List[tuple]: A list of tuples containing a boolean indicating if the image passes the test,
                the probabilities of the objects, and the number of objects that passed the test.
method
release(self, empty_cuda_cache = False)
Releases the model and optionally empties the CUDA cache.

Args:
    empty_cuda_cache (bool, optional): Whether to empty the CUDA cache. Defaults to False.
module

datadreamer.image_generation.image_generator

class
ImageGenerator
A class for generating images based on textual prompts, with optional CLIP model testing.
class

datadreamer.image_generation.image_generator.ImageGenerator

method
variable
variable
variable
variable
variable
variable
variable
variable
variable
static method
ImageGenerator.set_seed(seed: int)
Sets the seed for random number generators in Python and PyTorch.

Args:
    seed (int): The seed value to set.
method
generate_images(self, prompts: Union [ str , List [ str ] ], prompt_objects: Optional [ List [ List [ str ] ] ] = None)
Generates images based on the provided prompts and optional object prompts.

Args:
    prompts (Union[str, List[str]]): Single prompt or a list of prompts to guide the image generation.
    prompt_objects (Optional[List[List[str]]]): Optional list of objects for each prompt for CLIP model testing.

Yields:
    List[Image.Image]: A batch of generated images.
method
release(self, empty_cuda_cache = False)
Releases resources and optionally empties the CUDA cache.
method
generate_image(self, prompt: str, negative_prompt: str, prompt_objects: Optional [ List [ str ] ] = None) -> Image.Image: Image.Image
Generates a single image based on the provided prompt.

Args:
    prompt (str): The positive prompt to guide image generation.
    negative_prompt (str): The negative prompt to avoid certain features in the image.
    prompt_objects (Optional[List[str]]): Optional list of objects to be used in CLIP model testing.

Returns:
    Image.Image: The generated image.
class

datadreamer.image_generation.StableDiffusionImageGenerator(datadreamer.image_generation.image_generator.ImageGenerator)

method
__init__(self, args, kwargs)
Initializes the StableDiffusionImageGenerator with the given arguments.
variable
variable
variable
variable
method
generate_images_batch(self, prompts: List [ str ], negative_prompt: str, prompt_objects: Optional [ List [ List [ str ] ] ] = None)
Generates a batch of images based on the provided prompts.

Args:
    prompts (List[str]): A list of positive prompts to guide image generation.
    negative_prompt (str): The negative prompt to avoid certain features in the image.
    prompt_objects (Optional[List[List[str]]]): Optional list of objects to be used in CLIP model testing.

Returns:
    List[Image.Image]: A list of generated images.
method
release(self, empty_cuda_cache = False)
Releases the models and optionally empties the CUDA cache.
class

datadreamer.image_generation.StableDiffusionLightningImageGenerator(datadreamer.image_generation.image_generator.ImageGenerator)

method
__init__(self, args, kwargs)
Initializes the StableDiffusionLightningImageGenerator with the given
arguments.
variable
variable
method
generate_images_batch(self, prompts: List [ str ], negative_prompt: str, prompt_objects: Optional [ List [ List [ str ] ] ] = None, batch_size: int = 1) -> List[Image.Image]: List[Image.Image]
Generates a batch of images using the Stable Diffusion Lightning model based
on the provided prompts.

Args:
    prompts (List[str]): A list of positive prompts to guide image generation.
    negative_prompt (str): The negative prompt to avoid certain features in the image.
    prompt_objects (Optional[List[List[str]]]): Optional list of objects for each prompt for CLIP model testing.
    batch_size (int): The number of images to generate in each batch.

Returns:
    List[Image.Image]: A list of generated images.
method
release(self, empty_cuda_cache = False)
Releases the model and optionally empties the CUDA cache.
class

datadreamer.image_generation.StableDiffusionTurboImageGenerator(datadreamer.image_generation.image_generator.ImageGenerator)

method
__init__(self, args, kwargs)
Initializes the StableDiffusionTurboImageGenerator with the given
arguments.
variable
method
generate_images_batch(self, prompts: List [ str ], negative_prompt: str, prompt_objects: Optional [ List [ List [ str ] ] ] = None, batch_size: int = 1) -> List[Image.Image]: List[Image.Image]
Generates a batch of images using the Stable Diffusion Turbo model based on
the provided prompts.

Args:
    prompts (List[str]): A list of positive prompts to guide image generation.
    negative_prompt (str): The negative prompt to avoid certain features in the image.
    prompt_objects (Optional[List[List[str]]]): Optional list of objects for each prompt for CLIP model testing.
    batch_size (int): The number of images to generate in each batch.

Returns:
    List[Image.Image]: A list of generated images.
method
release(self, empty_cuda_cache = False)
Releases the model and optionally empties the CUDA cache.
package

datadreamer.pipelines

module

datadreamer.pipelines.generate_dataset_from_scratch

package

datadreamer.prompt_generation

module
module
module
module
module
module
module
class
LMPromptGenerator
A language model-based prompt generator class, extending PromptGenerator.
class
LMSynonymGenerator
Synonym generator that generates synonyms for a list of words using a language model.
class
SimplePromptGenerator
Prompt generator that creates simple prompts for text generation tasks.
class
TinyLlamaLMPromptGenerator
A language model-based prompt generator class, extending PromptGenerator.
class
WordNetSynonymGenerator
Synonym generator that generates synonyms for a list of words using WordNet.
module

datadreamer.prompt_generation.prompt_generator

class
PromptGenerator
Abstract base class for prompt generation.
class

datadreamer.prompt_generation.prompt_generator.PromptGenerator(abc.ABC)

method
variable
variable
variable
variable
variable
variable
variable
static method
PromptGenerator.set_seed(seed: int)
Sets the random seed for consistent prompt generation.

Args:
    seed (int): The random seed.
method
save_prompts(self, prompts: List [ str ], save_path: str)
Saves generated prompts to a JSON file.

Args:
    prompts (List[str]): List of generated prompts.
    save_path (str): Path to save the prompts JSON file.
method
generate_prompts(self) -> List[str]: List[str]
Abstract method to generate prompts (must be implemented in subclasses).

Returns:
    List[str]: A list of generated prompts.
method
release(self, empty_cuda_cache = False)
Abstract method to release resources (must be implemented in subclasses).
module

datadreamer.prompt_generation.synonym_generator

class
SynonymGenerator
Abstract base class for synonym generation.
class

datadreamer.prompt_generation.synonym_generator.SynonymGenerator(abc.ABC)

method
variable
variable
variable
method
generate_synonyms_for_list(self, words: List [ str ]) -> dict: dict
Generates synonyms for a list of words and returns them in a dictionary.

Args:
    words (List[str]): List of words for which synonyms are generated.

Returns:
    dict: A dictionary where each word is associated with a list of its most common synonyms.
method
save_synonyms(self, synonyms, save_path: str)
Saves the generated synonyms to a JSON file.

Args:
    synonyms: The synonyms to save (typically a dictionary).
    save_path (str): The path to the JSON file where synonyms will be saved.
method
generate_synonyms(self, word: str) -> List[str]: List[str]
Generates synonyms for a single word and returns them in a list.

Args:
    word (str): The word for which synonyms are generated.

Returns:
    List[str]: A list of generated synonyms for the word.
method
release(self, empty_cuda_cache = False)
Abstract method to release resources (must be implemented in subclasses).
class

datadreamer.prompt_generation.LMPromptGenerator(datadreamer.prompt_generation.prompt_generator.PromptGenerator)

method
variable
variable
variable
method
generate_prompts_batch(self, prompt_texts_batch: List [ str ]) -> List[str]: List[str]
Generates a list of prompts using the language model.

Args:
    prompt_texts_batch (List[str]): List of text prompts for the language model.

Returns:
    List[str]: List of generated prompts.
method
generate_prompts(self) -> List[str]: List[str]
Generates a list of text prompts based on the class names.

Returns:
    List[str]: A list of generated prompts.
method
release(self, empty_cuda_cache = False)
Releases the model and optionally empties the CUDA cache.
class

datadreamer.prompt_generation.LMSynonymGenerator(datadreamer.prompt_generation.synonym_generator.SynonymGenerator)

method
variable
variable
variable
method
generate_synonyms(self, word: str) -> List[str]: List[str]
Generates synonyms for a single word and returns them in a list.

Args:
    word (str): The word for which synonyms are generated.

Returns:
    List[str]: A list of generated synonyms for the word.
method
release(self, empty_cuda_cache = False)
Releases resources and optionally empties the CUDA cache.

Args:
    empty_cuda_cache (bool): Whether to empty the CUDA cache (default is False).
class

datadreamer.prompt_generation.SimplePromptGenerator(datadreamer.prompt_generation.prompt_generator.PromptGenerator)

method
__init__(self, args, kwargs)
Initializes the SimplePromptGenerator with class names and other settings.
method
generate_prompts(self) -> List[str]: List[str]
Generates a list of simple prompts.

Returns:
    List[str]: A list of generated prompts in the form of "A photo of a {selected_objects}".
method
generate_prompt(self, selected_objects: List [ str ]) -> str: str
Generates a single simple prompt based on selected objects.

Args:
    selected_objects (List[str]): List of selected objects to include in the prompt.

Returns:
    str: A simple prompt in the form of "A photo of a {selected_objects}".
method
release(self, empty_cuda_cache = False)
Releases resources (no action is taken in this implementation).
class

datadreamer.prompt_generation.TinyLlamaLMPromptGenerator(datadreamer.prompt_generation.LMPromptGenerator)

method
method
generate_prompts_batch(self, prompt_texts_batch: List [ str ]) -> List[str]: List[str]
Generates a list of prompts using the language model.

Args:
    prompt_texts_batch (List[str]): List of text prompts for the language model.

Returns:
    List[str]: List of generated prompts.
class

datadreamer.prompt_generation.WordNetSynonymGenerator(datadreamer.prompt_generation.synonym_generator.SynonymGenerator)

method
method
generate_synonyms(self, word: str) -> List[str]: List[str]
Generates synonyms for a single word and returns them in a list.

Args:
    word (str): The word for which synonyms are generated.

Returns:
    List[str]: A list of generated synonyms for the word.
method
release(self, empty_cuda_cache: bool = False)
Releases resources (no action is taken in this implementation).
package

datadreamer.utils

module
module
module
module
module
module
module
module
module
module
class
BaseConverter
Abstract base class for converter.
class
COCOConverter
Class for converting a dataset to COCO format.
class
class
LuxonisDatasetConverter
Class for converting a dataset to LuxonisDataset format.
class
SingleLabelClsConverter
Class for converting a dataset for single-label classification task.
class
YOLOConverter
Class for converting a dataset to YOLO format.
module

datadreamer.utils.merge_raw_datasets

module

datadreamer.utils.nms

function
xywh2xyxy(x)
Convert boxes with shape [n, 4] from [x, y, w, h] to [x1, y1, x2, y2] where x1y1
is top-left, x2y2=bottom-right.
function
non_max_suppression(prediction, conf_thres = 0.25, iou_thres = 0.45, classes = None, agnostic = False, multi_label = False, max_det = 300)
Runs Non-Maximum Suppression (NMS) on inference results.
This code is borrowed from: https://github.com/ultralytics/yolov5/blob/47233e1698b89fc437a4fb9463c815e9171be955/utils/general.py#L775
Args:
    prediction: (tensor), with shape [N, 5 + num_classes], N is the number of bboxes.
    conf_thres: (float) confidence threshold.
    iou_thres: (float) iou threshold.
    classes: (None or list[int]), if a list is provided, nms only keep the classes you provide.
    agnostic: (bool), when it is set to True, we do class-independent nms, otherwise, different class would do nms respectively.
    multi_label: (bool), when it is set to True, one box can have multi labels, otherwise, one box only huave one label.
    max_det:(int), max number of output bboxes.

Returns:
     list of detections, echo item is one tensor with shape (num_boxes, 6), 6 is for [xyxy, conf, cls].
class

datadreamer.utils.BaseConverter(abc.ABC)

method
method
convert(self, dataset_dir, output_dir, split_ratios, copy_files = True)
Converts a dataset into another format.

Args:
- dataset_dir (str): The directory where the source dataset is located.
- output_dir (str): The directory where the processed dataset should be saved.
- split_ratios (list of float): The ratios to split the data into training, validation, and test sets.
- copy_files (bool, optional): Whether to copy the source files to the output directory, otherwise move them. Defaults to True.


No return value.
static method
BaseConverter.read_annotations(annotation_path)
Reads annotations from a JSON file located at the specified path.

Args:
- annotation_path (str): The path to the JSON file containing annotations.

Returns:
- dict: A dictionary containing the data loaded from the JSON file.
static method
BaseConverter.make_splits(images, split_ratios, shuffle = True)
Splits the list of images into training, validation, and test sets.

Args:
- images (list of str): A list of image paths.
- split_ratios (list of float): The ratios to split the data into training, validation, and test sets.
- shuffle (bool, optional): Whether to shuffle the list of images. Defaults to True.

Returns:
- list of str: A list of image paths for the training set.
- list of str: A list of image paths for the validation set.
- list of str: A list of image paths for the test set.
class

datadreamer.utils.COCOConverter(datadreamer.utils.BaseConverter)

method
method
convert(self, dataset_dir, output_dir, split_ratios, copy_files = True)
Converts a dataset into a COCO format.

Args:
- dataset_dir (str): The directory where the source dataset is located.
- output_dir (str): The directory where the processed dataset should be saved.
- split_ratios (list of float): The ratios to split the data into training, validation, and test sets.
- copy_files (bool, optional): Whether to copy the source files to the output directory, otherwise move them. Defaults to True.

No return value.
method
process_data(self, data, image_dir, output_dir, split_ratios, copy_files = True)
Processes the data by dividing it into training and validation sets, and
saves the images and labels in COCO format.

Args:
- data (dict): The dictionary containing image annotations.
- image_dir (str): The directory where the source images are located.
- output_dir (str): The base directory where the processed data will be saved.
- split_ratios (float): The ratio to split the data into training, validation, and test sets.
- copy_files (bool, optional): Whether to copy the source files to the output directory, otherwise move them. Defaults to True.


No return value.
method
save_labels(self, dataset_output_dir, images_info, annotations, class_names)
Saves the labels to a JSON file.

Args:
- dataset_output_dir (str): The directory where the labels should be saved.
- images_info (list of dict): A list of dictionaries containing image information.
- annotations (list of dict): A list of dictionaries containing annotation information.
- class_names (list of str): A list of class names.

No return value.
class

datadreamer.utils.LuxonisDatasetConverter(datadreamer.utils.BaseConverter)

method
method
convert(self, dataset_dir, output_dir, split_ratios, copy_files = True)
Converts a dataset into a LuxonisDataset format.

Args:
- dataset_dir (str): The directory where the source dataset is located.
- output_dir (str): The directory where the processed dataset should be saved.
- split_ratios (list of float): The ratios to split the data into training, validation, and test sets.
- copy_files (bool, optional): Whether to copy the source files to the output directory, otherwise move them. Defaults to True.

No return value.
method
class

datadreamer.utils.SingleLabelClsConverter(datadreamer.utils.BaseConverter)

method
method
convert(self, dataset_dir, output_dir, split_ratios, copy_files = True)
Converts a dataset into a format suitable for single-label classification.

Args:
- dataset_dir (str): The directory where the source dataset is located.
- output_dir (str): The directory where the processed dataset should be saved.
- split_ratios (list of float): The ratios to split the data into training, validation, and test sets.
- copy_files (bool, optional): Whether to copy the source files to the output directory, otherwise move them. Defaults to True.

No return value.
method
process_data(self, data, image_dir, output_dir, split_ratios, copy_files = True)
Processes the data by removing images with multiple labels, then dividing it
into training and validation sets, and saves the images with single labels.

Args:
- data (dict): The dictionary containing image annotations.
- image_dir (str): The directory where the source images are located.
- output_dir (str): The base directory where the processed data will be saved.
- split_ratios (float): The ratio to split the data into training, validation, and test sets.
- copy_files (bool, optional): Whether to copy the source files to the output directory, otherwise move them. Defaults to True.

No return value.
class

datadreamer.utils.YOLOConverter(datadreamer.utils.BaseConverter)

method
method
convert(self, dataset_dir, output_dir, split_ratios, copy_files = True)
Converts a dataset into a format suitable for training with YOLO, including
creating training and validation splits.

Args:
- dataset_dir (str): The directory where the source dataset is located.
- output_dir (str): The directory where the processed dataset should be saved.
- split_ratios (list of float): The ratios to split the data into training, validation, and test sets.
- copy_files (bool, optional): Whether to copy the source files to the output directory, otherwise move them. Defaults to True.

No return value.
method
convert_to_yolo_format(self, box, image_width, image_height)
Converts bounding box coordinates to YOLO format.

Args:
- box (list of float): A list containing the bounding box coordinates [x_min, y_min, x_max, y_max].
- image_width (int): The width of the image.
- image_height (int): The height of the image.

Returns:
- list of float: A list containing the bounding box in YOLO format [x_center, y_center, width, height].
method
process_data(self, data, image_dir, output_dir, split_ratios, copy_files = True)
Processes the data by dividing it into training and validation sets, and
saves the images and labels in YOLO format.

Args:
- data (dict): The dictionary containing image annotations.
- image_dir (str): The directory where the source images are located.
- output_dir (str): The base directory where the processed data will be saved.
- split_ratios (float): The ratio to split the data into training, validation, and test sets.
- copy_files (bool, optional): Whether to copy the source files to the output directory, otherwise move them. Defaults to True.


No return value.
method
create_data_yaml(self, root_dir, class_names)
Creates a YAML file for dataset configuration, specifying paths and class
names.

Args:
- root_dir (str): The root directory where the dataset is located.
- class_names (list of str): A list of class names.

No return value.