Skip to content

simclr

SimCLR

Bases: SimCLR

A class representing a SimCLR model.

Attributes:

Name Type Description
backbone Module

The backbone model used in the SimCLR model.

num_ftrs int

The number of output features from the backbone model.

out_dim int

The dimension of the output representations.

projection_head SimCLRProjectionHead

The projection head used for projection head training.

Source code in fmcib/ssl/modules/simclr.py
class SimCLR(lightly_SimCLR):
    """
    A class representing a SimCLR model.

    Attributes:
        backbone (nn.Module): The backbone model used in the SimCLR model.
        num_ftrs (int): The number of output features from the backbone model.
        out_dim (int): The dimension of the output representations.
        projection_head (SimCLRProjectionHead): The projection head used for projection head training.
    """

    def __init__(self, backbone: nn.Module, num_ftrs: int = 32, out_dim: int = 128):
        """
        Initialize the object with a backbone network, number of features, and output dimension.

        Args:
            backbone (nn.Module): The backbone network.
            num_ftrs (int): The number of features. Default is 32.
            out_dim (int): The output dimension. Default is 128.

        Returns:
            None

        Raises:
            None
        """
        super().__init__(backbone, num_ftrs, out_dim)
        self.projection_head = SimCLRProjectionHead(num_ftrs, num_ftrs // 2, out_dim, batch_norm=False)

    def forward(self, x, return_features=False):
        """
        Perform a forward pass of the neural network.

        Args:
            x (tuple): A tuple of input data. Each element of the tuple represents a different input.
            return_features (bool, optional): Whether to return the intermediate features. Default is False.

        Returns:
            torch.Tensor or tuple: The output of the forward pass. If return_features is False, a single tensor is returned.
                If return_features is True, a tuple is returned consisting of the output tensor and the intermediate features.

        Raises:
            None.
        """
        return super().forward(*x, return_features)

__init__(backbone, num_ftrs=32, out_dim=128)

Initialize the object with a backbone network, number of features, and output dimension.

Parameters:

Name Type Description Default
backbone Module

The backbone network.

required
num_ftrs int

The number of features. Default is 32.

32
out_dim int

The output dimension. Default is 128.

128

Returns:

Type Description

None

Source code in fmcib/ssl/modules/simclr.py
def __init__(self, backbone: nn.Module, num_ftrs: int = 32, out_dim: int = 128):
    """
    Initialize the object with a backbone network, number of features, and output dimension.

    Args:
        backbone (nn.Module): The backbone network.
        num_ftrs (int): The number of features. Default is 32.
        out_dim (int): The output dimension. Default is 128.

    Returns:
        None

    Raises:
        None
    """
    super().__init__(backbone, num_ftrs, out_dim)
    self.projection_head = SimCLRProjectionHead(num_ftrs, num_ftrs // 2, out_dim, batch_norm=False)

forward(x, return_features=False)

Perform a forward pass of the neural network.

Parameters:

Name Type Description Default
x tuple

A tuple of input data. Each element of the tuple represents a different input.

required
return_features bool

Whether to return the intermediate features. Default is False.

False

Returns:

Type Description

torch.Tensor or tuple: The output of the forward pass. If return_features is False, a single tensor is returned. If return_features is True, a tuple is returned consisting of the output tensor and the intermediate features.

Source code in fmcib/ssl/modules/simclr.py
def forward(self, x, return_features=False):
    """
    Perform a forward pass of the neural network.

    Args:
        x (tuple): A tuple of input data. Each element of the tuple represents a different input.
        return_features (bool, optional): Whether to return the intermediate features. Default is False.

    Returns:
        torch.Tensor or tuple: The output of the forward pass. If return_features is False, a single tensor is returned.
            If return_features is True, a tuple is returned consisting of the output tensor and the intermediate features.

    Raises:
        None.
    """
    return super().forward(*x, return_features)