diff --git a/src/facebias/__init__.py b/src/facebias/__init__.py index 2159cd1..2009337 100644 --- a/src/facebias/__init__.py +++ b/src/facebias/__init__.py @@ -5,7 +5,7 @@ import logging from collections import OrderedDict from dataclasses import dataclass from pathlib import Path -from typing import Any, Callable, Optional +from typing import Any, Callable import cv2 import numpy as np @@ -41,9 +41,9 @@ def load_metadata(p: Path, key_id="image", key_proc_fn=None) -> dict[str, dict[s def load_dataset( root: Path, - meta_path: Optional[Path] = None, - imname_proc_fn: Optional[Callable]=None -) -> tuple[dict[Path, np.ndarray], Optional[dict[str, dict[str, Any]]]]: + meta_path: Path | None, + imname_proc_fn: Callable |None +) -> tuple[dict[str, np.ndarray], dict[str, dict[str, Any]] | None]: """ if `meta_path` is `None`, we won't attempt to read it. @@ -53,20 +53,22 @@ def load_dataset( Root path to the images. We assume that the images are positioned in a flat directory under `root`. - meta_path: Optional[Path] + meta_path: Path | None Path to the metadata file on the dataset. Default is `None`. - imname_proc_fn: Optional[Callable] + imname_proc_fn: Callable | None Function to apply to the image filenames when building the output dictionary. Use it to uniformize it with the metadata file. Default is `None`. Returns ------- - data: dict[Path, np.ndarray] - metadata, Optional[dict[str, dict[str, Any]]] + data: dict[str, np.ndarray] + metadata, dict[str, dict[str, Any]] | None + If metadata was not found, or not set to load (by leaving `meta_path` + as `None`), then we simply return `None`. """ - metadata = dict() + metadata = None paths = set([p for p in root.iterdir() if not p.is_dir()]) if meta_path is not None and meta_path in paths: # Just read the metadata and avoid needing to test for it in the main loop. @@ -74,14 +76,13 @@ def load_dataset( paths.remove(meta_path) ims = OrderedDict() - # TODO(gschardong): Paralellize this? for p in paths: try: im = cv2.cvtColor(cv2.imread(p), cv2.COLOR_BGR2RGB) except cv2.error: logger.info(f'File "{p}" is not an image. Skipping.') else: - proc_imname = imname_proc_fn(p.name) if imname_proc_fn is not None else p.name + proc_imname = imname_proc_fn(p.name) if imname_proc_fn is not None else str(p.name) ims[proc_imname] = im if not metadata: diff --git a/src/facebias/detectors/mediapipe.py b/src/facebias/detectors/mediapipe.py index 9aeb43e..b882cf7 100755 --- a/src/facebias/detectors/mediapipe.py +++ b/src/facebias/detectors/mediapipe.py @@ -15,7 +15,7 @@ class MediapipeDetector(BaseFaceDetector): Parameters ---------- model_path: str, PathLike - mode: Optional[mediapipe.tasks.python.vision.RunningMode] + mode: mediapipe.tasks.python.vision.RunningMode """ def __init__(self, model_path: str, mode=vision.RunningMode.IMAGE) -> None: diff --git a/src/facebias/estimators/fairface.py b/src/facebias/estimators/fairface.py index 3580c36..703f9d2 100644 --- a/src/facebias/estimators/fairface.py +++ b/src/facebias/estimators/fairface.py @@ -2,7 +2,7 @@ from collections import OrderedDict from pathlib import Path -from typing import Any, Optional +from typing import Any import numpy as np import torch @@ -24,10 +24,10 @@ class FairFace(BaseEstimator): Parameters ---------- - checkpoint_path: Optional[Path] + checkpoint_path: Path | None Path to the model weights stored as a PyTorch file. - device: Optional[torch.device] + device: torch.device The device to load the model into. By default is `"cpu"` Reference @@ -36,8 +36,8 @@ class FairFace(BaseEstimator): """ def __init__( self, - checkpoint_path: Optional[Path]=None, - device: Optional[torch.device]=torch.device("cpu") + checkpoint_path: Path | None, + device: torch.device=torch.device("cpu") ): self.T = transforms.Compose([ transforms.ToPILImage(), diff --git a/src/facebias/estimators/mivolov1.py b/src/facebias/estimators/mivolov1.py index 6baf019..ac40abb 100644 --- a/src/facebias/estimators/mivolov1.py +++ b/src/facebias/estimators/mivolov1.py @@ -2,7 +2,7 @@ from collections import OrderedDict from pathlib import Path -from typing import Any, Optional +from typing import Any import numpy as np import torch @@ -24,7 +24,7 @@ class MiVOLOv1(BaseEstimator): Path to the saved checkpoint. Note that we expect the checkpoint to be in PyTorch format. - device: Optional[torch.device] + device: torch.device Device to load the checkpoints into. By default is "cpu". Reference @@ -34,7 +34,7 @@ class MiVOLOv1(BaseEstimator): def __init__( self, checkpoint_path: Path, - device: Optional[torch.device]=torch.device("cpu") + device: torch.device=torch.device("cpu") ): self.T = transforms.Compose([ transforms.ToPILImage(),