Skip to content

utils

feat.utils

py-feat helper functions and variables

flatten_list(data)

Helper function to flatten a list of lists

Source code in feat/utils/__init__.py
def flatten_list(data):
    """Helper function to flatten a list of lists"""
    flat_list = []
    for row in data:
        flat_list += row
    return flat_list

generate_coordinate_names(num_points=478)

Generates a list of names for x, y, z coordinates for a given number of points.

Parameters:

Name Type Description Default
num_points int

Number of points (478 in this case).

478

Returns:

Name Type Description
list

List of coordinate names like ['x_1', 'y_1', 'z_1', ..., 'x_n', 'y_n', 'z_n'].

Source code in feat/utils/__init__.py
def generate_coordinate_names(num_points=478):
    """
    Generates a list of names for x, y, z coordinates for a given number of points.

    Args:
        num_points (int): Number of points (478 in this case).

    Returns:
        list: List of coordinate names like ['x_1', 'y_1', 'z_1', ..., 'x_n', 'y_n', 'z_n'].
    """
    coordinate_names = []
    for i in range(0, num_points):
        coordinate_names.extend([f"x_{i}", f"y_{i}", f"z_{i}"])

    return coordinate_names

hf_hub_download_with_fallback(repo_id, filename, fallback_filename, cache_dir)

Download filename from HuggingFace; on miss, download fallback_filename.

Used to roll out new model file versions without breaking installs in the window between a code release and the artifact upload. Once the new file is reliably present on the hub, the fallback path is dead code; once the old file is removed, the fallback path errors. Both are good outcomes.

Returns the local path of whichever file was successfully downloaded.

Source code in feat/utils/__init__.py
def hf_hub_download_with_fallback(repo_id, filename, fallback_filename, cache_dir):
    """Download ``filename`` from HuggingFace; on miss, download ``fallback_filename``.

    Used to roll out new model file versions without breaking installs in
    the window between a code release and the artifact upload. Once the
    new file is reliably present on the hub, the fallback path is dead
    code; once the old file is removed, the fallback path errors. Both
    are good outcomes.

    Returns the local path of whichever file was successfully downloaded.
    """
    from huggingface_hub import hf_hub_download
    from huggingface_hub.utils import EntryNotFoundError

    try:
        return hf_hub_download(repo_id=repo_id, filename=filename, cache_dir=cache_dir)
    except EntryNotFoundError:
        return hf_hub_download(
            repo_id=repo_id, filename=fallback_filename, cache_dir=cache_dir
        )

set_torch_device(device='cpu')

Helper function to set device for pytorch model

Source code in feat/utils/__init__.py
def set_torch_device(device="cpu"):
    """Helper function to set device for pytorch model"""

    if not isinstance(device, torch.device):
        if device not in ["cpu", "cuda", "mps", "auto"]:
            raise ValueError("Device must be ['cpu', 'cuda', 'mps', 'auto']")

        if device == "auto":
            # FIXME: This currently doesn't work on mac's where mps is available because
            # it results in a mix of cpu and mps operations which cause failures. E.g.
            # when we call torch.cat() inside of image_operations.decode from
            # FaceBoxes_tests.py(128)

            # In this case priors are on `cpu`, loc is on `mps`, variances is a list (so
            # cpu I assume). loc also contains all nans

            # This causes retinaface to fail to detect a face properly and tests to fail

            if torch.cuda.is_available():
                device = "cuda"
            elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
                device = "mps"
            else:
                device = "cpu"
        else:
            device = device
        return torch.device(device)

    else:
        return device