Skip to content

Networks

Reference information for the model Networks API.

eva.models.networks.MLP

Bases: Module

A Multi-layer Perceptron (MLP) network.

Parameters:

Name Type Description Default
input_size int

The number of input features.

required
output_size int

The number of output features.

required
hidden_layer_sizes Tuple[int, ...] | None

A list specifying the number of units in each hidden layer.

None
dropout float

Dropout probability for hidden layers.

0.0
hidden_activation_fn Type[Module] | None

Activation function to use for hidden layers. Default is ReLU.

ReLU
output_activation_fn Type[Module] | None

Activation function to use for the output layer. Default is None.

None
Source code in src/eva/core/models/networks/mlp.py
def __init__(
    self,
    input_size: int,
    output_size: int,
    hidden_layer_sizes: Tuple[int, ...] | None = None,
    hidden_activation_fn: Type[torch.nn.Module] | None = nn.ReLU,
    output_activation_fn: Type[torch.nn.Module] | None = None,
    dropout: float = 0.0,
) -> None:
    """Initializes the MLP.

    Args:
        input_size: The number of input features.
        output_size: The number of output features.
        hidden_layer_sizes: A list specifying the number of units in each hidden layer.
        dropout: Dropout probability for hidden layers.
        hidden_activation_fn: Activation function to use for hidden layers. Default is ReLU.
        output_activation_fn: Activation function to use for the output layer. Default is None.
    """
    super().__init__()

    self.input_size = input_size
    self.output_size = output_size
    self.hidden_layer_sizes = hidden_layer_sizes if hidden_layer_sizes is not None else ()
    self.hidden_activation_fn = hidden_activation_fn
    self.output_activation_fn = output_activation_fn
    self.dropout = dropout

    self._network = self._build_network()

forward

Defines the forward pass of the MLP.

Parameters:

Name Type Description Default
x Tensor

The input tensor.

required

Returns:

Type Description
Tensor

The output of the network.

Source code in src/eva/core/models/networks/mlp.py
def forward(self, x: torch.Tensor) -> torch.Tensor:
    """Defines the forward pass of the MLP.

    Args:
        x: The input tensor.

    Returns:
        The output of the network.
    """
    return self._network(x)

Wrappers

eva.models.wrappers.BaseModel

Bases: Module

Base class for model wrappers.

Parameters:

Name Type Description Default
tensor_transforms Callable | None

The transforms to apply to the output tensor produced by the model.

None
Source code in src/eva/core/models/wrappers/base.py
def __init__(self, tensor_transforms: Callable | None = None) -> None:
    """Initializes the model.

    Args:
        tensor_transforms: The transforms to apply to the output
            tensor produced by the model.
    """
    super().__init__()

    self._output_transforms = tensor_transforms

    self._model: Callable[..., torch.Tensor] | nn.Module

load_model abstractmethod

Loads the model.

Source code in src/eva/core/models/wrappers/base.py
@abc.abstractmethod
def load_model(self) -> Callable[..., torch.Tensor]:
    """Loads the model."""
    raise NotImplementedError

model_forward

Implements the forward pass of the model.

Parameters:

Name Type Description Default
tensor Tensor

The input tensor to the model.

required
Source code in src/eva/core/models/wrappers/base.py
def model_forward(self, tensor: torch.Tensor) -> torch.Tensor:
    """Implements the forward pass of the model.

    Args:
        tensor: The input tensor to the model.
    """
    return self._model(tensor)

eva.models.wrappers.ModelFromFunction

Bases: BaseModel

Wrapper class for models which are initialized from functions.

This is helpful for initializing models in a .yaml configuration file.

Parameters:

Name Type Description Default
path Callable[..., Module]

The path to the callable object (class or function).

required
arguments Dict[str, Any] | None

The extra callable function / class arguments.

None
checkpoint_path str | None

The path to the checkpoint to load the model weights from. This is currently only supported for torch model checkpoints. For other formats, the checkpoint loading should be handled within the provided callable object in .

None
tensor_transforms Callable | None

The transforms to apply to the output tensor produced by the model.

None
Source code in src/eva/core/models/wrappers/from_function.py
def __init__(
    self,
    path: Callable[..., nn.Module],
    arguments: Dict[str, Any] | None = None,
    checkpoint_path: str | None = None,
    tensor_transforms: Callable | None = None,
) -> None:
    """Initializes and constructs the model.

    Args:
        path: The path to the callable object (class or function).
        arguments: The extra callable function / class arguments.
        checkpoint_path: The path to the checkpoint to load the model
            weights from. This is currently only supported for torch
            model checkpoints. For other formats, the checkpoint loading
            should be handled within the provided callable object in <path>.
        tensor_transforms: The transforms to apply to the output tensor
            produced by the model.
    """
    super().__init__(tensor_transforms=tensor_transforms)

    self._path = path
    self._arguments = arguments
    self._checkpoint_path = checkpoint_path

    self.load_model()

eva.models.wrappers.HuggingFaceModel

Bases: BaseModel

Wrapper class for loading HuggingFace transformers models.

Parameters:

Name Type Description Default
model_name_or_path str

The model name or path to load the model from. This can be a local path or a model name from the HuggingFace model hub.

required
tensor_transforms Callable | None

The transforms to apply to the output tensor produced by the model.

None
model_kwargs Dict[str, Any] | None

The arguments used for instantiating the model.

None
Source code in src/eva/core/models/wrappers/huggingface.py
def __init__(
    self,
    model_name_or_path: str,
    tensor_transforms: Callable | None = None,
    model_kwargs: Dict[str, Any] | None = None,
) -> None:
    """Initializes the model.

    Args:
        model_name_or_path: The model name or path to load the model from.
            This can be a local path or a model name from the `HuggingFace`
            model hub.
        tensor_transforms: The transforms to apply to the output tensor
            produced by the model.
        model_kwargs: The arguments used for instantiating the model.
    """
    super().__init__(tensor_transforms=tensor_transforms)

    self._model_name_or_path = model_name_or_path
    self._model_kwargs = model_kwargs or {}

    self.load_model()

eva.models.wrappers.ONNXModel

Bases: BaseModel

Wrapper class for loading ONNX models.

Parameters:

Name Type Description Default
path str

The path to the .onnx model file.

required
device Literal['cpu', 'cuda'] | None

The device to run the model on. This can be either "cpu" or "cuda".

'cpu'
tensor_transforms Callable | None

The transforms to apply to the output tensor produced by the model.

None
Source code in src/eva/core/models/wrappers/onnx.py
def __init__(
    self,
    path: str,
    device: Literal["cpu", "cuda"] | None = "cpu",
    tensor_transforms: Callable | None = None,
):
    """Initializes the model.

    Args:
        path: The path to the .onnx model file.
        device: The device to run the model on. This can be either "cpu" or "cuda".
        tensor_transforms: The transforms to apply to the output tensor produced by the model.
    """
    super().__init__(tensor_transforms=tensor_transforms)

    self._path = path
    self._device = device

    self.load_model()