Skip to content

Commit

Permalink
dev(narugo): add docs for fidelity and utils
Browse files Browse the repository at this point in the history
  • Loading branch information
narugo1992 committed Nov 15, 2023
1 parent 4ecea34 commit f5d4ed1
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/source/api_doc/fidelity/ccip.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sdeval.fidelity.ccip
=================================

.. currentmodule:: sdeval.fidelity.ccip

.. automodule:: sdeval.fidelity.ccip


CCIPMetrics
--------------------------------

.. autoclass:: CCIPMetrics
:members: __init__, score


12 changes: 12 additions & 0 deletions docs/source/api_doc/fidelity/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sdeval.fidelity
=====================

.. currentmodule:: sdeval.fidelity

.. automodule:: sdeval.fidelity


.. toctree::
:maxdepth: 3

ccip
15 changes: 15 additions & 0 deletions docs/source/api_doc/utils/images.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sdeval.utils.images
=================================

.. currentmodule:: sdeval.utils.images

.. automodule:: sdeval.utils.images


load_images
----------------------------------

.. autofunction:: load_images



13 changes: 13 additions & 0 deletions docs/source/api_doc/utils/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sdeval.utils
=====================

.. currentmodule:: sdeval.utils

.. automodule:: sdeval.utils


.. toctree::
:maxdepth: 3

tqdm_
images
15 changes: 15 additions & 0 deletions docs/source/api_doc/utils/tqdm_.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
sdeval.utils.tqdm\_
=================================

.. currentmodule:: sdeval.utils.tqdm_

.. automodule:: sdeval.utils.tqdm_


tqdm
----------------------------------

.. autofunction:: tqdm



2 changes: 2 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions sdeval/fidelity/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
"""
Overview:
Metrics for evaluating training fidelity.
"""
from .ccip import CCIPMetrics
36 changes: 36 additions & 0 deletions sdeval/fidelity/ccip.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""
Overview:
CCIP-based metrics for anime character training.
See `imgutils.metrics.ccip <https://deepghs.github.io/imgutils/main/api_doc/metrics/ccip.html>`_ for more information.
"""
from typing import List, Optional

from PIL import Image
Expand All @@ -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)
Expand All @@ -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}.')
Expand Down
22 changes: 22 additions & 0 deletions sdeval/utils/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))

0 comments on commit f5d4ed1

Please sign in to comment.