Skip to content

exneg_simclr

ExNegSimCLR

Bases: SimCLR

Extended Negative Sampling SimCLR model.

Parameters:

Name Type Description Default
backbone Module

The backbone model.

required
num_ftrs int

Number of features in the bottleneck layer. Default is 32.

32
out_dim int

Dimension of the output feature embeddings. Default is 128.

128
Source code in fmcib/ssl/modules/exneg_simclr.py
class ExNegSimCLR(lightly_SimCLR):
    """
    Extended Negative Sampling SimCLR model.

    Args:
        backbone (nn.Module): The backbone model.
        num_ftrs (int): Number of features in the bottleneck layer. Default is 32.
        out_dim (int): Dimension of the output feature embeddings. Default is 128.
    """

    def __init__(self, backbone: nn.Module, num_ftrs: int = 32, out_dim: int = 128) -> None:
        """
        Initialize the object.

        Args:
            backbone (nn.Module): The backbone neural network.
            num_ftrs (int, optional): The number of input features for the projection head. Default is 32.
            out_dim (int, optional): The output dimension of the projection head. Default is 128.

        Returns:
            None

        Raises:
            None
        """
        super().__init__(backbone, num_ftrs, out_dim)
        # replace the projection head with a new one
        self.projection_head = SimCLRProjectionHead(num_ftrs, num_ftrs // 2, out_dim, batch_norm=False)

    def forward(self, x: Union[Dict, torch.Tensor], return_features: bool = False):
        """
        Forward pass of the ExNegSimCLR model.

        Args:
            x (Union[Dict, torch.Tensor]): Input data. If a dictionary, it should contain multiple views of the same image.
            return_features (bool): Whether to return the intermediate feature embeddings. Default is False.

        Returns:
            Dict: Output dictionary containing the forward pass results for each input view.
        """
        assert isinstance(x, dict), "Input to forward must be a `dict` for ExNegSimCLR"
        out = {}
        for key, value in x.items():
            if isinstance(value, list):
                out[key] = super().forward(*value, return_features)

        return out

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

Initialize the object.

Parameters:

Name Type Description Default
backbone Module

The backbone neural network.

required
num_ftrs int

The number of input features for the projection head. Default is 32.

32
out_dim int

The output dimension of the projection head. Default is 128.

128

Returns:

Type Description
None

None

Source code in fmcib/ssl/modules/exneg_simclr.py
def __init__(self, backbone: nn.Module, num_ftrs: int = 32, out_dim: int = 128) -> None:
    """
    Initialize the object.

    Args:
        backbone (nn.Module): The backbone neural network.
        num_ftrs (int, optional): The number of input features for the projection head. Default is 32.
        out_dim (int, optional): The output dimension of the projection head. Default is 128.

    Returns:
        None

    Raises:
        None
    """
    super().__init__(backbone, num_ftrs, out_dim)
    # replace the projection head with a new one
    self.projection_head = SimCLRProjectionHead(num_ftrs, num_ftrs // 2, out_dim, batch_norm=False)

forward(x, return_features=False)

Forward pass of the ExNegSimCLR model.

Parameters:

Name Type Description Default
x Union[Dict, Tensor]

Input data. If a dictionary, it should contain multiple views of the same image.

required
return_features bool

Whether to return the intermediate feature embeddings. Default is False.

False

Returns:

Name Type Description
Dict

Output dictionary containing the forward pass results for each input view.

Source code in fmcib/ssl/modules/exneg_simclr.py
def forward(self, x: Union[Dict, torch.Tensor], return_features: bool = False):
    """
    Forward pass of the ExNegSimCLR model.

    Args:
        x (Union[Dict, torch.Tensor]): Input data. If a dictionary, it should contain multiple views of the same image.
        return_features (bool): Whether to return the intermediate feature embeddings. Default is False.

    Returns:
        Dict: Output dictionary containing the forward pass results for each input view.
    """
    assert isinstance(x, dict), "Input to forward must be a `dict` for ExNegSimCLR"
    out = {}
    for key, value in x.items():
        if isinstance(value, list):
            out[key] = super().forward(*value, return_features)

    return out