diff --git a/docs/source/api_doc/fidelity/ccip.rst b/docs/source/api_doc/fidelity/ccip.rst new file mode 100644 index 0000000..b279c2e --- /dev/null +++ b/docs/source/api_doc/fidelity/ccip.rst @@ -0,0 +1,15 @@ +sdeval.fidelity.ccip +================================= + +.. currentmodule:: sdeval.fidelity.ccip + +.. automodule:: sdeval.fidelity.ccip + + +CCIPMetrics +-------------------------------- + +.. autoclass:: CCIPMetrics + :members: __init__, score + + diff --git a/docs/source/api_doc/fidelity/index.rst b/docs/source/api_doc/fidelity/index.rst new file mode 100644 index 0000000..f3ae1e8 --- /dev/null +++ b/docs/source/api_doc/fidelity/index.rst @@ -0,0 +1,12 @@ +sdeval.fidelity +===================== + +.. currentmodule:: sdeval.fidelity + +.. automodule:: sdeval.fidelity + + +.. toctree:: + :maxdepth: 3 + + ccip diff --git a/docs/source/api_doc/utils/images.rst b/docs/source/api_doc/utils/images.rst new file mode 100644 index 0000000..678709c --- /dev/null +++ b/docs/source/api_doc/utils/images.rst @@ -0,0 +1,15 @@ +sdeval.utils.images +================================= + +.. currentmodule:: sdeval.utils.images + +.. automodule:: sdeval.utils.images + + +load_images +---------------------------------- + +.. autofunction:: load_images + + + diff --git a/docs/source/api_doc/utils/index.rst b/docs/source/api_doc/utils/index.rst new file mode 100644 index 0000000..0855a55 --- /dev/null +++ b/docs/source/api_doc/utils/index.rst @@ -0,0 +1,13 @@ +sdeval.utils +===================== + +.. currentmodule:: sdeval.utils + +.. automodule:: sdeval.utils + + +.. toctree:: + :maxdepth: 3 + + tqdm_ + images diff --git a/docs/source/api_doc/utils/tqdm_.rst b/docs/source/api_doc/utils/tqdm_.rst new file mode 100644 index 0000000..2ab5eee --- /dev/null +++ b/docs/source/api_doc/utils/tqdm_.rst @@ -0,0 +1,15 @@ +sdeval.utils.tqdm\_ +================================= + +.. currentmodule:: sdeval.utils.tqdm_ + +.. automodule:: sdeval.utils.tqdm_ + + +tqdm +---------------------------------- + +.. autofunction:: tqdm + + + diff --git a/docs/source/index.rst b/docs/source/index.rst index 4ed10d5..a2ccbdb 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -29,6 +29,8 @@ configuration file's structure and their versions. :caption: API Documentation api_doc/config/index + api_doc/fidelity/index + api_doc/utils/index .. toctree:: :maxdepth: 2 diff --git a/sdeval/fidelity/__init__.py b/sdeval/fidelity/__init__.py index 5963d03..ae4fdc4 100644 --- a/sdeval/fidelity/__init__.py +++ b/sdeval/fidelity/__init__.py @@ -1 +1,5 @@ +""" +Overview: + Metrics for evaluating training fidelity. +""" from .ccip import CCIPMetrics diff --git a/sdeval/fidelity/ccip.py b/sdeval/fidelity/ccip.py index 97b10ab..efcf376 100644 --- a/sdeval/fidelity/ccip.py +++ b/sdeval/fidelity/ccip.py @@ -1,3 +1,9 @@ +""" +Overview: + CCIP-based metrics for anime character training. + + See `imgutils.metrics.ccip `_ for more information. +""" from typing import List, Optional from PIL import Image @@ -9,6 +15,23 @@ class CCIPMetrics: + """ + Class for calculating similarity scores between images using the CCIP (Content-Consistent Image Pairwise) metric. + + The `CCIPMetrics` class allows you to calculate the similarity score between a set of images and a reference dataset using the CCIP metric. + + :param images: The reference dataset of images for initializing CCIP metrics. + :type images: ImagesTyping + :param model: The CCIP model to use for feature extraction. Default is 'ccip-caformer-24-randaug-pruned'. + :type model: str + :param threshold: The threshold for the CCIP metric. If not provided, the default threshold for the chosen model is used. + :type threshold: Optional[float] + :param silent: If True, suppresses progress bars and additional output during initialization and calculation. + :type silent: bool + :param tqdm_desc: Description for the tqdm progress bar during initialization and calculation. + :type tqdm_desc: str + """ + def __init__(self, images: ImagesTyping, model: str = _DEFAULT_CCIP_MODEL, threshold: Optional[float] = None, silent: bool = False, tqdm_desc: str = None): image_list: List[Image.Image] = load_images(images) @@ -25,6 +48,19 @@ def __init__(self, images: ImagesTyping, model: str = _DEFAULT_CCIP_MODEL, self._threshold = ccip_default_threshold(self._ccip_model) if threshold is None else threshold def score(self, images: ImagesTyping, silent: bool = None) -> float: + """ + Calculate the similarity score between the reference dataset and a set of input images. + + This method calculates the similarity score between the reference dataset (used for initialization) and a set of input images using the CCIP metric. + + :param images: The set of input images for calculating CCIP metrics. + :type images: ImagesTyping + :param silent: If True, suppresses progress bars and additional output during calculation. + :type silent: bool + + :return: The similarity score between the reference dataset and the input images. + :rtype: float + """ image_list: List[Image.Image] = load_images(images) if not image_list: raise FileNotFoundError(f'Images for calculating CCIP metrics not provided - {images}.') diff --git a/sdeval/utils/images.py b/sdeval/utils/images.py index 94c83c2..df9b23e 100644 --- a/sdeval/utils/images.py +++ b/sdeval/utils/images.py @@ -8,6 +8,17 @@ def _yield_images(images: ImagesTyping) -> Iterator[Image.Image]: + """ + Yield PIL.Image objects from various sources. + + This internal function yields PIL.Image objects from a variety of sources, including PIL.Image objects, file paths, and lists of images. It supports recursive loading of images from directories. + + :param images: An image or a list of images (PIL.Image, file paths, or a combination). + :type images: ImagesTyping + + :return: An iterator yielding PIL.Image objects. + :rtype: Iterator[Image.Image] + """ if isinstance(images, list): for item in images: yield from _yield_images(item) @@ -25,4 +36,15 @@ def _yield_images(images: ImagesTyping) -> Iterator[Image.Image]: def load_images(images: ImagesTyping) -> List[Image.Image]: + """ + Load multiple PIL.Image objects from various sources. + + This function loads multiple PIL.Image objects from a variety of sources, including PIL.Image objects, file paths, and lists of images. It supports recursive loading of images from directories. + + :param images: An image or a list of images (PIL.Image, file paths, or a combination). + :type images: ImagesTyping + + :return: A list of PIL.Image objects. + :rtype: List[Image.Image] + """ return list(_yield_images(images))