Skip to content

deeplabcut.pose_estimation_pytorch.models.backbones.resnet

Classes:

Name Description
ResNet

ResNet backbone.

ResNet

Bases: BaseBackbone

ResNet backbone.

This class represents a typical ResNet backbone for pose estimation.

Attributes:

Name Type Description
model

the ResNet model

Methods:

Name Description
__init__

Initialize the ResNet backbone.

forward

Forward pass through the ResNet backbone.

Source code in deeplabcut/pose_estimation_pytorch/models/backbones/resnet.py
@BACKBONES.register_module
class ResNet(BaseBackbone):
    """ResNet backbone.

    This class represents a typical ResNet backbone for pose estimation.

    Attributes:
        model: the ResNet model
    """

    def __init__(
        self,
        model_name: str = "resnet50",
        output_stride: int = 32,
        pretrained: bool = False,
        drop_path_rate: float = 0.0,
        drop_block_rate: float = 0.0,
        **kwargs,
    ) -> None:
        """Initialize the ResNet backbone.

        Args:
            model_name: Name of the ResNet model to use, e.g., 'resnet50', 'resnet101'
            output_stride: Output stride of the network, 32, 16, or 8.
            pretrained: If True, initializes with ImageNet pretrained weights.
            drop_path_rate: Stochastic depth drop-path rate
            drop_block_rate: Drop block rate
            kwargs: BaseBackbone kwargs
        """
        super().__init__(stride=output_stride, **kwargs)
        self.model = timm.create_model(
            model_name,
            output_stride=output_stride,
            pretrained=pretrained,
            drop_path_rate=drop_path_rate,
            drop_block_rate=drop_block_rate,
        )
        self.model.fc = nn.Identity()  # remove the FC layer

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """Forward pass through the ResNet backbone.

        Args:
            x: Input tensor.

        Returns:
            torch.Tensor: Output tensor.
        Example:
            >>> import torch
            >>> from deeplabcut.pose_estimation_pytorch.models.backbones import ResNet
            >>> backbone = ResNet(model_name='resnet50', pretrained=False)
            >>> x = torch.randn(1, 3, 256, 256)
            >>> y = backbone(x)

            Expected Output Shape:
                If input size is (batch_size, 3, shape_x, shape_y), the output shape
                will be (batch_size, 3, shape_x//16, shape_y//16)
        """
        return self.model.forward_features(x)

__init__

__init__(
    model_name: str = "resnet50",
    output_stride: int = 32,
    pretrained: bool = False,
    drop_path_rate: float = 0.0,
    drop_block_rate: float = 0.0,
    **kwargs
) -> None

Initialize the ResNet backbone.

Parameters:

Name Type Description Default

model_name

str

Name of the ResNet model to use, e.g., 'resnet50', 'resnet101'

'resnet50'

output_stride

int

Output stride of the network, 32, 16, or 8.

32

pretrained

bool

If True, initializes with ImageNet pretrained weights.

False

drop_path_rate

float

Stochastic depth drop-path rate

0.0

drop_block_rate

float

Drop block rate

0.0

kwargs

BaseBackbone kwargs

{}
Source code in deeplabcut/pose_estimation_pytorch/models/backbones/resnet.py
def __init__(
    self,
    model_name: str = "resnet50",
    output_stride: int = 32,
    pretrained: bool = False,
    drop_path_rate: float = 0.0,
    drop_block_rate: float = 0.0,
    **kwargs,
) -> None:
    """Initialize the ResNet backbone.

    Args:
        model_name: Name of the ResNet model to use, e.g., 'resnet50', 'resnet101'
        output_stride: Output stride of the network, 32, 16, or 8.
        pretrained: If True, initializes with ImageNet pretrained weights.
        drop_path_rate: Stochastic depth drop-path rate
        drop_block_rate: Drop block rate
        kwargs: BaseBackbone kwargs
    """
    super().__init__(stride=output_stride, **kwargs)
    self.model = timm.create_model(
        model_name,
        output_stride=output_stride,
        pretrained=pretrained,
        drop_path_rate=drop_path_rate,
        drop_block_rate=drop_block_rate,
    )
    self.model.fc = nn.Identity()  # remove the FC layer

forward

forward(x: Tensor) -> torch.Tensor

Forward pass through the ResNet backbone.

Parameters:

Name Type Description Default

x

Tensor

Input tensor.

required

Returns:

Type Description
Tensor

torch.Tensor: Output tensor.

Example: >>> import torch >>> from deeplabcut.pose_estimation_pytorch.models.backbones import ResNet >>> backbone = ResNet(model_name='resnet50', pretrained=False) >>> x = torch.randn(1, 3, 256, 256) >>> y = backbone(x)

Expected Output Shape:
    If input size is (batch_size, 3, shape_x, shape_y), the output shape
    will be (batch_size, 3, shape_x//16, shape_y//16)
Source code in deeplabcut/pose_estimation_pytorch/models/backbones/resnet.py
def forward(self, x: torch.Tensor) -> torch.Tensor:
    """Forward pass through the ResNet backbone.

    Args:
        x: Input tensor.

    Returns:
        torch.Tensor: Output tensor.
    Example:
        >>> import torch
        >>> from deeplabcut.pose_estimation_pytorch.models.backbones import ResNet
        >>> backbone = ResNet(model_name='resnet50', pretrained=False)
        >>> x = torch.randn(1, 3, 256, 256)
        >>> y = backbone(x)

        Expected Output Shape:
            If input size is (batch_size, 3, shape_x, shape_y), the output shape
            will be (batch_size, 3, shape_x//16, shape_y//16)
    """
    return self.model.forward_features(x)