module documentation

PyTorch utilities.

Copyright 2017-2025, Voxel51, Inc.

Class ClassifierOutputProcessor Output processor for single label classifiers.
Class DetectorOutputProcessor Output processor for object detectors.
Class InstanceSegmenterOutputProcessor Output processor for instance segementers.
Class KeypointDetectorOutputProcessor Output processor for keypoint detection models.
Class MaxResize Transform that resizes the PIL image or torch Tensor, if necessary, so that its maximum dimensions are at most the specified size.
Class MinResize Transform that resizes the PIL image or torch Tensor, if necessary, so that its minimum dimensions are at least the specified size.
Class OutputProcessor Interface for processing the outputs of Torch models.
Class PatchSize Transform that center crops the PIL image or torch Tensor, if necessary, so that its dimensions are multiples of the specified patch size.
Class SaveLayerTensor Callback that saves the input/output tensor of the specified layer of a Torch model during each forward() call.
Class SemanticSegmenterOutputProcessor Output processor for semantic segementers.
Class ToPILImage Transform that converts a tensor or ndarray to a PIL image, while also allowing PIL images to passthrough.
Class TorchEmbeddingsMixin Mixin for Torch models that can generate embeddings.
Class TorchImageClassificationDataset A torch:torch.utils.data.Dataset for image classification.
Class TorchImageDataset A torch:torch.utils.data.Dataset of images.
Class TorchImageModel Wrapper for evaluating a Torch model on images.
Class TorchImageModelConfig Configuration for running a TorchImageModel.
Class TorchImagePatchesDataset A torch:torch.utils.data.Dataset of image patch tensors extracted from a list of images.
Class TorchSamplesMixin Undocumented
Function ensure_torch_hub_requirements Verifies that the package requirements from the requirements.txt file on disk associated with a downloaded PyTorch Hub model are installed.
Function find_torch_hub_requirements Locates the requirements.txt file on disk associated with a downloaded PyTorch Hub model.
Function from_image_classification_dir_tree Creates a torch:torch.utils.data.Dataset for the given image classification dataset directory tree.
Function install_torch_hub_requirements Installs the package requirements from the requirements.txt file on disk associated with a downloaded PyTorch Hub model.
Function load_torch_hub_image_model Loads an image model from PyTorch Hub as a TorchImageModel.
Function load_torch_hub_raw_model Loads a raw model from PyTorch Hub as a torch:torch.nn.Module.
Function load_torch_hub_requirements Loads the package requirements from the requirements.txt file on disk associated with a downloaded PyTorch Hub model.
Function recommend_num_workers Recommend a number of workers for running a torch:torch.utils.data.DataLoader.
Variable logger Undocumented
Function _is_string_array Undocumented
Function _load_image Undocumented
Function _polyline_to_bbox Undocumented
Function _polylines_to_bboxes Undocumented
Function _strip_comments Undocumented
Function _to_bytes_array Undocumented
Function _to_eta_bbox Undocumented
def ensure_torch_hub_requirements(repo_or_dir, source='github', error_level=None, log_success=False): (source)

Verifies that the package requirements from the requirements.txt file on disk associated with a downloaded PyTorch Hub model are installed.

Example usage:

import fiftyone.utils.torch as fout

fout.ensure_torch_hub_requirements("facebookresearch/dinov2")
Parameters
repo_or_dirsee torch:torch.hub.load
source:"github"see torch:torch.hub.load
error_level:None

the error level to use, defined as:

  • 0: raise error if requirement is not satisfied
  • 1: log warning if requirement is not satisfied
  • 2: ignore unsatisifed requirements

By default, fiftyone.config.requirement_error_level is used

log_success:Falsewhether to generate a log message if a requirement is satisfied
def find_torch_hub_requirements(repo_or_dir, source='github'): (source)

Locates the requirements.txt file on disk associated with a downloaded PyTorch Hub model.

Example usage:

import fiftyone.utils.torch as fout

req_path = fout.find_torch_hub_requirements("facebookresearch/dinov2")

print(req_path)
# '~/.cache/torch/hub/facebookresearch_dinov2_main/requirements.txt'
Parameters
repo_or_dirsee torch:torch.hub.load
source:"github"see torch:torch.hub.load
Returns
the path to the requirements file on disk
def from_image_classification_dir_tree(dataset_dir): (source)

Creates a torch:torch.utils.data.Dataset for the given image classification dataset directory tree.

The directory should have the following format:

<dataset_dir>/
    <classA>/
        <image1>.<ext>
        <image2>.<ext>
        ...
    <classB>/
        <image1>.<ext>
        <image2>.<ext>
        ...
Parameters
dataset_dirthe dataset directory
Returns
a torchvision:torchvision.datasets.ImageFolder
def install_torch_hub_requirements(repo_or_dir, source='github', error_level=None): (source)

Installs the package requirements from the requirements.txt file on disk associated with a downloaded PyTorch Hub model.

Example usage:

import fiftyone.utils.torch as fout

fout.install_torch_hub_requirements("facebookresearch/dinov2")
Parameters
repo_or_dirsee torch:torch.hub.load
source:"github"see torch:torch.hub.load
error_level:None

the error level to use, defined as:

  • 0: raise error if the install fails
  • 1: log warning if the install fails
  • 2: ignore install fails

By default, fiftyone.config.requirement_error_level is used

def load_torch_hub_image_model(repo_or_dir, model, hub_kwargs=None, **kwargs): (source)

Loads an image model from PyTorch Hub as a TorchImageModel.

Example usage:

import fiftyone.utils.torch as fout

model = fout.load_torch_hub_image_model(
    "facebookresearch/dinov2",
    "dinov2_vits14",
    image_patch_size=14,
    embeddings_layer="head",
)

assert model.has_embeddings is True
Parameters
repo_or_dirsee torch:torch.hub.load
modelsee torch:torch.hub.load
hub_kwargsUndocumented
**kwargsadditional parameters for TorchImageModelConfig
Returns
a TorchImageModel
def load_torch_hub_raw_model(*args, **kwargs): (source)

Loads a raw model from PyTorch Hub as a torch:torch.nn.Module.

Example usage:

import fiftyone.utils.torch as fout

model = fout.load_torch_hub_raw_model(
    "facebookresearch/dinov2",
    "dinov2_vits14",
)

print(type(model))
# <class 'dinov2.models.vision_transformer.DinoVisionTransformer'>
Parameters
*argspositional arguments for torch:torch.hub.load
**kwargskeyword arguments for torch:torch.hub.load
Returns
a torch:torch.nn.Module
def load_torch_hub_requirements(repo_or_dir, source='github'): (source)

Loads the package requirements from the requirements.txt file on disk associated with a downloaded PyTorch Hub model.

Example usage:

import fiftyone.utils.torch as fout

requirements = fout.load_torch_hub_requirements("facebookresearch/dinov2")

print(requirements)
# ['torch==2.0.0', 'torchvision==0.15.0', ...]
Parameters
repo_or_dirsee torch:torch.hub.load
source:"github"see torch:torch.hub.load
Returns
a list of requirement strings
def recommend_num_workers(): (source)

Recommend a number of workers for running a torch:torch.utils.data.DataLoader.

Returns
the recommended number of workers

Undocumented

def _is_string_array(targets): (source)

Undocumented

def _load_image(image_path, use_numpy, force_rgb): (source)

Undocumented

def _polyline_to_bbox(points): (source)

Undocumented

def _polylines_to_bboxes(points): (source)

Undocumented

def _strip_comments(requirement_str): (source)

Undocumented

def _to_bytes_array(strs): (source)

Undocumented

def _to_eta_bbox(bounding_box): (source)

Undocumented