Skip to content

model

Module diagram

classDiagram
  class model {
  }
  class backbone {
  }
  class builder {
  }
  class norm {
  }
  class torchvision {
  }
  class regnet {
  }
  class resnet {
  }
  class squeezenet {
  }
  class vgg {
  }
  class van {
  }
  class benchmark {
  }
  class feature_extractor {
  }
  class neck {
  }
  class builder {
  }
  class fpem {
  }
  class fpn {
  }
  class util {
  }
  backbone --> builder
  builder --> regnet
  builder --> resnet
  builder --> squeezenet
  builder --> vgg
  builder --> van
  benchmark --> backbone
  feature_extractor --> backbone
  neck --> builder
  builder --> fpem
  builder --> fpn

model

backbone

builder

logger module-attribute
logger = getLogger('SaigeResearch')
native_models module-attribute
native_models = {'van_b0': van_b0, 'van_b1': van_b1, 'van_b2': van_b2, 'van_b3': van_b3, 'regnet_y_400mf_torchvision': regnet_y_400mf, 'regnet_y_800mf_torchvision': regnet_y_800mf, 'regnet_y_1_6gf_torchvision': regnet_y_1_6gf, 'regnet_y_3_2gf_torchvision': regnet_y_3_2gf, 'regnet_y_8gf_torchvision': regnet_y_8gf, 'regnet_y_16gf_torchvision': regnet_y_16gf, 'regnet_y_32gf_torchvision': regnet_y_32gf, 'regnet_y_128gf_torchvision': regnet_y_128gf, 'regnet_x_400mf_torchvision': regnet_x_400mf, 'regnet_x_800mf_torchvision': regnet_x_800mf, 'regnet_x_1_6gf_torchvision': regnet_x_1_6gf, 'regnet_x_3_2gf_torchvision': regnet_x_3_2gf, 'regnet_x_8gf_torchvision': regnet_x_8gf, 'regnet_x_16gf_torchvision': regnet_x_16gf, 'regnet_x_32gf_torchvision': regnet_x_32gf, 'resnet18_torchvision': resnet18, 'resnet34_torchvision': resnet34, 'resnet50_torchvision': resnet50, 'resnet101_torchvision': resnet101, 'resnet152_torchvision': resnet152, 'resnext50_32x4d_torchvision': resnext50_32x4d, 'resnext101_32x8d_torchvision': resnext101_32x8d, 'resnext101_64x4d_torchvision': resnext101_64x4d, 'wide_resnet50_2_torchvision': wide_resnet50_2, 'wide_resnet101_2_torchvision': wide_resnet101_2, 'squeezenet1_0_torchvision': squeezenet1_0, 'squeezenet1_1_torchvision': squeezenet1_1, 'vgg11_torchvision': vgg11, 'vgg11_bn_torchvision': vgg11_bn, 'vgg13_torchvision': vgg13, 'vgg13_bn_torchvision': vgg13_bn, 'vgg16_torchvision': vgg16, 'vgg16_bn_torchvision': vgg16_bn, 'vgg19_torchvision': vgg19, 'vgg19_bn_torchvision': vgg19_bn}
register_backbone
register_backbone(backbone, name: Optional[str] = None)

native_models에 백본을 등록해 build_backbone()에서 사용할 수 있도록 합니다

Source code in SaigeToolkit/model/backbone/builder.py
def register_backbone(backbone, name: Optional[str] = None):
    """`native_models`에 백본을 등록해 `build_backbone()`에서 사용할 수 있도록 합니다"""
    name = name or backbone.__name__
    if name in native_models and backbone != native_models[name]:
        raise ValueError(f"backbone name {name} already exists.")
    native_models[name] = backbone
build_backbone
build_backbone(_target_: str, **params) -> Module
Source code in SaigeToolkit/model/backbone/builder.py
def build_backbone(_target_: str, **params) -> nn.Module:
    if _target_ in native_models:
        builder = native_models[_target_]
    elif _target_.startswith("torchvision.models."):
        builder = getattr(torchvision.models, _target_.split(".")[-1])
    elif _target_ == "timm.create_model":
        import timm

        builder = timm.create_model
    elif _target_ == "torch.hub.load":
        builder = torch.hub.load
    else:
        raise ValueError(f"Unknown backbone target: {_target_}")

    return builder(**params)

norm

FrozenBatchNorm2d
FrozenBatchNorm2d(n: int)

Bases: Module

BatchNorm2d where the batch statistics and the affine parameters are fixed

Attributes:

  • weight (Tensor) –

    batch_norm weight. not nn.Parameter for freezing values.

  • bias (Tensor) –

    batch_norm bias. not nn.Parameter for freezing values.

  • running_mean (Tensor) –

    batch_norm running_mean. not nn.Parameter for freezing values.

  • running_var (Tensor) –

    batch_norm running_var. not nn.Parameter for freezing values.

Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.

Initializing FrozenBatchNorm2d

Parameters:

  • n (int) –

    number of channels for torch.Tensor

Source code in SaigeToolkit/model/backbone/norm.py
def __init__(self, n: int) -> None:
    """Initializing FrozenBatchNorm2d

    Args:
        n (int): number of channels for torch.Tensor
    """
    super(FrozenBatchNorm2d, self).__init__()
    self.register_buffer("weight", torch.ones(n))
    self.register_buffer("bias", torch.zeros(n))
    self.register_buffer("running_mean", torch.zeros(n))
    self.register_buffer("running_var", torch.ones(n))
forward
forward(x: Tensor) -> Tensor

forward function for FrozenBatchNorm2d

Parameters:

  • x (Tensor) –

    input tensor

Returns:

  • Tensor

    torch.Tensor: batch_normalized output tensor

Source code in SaigeToolkit/model/backbone/norm.py
def forward(self, x: torch.Tensor) -> torch.Tensor:
    """forward function for FrozenBatchNorm2d

    Args:
        x (torch.Tensor): input tensor

    Returns:
        torch.Tensor: batch_normalized output tensor
    """
    # Cast all fixed parameters to half() if necessary
    if x.dtype == torch.float16:
        self.weight = self.weight.half()
        self.bias = self.bias.half()
        self.running_mean = self.running_mean.half()
        self.running_var = self.running_var.half()
    scale = self.weight * self.running_var.rsqrt()
    bias = self.bias - self.running_mean * scale
    scale = scale.reshape(1, -1, 1, 1)
    bias = bias.reshape(1, -1, 1, 1)
    return x * scale + bias
get_norm
get_norm(name: str) -> Type[Module]

getting customized batch norm class

Parameters:

  • name (str) –

    batch norm class name

Returns:

  • Type[Module]

    Type[nn.Module]: custom batch norm class

Source code in SaigeToolkit/model/backbone/norm.py
def get_norm(name: str) -> Type[nn.Module]:
    """getting customized batch norm class

    Args:
        name (str): batch norm class name

    Returns:
        Type[nn.Module]: custom batch norm class
    """
    return {"fixed_batch_norm": FrozenBatchNorm2d}[name]

torchvision

regnet
__all__ module-attribute
__all__ = ['RegNet', 'regnet_y_400mf', 'regnet_y_800mf', 'regnet_y_1_6gf', 'regnet_y_3_2gf', 'regnet_y_8gf', 'regnet_y_16gf', 'regnet_y_32gf', 'regnet_y_128gf', 'regnet_x_400mf', 'regnet_x_800mf', 'regnet_x_1_6gf', 'regnet_x_3_2gf', 'regnet_x_8gf', 'regnet_x_16gf', 'regnet_x_32gf']
SimpleStemIN
SimpleStemIN(width_in: int, width_out: int, norm_layer: Callable[..., Module], activation_layer: Callable[..., Module])

Bases: Conv2dNormActivation

Simple stem for ImageNet: 3x3, BN, ReLU.

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
def __init__(
    self,
    width_in: int,
    width_out: int,
    norm_layer: Callable[..., nn.Module],
    activation_layer: Callable[..., nn.Module],
) -> None:
    super().__init__(
        width_in,
        width_out,
        kernel_size=3,
        stride=2,
        norm_layer=norm_layer,
        activation_layer=activation_layer,
    )
BottleneckTransform
BottleneckTransform(width_in: int, width_out: int, stride: int, norm_layer: Callable[..., Module], activation_layer: Callable[..., Module], group_width: int, bottleneck_multiplier: float, se_ratio: Optional[float])

Bases: Sequential

Bottleneck transformation: 1x1, 3x3 [+SE], 1x1.

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
def __init__(
    self,
    width_in: int,
    width_out: int,
    stride: int,
    norm_layer: Callable[..., nn.Module],
    activation_layer: Callable[..., nn.Module],
    group_width: int,
    bottleneck_multiplier: float,
    se_ratio: Optional[float],
) -> None:
    layers: OrderedDict[str, nn.Module] = OrderedDict()
    w_b = int(round(width_out * bottleneck_multiplier))
    g = w_b // group_width

    layers["a"] = Conv2dNormActivation(
        width_in,
        w_b,
        kernel_size=1,
        stride=1,
        norm_layer=norm_layer,
        activation_layer=activation_layer,
    )
    layers["b"] = Conv2dNormActivation(
        w_b,
        w_b,
        kernel_size=3,
        stride=stride,
        groups=g,
        norm_layer=norm_layer,
        activation_layer=activation_layer,
    )

    if se_ratio:
        # The SE reduction ratio is defined with respect to the
        # beginning of the block
        width_se_out = int(round(se_ratio * width_in))
        layers["se"] = SqueezeExcitation(
            input_channels=w_b,
            squeeze_channels=width_se_out,
            activation=activation_layer,
        )

    layers["c"] = Conv2dNormActivation(
        w_b, width_out, kernel_size=1, stride=1, norm_layer=norm_layer, activation_layer=None
    )
    super().__init__(layers)
ResBottleneckBlock
ResBottleneckBlock(width_in: int, width_out: int, stride: int, norm_layer: Callable[..., Module], activation_layer: Callable[..., Module], group_width: int = 1, bottleneck_multiplier: float = 1.0, se_ratio: Optional[float] = None)

Bases: Module

Residual bottleneck block: x + F(x), F = bottleneck transform.

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
def __init__(
    self,
    width_in: int,
    width_out: int,
    stride: int,
    norm_layer: Callable[..., nn.Module],
    activation_layer: Callable[..., nn.Module],
    group_width: int = 1,
    bottleneck_multiplier: float = 1.0,
    se_ratio: Optional[float] = None,
) -> None:
    super().__init__()

    # Use skip connection with projection if shape changes
    self.proj = None
    should_proj = (width_in != width_out) or (stride != 1)
    if should_proj:
        self.proj = Conv2dNormActivation(
            width_in,
            width_out,
            kernel_size=1,
            stride=stride,
            norm_layer=norm_layer,
            activation_layer=None,
        )
    self.f = BottleneckTransform(
        width_in,
        width_out,
        stride,
        norm_layer,
        activation_layer,
        group_width,
        bottleneck_multiplier,
        se_ratio,
    )
    self.activation = activation_layer(inplace=True)
proj instance-attribute
proj = None
f instance-attribute
f = BottleneckTransform(width_in, width_out, stride, norm_layer, activation_layer, group_width, bottleneck_multiplier, se_ratio)
activation instance-attribute
activation = activation_layer(inplace=True)
forward
forward(x: Tensor) -> Tensor
Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
def forward(self, x: Tensor) -> Tensor:
    if self.proj is not None:
        x = self.proj(x) + self.f(x)
    else:
        x = x + self.f(x)
    return self.activation(x)
AnyStage
AnyStage(width_in: int, width_out: int, stride: int, depth: int, block_constructor: Callable[..., Module], norm_layer: Callable[..., Module], activation_layer: Callable[..., Module], group_width: int, bottleneck_multiplier: float, se_ratio: Optional[float] = None, stage_index: int = 0)

Bases: Sequential

AnyNet stage (sequence of blocks w/ the same output shape).

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
def __init__(
    self,
    width_in: int,
    width_out: int,
    stride: int,
    depth: int,
    block_constructor: Callable[..., nn.Module],
    norm_layer: Callable[..., nn.Module],
    activation_layer: Callable[..., nn.Module],
    group_width: int,
    bottleneck_multiplier: float,
    se_ratio: Optional[float] = None,
    stage_index: int = 0,
) -> None:
    super().__init__()

    for i in range(depth):
        block = block_constructor(
            width_in if i == 0 else width_out,
            width_out,
            stride if i == 0 else 1,
            norm_layer,
            activation_layer,
            group_width,
            bottleneck_multiplier,
            se_ratio,
        )

        self.add_module(f"block{stage_index}-{i}", block)
BlockParams
BlockParams(depths: List[int], widths: List[int], group_widths: List[int], bottleneck_multipliers: List[float], strides: List[int], se_ratio: Optional[float] = None)
Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
def __init__(
    self,
    depths: List[int],
    widths: List[int],
    group_widths: List[int],
    bottleneck_multipliers: List[float],
    strides: List[int],
    se_ratio: Optional[float] = None,
) -> None:
    self.depths = depths
    self.widths = widths
    self.group_widths = group_widths
    self.bottleneck_multipliers = bottleneck_multipliers
    self.strides = strides
    self.se_ratio = se_ratio
depths instance-attribute
depths = depths
widths instance-attribute
widths = widths
group_widths instance-attribute
group_widths = group_widths
bottleneck_multipliers instance-attribute
bottleneck_multipliers = bottleneck_multipliers
strides instance-attribute
strides = strides
se_ratio instance-attribute
se_ratio = se_ratio
from_init_params classmethod
from_init_params(depth: int, w_0: int, w_a: float, w_m: float, group_width: int, bottleneck_multiplier: float = 1.0, se_ratio: Optional[float] = None, **kwargs: Any) -> BlockParams

Programmatically compute all the per-block settings, given the RegNet parameters. The first step is to compute the quantized linear block parameters, in log space. Key parameters are: - w_a is the width progression slope - w_0 is the initial width - w_m is the width stepping in the log space In other terms log(block_width) = log(w_0) + w_m * block_capacity, with bock_capacity ramping up following the w_0 and w_a params. This block width is finally quantized to multiples of 8. The second step is to compute the parameters per stage, taking into account the skip connection and the final 1x1 convolutions. We use the fact that the output width is constant within a stage.

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@classmethod
def from_init_params(
    cls,
    depth: int,
    w_0: int,
    w_a: float,
    w_m: float,
    group_width: int,
    bottleneck_multiplier: float = 1.0,
    se_ratio: Optional[float] = None,
    **kwargs: Any,
) -> "BlockParams":
    """
    Programmatically compute all the per-block settings,
    given the RegNet parameters.
    The first step is to compute the quantized linear block parameters,
    in log space. Key parameters are:
    - `w_a` is the width progression slope
    - `w_0` is the initial width
    - `w_m` is the width stepping in the log space
    In other terms
    `log(block_width) = log(w_0) + w_m * block_capacity`,
    with `bock_capacity` ramping up following the w_0 and w_a params.
    This block width is finally quantized to multiples of 8.
    The second step is to compute the parameters per stage,
    taking into account the skip connection and the final 1x1 convolutions.
    We use the fact that the output width is constant within a stage.
    """

    QUANT = 8
    STRIDE = 2

    if w_a < 0 or w_0 <= 0 or w_m <= 1 or w_0 % 8 != 0:
        raise ValueError("Invalid RegNet settings")
    # Compute the block widths. Each stage has one unique block width
    widths_cont = torch.arange(depth) * w_a + w_0
    block_capacity = torch.round(torch.log(widths_cont / w_0) / math.log(w_m))
    block_widths = (
        (torch.round(torch.divide(w_0 * torch.pow(w_m, block_capacity), QUANT)) * QUANT)
        .int()
        .tolist()
    )
    num_stages = len(set(block_widths))

    # Convert to per stage parameters
    split_helper = zip(
        block_widths + [0],
        [0] + block_widths,
        block_widths + [0],
        [0] + block_widths,
    )
    splits = [w != wp or r != rp for w, wp, r, rp in split_helper]

    stage_widths = [w for w, t in zip(block_widths, splits[:-1]) if t]
    stage_depths = torch.diff(torch.tensor([d for d, t in enumerate(splits) if t])).int().tolist()

    strides = [STRIDE] * num_stages
    bottleneck_multipliers = [bottleneck_multiplier] * num_stages
    group_widths = [group_width] * num_stages

    # Adjust the compatibility of stage widths and group widths
    stage_widths, group_widths = cls._adjust_widths_groups_compatibilty(
        stage_widths, bottleneck_multipliers, group_widths
    )

    return cls(
        depths=stage_depths,
        widths=stage_widths,
        group_widths=group_widths,
        bottleneck_multipliers=bottleneck_multipliers,
        strides=strides,
        se_ratio=se_ratio,
    )
_get_expanded_params
_get_expanded_params()
Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
def _get_expanded_params(self):
    return zip(
        self.widths, self.strides, self.depths, self.group_widths, self.bottleneck_multipliers
    )
_adjust_widths_groups_compatibilty staticmethod
_adjust_widths_groups_compatibilty(stage_widths: List[int], bottleneck_ratios: List[float], group_widths: List[int]) -> Tuple[List[int], List[int]]

Adjusts the compatibility of widths and groups, depending on the bottleneck ratio.

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@staticmethod
def _adjust_widths_groups_compatibilty(
    stage_widths: List[int], bottleneck_ratios: List[float], group_widths: List[int]
) -> Tuple[List[int], List[int]]:
    """
    Adjusts the compatibility of widths and groups,
    depending on the bottleneck ratio.
    """
    # Compute all widths for the current settings
    widths = [int(w * b) for w, b in zip(stage_widths, bottleneck_ratios)]
    group_widths_min = [min(g, w_bot) for g, w_bot in zip(group_widths, widths)]

    # Compute the adjusted widths so that stage and group widths fit
    ws_bot = [_make_divisible(w_bot, g) for w_bot, g in zip(widths, group_widths_min)]
    stage_widths = [int(w_bot / b) for w_bot, b in zip(ws_bot, bottleneck_ratios)]
    return stage_widths, group_widths_min
RegNet
RegNet(block_params: BlockParams, num_classes: int = 1000, stem_width: int = 32, stem_type: Optional[Callable[..., Module]] = None, block_type: Optional[Callable[..., Module]] = None, norm_layer: Optional[Callable[..., Module]] = None, activation: Optional[Callable[..., Module]] = None, in_channels: int = 3)

Bases: Module

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
def __init__(
    self,
    block_params: BlockParams,
    num_classes: int = 1000,
    stem_width: int = 32,
    stem_type: Optional[Callable[..., nn.Module]] = None,
    block_type: Optional[Callable[..., nn.Module]] = None,
    norm_layer: Optional[Callable[..., nn.Module]] = None,
    activation: Optional[Callable[..., nn.Module]] = None,
    in_channels: int = 3,
) -> None:
    super().__init__()
    _log_api_usage_once(self)

    if stem_type is None:
        stem_type = SimpleStemIN
    if norm_layer is None:
        norm_layer = nn.BatchNorm2d
    if block_type is None:
        block_type = ResBottleneckBlock
    if activation is None:
        activation = nn.ReLU

    self.in_channels = in_channels

    # Ad hoc stem
    self.stem = stem_type(
        self.in_channels,  # width_in
        stem_width,
        norm_layer,
        activation,
    )

    current_width = stem_width

    blocks = []
    for i, (
        width_out,
        stride,
        depth,
        group_width,
        bottleneck_multiplier,
    ) in enumerate(block_params._get_expanded_params()):
        blocks.append(
            (
                f"block{i+1}",
                AnyStage(
                    current_width,
                    width_out,
                    stride,
                    depth,
                    block_type,
                    norm_layer,
                    activation,
                    group_width,
                    bottleneck_multiplier,
                    block_params.se_ratio,
                    stage_index=i + 1,
                ),
            )
        )

        current_width = width_out

    self.trunk_output = nn.Sequential(OrderedDict(blocks))

    # Performs ResNet-style weight initialization
    for m in self.modules():
        if isinstance(m, nn.Conv2d):
            # Note that there is no bias due to BN
            fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
            nn.init.normal_(m.weight, mean=0.0, std=math.sqrt(2.0 / fan_out))
        elif isinstance(m, nn.BatchNorm2d):
            nn.init.ones_(m.weight)
            nn.init.zeros_(m.bias)
        elif isinstance(m, nn.Linear):
            nn.init.normal_(m.weight, mean=0.0, std=0.01)
            nn.init.zeros_(m.bias)
in_channels instance-attribute
in_channels = in_channels
stem instance-attribute
stem = stem_type(in_channels, stem_width, norm_layer, activation)
trunk_output instance-attribute
trunk_output = Sequential(OrderedDict(blocks))
forward
forward(x: Tensor) -> Tensor
Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
def forward(self, x: Tensor) -> Tensor:
    x = self.stem(x)
    x = self.trunk_output(x)
    return x
load_state_dict
load_state_dict(state_dict, strict: bool = True)
Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
def load_state_dict(self, state_dict, strict: bool = True):
    extend_state_dict_input_channel(state_dict, "stem.0.weight", self.stem[0])
    for key in list(state_dict):
        if key.startswith("fc."):
            state_dict.pop(key)
    return super().load_state_dict(state_dict, strict)
_regnet
_regnet(block_params: BlockParams, weights: Optional[WeightsEnum], progress: bool, **kwargs: Any) -> RegNet
Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
def _regnet(
    block_params: BlockParams,
    weights: Optional[WeightsEnum],
    progress: bool,
    **kwargs: Any,
) -> RegNet:
    if weights is not None:
        _ovewrite_named_param(kwargs, "num_classes", len(weights.meta["categories"]))

    norm_layer = kwargs.pop("norm_layer", partial(nn.BatchNorm2d, eps=1e-05, momentum=0.1))
    model = RegNet(block_params, norm_layer=norm_layer, **kwargs)

    if weights is not None:
        model.load_state_dict(weights.get_state_dict(progress=progress))

    return model
regnet_y_400mf
regnet_y_400mf(*, weights: Optional[RegNet_Y_400MF_Weights] = None, progress: bool = True, **kwargs: Any) -> RegNet

Constructs a RegNetY_400MF architecture from Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>. Args: weights (:class:~torchvision.models.RegNet_Y_400MF_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.RegNet_Y_400MF_Weights below for more details and possible values. By default, no pretrained weights are used. progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True. **kwargs: parameters passed to either torchvision.models.regnet.RegNet or torchvision.models.regnet.BlockParams class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py> for more detail about the classes. .. autoclass:: torchvision.models.RegNet_Y_400MF_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@handle_legacy_interface(weights=("pretrained", RegNet_Y_400MF_Weights.IMAGENET1K_V1))
def regnet_y_400mf(
    *, weights: Optional[RegNet_Y_400MF_Weights] = None, progress: bool = True, **kwargs: Any
) -> RegNet:
    """
    Constructs a RegNetY_400MF architecture from
    `Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>`_.
    Args:
        weights (:class:`~torchvision.models.RegNet_Y_400MF_Weights`, optional): The pretrained weights to use.
            See :class:`~torchvision.models.RegNet_Y_400MF_Weights` below for more details and possible values.
            By default, no pretrained weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to either ``torchvision.models.regnet.RegNet`` or
            ``torchvision.models.regnet.BlockParams`` class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py>`_
            for more detail about the classes.
    .. autoclass:: torchvision.models.RegNet_Y_400MF_Weights
        :members:
    """
    weights = RegNet_Y_400MF_Weights.verify(weights)

    params = BlockParams.from_init_params(
        depth=16, w_0=48, w_a=27.89, w_m=2.09, group_width=8, se_ratio=0.25, **kwargs
    )
    return _regnet(params, weights, progress, **kwargs)
regnet_y_800mf
regnet_y_800mf(*, weights: Optional[RegNet_Y_800MF_Weights] = None, progress: bool = True, **kwargs: Any) -> RegNet

Constructs a RegNetY_800MF architecture from Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>. Args: weights (:class:~torchvision.models.RegNet_Y_800MF_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.RegNet_Y_800MF_Weights below for more details and possible values. By default, no pretrained weights are used. progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True. **kwargs: parameters passed to either torchvision.models.regnet.RegNet or torchvision.models.regnet.BlockParams class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py> for more detail about the classes. .. autoclass:: torchvision.models.RegNet_Y_800MF_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@handle_legacy_interface(weights=("pretrained", RegNet_Y_800MF_Weights.IMAGENET1K_V1))
def regnet_y_800mf(
    *, weights: Optional[RegNet_Y_800MF_Weights] = None, progress: bool = True, **kwargs: Any
) -> RegNet:
    """
    Constructs a RegNetY_800MF architecture from
    `Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>`_.
    Args:
        weights (:class:`~torchvision.models.RegNet_Y_800MF_Weights`, optional): The pretrained weights to use.
            See :class:`~torchvision.models.RegNet_Y_800MF_Weights` below for more details and possible values.
            By default, no pretrained weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to either ``torchvision.models.regnet.RegNet`` or
            ``torchvision.models.regnet.BlockParams`` class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py>`_
            for more detail about the classes.
    .. autoclass:: torchvision.models.RegNet_Y_800MF_Weights
        :members:
    """
    weights = RegNet_Y_800MF_Weights.verify(weights)

    params = BlockParams.from_init_params(
        depth=14, w_0=56, w_a=38.84, w_m=2.4, group_width=16, se_ratio=0.25, **kwargs
    )
    return _regnet(params, weights, progress, **kwargs)
regnet_y_1_6gf
regnet_y_1_6gf(*, weights: Optional[RegNet_Y_1_6GF_Weights] = None, progress: bool = True, **kwargs: Any) -> RegNet

Constructs a RegNetY_1.6GF architecture from Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>. Args: weights (:class:~torchvision.models.RegNet_Y_1_6GF_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.RegNet_Y_1_6GF_Weights below for more details and possible values. By default, no pretrained weights are used. progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True. **kwargs: parameters passed to either torchvision.models.regnet.RegNet or torchvision.models.regnet.BlockParams class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py> for more detail about the classes. .. autoclass:: torchvision.models.RegNet_Y_1_6GF_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@handle_legacy_interface(weights=("pretrained", RegNet_Y_1_6GF_Weights.IMAGENET1K_V1))
def regnet_y_1_6gf(
    *, weights: Optional[RegNet_Y_1_6GF_Weights] = None, progress: bool = True, **kwargs: Any
) -> RegNet:
    """
    Constructs a RegNetY_1.6GF architecture from
    `Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>`_.
    Args:
        weights (:class:`~torchvision.models.RegNet_Y_1_6GF_Weights`, optional): The pretrained weights to use.
            See :class:`~torchvision.models.RegNet_Y_1_6GF_Weights` below for more details and possible values.
            By default, no pretrained weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to either ``torchvision.models.regnet.RegNet`` or
            ``torchvision.models.regnet.BlockParams`` class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py>`_
            for more detail about the classes.
    .. autoclass:: torchvision.models.RegNet_Y_1_6GF_Weights
        :members:
    """
    weights = RegNet_Y_1_6GF_Weights.verify(weights)

    params = BlockParams.from_init_params(
        depth=27, w_0=48, w_a=20.71, w_m=2.65, group_width=24, se_ratio=0.25, **kwargs
    )
    return _regnet(params, weights, progress, **kwargs)
regnet_y_3_2gf
regnet_y_3_2gf(*, weights: Optional[RegNet_Y_3_2GF_Weights] = None, progress: bool = True, **kwargs: Any) -> RegNet

Constructs a RegNetY_3.2GF architecture from Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>. Args: weights (:class:~torchvision.models.RegNet_Y_3_2GF_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.RegNet_Y_3_2GF_Weights below for more details and possible values. By default, no pretrained weights are used. progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True. **kwargs: parameters passed to either torchvision.models.regnet.RegNet or torchvision.models.regnet.BlockParams class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py> for more detail about the classes. .. autoclass:: torchvision.models.RegNet_Y_3_2GF_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@handle_legacy_interface(weights=("pretrained", RegNet_Y_3_2GF_Weights.IMAGENET1K_V1))
def regnet_y_3_2gf(
    *, weights: Optional[RegNet_Y_3_2GF_Weights] = None, progress: bool = True, **kwargs: Any
) -> RegNet:
    """
    Constructs a RegNetY_3.2GF architecture from
    `Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>`_.
    Args:
        weights (:class:`~torchvision.models.RegNet_Y_3_2GF_Weights`, optional): The pretrained weights to use.
            See :class:`~torchvision.models.RegNet_Y_3_2GF_Weights` below for more details and possible values.
            By default, no pretrained weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to either ``torchvision.models.regnet.RegNet`` or
            ``torchvision.models.regnet.BlockParams`` class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py>`_
            for more detail about the classes.
    .. autoclass:: torchvision.models.RegNet_Y_3_2GF_Weights
        :members:
    """
    weights = RegNet_Y_3_2GF_Weights.verify(weights)

    params = BlockParams.from_init_params(
        depth=21, w_0=80, w_a=42.63, w_m=2.66, group_width=24, se_ratio=0.25, **kwargs
    )
    return _regnet(params, weights, progress, **kwargs)
regnet_y_8gf
regnet_y_8gf(*, weights: Optional[RegNet_Y_8GF_Weights] = None, progress: bool = True, **kwargs: Any) -> RegNet

Constructs a RegNetY_8GF architecture from Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>. Args: weights (:class:~torchvision.models.RegNet_Y_8GF_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.RegNet_Y_8GF_Weights below for more details and possible values. By default, no pretrained weights are used. progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True. **kwargs: parameters passed to either torchvision.models.regnet.RegNet or torchvision.models.regnet.BlockParams class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py> for more detail about the classes. .. autoclass:: torchvision.models.RegNet_Y_8GF_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@handle_legacy_interface(weights=("pretrained", RegNet_Y_8GF_Weights.IMAGENET1K_V1))
def regnet_y_8gf(
    *, weights: Optional[RegNet_Y_8GF_Weights] = None, progress: bool = True, **kwargs: Any
) -> RegNet:
    """
    Constructs a RegNetY_8GF architecture from
    `Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>`_.
    Args:
        weights (:class:`~torchvision.models.RegNet_Y_8GF_Weights`, optional): The pretrained weights to use.
            See :class:`~torchvision.models.RegNet_Y_8GF_Weights` below for more details and possible values.
            By default, no pretrained weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to either ``torchvision.models.regnet.RegNet`` or
            ``torchvision.models.regnet.BlockParams`` class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py>`_
            for more detail about the classes.
    .. autoclass:: torchvision.models.RegNet_Y_8GF_Weights
        :members:
    """
    weights = RegNet_Y_8GF_Weights.verify(weights)

    params = BlockParams.from_init_params(
        depth=17, w_0=192, w_a=76.82, w_m=2.19, group_width=56, se_ratio=0.25, **kwargs
    )
    return _regnet(params, weights, progress, **kwargs)
regnet_y_16gf
regnet_y_16gf(*, weights: Optional[RegNet_Y_16GF_Weights] = None, progress: bool = True, **kwargs: Any) -> RegNet

Constructs a RegNetY_16GF architecture from Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>. Args: weights (:class:~torchvision.models.RegNet_Y_16GF_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.RegNet_Y_16GF_Weights below for more details and possible values. By default, no pretrained weights are used. progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True. **kwargs: parameters passed to either torchvision.models.regnet.RegNet or torchvision.models.regnet.BlockParams class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py> for more detail about the classes. .. autoclass:: torchvision.models.RegNet_Y_16GF_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@handle_legacy_interface(weights=("pretrained", RegNet_Y_16GF_Weights.IMAGENET1K_V1))
def regnet_y_16gf(
    *, weights: Optional[RegNet_Y_16GF_Weights] = None, progress: bool = True, **kwargs: Any
) -> RegNet:
    """
    Constructs a RegNetY_16GF architecture from
    `Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>`_.
    Args:
        weights (:class:`~torchvision.models.RegNet_Y_16GF_Weights`, optional): The pretrained weights to use.
            See :class:`~torchvision.models.RegNet_Y_16GF_Weights` below for more details and possible values.
            By default, no pretrained weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to either ``torchvision.models.regnet.RegNet`` or
            ``torchvision.models.regnet.BlockParams`` class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py>`_
            for more detail about the classes.
    .. autoclass:: torchvision.models.RegNet_Y_16GF_Weights
        :members:
    """
    weights = RegNet_Y_16GF_Weights.verify(weights)

    params = BlockParams.from_init_params(
        depth=18, w_0=200, w_a=106.23, w_m=2.48, group_width=112, se_ratio=0.25, **kwargs
    )
    return _regnet(params, weights, progress, **kwargs)
regnet_y_32gf
regnet_y_32gf(*, weights: Optional[RegNet_Y_32GF_Weights] = None, progress: bool = True, **kwargs: Any) -> RegNet

Constructs a RegNetY_32GF architecture from Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>. Args: weights (:class:~torchvision.models.RegNet_Y_32GF_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.RegNet_Y_32GF_Weights below for more details and possible values. By default, no pretrained weights are used. progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True. **kwargs: parameters passed to either torchvision.models.regnet.RegNet or torchvision.models.regnet.BlockParams class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py> for more detail about the classes. .. autoclass:: torchvision.models.RegNet_Y_32GF_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@handle_legacy_interface(weights=("pretrained", RegNet_Y_32GF_Weights.IMAGENET1K_V1))
def regnet_y_32gf(
    *, weights: Optional[RegNet_Y_32GF_Weights] = None, progress: bool = True, **kwargs: Any
) -> RegNet:
    """
    Constructs a RegNetY_32GF architecture from
    `Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>`_.
    Args:
        weights (:class:`~torchvision.models.RegNet_Y_32GF_Weights`, optional): The pretrained weights to use.
            See :class:`~torchvision.models.RegNet_Y_32GF_Weights` below for more details and possible values.
            By default, no pretrained weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to either ``torchvision.models.regnet.RegNet`` or
            ``torchvision.models.regnet.BlockParams`` class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py>`_
            for more detail about the classes.
    .. autoclass:: torchvision.models.RegNet_Y_32GF_Weights
        :members:
    """
    weights = RegNet_Y_32GF_Weights.verify(weights)

    params = BlockParams.from_init_params(
        depth=20, w_0=232, w_a=115.89, w_m=2.53, group_width=232, se_ratio=0.25, **kwargs
    )
    return _regnet(params, weights, progress, **kwargs)
regnet_y_128gf
regnet_y_128gf(*, weights: Optional[RegNet_Y_128GF_Weights] = None, progress: bool = True, **kwargs: Any) -> RegNet

Constructs a RegNetY_128GF architecture from Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>. Args: weights (:class:~torchvision.models.RegNet_Y_128GF_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.RegNet_Y_128GF_Weights below for more details and possible values. By default, no pretrained weights are used. progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True. **kwargs: parameters passed to either torchvision.models.regnet.RegNet or torchvision.models.regnet.BlockParams class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py> for more detail about the classes. .. autoclass:: torchvision.models.RegNet_Y_128GF_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@handle_legacy_interface(weights=("pretrained", None))
def regnet_y_128gf(
    *, weights: Optional[RegNet_Y_128GF_Weights] = None, progress: bool = True, **kwargs: Any
) -> RegNet:
    """
    Constructs a RegNetY_128GF architecture from
    `Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>`_.
    Args:
        weights (:class:`~torchvision.models.RegNet_Y_128GF_Weights`, optional): The pretrained weights to use.
            See :class:`~torchvision.models.RegNet_Y_128GF_Weights` below for more details and possible values.
            By default, no pretrained weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to either ``torchvision.models.regnet.RegNet`` or
            ``torchvision.models.regnet.BlockParams`` class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py>`_
            for more detail about the classes.
    .. autoclass:: torchvision.models.RegNet_Y_128GF_Weights
        :members:
    """
    weights = RegNet_Y_128GF_Weights.verify(weights)

    params = BlockParams.from_init_params(
        depth=27, w_0=456, w_a=160.83, w_m=2.52, group_width=264, se_ratio=0.25, **kwargs
    )
    return _regnet(params, weights, progress, **kwargs)
regnet_x_400mf
regnet_x_400mf(*, weights: Optional[RegNet_X_400MF_Weights] = None, progress: bool = True, **kwargs: Any) -> RegNet

Constructs a RegNetX_400MF architecture from Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>. Args: weights (:class:~torchvision.models.RegNet_X_400MF_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.RegNet_X_400MF_Weights below for more details and possible values. By default, no pretrained weights are used. progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True. **kwargs: parameters passed to either torchvision.models.regnet.RegNet or torchvision.models.regnet.BlockParams class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py> for more detail about the classes. .. autoclass:: torchvision.models.RegNet_X_400MF_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@handle_legacy_interface(weights=("pretrained", RegNet_X_400MF_Weights.IMAGENET1K_V1))
def regnet_x_400mf(
    *, weights: Optional[RegNet_X_400MF_Weights] = None, progress: bool = True, **kwargs: Any
) -> RegNet:
    """
    Constructs a RegNetX_400MF architecture from
    `Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>`_.
    Args:
        weights (:class:`~torchvision.models.RegNet_X_400MF_Weights`, optional): The pretrained weights to use.
            See :class:`~torchvision.models.RegNet_X_400MF_Weights` below for more details and possible values.
            By default, no pretrained weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to either ``torchvision.models.regnet.RegNet`` or
            ``torchvision.models.regnet.BlockParams`` class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py>`_
            for more detail about the classes.
    .. autoclass:: torchvision.models.RegNet_X_400MF_Weights
        :members:
    """
    weights = RegNet_X_400MF_Weights.verify(weights)

    params = BlockParams.from_init_params(
        depth=22, w_0=24, w_a=24.48, w_m=2.54, group_width=16, **kwargs
    )
    return _regnet(params, weights, progress, **kwargs)
regnet_x_800mf
regnet_x_800mf(*, weights: Optional[RegNet_X_800MF_Weights] = None, progress: bool = True, **kwargs: Any) -> RegNet

Constructs a RegNetX_800MF architecture from Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>. Args: weights (:class:~torchvision.models.RegNet_X_800MF_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.RegNet_X_800MF_Weights below for more details and possible values. By default, no pretrained weights are used. progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True. **kwargs: parameters passed to either torchvision.models.regnet.RegNet or torchvision.models.regnet.BlockParams class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py> for more detail about the classes. .. autoclass:: torchvision.models.RegNet_X_800MF_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@handle_legacy_interface(weights=("pretrained", RegNet_X_800MF_Weights.IMAGENET1K_V1))
def regnet_x_800mf(
    *, weights: Optional[RegNet_X_800MF_Weights] = None, progress: bool = True, **kwargs: Any
) -> RegNet:
    """
    Constructs a RegNetX_800MF architecture from
    `Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>`_.
    Args:
        weights (:class:`~torchvision.models.RegNet_X_800MF_Weights`, optional): The pretrained weights to use.
            See :class:`~torchvision.models.RegNet_X_800MF_Weights` below for more details and possible values.
            By default, no pretrained weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to either ``torchvision.models.regnet.RegNet`` or
            ``torchvision.models.regnet.BlockParams`` class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py>`_
            for more detail about the classes.
    .. autoclass:: torchvision.models.RegNet_X_800MF_Weights
        :members:
    """
    weights = RegNet_X_800MF_Weights.verify(weights)

    params = BlockParams.from_init_params(
        depth=16, w_0=56, w_a=35.73, w_m=2.28, group_width=16, **kwargs
    )
    return _regnet(params, weights, progress, **kwargs)
regnet_x_1_6gf
regnet_x_1_6gf(*, weights: Optional[RegNet_X_1_6GF_Weights] = None, progress: bool = True, **kwargs: Any) -> RegNet

Constructs a RegNetX_1.6GF architecture from Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>. Args: weights (:class:~torchvision.models.RegNet_X_1_6GF_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.RegNet_X_1_6GF_Weights below for more details and possible values. By default, no pretrained weights are used. progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True. **kwargs: parameters passed to either torchvision.models.regnet.RegNet or torchvision.models.regnet.BlockParams class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py> for more detail about the classes. .. autoclass:: torchvision.models.RegNet_X_1_6GF_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@handle_legacy_interface(weights=("pretrained", RegNet_X_1_6GF_Weights.IMAGENET1K_V1))
def regnet_x_1_6gf(
    *, weights: Optional[RegNet_X_1_6GF_Weights] = None, progress: bool = True, **kwargs: Any
) -> RegNet:
    """
    Constructs a RegNetX_1.6GF architecture from
    `Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>`_.
    Args:
        weights (:class:`~torchvision.models.RegNet_X_1_6GF_Weights`, optional): The pretrained weights to use.
            See :class:`~torchvision.models.RegNet_X_1_6GF_Weights` below for more details and possible values.
            By default, no pretrained weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to either ``torchvision.models.regnet.RegNet`` or
            ``torchvision.models.regnet.BlockParams`` class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py>`_
            for more detail about the classes.
    .. autoclass:: torchvision.models.RegNet_X_1_6GF_Weights
        :members:
    """
    weights = RegNet_X_1_6GF_Weights.verify(weights)

    params = BlockParams.from_init_params(
        depth=18, w_0=80, w_a=34.01, w_m=2.25, group_width=24, **kwargs
    )
    return _regnet(params, weights, progress, **kwargs)
regnet_x_3_2gf
regnet_x_3_2gf(*, weights: Optional[RegNet_X_3_2GF_Weights] = None, progress: bool = True, **kwargs: Any) -> RegNet

Constructs a RegNetX_3.2GF architecture from Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>. Args: weights (:class:~torchvision.models.RegNet_X_3_2GF_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.RegNet_X_3_2GF_Weights below for more details and possible values. By default, no pretrained weights are used. progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True. **kwargs: parameters passed to either torchvision.models.regnet.RegNet or torchvision.models.regnet.BlockParams class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py> for more detail about the classes. .. autoclass:: torchvision.models.RegNet_X_3_2GF_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@handle_legacy_interface(weights=("pretrained", RegNet_X_3_2GF_Weights.IMAGENET1K_V1))
def regnet_x_3_2gf(
    *, weights: Optional[RegNet_X_3_2GF_Weights] = None, progress: bool = True, **kwargs: Any
) -> RegNet:
    """
    Constructs a RegNetX_3.2GF architecture from
    `Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>`_.
    Args:
        weights (:class:`~torchvision.models.RegNet_X_3_2GF_Weights`, optional): The pretrained weights to use.
            See :class:`~torchvision.models.RegNet_X_3_2GF_Weights` below for more details and possible values.
            By default, no pretrained weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to either ``torchvision.models.regnet.RegNet`` or
            ``torchvision.models.regnet.BlockParams`` class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py>`_
            for more detail about the classes.
    .. autoclass:: torchvision.models.RegNet_X_3_2GF_Weights
        :members:
    """
    weights = RegNet_X_3_2GF_Weights.verify(weights)

    params = BlockParams.from_init_params(
        depth=25, w_0=88, w_a=26.31, w_m=2.25, group_width=48, **kwargs
    )
    return _regnet(params, weights, progress, **kwargs)
regnet_x_8gf
regnet_x_8gf(*, weights: Optional[RegNet_X_8GF_Weights] = None, progress: bool = True, **kwargs: Any) -> RegNet

Constructs a RegNetX_8GF architecture from Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>. Args: weights (:class:~torchvision.models.RegNet_X_8GF_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.RegNet_X_8GF_Weights below for more details and possible values. By default, no pretrained weights are used. progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True. **kwargs: parameters passed to either torchvision.models.regnet.RegNet or torchvision.models.regnet.BlockParams class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py> for more detail about the classes. .. autoclass:: torchvision.models.RegNet_X_8GF_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@handle_legacy_interface(weights=("pretrained", RegNet_X_8GF_Weights.IMAGENET1K_V1))
def regnet_x_8gf(
    *, weights: Optional[RegNet_X_8GF_Weights] = None, progress: bool = True, **kwargs: Any
) -> RegNet:
    """
    Constructs a RegNetX_8GF architecture from
    `Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>`_.
    Args:
        weights (:class:`~torchvision.models.RegNet_X_8GF_Weights`, optional): The pretrained weights to use.
            See :class:`~torchvision.models.RegNet_X_8GF_Weights` below for more details and possible values.
            By default, no pretrained weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to either ``torchvision.models.regnet.RegNet`` or
            ``torchvision.models.regnet.BlockParams`` class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py>`_
            for more detail about the classes.
    .. autoclass:: torchvision.models.RegNet_X_8GF_Weights
        :members:
    """
    weights = RegNet_X_8GF_Weights.verify(weights)

    params = BlockParams.from_init_params(
        depth=23, w_0=80, w_a=49.56, w_m=2.88, group_width=120, **kwargs
    )
    return _regnet(params, weights, progress, **kwargs)
regnet_x_16gf
regnet_x_16gf(*, weights: Optional[RegNet_X_16GF_Weights] = None, progress: bool = True, **kwargs: Any) -> RegNet

Constructs a RegNetX_16GF architecture from Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>. Args: weights (:class:~torchvision.models.RegNet_X_16GF_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.RegNet_X_16GF_Weights below for more details and possible values. By default, no pretrained weights are used. progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True. **kwargs: parameters passed to either torchvision.models.regnet.RegNet or torchvision.models.regnet.BlockParams class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py> for more detail about the classes. .. autoclass:: torchvision.models.RegNet_X_16GF_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@handle_legacy_interface(weights=("pretrained", RegNet_X_16GF_Weights.IMAGENET1K_V1))
def regnet_x_16gf(
    *, weights: Optional[RegNet_X_16GF_Weights] = None, progress: bool = True, **kwargs: Any
) -> RegNet:
    """
    Constructs a RegNetX_16GF architecture from
    `Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>`_.
    Args:
        weights (:class:`~torchvision.models.RegNet_X_16GF_Weights`, optional): The pretrained weights to use.
            See :class:`~torchvision.models.RegNet_X_16GF_Weights` below for more details and possible values.
            By default, no pretrained weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to either ``torchvision.models.regnet.RegNet`` or
            ``torchvision.models.regnet.BlockParams`` class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py>`_
            for more detail about the classes.
    .. autoclass:: torchvision.models.RegNet_X_16GF_Weights
        :members:
    """
    weights = RegNet_X_16GF_Weights.verify(weights)

    params = BlockParams.from_init_params(
        depth=22, w_0=216, w_a=55.59, w_m=2.1, group_width=128, **kwargs
    )
    return _regnet(params, weights, progress, **kwargs)
regnet_x_32gf
regnet_x_32gf(*, weights: Optional[RegNet_X_32GF_Weights] = None, progress: bool = True, **kwargs: Any) -> RegNet

Constructs a RegNetX_32GF architecture from Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>. Args: weights (:class:~torchvision.models.RegNet_X_32GF_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.RegNet_X_32GF_Weights below for more details and possible values. By default, no pretrained weights are used. progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True. **kwargs: parameters passed to either torchvision.models.regnet.RegNet or torchvision.models.regnet.BlockParams class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py> for more detail about the classes. .. autoclass:: torchvision.models.RegNet_X_32GF_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/regnet.py
@handle_legacy_interface(weights=("pretrained", RegNet_X_32GF_Weights.IMAGENET1K_V1))
def regnet_x_32gf(
    *, weights: Optional[RegNet_X_32GF_Weights] = None, progress: bool = True, **kwargs: Any
) -> RegNet:
    """
    Constructs a RegNetX_32GF architecture from
    `Designing Network Design Spaces <https://arxiv.org/abs/2003.13678>`_.
    Args:
        weights (:class:`~torchvision.models.RegNet_X_32GF_Weights`, optional): The pretrained weights to use.
            See :class:`~torchvision.models.RegNet_X_32GF_Weights` below for more details and possible values.
            By default, no pretrained weights are used.
        progress (bool, optional): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to either ``torchvision.models.regnet.RegNet`` or
            ``torchvision.models.regnet.BlockParams`` class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/regnet.py>`_
            for more detail about the classes.
    .. autoclass:: torchvision.models.RegNet_X_32GF_Weights
        :members:
    """
    weights = RegNet_X_32GF_Weights.verify(weights)

    params = BlockParams.from_init_params(
        depth=23, w_0=320, w_a=69.86, w_m=2.0, group_width=168, **kwargs
    )
    return _regnet(params, weights, progress, **kwargs)
resnet
__all__ module-attribute
__all__ = ['ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152', 'resnext50_32x4d', 'resnext101_32x8d', 'resnext101_64x4d', 'wide_resnet50_2', 'wide_resnet101_2']
BasicBlock
BasicBlock(inplanes: int, planes: int, stride: int = 1, downsample: Optional[Module] = None, groups: int = 1, base_width: int = 64, dilation: int = 1, norm_layer: Optional[Callable[..., Module]] = None)

Bases: Module

Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
def __init__(
    self,
    inplanes: int,
    planes: int,
    stride: int = 1,
    downsample: Optional[nn.Module] = None,
    groups: int = 1,
    base_width: int = 64,
    dilation: int = 1,
    norm_layer: Optional[Callable[..., nn.Module]] = None,
) -> None:
    super().__init__()
    if norm_layer is None:
        norm_layer = nn.BatchNorm2d
    if groups != 1 or base_width != 64:
        raise ValueError("BasicBlock only supports groups=1 and base_width=64")
    if dilation > 1:
        raise NotImplementedError("Dilation > 1 not supported in BasicBlock")
    # Both self.conv1 and self.downsample layers downsample the input when stride != 1
    self.conv1 = conv3x3(inplanes, planes, stride)
    self.bn1 = norm_layer(planes)
    self.relu = nn.ReLU(inplace=True)
    self.conv2 = conv3x3(planes, planes)
    self.bn2 = norm_layer(planes)
    self.downsample = downsample
    self.stride = stride
expansion class-attribute instance-attribute
expansion: int = 1
conv1 instance-attribute
conv1 = conv3x3(inplanes, planes, stride)
bn1 instance-attribute
bn1 = norm_layer(planes)
relu instance-attribute
relu = ReLU(inplace=True)
conv2 instance-attribute
conv2 = conv3x3(planes, planes)
bn2 instance-attribute
bn2 = norm_layer(planes)
downsample instance-attribute
downsample = downsample
stride instance-attribute
stride = stride
forward
forward(x: Tensor) -> Tensor
Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
def forward(self, x: Tensor) -> Tensor:
    identity = x

    out = self.conv1(x)
    out = self.bn1(out)
    out = self.relu(out)

    out = self.conv2(out)
    out = self.bn2(out)

    if self.downsample is not None:
        identity = self.downsample(x)

    out += identity
    out = self.relu(out)

    return out
Bottleneck
Bottleneck(inplanes: int, planes: int, stride: int = 1, downsample: Optional[Module] = None, groups: int = 1, base_width: int = 64, dilation: int = 1, norm_layer: Optional[Callable[..., Module]] = None)

Bases: Module

Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
def __init__(
    self,
    inplanes: int,
    planes: int,
    stride: int = 1,
    downsample: Optional[nn.Module] = None,
    groups: int = 1,
    base_width: int = 64,
    dilation: int = 1,
    norm_layer: Optional[Callable[..., nn.Module]] = None,
) -> None:
    super().__init__()
    if norm_layer is None:
        norm_layer = nn.BatchNorm2d
    width = int(planes * (base_width / 64.0)) * groups
    # Both self.conv2 and self.downsample layers downsample the input when stride != 1
    self.conv1 = conv1x1(inplanes, width)
    self.bn1 = norm_layer(width)
    self.conv2 = conv3x3(width, width, stride, groups, dilation)
    self.bn2 = norm_layer(width)
    self.conv3 = conv1x1(width, planes * self.expansion)
    self.bn3 = norm_layer(planes * self.expansion)
    self.relu = nn.ReLU(inplace=True)
    self.downsample = downsample
    self.stride = stride
expansion class-attribute instance-attribute
expansion: int = 4
conv1 instance-attribute
conv1 = conv1x1(inplanes, width)
bn1 instance-attribute
bn1 = norm_layer(width)
conv2 instance-attribute
conv2 = conv3x3(width, width, stride, groups, dilation)
bn2 instance-attribute
bn2 = norm_layer(width)
conv3 instance-attribute
conv3 = conv1x1(width, planes * expansion)
bn3 instance-attribute
bn3 = norm_layer(planes * expansion)
relu instance-attribute
relu = ReLU(inplace=True)
downsample instance-attribute
downsample = downsample
stride instance-attribute
stride = stride
forward
forward(x: Tensor) -> Tensor
Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
def forward(self, x: Tensor) -> Tensor:
    identity = x

    out = self.conv1(x)
    out = self.bn1(out)
    out = self.relu(out)

    out = self.conv2(out)
    out = self.bn2(out)
    out = self.relu(out)

    out = self.conv3(out)
    out = self.bn3(out)

    if self.downsample is not None:
        identity = self.downsample(x)

    out += identity
    out = self.relu(out)

    return out
ResNet
ResNet(block: Type[Union[BasicBlock, Bottleneck]], layers: List[int], num_classes: int = 1000, zero_init_residual: bool = False, groups: int = 1, width_per_group: int = 64, replace_stride_with_dilation: Optional[List[bool]] = None, norm_layer: Optional[Callable[..., Module]] = None, in_channels: int = 3)

Bases: Module

Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
def __init__(
    self,
    block: Type[Union[BasicBlock, Bottleneck]],
    layers: List[int],
    num_classes: int = 1000,
    zero_init_residual: bool = False,
    groups: int = 1,
    width_per_group: int = 64,
    replace_stride_with_dilation: Optional[List[bool]] = None,
    norm_layer: Optional[Callable[..., nn.Module]] = None,
    in_channels: int = 3,
) -> None:
    super().__init__()
    _log_api_usage_once(self)
    if norm_layer is None:
        norm_layer = nn.BatchNorm2d
    self._norm_layer = norm_layer
    self.in_channels = in_channels

    self.inplanes = 64
    self.dilation = 1
    if replace_stride_with_dilation is None:
        # each element in the tuple indicates if we should replace
        # the 2x2 stride with a dilated convolution instead
        replace_stride_with_dilation = [False, False, False]
    if len(replace_stride_with_dilation) != 3:
        raise ValueError(
            "replace_stride_with_dilation should be None "
            f"or a 3-element tuple, got {replace_stride_with_dilation}"
        )
    self.groups = groups
    self.base_width = width_per_group
    self.conv1 = nn.Conv2d(
        self.in_channels, self.inplanes, kernel_size=7, stride=2, padding=3, bias=False
    )
    self.bn1 = norm_layer(self.inplanes)
    self.relu = nn.ReLU(inplace=True)
    self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
    self.layer1 = self._make_layer(block, 64, layers[0])
    self.layer2 = self._make_layer(
        block, 128, layers[1], stride=2, dilate=replace_stride_with_dilation[0]
    )
    self.layer3 = self._make_layer(
        block, 256, layers[2], stride=2, dilate=replace_stride_with_dilation[1]
    )
    self.layer4 = self._make_layer(
        block, 512, layers[3], stride=2, dilate=replace_stride_with_dilation[2]
    )

    for m in self.modules():
        if isinstance(m, nn.Conv2d):
            nn.init.kaiming_normal_(m.weight, mode="fan_out", nonlinearity="relu")
        elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
            nn.init.constant_(m.weight, 1)
            nn.init.constant_(m.bias, 0)

    # Zero-initialize the last BN in each residual branch,
    # so that the residual branch starts with zeros, and each residual block behaves like an identity.
    # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
    if zero_init_residual:
        for m in self.modules():
            if isinstance(m, Bottleneck) and m.bn3.weight is not None:
                nn.init.constant_(m.bn3.weight, 0)  # type: ignore[arg-type]
            elif isinstance(m, BasicBlock) and m.bn2.weight is not None:
                nn.init.constant_(m.bn2.weight, 0)  # type: ignore[arg-type]
_norm_layer instance-attribute
_norm_layer = norm_layer
in_channels instance-attribute
in_channels = in_channels
inplanes instance-attribute
inplanes = 64
dilation instance-attribute
dilation = 1
groups instance-attribute
groups = groups
base_width instance-attribute
base_width = width_per_group
conv1 instance-attribute
conv1 = Conv2d(in_channels, inplanes, kernel_size=7, stride=2, padding=3, bias=False)
bn1 instance-attribute
bn1 = norm_layer(inplanes)
relu instance-attribute
relu = ReLU(inplace=True)
maxpool instance-attribute
maxpool = MaxPool2d(kernel_size=3, stride=2, padding=1)
layer1 instance-attribute
layer1 = _make_layer(block, 64, layers[0])
layer2 instance-attribute
layer2 = _make_layer(block, 128, layers[1], stride=2, dilate=replace_stride_with_dilation[0])
layer3 instance-attribute
layer3 = _make_layer(block, 256, layers[2], stride=2, dilate=replace_stride_with_dilation[1])
layer4 instance-attribute
layer4 = _make_layer(block, 512, layers[3], stride=2, dilate=replace_stride_with_dilation[2])
_make_layer
_make_layer(block: Type[Union[BasicBlock, Bottleneck]], planes: int, blocks: int, stride: int = 1, dilate: bool = False) -> Sequential
Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
def _make_layer(
    self,
    block: Type[Union[BasicBlock, Bottleneck]],
    planes: int,
    blocks: int,
    stride: int = 1,
    dilate: bool = False,
) -> nn.Sequential:
    norm_layer = self._norm_layer
    downsample = None
    previous_dilation = self.dilation
    if dilate:
        self.dilation *= stride
        stride = 1
    if stride != 1 or self.inplanes != planes * block.expansion:
        downsample = nn.Sequential(
            conv1x1(self.inplanes, planes * block.expansion, stride),
            norm_layer(planes * block.expansion),
        )

    layers = []
    layers.append(
        block(
            self.inplanes,
            planes,
            stride,
            downsample,
            self.groups,
            self.base_width,
            previous_dilation,
            norm_layer,
        )
    )
    self.inplanes = planes * block.expansion
    for _ in range(1, blocks):
        layers.append(
            block(
                self.inplanes,
                planes,
                groups=self.groups,
                base_width=self.base_width,
                dilation=self.dilation,
                norm_layer=norm_layer,
            )
        )

    return nn.Sequential(*layers)
_forward_impl
_forward_impl(x: Tensor) -> Tensor
Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
def _forward_impl(self, x: Tensor) -> Tensor:
    # See note [TorchScript super()]
    x = self.conv1(x)
    x = self.bn1(x)
    x = self.relu(x)
    x = self.maxpool(x)

    x = self.layer1(x)
    x = self.layer2(x)
    x = self.layer3(x)
    x = self.layer4(x)

    return x
forward
forward(x: Tensor) -> Tensor
Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
def forward(self, x: Tensor) -> Tensor:
    return self._forward_impl(x)
load_state_dict
load_state_dict(state_dict, strict: bool = True)
Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
def load_state_dict(self, state_dict, strict: bool = True):
    extend_state_dict_input_channel(state_dict, "conv1.weight", self.conv1)
    for key in list(state_dict):
        if key.startswith("fc."):
            state_dict.pop(key)
    return super().load_state_dict(state_dict, strict)
conv3x3
conv3x3(in_planes: int, out_planes: int, stride: int = 1, groups: int = 1, dilation: int = 1) -> Conv2d

3x3 convolution with padding

Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
def conv3x3(
    in_planes: int, out_planes: int, stride: int = 1, groups: int = 1, dilation: int = 1
) -> nn.Conv2d:
    """3x3 convolution with padding"""
    return nn.Conv2d(
        in_planes,
        out_planes,
        kernel_size=3,
        stride=stride,
        padding=dilation,
        groups=groups,
        bias=False,
        dilation=dilation,
    )
conv1x1
conv1x1(in_planes: int, out_planes: int, stride: int = 1) -> Conv2d

1x1 convolution

Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
def conv1x1(in_planes: int, out_planes: int, stride: int = 1) -> nn.Conv2d:
    """1x1 convolution"""
    return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
_resnet
_resnet(block: Type[Union[BasicBlock, Bottleneck]], layers: List[int], weights: Optional[WeightsEnum], progress: bool, **kwargs: Any) -> ResNet
Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
def _resnet(
    block: Type[Union[BasicBlock, Bottleneck]],
    layers: List[int],
    weights: Optional[WeightsEnum],
    progress: bool,
    **kwargs: Any,
) -> ResNet:
    if weights is not None:
        _ovewrite_named_param(kwargs, "num_classes", len(weights.meta["categories"]))

    model = ResNet(block, layers, **kwargs)

    if weights is not None:
        model.load_state_dict(weights.get_state_dict(progress=progress))

    return model
resnet18
resnet18(*, weights: Optional[ResNet18_Weights] = None, progress: bool = True, **kwargs: Any) -> ResNet

ResNet-18 from Deep Residual Learning for Image Recognition <https://arxiv.org/pdf/1512.03385.pdf>__.

Parameters:

  • weights

    class:~torchvision.models.ResNet18_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.ResNet18_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.resnet.ResNet base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>_ for more details about this class.

.. autoclass:: torchvision.models.ResNet18_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
@handle_legacy_interface(weights=("pretrained", ResNet18_Weights.IMAGENET1K_V1))
def resnet18(
    *, weights: Optional[ResNet18_Weights] = None, progress: bool = True, **kwargs: Any
) -> ResNet:
    """ResNet-18 from `Deep Residual Learning for Image Recognition <https://arxiv.org/pdf/1512.03385.pdf>`__.

    Args:
        weights (:class:`~torchvision.models.ResNet18_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.ResNet18_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.resnet.ResNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ResNet18_Weights
        :members:
    """
    weights = ResNet18_Weights.verify(weights)

    return _resnet(BasicBlock, [2, 2, 2, 2], weights, progress, **kwargs)
resnet34
resnet34(*, weights: Optional[ResNet34_Weights] = None, progress: bool = True, **kwargs: Any) -> ResNet

ResNet-34 from Deep Residual Learning for Image Recognition <https://arxiv.org/pdf/1512.03385.pdf>__.

Parameters:

  • weights

    class:~torchvision.models.ResNet34_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.ResNet34_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.resnet.ResNet base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>_ for more details about this class.

.. autoclass:: torchvision.models.ResNet34_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
@handle_legacy_interface(weights=("pretrained", ResNet34_Weights.IMAGENET1K_V1))
def resnet34(
    *, weights: Optional[ResNet34_Weights] = None, progress: bool = True, **kwargs: Any
) -> ResNet:
    """ResNet-34 from `Deep Residual Learning for Image Recognition <https://arxiv.org/pdf/1512.03385.pdf>`__.

    Args:
        weights (:class:`~torchvision.models.ResNet34_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.ResNet34_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.resnet.ResNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ResNet34_Weights
        :members:
    """
    weights = ResNet34_Weights.verify(weights)

    return _resnet(BasicBlock, [3, 4, 6, 3], weights, progress, **kwargs)
resnet50
resnet50(*, weights: Optional[ResNet50_Weights] = None, progress: bool = True, **kwargs: Any) -> ResNet

ResNet-50 from Deep Residual Learning for Image Recognition <https://arxiv.org/pdf/1512.03385.pdf>__.

.. note:: The bottleneck of TorchVision places the stride for downsampling to the second 3x3 convolution while the original paper places it to the first 1x1 convolution. This variant improves the accuracy and is known as ResNet V1.5 <https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch>_.

Parameters:

  • weights

    class:~torchvision.models.ResNet50_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.ResNet50_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.resnet.ResNet base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>_ for more details about this class.

.. autoclass:: torchvision.models.ResNet50_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
@handle_legacy_interface(weights=("pretrained", ResNet50_Weights.IMAGENET1K_V1))
def resnet50(
    *, weights: Optional[ResNet50_Weights] = None, progress: bool = True, **kwargs: Any
) -> ResNet:
    """ResNet-50 from `Deep Residual Learning for Image Recognition <https://arxiv.org/pdf/1512.03385.pdf>`__.

    .. note::
       The bottleneck of TorchVision places the stride for downsampling to the second 3x3
       convolution while the original paper places it to the first 1x1 convolution.
       This variant improves the accuracy and is known as `ResNet V1.5
       <https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch>`_.

    Args:
        weights (:class:`~torchvision.models.ResNet50_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.ResNet50_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.resnet.ResNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ResNet50_Weights
        :members:
    """
    weights = ResNet50_Weights.verify(weights)

    return _resnet(Bottleneck, [3, 4, 6, 3], weights, progress, **kwargs)
resnet101
resnet101(*, weights: Optional[ResNet101_Weights] = None, progress: bool = True, **kwargs: Any) -> ResNet

ResNet-101 from Deep Residual Learning for Image Recognition <https://arxiv.org/pdf/1512.03385.pdf>__.

.. note:: The bottleneck of TorchVision places the stride for downsampling to the second 3x3 convolution while the original paper places it to the first 1x1 convolution. This variant improves the accuracy and is known as ResNet V1.5 <https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch>_.

Parameters:

  • weights

    class:~torchvision.models.ResNet101_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.ResNet101_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.resnet.ResNet base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>_ for more details about this class.

.. autoclass:: torchvision.models.ResNet101_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
@handle_legacy_interface(weights=("pretrained", ResNet101_Weights.IMAGENET1K_V1))
def resnet101(
    *, weights: Optional[ResNet101_Weights] = None, progress: bool = True, **kwargs: Any
) -> ResNet:
    """ResNet-101 from `Deep Residual Learning for Image Recognition <https://arxiv.org/pdf/1512.03385.pdf>`__.

    .. note::
       The bottleneck of TorchVision places the stride for downsampling to the second 3x3
       convolution while the original paper places it to the first 1x1 convolution.
       This variant improves the accuracy and is known as `ResNet V1.5
       <https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch>`_.

    Args:
        weights (:class:`~torchvision.models.ResNet101_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.ResNet101_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.resnet.ResNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ResNet101_Weights
        :members:
    """
    weights = ResNet101_Weights.verify(weights)

    return _resnet(Bottleneck, [3, 4, 23, 3], weights, progress, **kwargs)
resnet152
resnet152(*, weights: Optional[ResNet152_Weights] = None, progress: bool = True, **kwargs: Any) -> ResNet

ResNet-152 from Deep Residual Learning for Image Recognition <https://arxiv.org/pdf/1512.03385.pdf>__.

.. note:: The bottleneck of TorchVision places the stride for downsampling to the second 3x3 convolution while the original paper places it to the first 1x1 convolution. This variant improves the accuracy and is known as ResNet V1.5 <https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch>_.

Parameters:

  • weights

    class:~torchvision.models.ResNet152_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.ResNet152_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.resnet.ResNet base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>_ for more details about this class.

.. autoclass:: torchvision.models.ResNet152_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
@handle_legacy_interface(weights=("pretrained", ResNet152_Weights.IMAGENET1K_V1))
def resnet152(
    *, weights: Optional[ResNet152_Weights] = None, progress: bool = True, **kwargs: Any
) -> ResNet:
    """ResNet-152 from `Deep Residual Learning for Image Recognition <https://arxiv.org/pdf/1512.03385.pdf>`__.

    .. note::
       The bottleneck of TorchVision places the stride for downsampling to the second 3x3
       convolution while the original paper places it to the first 1x1 convolution.
       This variant improves the accuracy and is known as `ResNet V1.5
       <https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch>`_.

    Args:
        weights (:class:`~torchvision.models.ResNet152_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.ResNet152_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.resnet.ResNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.ResNet152_Weights
        :members:
    """
    weights = ResNet152_Weights.verify(weights)

    return _resnet(Bottleneck, [3, 8, 36, 3], weights, progress, **kwargs)
resnext50_32x4d
resnext50_32x4d(*, weights: Optional[ResNeXt50_32X4D_Weights] = None, progress: bool = True, **kwargs: Any) -> ResNet

ResNeXt-50 32x4d model from Aggregated Residual Transformation for Deep Neural Networks <https://arxiv.org/abs/1611.05431>_.

Parameters:

  • weights

    class:~torchvision.models.ResNeXt50_32X4D_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.ResNext50_32X4D_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.resnet.ResNet base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>_ for more details about this class.

.. autoclass:: torchvision.models.ResNeXt50_32X4D_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
@handle_legacy_interface(weights=("pretrained", ResNeXt50_32X4D_Weights.IMAGENET1K_V1))
def resnext50_32x4d(
    *, weights: Optional[ResNeXt50_32X4D_Weights] = None, progress: bool = True, **kwargs: Any
) -> ResNet:
    """ResNeXt-50 32x4d model from
    `Aggregated Residual Transformation for Deep Neural Networks <https://arxiv.org/abs/1611.05431>`_.

    Args:
        weights (:class:`~torchvision.models.ResNeXt50_32X4D_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.ResNext50_32X4D_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.resnet.ResNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>`_
            for more details about this class.
    .. autoclass:: torchvision.models.ResNeXt50_32X4D_Weights
        :members:
    """
    weights = ResNeXt50_32X4D_Weights.verify(weights)

    _ovewrite_named_param(kwargs, "groups", 32)
    _ovewrite_named_param(kwargs, "width_per_group", 4)
    return _resnet(Bottleneck, [3, 4, 6, 3], weights, progress, **kwargs)
resnext101_32x8d
resnext101_32x8d(*, weights: Optional[ResNeXt101_32X8D_Weights] = None, progress: bool = True, **kwargs: Any) -> ResNet

ResNeXt-101 32x8d model from Aggregated Residual Transformation for Deep Neural Networks <https://arxiv.org/abs/1611.05431>_.

Parameters:

  • weights

    class:~torchvision.models.ResNeXt101_32X8D_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.ResNeXt101_32X8D_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.resnet.ResNet base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>_ for more details about this class.

.. autoclass:: torchvision.models.ResNeXt101_32X8D_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
@handle_legacy_interface(weights=("pretrained", ResNeXt101_32X8D_Weights.IMAGENET1K_V1))
def resnext101_32x8d(
    *, weights: Optional[ResNeXt101_32X8D_Weights] = None, progress: bool = True, **kwargs: Any
) -> ResNet:
    """ResNeXt-101 32x8d model from
    `Aggregated Residual Transformation for Deep Neural Networks <https://arxiv.org/abs/1611.05431>`_.

    Args:
        weights (:class:`~torchvision.models.ResNeXt101_32X8D_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.ResNeXt101_32X8D_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.resnet.ResNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>`_
            for more details about this class.
    .. autoclass:: torchvision.models.ResNeXt101_32X8D_Weights
        :members:
    """
    weights = ResNeXt101_32X8D_Weights.verify(weights)

    _ovewrite_named_param(kwargs, "groups", 32)
    _ovewrite_named_param(kwargs, "width_per_group", 8)
    return _resnet(Bottleneck, [3, 4, 23, 3], weights, progress, **kwargs)
resnext101_64x4d
resnext101_64x4d(*, weights: Optional[ResNeXt101_64X4D_Weights] = None, progress: bool = True, **kwargs: Any) -> ResNet

ResNeXt-101 64x4d model from Aggregated Residual Transformation for Deep Neural Networks <https://arxiv.org/abs/1611.05431>_.

Parameters:

  • weights

    class:~torchvision.models.ResNeXt101_64X4D_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.ResNeXt101_64X4D_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.resnet.ResNet base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>_ for more details about this class.

.. autoclass:: torchvision.models.ResNeXt101_64X4D_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
def resnext101_64x4d(
    *, weights: Optional[ResNeXt101_64X4D_Weights] = None, progress: bool = True, **kwargs: Any
) -> ResNet:
    """ResNeXt-101 64x4d model from
    `Aggregated Residual Transformation for Deep Neural Networks <https://arxiv.org/abs/1611.05431>`_.

    Args:
        weights (:class:`~torchvision.models.ResNeXt101_64X4D_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.ResNeXt101_64X4D_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.resnet.ResNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>`_
            for more details about this class.
    .. autoclass:: torchvision.models.ResNeXt101_64X4D_Weights
        :members:
    """
    weights = ResNeXt101_64X4D_Weights.verify(weights)

    _ovewrite_named_param(kwargs, "groups", 64)
    _ovewrite_named_param(kwargs, "width_per_group", 4)
    return _resnet(Bottleneck, [3, 4, 23, 3], weights, progress, **kwargs)
wide_resnet50_2
wide_resnet50_2(*, weights: Optional[Wide_ResNet50_2_Weights] = None, progress: bool = True, **kwargs: Any) -> ResNet

Wide ResNet-50-2 model from Wide Residual Networks <https://arxiv.org/abs/1605.07146>_.

The model is the same as ResNet except for the bottleneck number of channels which is twice larger in every block. The number of channels in outer 1x1 convolutions is the same, e.g. last block in ResNet-50 has 2048-512-2048 channels, and in Wide ResNet-50-2 has 2048-1024-2048.

Parameters:

  • weights

    class:~torchvision.models.Wide_ResNet50_2_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.Wide_ResNet50_2_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.resnet.ResNet base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>_ for more details about this class.

.. autoclass:: torchvision.models.Wide_ResNet50_2_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
@handle_legacy_interface(weights=("pretrained", Wide_ResNet50_2_Weights.IMAGENET1K_V1))
def wide_resnet50_2(
    *, weights: Optional[Wide_ResNet50_2_Weights] = None, progress: bool = True, **kwargs: Any
) -> ResNet:
    """Wide ResNet-50-2 model from
    `Wide Residual Networks <https://arxiv.org/abs/1605.07146>`_.

    The model is the same as ResNet except for the bottleneck number of channels
    which is twice larger in every block. The number of channels in outer 1x1
    convolutions is the same, e.g. last block in ResNet-50 has 2048-512-2048
    channels, and in Wide ResNet-50-2 has 2048-1024-2048.

    Args:
        weights (:class:`~torchvision.models.Wide_ResNet50_2_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.Wide_ResNet50_2_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.resnet.ResNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>`_
            for more details about this class.
    .. autoclass:: torchvision.models.Wide_ResNet50_2_Weights
        :members:
    """
    weights = Wide_ResNet50_2_Weights.verify(weights)

    _ovewrite_named_param(kwargs, "width_per_group", 64 * 2)
    return _resnet(Bottleneck, [3, 4, 6, 3], weights, progress, **kwargs)
wide_resnet101_2
wide_resnet101_2(*, weights: Optional[Wide_ResNet101_2_Weights] = None, progress: bool = True, **kwargs: Any) -> ResNet

Wide ResNet-101-2 model from Wide Residual Networks <https://arxiv.org/abs/1605.07146>_.

The model is the same as ResNet except for the bottleneck number of channels which is twice larger in every block. The number of channels in outer 1x1 convolutions is the same, e.g. last block in ResNet-101 has 2048-512-2048 channels, and in Wide ResNet-101-2 has 2048-1024-2048.

Parameters:

  • weights

    class:~torchvision.models.Wide_ResNet101_2_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.Wide_ResNet101_2_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.resnet.ResNet base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>_ for more details about this class.

.. autoclass:: torchvision.models.Wide_ResNet101_2_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/resnet.py
@handle_legacy_interface(weights=("pretrained", Wide_ResNet101_2_Weights.IMAGENET1K_V1))
def wide_resnet101_2(
    *, weights: Optional[Wide_ResNet101_2_Weights] = None, progress: bool = True, **kwargs: Any
) -> ResNet:
    """Wide ResNet-101-2 model from
    `Wide Residual Networks <https://arxiv.org/abs/1605.07146>`_.

    The model is the same as ResNet except for the bottleneck number of channels
    which is twice larger in every block. The number of channels in outer 1x1
    convolutions is the same, e.g. last block in ResNet-101 has 2048-512-2048
    channels, and in Wide ResNet-101-2 has 2048-1024-2048.

    Args:
        weights (:class:`~torchvision.models.Wide_ResNet101_2_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.Wide_ResNet101_2_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.resnet.ResNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py>`_
            for more details about this class.
    .. autoclass:: torchvision.models.Wide_ResNet101_2_Weights
        :members:
    """
    weights = Wide_ResNet101_2_Weights.verify(weights)

    _ovewrite_named_param(kwargs, "width_per_group", 64 * 2)
    return _resnet(Bottleneck, [3, 4, 23, 3], weights, progress, **kwargs)
squeezenet
__all__ module-attribute
__all__ = ['SqueezeNet', 'squeezenet1_0', 'squeezenet1_1']
Fire
Fire(inplanes: int, squeeze_planes: int, expand1x1_planes: int, expand3x3_planes: int)

Bases: Module

Source code in SaigeToolkit/model/backbone/torchvision/squeezenet.py
def __init__(
    self, inplanes: int, squeeze_planes: int, expand1x1_planes: int, expand3x3_planes: int
) -> None:
    super().__init__()
    self.inplanes = inplanes
    self.squeeze = nn.Conv2d(inplanes, squeeze_planes, kernel_size=1)
    self.squeeze_activation = nn.ReLU(inplace=True)
    self.expand1x1 = nn.Conv2d(squeeze_planes, expand1x1_planes, kernel_size=1)
    self.expand1x1_activation = nn.ReLU(inplace=True)
    self.expand3x3 = nn.Conv2d(squeeze_planes, expand3x3_planes, kernel_size=3, padding=1)
    self.expand3x3_activation = nn.ReLU(inplace=True)
inplanes instance-attribute
inplanes = inplanes
squeeze instance-attribute
squeeze = Conv2d(inplanes, squeeze_planes, kernel_size=1)
squeeze_activation instance-attribute
squeeze_activation = ReLU(inplace=True)
expand1x1 instance-attribute
expand1x1 = Conv2d(squeeze_planes, expand1x1_planes, kernel_size=1)
expand1x1_activation instance-attribute
expand1x1_activation = ReLU(inplace=True)
expand3x3 instance-attribute
expand3x3 = Conv2d(squeeze_planes, expand3x3_planes, kernel_size=3, padding=1)
expand3x3_activation instance-attribute
expand3x3_activation = ReLU(inplace=True)
forward
forward(x: Tensor) -> Tensor
Source code in SaigeToolkit/model/backbone/torchvision/squeezenet.py
def forward(self, x: torch.Tensor) -> torch.Tensor:
    x = self.squeeze_activation(self.squeeze(x))
    return torch.cat(
        [self.expand1x1_activation(self.expand1x1(x)), self.expand3x3_activation(self.expand3x3(x))],
        1,
    )
SqueezeNet
SqueezeNet(version: str = '1_0', num_classes: int = 1000, dropout: float = 0.5, in_channels: int = 3)

Bases: Module

Source code in SaigeToolkit/model/backbone/torchvision/squeezenet.py
def __init__(
    self,
    version: str = "1_0",
    num_classes: int = 1000,
    dropout: float = 0.5,
    in_channels: int = 3,
) -> None:
    super().__init__()
    _log_api_usage_once(self)
    self.num_classes = num_classes
    self.in_channels = in_channels
    if version == "1_0":
        self.features = nn.Sequential(
            nn.Conv2d(self.in_channels, 96, kernel_size=7, stride=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
            Fire(96, 16, 64, 64),
            Fire(128, 16, 64, 64),
            Fire(128, 32, 128, 128),
            nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
            Fire(256, 32, 128, 128),
            Fire(256, 48, 192, 192),
            Fire(384, 48, 192, 192),
            Fire(384, 64, 256, 256),
            nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
            Fire(512, 64, 256, 256),
        )
    elif version == "1_1":
        self.features = nn.Sequential(
            nn.Conv2d(self.in_channels, 64, kernel_size=3, stride=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
            Fire(64, 16, 64, 64),
            Fire(128, 16, 64, 64),
            nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
            Fire(128, 32, 128, 128),
            Fire(256, 32, 128, 128),
            nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
            Fire(256, 48, 192, 192),
            Fire(384, 48, 192, 192),
            Fire(384, 64, 256, 256),
            Fire(512, 64, 256, 256),
        )
    else:
        # FIXME: Is this needed? SqueezeNet should only be called from the
        # FIXME: squeezenet1_x() functions
        # FIXME: This checking is not done for the other models
        raise ValueError(f"Unsupported SqueezeNet version {version}: 1_0 or 1_1 expected")

    for m in self.modules():
        if isinstance(m, nn.Conv2d):
            init.kaiming_uniform_(m.weight)
            if m.bias is not None:
                init.constant_(m.bias, 0)
num_classes instance-attribute
num_classes = num_classes
in_channels instance-attribute
in_channels = in_channels
features instance-attribute
features = Sequential(Conv2d(in_channels, 96, kernel_size=7, stride=2), ReLU(inplace=True), MaxPool2d(kernel_size=3, stride=2, ceil_mode=True), Fire(96, 16, 64, 64), Fire(128, 16, 64, 64), Fire(128, 32, 128, 128), MaxPool2d(kernel_size=3, stride=2, ceil_mode=True), Fire(256, 32, 128, 128), Fire(256, 48, 192, 192), Fire(384, 48, 192, 192), Fire(384, 64, 256, 256), MaxPool2d(kernel_size=3, stride=2, ceil_mode=True), Fire(512, 64, 256, 256))
forward
forward(x: Tensor) -> Tensor
Source code in SaigeToolkit/model/backbone/torchvision/squeezenet.py
def forward(self, x: torch.Tensor) -> torch.Tensor:
    x = self.features(x)
    return x
load_state_dict
load_state_dict(state_dict, strict: bool = True)
Source code in SaigeToolkit/model/backbone/torchvision/squeezenet.py
def load_state_dict(self, state_dict, strict: bool = True):
    extend_state_dict_input_channel(state_dict, "features.0.weight", self.features[0])
    for key in list(state_dict):
        if key.startswith("classifier."):
            state_dict.pop(key)
    return super().load_state_dict(state_dict, strict)
_squeezenet
_squeezenet(version: str, weights: Optional[WeightsEnum], progress: bool, **kwargs: Any) -> SqueezeNet
Source code in SaigeToolkit/model/backbone/torchvision/squeezenet.py
def _squeezenet(
    version: str,
    weights: Optional[WeightsEnum],
    progress: bool,
    **kwargs: Any,
) -> SqueezeNet:
    if weights is not None:
        _ovewrite_named_param(kwargs, "num_classes", len(weights.meta["categories"]))

    model = SqueezeNet(version, **kwargs)

    if weights is not None:
        model.load_state_dict(weights.get_state_dict(progress=progress))

    return model
squeezenet1_0
squeezenet1_0(*, weights: Optional[SqueezeNet1_0_Weights] = None, progress: bool = True, **kwargs: Any) -> SqueezeNet

SqueezeNet model architecture from the SqueezeNet: AlexNet-level accuracy with 50x fewer parameters and <0.5MB model size <https://arxiv.org/abs/1602.07360>_ paper.

Parameters:

  • weights

    class:~torchvision.models.SqueezeNet1_0_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.SqueezeNet1_0_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.squeezenet.SqueezeNet base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/squeezenet.py>_ for more details about this class.

.. autoclass:: torchvision.models.SqueezeNet1_0_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/squeezenet.py
@handle_legacy_interface(weights=("pretrained", SqueezeNet1_0_Weights.IMAGENET1K_V1))
def squeezenet1_0(
    *, weights: Optional[SqueezeNet1_0_Weights] = None, progress: bool = True, **kwargs: Any
) -> SqueezeNet:
    """SqueezeNet model architecture from the `SqueezeNet: AlexNet-level
    accuracy with 50x fewer parameters and <0.5MB model size
    <https://arxiv.org/abs/1602.07360>`_ paper.

    Args:
        weights (:class:`~torchvision.models.SqueezeNet1_0_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.SqueezeNet1_0_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.squeezenet.SqueezeNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/squeezenet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.SqueezeNet1_0_Weights
        :members:
    """
    weights = SqueezeNet1_0_Weights.verify(weights)
    return _squeezenet("1_0", weights, progress, **kwargs)
squeezenet1_1
squeezenet1_1(*, weights: Optional[SqueezeNet1_1_Weights] = None, progress: bool = True, **kwargs: Any) -> SqueezeNet

SqueezeNet 1.1 model from the official SqueezeNet repo <https://github.com/DeepScale/SqueezeNet/tree/master/SqueezeNet_v1.1>_.

SqueezeNet 1.1 has 2.4x less computation and slightly fewer parameters than SqueezeNet 1.0, without sacrificing accuracy.

Parameters:

  • weights

    class:~torchvision.models.SqueezeNet1_1_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.SqueezeNet1_1_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.squeezenet.SqueezeNet base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/squeezenet.py>_ for more details about this class.

.. autoclass:: torchvision.models.SqueezeNet1_1_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/squeezenet.py
@handle_legacy_interface(weights=("pretrained", SqueezeNet1_1_Weights.IMAGENET1K_V1))
def squeezenet1_1(
    *, weights: Optional[SqueezeNet1_1_Weights] = None, progress: bool = True, **kwargs: Any
) -> SqueezeNet:
    """SqueezeNet 1.1 model from the `official SqueezeNet repo
    <https://github.com/DeepScale/SqueezeNet/tree/master/SqueezeNet_v1.1>`_.

    SqueezeNet 1.1 has 2.4x less computation and slightly fewer parameters
    than SqueezeNet 1.0, without sacrificing accuracy.

    Args:
        weights (:class:`~torchvision.models.SqueezeNet1_1_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.SqueezeNet1_1_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.squeezenet.SqueezeNet``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/squeezenet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.SqueezeNet1_1_Weights
        :members:
    """
    weights = SqueezeNet1_1_Weights.verify(weights)
    return _squeezenet("1_1", weights, progress, **kwargs)
vgg
__all__ module-attribute
__all__ = ['VGG', 'vgg11', 'vgg11_bn', 'vgg13', 'vgg13_bn', 'vgg16', 'vgg16_bn', 'vgg19', 'vgg19_bn']
cfgs module-attribute
cfgs: Dict[str, List[Union[str, int]]] = {'A': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], 'B': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'], 'D': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'], 'E': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M']}
VGG
VGG(features: Module, num_classes: int = 1000, init_weights: bool = True, dropout: float = 0.5)

Bases: Module

Source code in SaigeToolkit/model/backbone/torchvision/vgg.py
def __init__(
    self,
    features: nn.Module,
    num_classes: int = 1000,
    init_weights: bool = True,
    dropout: float = 0.5,
) -> None:
    super().__init__()
    _log_api_usage_once(self)
    self.features = features
    if init_weights:
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode="fan_out", nonlinearity="relu")
                if m.bias is not None:
                    nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                nn.init.normal_(m.weight, 0, 0.01)
                nn.init.constant_(m.bias, 0)
features instance-attribute
features = features
forward
forward(x: Tensor) -> Tensor
Source code in SaigeToolkit/model/backbone/torchvision/vgg.py
def forward(self, x: torch.Tensor) -> torch.Tensor:
    x = self.features(x)
    return x
load_state_dict
load_state_dict(state_dict, strict: bool = True)
Source code in SaigeToolkit/model/backbone/torchvision/vgg.py
def load_state_dict(self, state_dict, strict: bool = True):
    extend_state_dict_input_channel(state_dict, "features.0.weight", self.features[0])
    for key in list(state_dict):
        if key.startswith("classifier."):
            state_dict.pop(key)
    return super().load_state_dict(state_dict, strict)
make_layers
make_layers(cfg: List[Union[str, int]], batch_norm: bool = False, in_channels: int = 3) -> Sequential
Source code in SaigeToolkit/model/backbone/torchvision/vgg.py
def make_layers(
    cfg: List[Union[str, int]], batch_norm: bool = False, in_channels: int = 3
) -> nn.Sequential:
    layers: List[nn.Module] = []
    for v in cfg:
        if v == "M":
            layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
        else:
            v = cast(int, v)
            conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
            if batch_norm:
                layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]
            else:
                layers += [conv2d, nn.ReLU(inplace=True)]
            in_channels = v
    return nn.Sequential(*layers)
_vgg
_vgg(cfg: str, batch_norm: bool, weights: Optional[WeightsEnum], progress: bool, in_channels: int = 3, **kwargs: Any) -> VGG
Source code in SaigeToolkit/model/backbone/torchvision/vgg.py
def _vgg(
    cfg: str,
    batch_norm: bool,
    weights: Optional[WeightsEnum],
    progress: bool,
    in_channels: int = 3,
    **kwargs: Any,
) -> VGG:
    if weights is not None:
        kwargs["init_weights"] = False
        if weights.meta["categories"] is not None:
            _ovewrite_named_param(kwargs, "num_classes", len(weights.meta["categories"]))
    model = VGG(make_layers(cfgs[cfg], batch_norm=batch_norm, in_channels=in_channels), **kwargs)
    if weights is not None:
        model.load_state_dict(weights.get_state_dict(progress=progress))
    return model
vgg11
vgg11(*, weights: Optional[VGG11_Weights] = None, progress: bool = True, **kwargs: Any) -> VGG

VGG-11 from Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/abs/1409.1556>__.

Parameters:

  • weights

    class:~torchvision.models.VGG11_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.VGG11_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.vgg.VGG base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/vgg.py>_ for more details about this class.

.. autoclass:: torchvision.models.VGG11_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/vgg.py
@handle_legacy_interface(weights=("pretrained", VGG11_Weights.IMAGENET1K_V1))
def vgg11(*, weights: Optional[VGG11_Weights] = None, progress: bool = True, **kwargs: Any) -> VGG:
    """VGG-11 from `Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/abs/1409.1556>`__.

    Args:
        weights (:class:`~torchvision.models.VGG11_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.VGG11_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.vgg.VGG``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/vgg.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.VGG11_Weights
        :members:
    """
    weights = VGG11_Weights.verify(weights)

    return _vgg("A", False, weights, progress, **kwargs)
vgg11_bn
vgg11_bn(*, weights: Optional[VGG11_BN_Weights] = None, progress: bool = True, **kwargs: Any) -> VGG

VGG-11-BN from Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/abs/1409.1556>__.

Parameters:

  • weights

    class:~torchvision.models.VGG11_BN_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.VGG11_BN_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.vgg.VGG base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/vgg.py>_ for more details about this class.

.. autoclass:: torchvision.models.VGG11_BN_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/vgg.py
@handle_legacy_interface(weights=("pretrained", VGG11_BN_Weights.IMAGENET1K_V1))
def vgg11_bn(*, weights: Optional[VGG11_BN_Weights] = None, progress: bool = True, **kwargs: Any) -> VGG:
    """VGG-11-BN from `Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/abs/1409.1556>`__.

    Args:
        weights (:class:`~torchvision.models.VGG11_BN_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.VGG11_BN_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.vgg.VGG``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/vgg.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.VGG11_BN_Weights
        :members:
    """
    weights = VGG11_BN_Weights.verify(weights)

    return _vgg("A", True, weights, progress, **kwargs)
vgg13
vgg13(*, weights: Optional[VGG13_Weights] = None, progress: bool = True, **kwargs: Any) -> VGG

VGG-13 from Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/abs/1409.1556>__.

Parameters:

  • weights

    class:~torchvision.models.VGG13_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.VGG13_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.vgg.VGG base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/vgg.py>_ for more details about this class.

.. autoclass:: torchvision.models.VGG13_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/vgg.py
@handle_legacy_interface(weights=("pretrained", VGG13_Weights.IMAGENET1K_V1))
def vgg13(*, weights: Optional[VGG13_Weights] = None, progress: bool = True, **kwargs: Any) -> VGG:
    """VGG-13 from `Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/abs/1409.1556>`__.

    Args:
        weights (:class:`~torchvision.models.VGG13_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.VGG13_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.vgg.VGG``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/vgg.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.VGG13_Weights
        :members:
    """
    weights = VGG13_Weights.verify(weights)

    return _vgg("B", False, weights, progress, **kwargs)
vgg13_bn
vgg13_bn(*, weights: Optional[VGG13_BN_Weights] = None, progress: bool = True, **kwargs: Any) -> VGG

VGG-13-BN from Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/abs/1409.1556>__.

Parameters:

  • weights

    class:~torchvision.models.VGG13_BN_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.VGG13_BN_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.vgg.VGG base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/vgg.py>_ for more details about this class.

.. autoclass:: torchvision.models.VGG13_BN_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/vgg.py
@handle_legacy_interface(weights=("pretrained", VGG13_BN_Weights.IMAGENET1K_V1))
def vgg13_bn(*, weights: Optional[VGG13_BN_Weights] = None, progress: bool = True, **kwargs: Any) -> VGG:
    """VGG-13-BN from `Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/abs/1409.1556>`__.

    Args:
        weights (:class:`~torchvision.models.VGG13_BN_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.VGG13_BN_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.vgg.VGG``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/vgg.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.VGG13_BN_Weights
        :members:
    """
    weights = VGG13_BN_Weights.verify(weights)

    return _vgg("B", True, weights, progress, **kwargs)
vgg16
vgg16(*, weights: Optional[VGG16_Weights] = None, progress: bool = True, **kwargs: Any) -> VGG

VGG-16 from Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/abs/1409.1556>__.

Parameters:

  • weights

    class:~torchvision.models.VGG16_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.VGG16_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.vgg.VGG base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/vgg.py>_ for more details about this class.

.. autoclass:: torchvision.models.VGG16_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/vgg.py
@handle_legacy_interface(weights=("pretrained", VGG16_Weights.IMAGENET1K_V1))
def vgg16(*, weights: Optional[VGG16_Weights] = None, progress: bool = True, **kwargs: Any) -> VGG:
    """VGG-16 from `Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/abs/1409.1556>`__.

    Args:
        weights (:class:`~torchvision.models.VGG16_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.VGG16_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.vgg.VGG``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/vgg.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.VGG16_Weights
        :members:
    """
    weights = VGG16_Weights.verify(weights)

    return _vgg("D", False, weights, progress, **kwargs)
vgg16_bn
vgg16_bn(*, weights: Optional[VGG16_BN_Weights] = None, progress: bool = True, **kwargs: Any) -> VGG

VGG-16-BN from Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/abs/1409.1556>__.

Parameters:

  • weights

    class:~torchvision.models.VGG16_BN_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.VGG16_BN_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.vgg.VGG base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/vgg.py>_ for more details about this class.

.. autoclass:: torchvision.models.VGG16_BN_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/vgg.py
@handle_legacy_interface(weights=("pretrained", VGG16_BN_Weights.IMAGENET1K_V1))
def vgg16_bn(*, weights: Optional[VGG16_BN_Weights] = None, progress: bool = True, **kwargs: Any) -> VGG:
    """VGG-16-BN from `Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/abs/1409.1556>`__.

    Args:
        weights (:class:`~torchvision.models.VGG16_BN_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.VGG16_BN_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.vgg.VGG``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/vgg.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.VGG16_BN_Weights
        :members:
    """
    weights = VGG16_BN_Weights.verify(weights)

    return _vgg("D", True, weights, progress, **kwargs)
vgg19
vgg19(*, weights: Optional[VGG19_Weights] = None, progress: bool = True, **kwargs: Any) -> VGG

VGG-19 from Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/abs/1409.1556>__.

Parameters:

  • weights

    class:~torchvision.models.VGG19_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.VGG19_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.vgg.VGG base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/vgg.py>_ for more details about this class.

.. autoclass:: torchvision.models.VGG19_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/vgg.py
@handle_legacy_interface(weights=("pretrained", VGG19_Weights.IMAGENET1K_V1))
def vgg19(*, weights: Optional[VGG19_Weights] = None, progress: bool = True, **kwargs: Any) -> VGG:
    """VGG-19 from `Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/abs/1409.1556>`__.

    Args:
        weights (:class:`~torchvision.models.VGG19_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.VGG19_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.vgg.VGG``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/vgg.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.VGG19_Weights
        :members:
    """
    weights = VGG19_Weights.verify(weights)

    return _vgg("E", False, weights, progress, **kwargs)
vgg19_bn
vgg19_bn(*, weights: Optional[VGG19_BN_Weights] = None, progress: bool = True, **kwargs: Any) -> VGG

VGG-19_BN from Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/abs/1409.1556>__.

Parameters:

  • weights

    class:~torchvision.models.VGG19_BN_Weights, optional): The pretrained weights to use. See :class:~torchvision.models.VGG19_BN_Weights below for more details, and possible values. By default, no pre-trained weights are used.

  • progress (bool, default: True ) –

    If True, displays a progress bar of the download to stderr. Default is True.

  • **kwargs (Any, default: {} ) –

    parameters passed to the torchvision.models.vgg.VGG base class. Please refer to the source code <https://github.com/pytorch/vision/blob/main/torchvision/models/vgg.py>_ for more details about this class.

.. autoclass:: torchvision.models.VGG19_BN_Weights :members:

Source code in SaigeToolkit/model/backbone/torchvision/vgg.py
@handle_legacy_interface(weights=("pretrained", VGG19_BN_Weights.IMAGENET1K_V1))
def vgg19_bn(*, weights: Optional[VGG19_BN_Weights] = None, progress: bool = True, **kwargs: Any) -> VGG:
    """VGG-19_BN from `Very Deep Convolutional Networks for Large-Scale Image Recognition <https://arxiv.org/abs/1409.1556>`__.

    Args:
        weights (:class:`~torchvision.models.VGG19_BN_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.VGG19_BN_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.vgg.VGG``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/vgg.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.VGG19_BN_Weights
        :members:
    """
    weights = VGG19_BN_Weights.verify(weights)

    return _vgg("E", True, weights, progress, **kwargs)

van

PyTorch Visual Attention Network (VAN) model.

URL: https://github.com/Visual-Attention-Network/VAN-Classification

model_urls module-attribute
model_urls = {'van_b0': 'https://huggingface.co/Visual-Attention-Network/VAN-Tiny-original/resolve/main/van_tiny_754.pth.tar', 'van_b1': 'https://huggingface.co/Visual-Attention-Network/VAN-Small-original/resolve/main/van_small_811.pth.tar', 'van_b2': 'https://huggingface.co/Visual-Attention-Network/VAN-Base-original/resolve/main/van_base_828.pth.tar', 'van_b3': 'https://huggingface.co/Visual-Attention-Network/VAN-Large-original/resolve/main/van_large_839.pth.tar'}
DWConv
DWConv(dim=768)

Bases: Module

Source code in SaigeToolkit/model/backbone/van.py
def __init__(self, dim=768):
    super(DWConv, self).__init__()
    self.dwconv = nn.Conv2d(dim, dim, 3, 1, 1, bias=True, groups=dim)
dwconv instance-attribute
dwconv = Conv2d(dim, dim, 3, 1, 1, bias=True, groups=dim)
forward
forward(x)
Source code in SaigeToolkit/model/backbone/van.py
def forward(self, x):
    x = self.dwconv(x)
    return x
Mlp
Mlp(in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.0)

Bases: Module

Source code in SaigeToolkit/model/backbone/van.py
def __init__(
    self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.0
):
    super().__init__()
    out_features = out_features or in_features
    hidden_features = hidden_features or in_features
    self.fc1 = nn.Conv2d(in_features, hidden_features, 1)
    self.dwconv = DWConv(hidden_features)
    self.act = act_layer()
    self.fc2 = nn.Conv2d(hidden_features, out_features, 1)
    self.drop = nn.Dropout(drop)
    self.apply(self._init_weights)
fc1 instance-attribute
fc1 = Conv2d(in_features, hidden_features, 1)
dwconv instance-attribute
dwconv = DWConv(hidden_features)
act instance-attribute
act = act_layer()
fc2 instance-attribute
fc2 = Conv2d(hidden_features, out_features, 1)
drop instance-attribute
drop = Dropout(drop)
_init_weights
_init_weights(m)
Source code in SaigeToolkit/model/backbone/van.py
def _init_weights(self, m):
    if isinstance(m, nn.Linear):
        trunc_normal_(m.weight, std=0.02)
        if isinstance(m, nn.Linear) and m.bias is not None:
            nn.init.constant_(m.bias, 0)
    elif isinstance(m, nn.LayerNorm):
        nn.init.constant_(m.bias, 0)
        nn.init.constant_(m.weight, 1.0)
    elif isinstance(m, nn.Conv2d):
        fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
        fan_out //= m.groups
        m.weight.data.normal_(0, math.sqrt(2.0 / fan_out))
        if m.bias is not None:
            m.bias.data.zero_()
forward
forward(x)
Source code in SaigeToolkit/model/backbone/van.py
def forward(self, x):
    x = self.fc1(x)
    x = self.dwconv(x)
    x = self.act(x)
    x = self.drop(x)
    x = self.fc2(x)
    x = self.drop(x)
    return x
LKA
LKA(dim, clip_attn=None)

Bases: Module

Source code in SaigeToolkit/model/backbone/van.py
def __init__(self, dim, clip_attn=None):
    super().__init__()
    self.conv0 = nn.Conv2d(dim, dim, 5, padding=2, groups=dim)
    self.conv_spatial = nn.Conv2d(dim, dim, 7, stride=1, padding=9, groups=dim, dilation=3)
    self.conv1 = nn.Conv2d(dim, dim, 1)
    self.clip_attn = clip_attn
conv0 instance-attribute
conv0 = Conv2d(dim, dim, 5, padding=2, groups=dim)
conv_spatial instance-attribute
conv_spatial = Conv2d(dim, dim, 7, stride=1, padding=9, groups=dim, dilation=3)
conv1 instance-attribute
conv1 = Conv2d(dim, dim, 1)
clip_attn instance-attribute
clip_attn = clip_attn
forward
forward(x)
Source code in SaigeToolkit/model/backbone/van.py
def forward(self, x):
    u = x.clone()
    attn = self.conv0(x)
    attn = self.conv_spatial(attn)
    attn = self.conv1(attn)
    attn = u * attn

    if self.clip_attn is not None:
        attn = torch.clamp(attn, min=-self.clip_attn, max=self.clip_attn)

    return attn
Attention
Attention(d_model, clip_attn=None)

Bases: Module

Source code in SaigeToolkit/model/backbone/van.py
def __init__(self, d_model, clip_attn=None):
    super().__init__()

    self.proj_1 = nn.Conv2d(d_model, d_model, 1)
    self.activation = nn.GELU()
    self.spatial_gating_unit = LKA(d_model, clip_attn=clip_attn)
    self.proj_2 = nn.Conv2d(d_model, d_model, 1)
proj_1 instance-attribute
proj_1 = Conv2d(d_model, d_model, 1)
activation instance-attribute
activation = GELU()
spatial_gating_unit instance-attribute
spatial_gating_unit = LKA(d_model, clip_attn=clip_attn)
proj_2 instance-attribute
proj_2 = Conv2d(d_model, d_model, 1)
forward
forward(x)
Source code in SaigeToolkit/model/backbone/van.py
def forward(self, x):
    shorcut = x.clone()
    x = self.proj_1(x)
    x = self.activation(x)
    x = self.spatial_gating_unit(x)
    x = self.proj_2(x)
    x = x + shorcut
    return x
Block
Block(dim, mlp_ratio=4.0, drop=0.0, drop_path=0.0, act_layer=nn.GELU, clip_attn=None)

Bases: Module

Source code in SaigeToolkit/model/backbone/van.py
def __init__(self, dim, mlp_ratio=4.0, drop=0.0, drop_path=0.0, act_layer=nn.GELU, clip_attn=None):
    super().__init__()
    self.norm1 = nn.BatchNorm2d(dim)
    self.attn = Attention(dim, clip_attn=clip_attn)
    self.drop_path = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()

    self.norm2 = nn.BatchNorm2d(dim)
    mlp_hidden_dim = int(dim * mlp_ratio)
    self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop)
    layer_scale_init_value = 1e-2
    self.layer_scale_1 = nn.Parameter(layer_scale_init_value * torch.ones((dim)), requires_grad=True)
    self.layer_scale_2 = nn.Parameter(layer_scale_init_value * torch.ones((dim)), requires_grad=True)

    self.apply(self._init_weights)
norm1 instance-attribute
norm1 = BatchNorm2d(dim)
attn instance-attribute
attn = Attention(dim, clip_attn=clip_attn)
drop_path instance-attribute
drop_path = DropPath(drop_path) if drop_path > 0.0 else Identity()
norm2 instance-attribute
norm2 = BatchNorm2d(dim)
mlp instance-attribute
mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop)
layer_scale_1 instance-attribute
layer_scale_1 = Parameter(layer_scale_init_value * ones(dim), requires_grad=True)
layer_scale_2 instance-attribute
layer_scale_2 = Parameter(layer_scale_init_value * ones(dim), requires_grad=True)
_init_weights
_init_weights(m)
Source code in SaigeToolkit/model/backbone/van.py
def _init_weights(self, m):
    if isinstance(m, nn.Linear):
        trunc_normal_(m.weight, std=0.02)
        if isinstance(m, nn.Linear) and m.bias is not None:
            nn.init.constant_(m.bias, 0)
    elif isinstance(m, nn.LayerNorm):
        nn.init.constant_(m.bias, 0)
        nn.init.constant_(m.weight, 1.0)
    elif isinstance(m, nn.Conv2d):
        fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
        fan_out //= m.groups
        m.weight.data.normal_(0, math.sqrt(2.0 / fan_out))
        if m.bias is not None:
            m.bias.data.zero_()
forward
forward(x)
Source code in SaigeToolkit/model/backbone/van.py
def forward(self, x):
    x = x + self.drop_path(self.layer_scale_1.unsqueeze(-1).unsqueeze(-1) * self.attn(self.norm1(x)))
    x = x + self.drop_path(self.layer_scale_2.unsqueeze(-1).unsqueeze(-1) * self.mlp(self.norm2(x)))
    return x
OverlapPatchEmbed
OverlapPatchEmbed(img_size=224, patch_size=7, stride=4, in_channels=3, embed_dim=768)

Bases: Module

Image to Patch Embedding

Source code in SaigeToolkit/model/backbone/van.py
def __init__(self, img_size=224, patch_size=7, stride=4, in_channels=3, embed_dim=768):
    super().__init__()
    patch_size = to_2tuple(patch_size)
    self.proj = nn.Conv2d(
        in_channels,
        embed_dim,
        kernel_size=patch_size,
        stride=stride,
        padding=(patch_size[0] // 2, patch_size[1] // 2),
    )
    self.norm = nn.BatchNorm2d(embed_dim)

    self.apply(self._init_weights)
proj instance-attribute
proj = Conv2d(in_channels, embed_dim, kernel_size=patch_size, stride=stride, padding=(patch_size[0] // 2, patch_size[1] // 2))
norm instance-attribute
norm = BatchNorm2d(embed_dim)
_init_weights
_init_weights(m)
Source code in SaigeToolkit/model/backbone/van.py
def _init_weights(self, m):
    if isinstance(m, nn.Linear):
        trunc_normal_(m.weight, std=0.02)
        if isinstance(m, nn.Linear) and m.bias is not None:
            nn.init.constant_(m.bias, 0)
    elif isinstance(m, nn.LayerNorm):
        nn.init.constant_(m.bias, 0)
        nn.init.constant_(m.weight, 1.0)
    elif isinstance(m, nn.Conv2d):
        fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
        fan_out //= m.groups
        m.weight.data.normal_(0, math.sqrt(2.0 / fan_out))
        if m.bias is not None:
            m.bias.data.zero_()
forward
forward(x)
Source code in SaigeToolkit/model/backbone/van.py
def forward(self, x):
    x = self.proj(x)
    _, _, H, W = x.shape
    x = self.norm(x)
    return x, H, W
VAN
VAN(img_size: int = 224, in_channels: int = 3, embed_dims: List[int] = [64, 128, 256, 512], mlp_ratios: List[int] = [4, 4, 4, 4], drop_rate: float = 0.0, drop_path_rate: float = 0.0, norm_layer: Module = nn.LayerNorm, depths: List[int] = [3, 4, 6, 3], num_stages: int = 4, clip_attn: Optional[float] = None)

Bases: Module

Source code in SaigeToolkit/model/backbone/van.py
def __init__(
    self,
    img_size: int = 224,
    in_channels: int = 3,
    embed_dims: List[int] = [64, 128, 256, 512],
    mlp_ratios: List[int] = [4, 4, 4, 4],
    drop_rate: float = 0.0,
    drop_path_rate: float = 0.0,
    norm_layer: nn.Module = nn.LayerNorm,
    depths: List[int] = [3, 4, 6, 3],
    num_stages: int = 4,
    clip_attn: Optional[float] = None,
):
    super().__init__()

    self.depths = depths
    self.num_stages = num_stages
    self.embed_dims = embed_dims

    dpr = [
        x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))
    ]  # stochastic depth decay rule
    cur = 0

    for i in range(num_stages):
        patch_embed = OverlapPatchEmbed(
            img_size=img_size if i == 0 else img_size // (2 ** (i + 1)),
            patch_size=7 if i == 0 else 3,
            stride=4 if i == 0 else 2,
            in_channels=in_channels if i == 0 else embed_dims[i - 1],
            embed_dim=embed_dims[i],
        )

        block = nn.ModuleList(
            [
                Block(
                    dim=embed_dims[i],
                    mlp_ratio=mlp_ratios[i],
                    drop=drop_rate,
                    drop_path=dpr[cur + j],
                    clip_attn=clip_attn,
                )
                for j in range(depths[i])
            ]
        )
        norm = norm_layer(embed_dims[i])
        cur += depths[i]

        setattr(self, f"patch_embed{i + 1}", patch_embed)
        setattr(self, f"block{i + 1}", block)
        setattr(self, f"norm{i + 1}", norm)

    self.apply(self._init_weights)
depths instance-attribute
depths = depths
num_stages instance-attribute
num_stages = num_stages
embed_dims instance-attribute
embed_dims = embed_dims
_init_weights
_init_weights(m)
Source code in SaigeToolkit/model/backbone/van.py
def _init_weights(self, m):
    if isinstance(m, nn.Linear):
        trunc_normal_(m.weight, std=0.02)
        if isinstance(m, nn.Linear) and m.bias is not None:
            nn.init.constant_(m.bias, 0)
    elif isinstance(m, nn.LayerNorm):
        nn.init.constant_(m.bias, 0)
        nn.init.constant_(m.weight, 1.0)
    elif isinstance(m, nn.Conv2d):
        fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
        fan_out //= m.groups
        m.weight.data.normal_(0, math.sqrt(2.0 / fan_out))
        if m.bias is not None:
            m.bias.data.zero_()
freeze_patch_emb
freeze_patch_emb()
Source code in SaigeToolkit/model/backbone/van.py
def freeze_patch_emb(self):
    self.patch_embed1.requires_grad = False
no_weight_decay
no_weight_decay()
Source code in SaigeToolkit/model/backbone/van.py
@torch.jit.ignore
def no_weight_decay(self):
    return {
        "pos_embed1",
        "pos_embed2",
        "pos_embed3",
        "pos_embed4",
        "cls_token",
    }  # has pos_embed may be better
forward
forward(x)
Source code in SaigeToolkit/model/backbone/van.py
def forward(self, x):
    B = x.shape[0]

    for i in range(self.num_stages):
        patch_embed = getattr(self, f"patch_embed{i + 1}")
        block = getattr(self, f"block{i + 1}")
        norm = getattr(self, f"norm{i + 1}")
        x, H, W = patch_embed(x)
        for blk in block:
            x = blk(x)
        x = x.flatten(2).transpose(1, 2)
        x = norm(x)
        if i != self.num_stages - 1:
            x = x.reshape(B, H, W, -1).permute(0, 3, 1, 2).contiguous()

    return x.mean(dim=1)
load_state_dict
load_state_dict(state_dict, strict: bool = True)
Source code in SaigeToolkit/model/backbone/van.py
def load_state_dict(self, state_dict, strict: bool = True):
    extend_state_dict_input_channel(state_dict, "patch_embed1.proj.weight", self.patch_embed1.proj)
    for key in list(state_dict):
        if key.startswith("head."):
            state_dict.pop(key)
    return super().load_state_dict(state_dict, strict)
load_model_weights
load_model_weights(model, arch, kwargs)
Source code in SaigeToolkit/model/backbone/van.py
def load_model_weights(model, arch, kwargs):
    url = model_urls[arch]
    checkpoint = torch.hub.load_state_dict_from_url(url=url, map_location="cpu", check_hash=True)
    strict = True
    if "num_classes" in kwargs and kwargs["num_classes"] != 1000:
        strict = False
        del checkpoint["state_dict"]["head.weight"]
        del checkpoint["state_dict"]["head.bias"]
    model.load_state_dict(checkpoint["state_dict"], strict=strict)
    return model
van_b0
van_b0(pretrained=False, **kwargs)
Source code in SaigeToolkit/model/backbone/van.py
def van_b0(pretrained=False, **kwargs):
    model = VAN(
        embed_dims=[32, 64, 160, 256],
        mlp_ratios=[8, 8, 4, 4],
        norm_layer=partial(nn.LayerNorm, eps=1e-6),
        depths=[3, 3, 5, 2],
        **kwargs,
    )
    model.default_cfg = _cfg()
    if pretrained:
        model = load_model_weights(model, "van_b0", kwargs)
    return model
van_b1
van_b1(pretrained=False, **kwargs)
Source code in SaigeToolkit/model/backbone/van.py
def van_b1(pretrained=False, **kwargs):
    model = VAN(
        embed_dims=[64, 128, 320, 512],
        mlp_ratios=[8, 8, 4, 4],
        norm_layer=partial(nn.LayerNorm, eps=1e-6),
        depths=[2, 2, 4, 2],
        **kwargs,
    )
    model.default_cfg = _cfg()
    if pretrained:
        model = load_model_weights(model, "van_b1", kwargs)
    return model
van_b2
van_b2(pretrained=False, **kwargs)
Source code in SaigeToolkit/model/backbone/van.py
def van_b2(pretrained=False, **kwargs):
    model = VAN(
        embed_dims=[64, 128, 320, 512],
        mlp_ratios=[8, 8, 4, 4],
        norm_layer=partial(nn.LayerNorm, eps=1e-6),
        depths=[3, 3, 12, 3],
        **kwargs,
    )
    model.default_cfg = _cfg()
    if pretrained:
        model = load_model_weights(model, "van_b2", kwargs)
    return model
van_b3
van_b3(pretrained=False, **kwargs)
Source code in SaigeToolkit/model/backbone/van.py
def van_b3(pretrained=False, **kwargs):
    model = VAN(
        embed_dims=[64, 128, 320, 512],
        mlp_ratios=[8, 8, 4, 4],
        norm_layer=partial(nn.LayerNorm, eps=1e-6),
        depths=[3, 5, 27, 3],
        **kwargs,
    )
    model.default_cfg = _cfg()
    if pretrained:
        model = load_model_weights(model, "van_b3", kwargs)
    return model

benchmark

ModelSpecInspector

measure_model_inference_spec classmethod
measure_model_inference_spec(model: Module, batch: int = 1, image_size: int = 256, image_channels: int = 3, warmup: int = 3, device: device = torch.device('cuda'), num_iteration: int = 100)

모델 인퍼런스 시 속도와 메모리 등을 측정합니다.

Source code in SaigeToolkit/model/benchmark.py
@classmethod
def measure_model_inference_spec(
    cls,
    model: torch.nn.Module,
    batch: int = 1,
    image_size: int = 256,
    image_channels: int = 3,
    warmup: int = 3,
    device: torch.device = torch.device("cuda"),
    num_iteration: int = 100,
):
    """모델 인퍼런스 시 속도와 메모리 등을 측정합니다."""
    model.to(device)
    model.eval()

    dummy_input = torch.zeros(batch, image_channels, image_size, image_size)
    dummy_input = dummy_input.to(device)

    timer = Timer(device)

    info = {
        "system": {
            "device": get_torch_device_name(device=device),
            **get_system_info(),
            **get_torch_info(),
        },
        "options": {
            "batch": batch,
            "image_size": image_size,
            "image_channels": image_channels,
            "warmup": warmup,
            "num_iteration": num_iteration,
        },
    }

    try:
        with torch.inference_mode():
            for _ in range(warmup):
                _ = model(dummy_input)

            with timer:
                for _ in range(num_iteration):
                    _ = model(dummy_input)

        total_time = timer.accumulated_time

    except RuntimeError as error:
        if "CUDA out of memory" in str(error):
            total_time = 0
        else:
            raise error

    time_per_batch = total_time / num_iteration
    time_per_image = time_per_batch / batch

    info["results"] = {
        "total_time(ms)": total_time,
        "ms/batch": time_per_batch,
        "ms/image": time_per_image,
        **get_memory_stats(),
    }

    return info
measure_and_save_model_inference_spec_from_config classmethod
measure_and_save_model_inference_spec_from_config(model: Union[str, Dict], json_path: str, model_builder: Callable = build_backbone, **measure_kwargs)

모델 config 또는 config 파일의 경로를 받아서 모델 인퍼런스 스펙 측정 후 json_path에 결과를 저장합니다.

Source code in SaigeToolkit/model/benchmark.py
@classmethod
def measure_and_save_model_inference_spec_from_config(
    cls,
    model: Union[str, Dict],
    json_path: str,
    model_builder: Callable = build_backbone,
    **measure_kwargs,
):
    """모델 config 또는 config 파일의 경로를 받아서 모델 인퍼런스 스펙 측정 후 json_path에 결과를 저장합니다."""
    if isinstance(model, str) and model.lower().endswith((".yml", ".yaml")):
        model = _load_yml(model)
    model_instance = model_builder(**model)
    results = cls.measure_model_inference_spec(model=model_instance, **measure_kwargs)
    results["options"]["model"] = model
    os.makedirs(os.path.dirname(json_path), exist_ok=True)
    with open(json_path, "w") as f:
        json.dump(results, f, sort_keys=False, indent=4)
benchmark_model_inference_spec classmethod
benchmark_model_inference_spec(logdir, add_time_to_logdir: bool = True, image_sizes=[256, 512, 1024, 2048], **measure_kwargs)

여러 이미지 사이즈에 인퍼런스 스펙을 별도 프로세스로 측정하고 logdir 하위에 json들로 저장합니다.

Source code in SaigeToolkit/model/benchmark.py
@classmethod
def benchmark_model_inference_spec(
    cls,
    logdir,
    add_time_to_logdir: bool = True,
    image_sizes=[256, 512, 1024, 2048],
    **measure_kwargs,
):
    """여러 이미지 사이즈에 인퍼런스 스펙을 별도 프로세스로 측정하고 logdir 하위에 json들로 저장합니다."""
    if add_time_to_logdir:
        logdir = os.path.join(logdir, get_time_string())

    json_paths = []
    for image_size in image_sizes:
        json_path = os.path.join(logdir, f"{image_size}.json")
        json_paths.append(json_path)
        process = multiprocessing.Process(
            target=cls.measure_and_save_model_inference_spec_from_config,
            kwargs=dict(json_path=json_path, image_size=image_size, **measure_kwargs),
        )
        process.start()
        process.join()
    return json_paths

feature_extractor

_types module-attribute

_types = {__name__: _d90tfor _type in _types}

ForwardHookFeatureExtractor

ForwardHookFeatureExtractor(model: Module, layers: Dict[str, str])

Bases: Module

Extract intermediate features from the model with forward hooks.

return example: {"layer1": layer1_output, "layer2": layer2_output, ..}

Note
  • DOES NOT work with DataParallel. forward input이 단일 tensor가 아닌 경우 device로 분류하는 기존 방식이 동작하지 않기 때문에 제거함.
  • DOES work with DistributedDataParallel.
Source code in SaigeToolkit/model/feature_extractor.py
def __init__(
    self,
    model: nn.Module,
    layers: Dict[str, str],
):
    super().__init__()

    self.model = model
    self.layers = layers
    self._outputs = []

    for layer_name, output_name in self.layers.items():
        hook = self._get_hook_function(output_name)
        self.model.get_submodule(layer_name).register_forward_hook(hook)
model instance-attribute
model = model
layers instance-attribute
layers = layers
_outputs instance-attribute
_outputs = []
_get_hook_function
_get_hook_function(output_name: str)
Source code in SaigeToolkit/model/feature_extractor.py
def _get_hook_function(self, output_name: str):
    def _hook(module, input, output):
        if not self._outputs:
            self._outputs.append({})
        self._outputs[0][output_name] = output

    return _hook
forward
forward(input: Any) -> Dict[str, Any]
Source code in SaigeToolkit/model/feature_extractor.py
def forward(self, input: Any) -> Dict[str, Any]:
    _ = self.model(input)
    return self._outputs.pop()

build_feature_extractor

build_feature_extractor(backbone: Union[dict, Module], _target_: str = 'create_feature_extractor', layers: Optional[List[str]] = None, **params) -> Module

Build feature extractor

Parameters:

  • backbone (dict | Module) –

    backbone config.

  • _target_ (str, default: 'create_feature_extractor' ) –

    feature extractor target. Defaults to "create_feature_extractor".

  • layers (List[str], default: None ) –

    feature layers to extract. Defaults to None.

Returns:

  • Module

    nn.Module: feature extractor

Note
  • ForwardHookFeatureExtractor 의 경우 hook을 사용하므로 기존 모델과의 동일성이 보장되지만 불필요한 레이어까지 모두 사용하게됨.
  • IntermediateLayerGetter 의 경우 타겟 레이어 이후의 레이어를 제거해 효율적. 하지만 기존 모델의 forward가 복잡한 경우 동일성이 보장되지 않을 수 있음.
  • create_feature_extractor 의 경우 연산 그래프를 고려하여 타겟 레이어 이외의 레이어를 제거해 동일성과 효율성 측면에서 좋음. 하지만 timm 모델은 사용할 수 없는듯.
Source code in SaigeToolkit/model/feature_extractor.py
def build_feature_extractor(
    backbone: Union[dict, nn.Module],
    _target_: str = "create_feature_extractor",
    layers: Optional[List[str]] = None,
    **params,
) -> nn.Module:
    """Build feature extractor

    Args:
        backbone (dict | nn.Module): backbone config.
        _target_ (str, optional): feature extractor target. Defaults to "create_feature_extractor".
        layers (List[str], optional): feature layers to extract. Defaults to None.

    Returns:
        nn.Module: feature extractor

    Note:
        - `ForwardHookFeatureExtractor` 의 경우 hook을 사용하므로 기존 모델과의 동일성이 보장되지만 불필요한 레이어까지 모두 사용하게됨.
        - `IntermediateLayerGetter` 의 경우 타겟 레이어 이후의 레이어를 제거해 효율적. 하지만 기존 모델의 forward가 복잡한 경우 동일성이 보장되지 않을 수 있음.
        - `create_feature_extractor` 의 경우 연산 그래프를 고려하여 타겟 레이어 이외의 레이어를 제거해 동일성과 효율성 측면에서 좋음. 하지만 timm 모델은 사용할 수 없는듯.

    """
    if layers is None:
        layers = ["layer1", "layer2", "layer3"]

    if isinstance(backbone, dict):
        backbone = build_backbone(**backbone)
    return_nodes = {layer: layer for layer in layers}
    return _types[_target_](backbone, return_nodes, **params)

neck

builder

logger module-attribute
logger = getLogger('SaigeResearch')
build_neck
build_neck(_target_: str, **params) -> Module

build network 'neck' part, such as FPN "Feature Pyramid Networks for Object Detection".

Parameters:

  • cfg_neck (dict) –

    configuration parameters for building 'neck' network

Returns:

  • Module

    nn.Module: 'neck' network

Source code in SaigeToolkit/model/neck/builder.py
def build_neck(_target_: str, **params) -> nn.Module:
    """build network 'neck' part, such as FPN
    ["Feature Pyramid Networks for Object Detection"](https://arxiv.org/abs/1612.03144).

    Args:
        cfg_neck (dict): configuration parameters for building 'neck' network

    Returns:
        nn.Module: 'neck' network

    """
    _types = get_neck_list()
    if _target_ not in _types:
        raise NotImplementedError(f"NECK {_target_} not implemented")
    logger.info(f'[{"NECK".center(9)}] {_target_} [params] {params}')
    return _types[_target_](**params)
get_neck_class
get_neck_class(cfg_neck_name: str) -> Type[Module]

returns neck network class.

Parameters:

  • cfg_neck_name (str) –

    neck network class name

Returns:

  • Type[Module]

    Type[nn.Module]: neck network class

Author

Sukho Yoon

Source code in SaigeToolkit/model/neck/builder.py
def get_neck_class(cfg_neck_name: str) -> Type[nn.Module]:
    """returns neck network class.

    Args:
        cfg_neck_name (str): neck network class name

    Returns:
        Type[nn.Module]: neck network class

    Author:
        Sukho Yoon
    """
    try:
        return get_neck_list()[cfg_neck_name]
    except:
        raise (f"Dataset {cfg_neck_name} not available")
get_neck_list
get_neck_list()
Source code in SaigeToolkit/model/neck/builder.py
def get_neck_list():
    return {
        "db": FPN_DB,
        "fpem": FPEM_FFM,
        "fpn": FPN,
        "bifpn": BiFPN,
    }

fpem

FPEM_FFM
FPEM_FFM(backbone_out_channels: Optional[List[int]] = None, **kwargs)

Bases: Module

PANnet :param backbone_out_channels: 基础网络输出的维度

Source code in SaigeToolkit/model/neck/fpem.py
def __init__(self, backbone_out_channels: Optional[List[int]] = None, **kwargs):
    """
    PANnet
    :param backbone_out_channels: 基础网络输出的维度
    """
    super().__init__()
    if backbone_out_channels is None:
        backbone_out_channels = [64, 128, 256, 512]

    fpem_repeat = kwargs.get("fpem_repeat", 2)
    conv_out = 64
    # reduce layers
    self.reduce_conv_c2 = nn.Sequential(
        nn.Conv2d(in_channels=backbone_out_channels[0], out_channels=conv_out, kernel_size=1),
        nn.BatchNorm2d(conv_out),
        nn.ReLU(),
    )
    self.reduce_conv_c3 = nn.Sequential(
        nn.Conv2d(in_channels=backbone_out_channels[1], out_channels=conv_out, kernel_size=1),
        nn.BatchNorm2d(conv_out),
        nn.ReLU(),
    )
    self.reduce_conv_c4 = nn.Sequential(
        nn.Conv2d(in_channels=backbone_out_channels[2], out_channels=conv_out, kernel_size=1),
        nn.BatchNorm2d(conv_out),
        nn.ReLU(),
    )
    self.reduce_conv_c5 = nn.Sequential(
        nn.Conv2d(in_channels=backbone_out_channels[3], out_channels=conv_out, kernel_size=1),
        nn.BatchNorm2d(conv_out),
        nn.ReLU(),
    )
    self.fpems = nn.ModuleList()
    for _ in range(fpem_repeat):
        self.fpems.append(FPEM(conv_out))
    self.out_conv = nn.Conv2d(in_channels=conv_out * 4, out_channels=6, kernel_size=1)
reduce_conv_c2 instance-attribute
reduce_conv_c2 = Sequential(Conv2d(in_channels=backbone_out_channels[0], out_channels=conv_out, kernel_size=1), BatchNorm2d(conv_out), ReLU())
reduce_conv_c3 instance-attribute
reduce_conv_c3 = Sequential(Conv2d(in_channels=backbone_out_channels[1], out_channels=conv_out, kernel_size=1), BatchNorm2d(conv_out), ReLU())
reduce_conv_c4 instance-attribute
reduce_conv_c4 = Sequential(Conv2d(in_channels=backbone_out_channels[2], out_channels=conv_out, kernel_size=1), BatchNorm2d(conv_out), ReLU())
reduce_conv_c5 instance-attribute
reduce_conv_c5 = Sequential(Conv2d(in_channels=backbone_out_channels[3], out_channels=conv_out, kernel_size=1), BatchNorm2d(conv_out), ReLU())
fpems instance-attribute
fpems = ModuleList()
out_conv instance-attribute
out_conv = Conv2d(in_channels=conv_out * 4, out_channels=6, kernel_size=1)
forward
forward(x)
Source code in SaigeToolkit/model/neck/fpem.py
def forward(self, x):
    c2, c3, c4, c5 = x
    # reduce channel
    c2 = self.reduce_conv_c2(c2)
    c3 = self.reduce_conv_c3(c3)
    c4 = self.reduce_conv_c4(c4)
    c5 = self.reduce_conv_c5(c5)

    # FPEM
    for i, fpem in enumerate(self.fpems):
        c2, c3, c4, c5 = fpem(c2, c3, c4, c5)
        if i == 0:
            c2_ffm = c2
            c3_ffm = c3
            c4_ffm = c4
            c5_ffm = c5
        else:
            c2_ffm += c2
            c3_ffm += c3
            c4_ffm += c4
            c5_ffm += c5

    # # FFM
    # c5 = F.interpolate(c5_ffm, c2_ffm.size()[-2:], mode="bilinear")
    # c4 = F.interpolate(c4_ffm, c2_ffm.size()[-2:], mode="bilinear")
    # c3 = F.interpolate(c3_ffm, c2_ffm.size()[-2:], mode="bilinear")
    # Fy = torch.cat([c2_ffm, c3, c4, c5], dim=1)
    # y = self.out_conv(Fy)
    return c2_ffm, c3_ffm, c4_ffm, c5_ffm
FPEM
FPEM(in_channels=128)

Bases: Module

Source code in SaigeToolkit/model/neck/fpem.py
def __init__(self, in_channels=128):
    super().__init__()
    self.up_add1 = SeparableConv2d(in_channels, in_channels, 1)
    self.up_add2 = SeparableConv2d(in_channels, in_channels, 1)
    self.up_add3 = SeparableConv2d(in_channels, in_channels, 1)
    self.down_add1 = SeparableConv2d(in_channels, in_channels, 2)
    self.down_add2 = SeparableConv2d(in_channels, in_channels, 2)
    self.down_add3 = SeparableConv2d(in_channels, in_channels, 2)
up_add1 instance-attribute
up_add1 = SeparableConv2d(in_channels, in_channels, 1)
up_add2 instance-attribute
up_add2 = SeparableConv2d(in_channels, in_channels, 1)
up_add3 instance-attribute
up_add3 = SeparableConv2d(in_channels, in_channels, 1)
down_add1 instance-attribute
down_add1 = SeparableConv2d(in_channels, in_channels, 2)
down_add2 instance-attribute
down_add2 = SeparableConv2d(in_channels, in_channels, 2)
down_add3 instance-attribute
down_add3 = SeparableConv2d(in_channels, in_channels, 2)
forward
forward(c2, c3, c4, c5)
Source code in SaigeToolkit/model/neck/fpem.py
def forward(self, c2, c3, c4, c5):
    # up阶段
    c4 = self.up_add1(self._upsample_add(c5, c4))
    c3 = self.up_add2(self._upsample_add(c4, c3))
    c2 = self.up_add3(self._upsample_add(c3, c2))

    # down 阶段
    c3 = self.down_add1(self._upsample_add(c3, c2))
    c4 = self.down_add2(self._upsample_add(c4, c3))
    c5 = self.down_add3(self._upsample_add(c5, c4))
    return c2, c3, c4, c5
_upsample_add
_upsample_add(x, y)
Source code in SaigeToolkit/model/neck/fpem.py
def _upsample_add(self, x, y):
    return F.interpolate(x, size=y.size()[2:], mode="bilinear") + y
SeparableConv2d
SeparableConv2d(in_channels, out_channels, stride=1)

Bases: Module

Source code in SaigeToolkit/model/neck/fpem.py
def __init__(self, in_channels, out_channels, stride=1):
    super(SeparableConv2d, self).__init__()

    self.depthwise_conv = nn.Conv2d(
        in_channels=in_channels,
        out_channels=in_channels,
        kernel_size=3,
        padding=1,
        stride=stride,
        groups=in_channels,
    )
    self.pointwise_conv = nn.Conv2d(
        in_channels=in_channels, out_channels=out_channels, kernel_size=1
    )
    self.bn = nn.BatchNorm2d(out_channels)
    self.relu = nn.ReLU()
depthwise_conv instance-attribute
depthwise_conv = Conv2d(in_channels=in_channels, out_channels=in_channels, kernel_size=3, padding=1, stride=stride, groups=in_channels)
pointwise_conv instance-attribute
pointwise_conv = Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1)
bn instance-attribute
bn = BatchNorm2d(out_channels)
relu instance-attribute
relu = ReLU()
forward
forward(x)
Source code in SaigeToolkit/model/neck/fpem.py
def forward(self, x):
    x = self.depthwise_conv(x)
    x = self.pointwise_conv(x)
    x = self.bn(x)
    x = self.relu(x)
    return x

fpn

ConvBlock
ConvBlock(in_channels: int, out_channels: int, kernel_size: int = 1, stride: int = 1, padding: int = 0)

Bases: Module

ConvBlock defined, which consists of Convolution, BatchNorm, ReLU

Attributes:

  • conv (Conv2d) –

    convolution layer for ConvBlock

  • bn (BatchNorm2d) –

    2d batch norm layer for ConvBlock

  • act (ReLU) –

    ReLU activation function for ConvBlock

Author

Sukho Yoon

initializing ConvBlock

Parameters:

  • in_channels (int) –

    'input channel' for conv layer

  • out_channels (int) –

    'output channel' for conv layer, which is also 'input channel' for bn layer

  • kernel_size (int, default: 1 ) –

    kernel size for conv layer. Defaults to 1.

  • stride (int, default: 1 ) –

    stride for conv layer. Defaults to 1.

  • padding (int, default: 0 ) –

    padding for conv layer. Defaults to 0.

Source code in SaigeToolkit/model/neck/fpn.py
def __init__(
    self,
    in_channels: int,
    out_channels: int,
    kernel_size: int = 1,
    stride: int = 1,
    padding: int = 0,
) -> None:
    """initializing ConvBlock

    Args:
        in_channels (int): 'input channel' for conv layer
        out_channels (int): 'output channel' for conv layer, which is also 'input channel' for bn layer
        kernel_size (int, optional): kernel size for conv layer. Defaults to 1.
        stride (int, optional): stride for conv layer. Defaults to 1.
        padding (int, optional): padding for conv layer. Defaults to 0.
    """
    super(ConvBlock, self).__init__()
    self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=stride, padding=padding)
    self.bn = nn.BatchNorm2d(out_channels)
    self.act = nn.ReLU()
conv instance-attribute
conv = Conv2d(in_channels, out_channels, kernel_size, stride=stride, padding=padding)
bn instance-attribute
bn = BatchNorm2d(out_channels)
act instance-attribute
act = ReLU()
forward
forward(inputs: Tensor) -> Tensor

forward function for nn.Module class

Parameters:

  • inputs (Tensor) –

    input tensor for ConvBlock

Returns:

  • Tensor

    torch.Tensor: output tensor for ConvBlock

Source code in SaigeToolkit/model/neck/fpn.py
def forward(self, inputs: torch.Tensor) -> torch.Tensor:
    """forward function for nn.Module class

    Args:
        inputs (torch.Tensor): input tensor for ConvBlock

    Returns:
        torch.Tensor: output tensor for ConvBlock
    """
    x = self.conv(inputs)
    x = self.bn(x)
    return self.act(x)
FpnBlock
FpnBlock(in_channels: Union[List[int], int, None] = [64, 128, 256, 512], out_channels: Union[List[int], int, None] = None, channel_fpn: int = 64, num_feat: int = 4)

Bases: Module

block for constructing FeaturePyramidNetwork.

Attributes:

  • block_in (ModuleList) –

    Convolution layer, that backbone output feature is directly applied

  • block_out (ModuleList) –

    Convolution layer, that tensors from 'block_in' is applied after 'upsampled and sumed'

initializing FpnBlock

Parameters:

  • in_channels (Union[List[int], int, None], default: [64, 128, 256, 512] ) –

    input channels for block_in. Defaults to [64, 128, 256, 512].

  • out_channels (Union[List[int], int, None], default: None ) –

    output channels for block_out. Defaults to None.

  • channel_fpn (int, default: 64 ) –

    output channels for block_in, also input channels for block_out. Defaults to 64.

  • num_feat (int, default: 4 ) –

    number of layer of block_in/block_out. Defaults to 4.

Source code in SaigeToolkit/model/neck/fpn.py
def __init__(
    self,
    in_channels: Union[List[int], int, None] = [64, 128, 256, 512],
    out_channels: Union[List[int], int, None] = None,
    channel_fpn: int = 64,
    num_feat: int = 4,
) -> None:
    """initializing FpnBlock

    Args:
        in_channels (Union[List[int], int, None], optional):
            input channels for block_in. Defaults to [64, 128, 256, 512].
        out_channels (Union[List[int], int, None], optional):
            output channels for block_out. Defaults to None.
        channel_fpn (int, optional):
            output channels for block_in, also input channels for block_out. Defaults to 64.
        num_feat (int, optional): number of layer of block_in/block_out. Defaults to 4.
    """
    super(FpnBlock, self).__init__()

    if in_channels is None:
        in_channels = [channel_fpn for _ in range(num_feat)]
    elif type(in_channels) == int:
        in_channels = [in_channels for _ in range(num_feat)]
    else:
        assert type(in_channels) == list, f"wrong type of in_channels: {type(in_channels)}"

    if out_channels is None:
        out_channels = [channel_fpn for _ in range(num_feat)]
    elif type(out_channels) == int:
        out_channels = [out_channels for _ in range(num_feat)]
    else:
        assert type(out_channels) == list, f"wrong type of out_channels: {type(in_channels)}"

    self.block_in, self.block_out = [], []

    for c_in, c_out in zip(in_channels, out_channels):
        self.block_in.append(nn.Conv2d(c_in, channel_fpn, kernel_size=1, stride=1, padding=0))
        self.block_out.append(nn.Conv2d(channel_fpn, c_out, kernel_size=3, stride=1, padding=1))

    self.block_in = nn.ModuleList(self.block_in)
    self.block_out = nn.ModuleList(self.block_out)
block_in instance-attribute
block_in = ModuleList(block_in)
block_out instance-attribute
block_out = ModuleList(block_out)
_upsample_add
_upsample_add(x: Tensor, y: Tensor) -> Tensor

upsample intput x to size of input y, and sum both inputs

Parameters:

  • x (Tensor) –

    smaller (size) input

  • y (Tensor) –

    larger (size) input

Returns:

  • Tensor

    torch.Tensor: upsampled and sumed tensor

Source code in SaigeToolkit/model/neck/fpn.py
def _upsample_add(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
    """upsample intput `x` to `size of input y`, and sum both inputs

    Args:
        x (torch.Tensor): smaller (size) input
        y (torch.Tensor): larger (size) input

    Returns:
        torch.Tensor: upsampled and sumed tensor
    """
    _, _, H, W = y.size()
    return F.upsample(x, size=(H, W), mode="bilinear") + y
forward
forward(features: List[Tensor]) -> List[Tensor]

forward function for nn.Module class

Parameters:

  • features (List[Tensor]) –

    list of tensors from backbone network

Returns:

  • List[Tensor]

    List[torch.Tensor]: list of tensors processed after FpnBlock

Source code in SaigeToolkit/model/neck/fpn.py
def forward(self, features: List[torch.Tensor]) -> List[torch.Tensor]:
    """forward function for nn.Module class

    Args:
        features (List[torch.Tensor]): list of tensors from backbone network

    Returns:
        List[torch.Tensor]: list of tensors processed after FpnBlock
    """
    ls_out = []
    ls_out.insert(0, self.block_in[-1](features.pop(-1)))
    for i in range(len(features) - 1, -1, -1):
        ls_out.insert(0, self._upsample_add(ls_out[0], self.block_in[i](features.pop(-1))))

    for i_o in range(len(ls_out)):
        ls_out[i_o] = self.block_out[i_o](ls_out[i_o])

    return ls_out
FPN
FPN(in_channels: Optional[List[int]] = None, out_channels: int = 64, channel_fpn: int = 256, num_feat: int = 4, num_block: int = 1)

Bases: Module

"Feature Pyramid Networks for Object Detection".

Attributes:

  • block_fpn (ModuleList) –

    torch module list, consists of FpnBlocks.

initializing Feature Pyramid Networks

Parameters:

  • in_channels (List[int], default: None ) –

    channels from backbone network output tensors. Defaults to None.

  • out_channels (int, default: 64 ) –

    final output channels for FPN. Defaults to 64.

  • channel_fpn (int, default: 256 ) –

    inner channels for FpnBlocks. Defaults to 256.

  • num_feat (int, default: 4 ) –

    number of blocks inside of FpnBlocks. Defaults to 4.

  • num_block (int, default: 1 ) –

    number of FpnBlocks. Defaults to 1.

Source code in SaigeToolkit/model/neck/fpn.py
def __init__(
    self,
    in_channels: Optional[List[int]] = None,
    out_channels: int = 64,
    channel_fpn: int = 256,
    num_feat: int = 4,
    num_block: int = 1,
) -> None:
    """initializing Feature Pyramid Networks

    Args:
        in_channels (List[int], optional): channels from backbone network output tensors. Defaults to None.
        out_channels (int, optional): final output channels for FPN. Defaults to 64.
        channel_fpn (int, optional): inner channels for FpnBlocks. Defaults to 256.
        num_feat (int, optional): number of blocks inside of FpnBlocks. Defaults to 4.
        num_block (int, optional): number of FpnBlocks. Defaults to 1.
    """

    super(FPN, self).__init__()

    if in_channels is None:
        in_channels = [64, 128, 256, 512]

    self.block_fpn = []
    for i_nb in range(num_block):
        c_in = in_channels if i_nb == 0 else None
        c_out = out_channels if i_nb == num_block - 1 else None

        self.block_fpn.append(FpnBlock(c_in, c_out, channel_fpn, num_feat))

    self.block_fpn = nn.ModuleList(self.block_fpn)
block_fpn instance-attribute
block_fpn = ModuleList(block_fpn)
forward
forward(features: List[Tensor]) -> List[Tensor]

forward function for FPN

Parameters:

  • features (List[Tensor]) –

    output tensors from backbone network.

Returns:

  • List[Tensor]

    List[torch.Tensor]: output tensors from FPN.

Source code in SaigeToolkit/model/neck/fpn.py
def forward(self, features: List[torch.Tensor]) -> List[torch.Tensor]:
    """forward function for FPN

    Args:
        features (List[torch.Tensor]): output tensors from backbone network.

    Returns:
        List[torch.Tensor]: output tensors from FPN.
    """
    for b_fpn in self.block_fpn:
        features = b_fpn(features)

    return features
BiFpnBlock
BiFpnBlock(channel_fpn: int = 256, num_feat: int = 4, epsilon: float = 1e-06)

Bases: Module

block for constructing BidirectionalFeaturePyramidNetwork.

Attributes:

  • epsilon (float) –

    epsilon value for BiFpnBlock

  • block_td (ModuleList) –

    blocks for upsample_weighted_sum

  • block_out (ModuleList) –

    blocks for downsample_weighted_sum

  • w1 (Parameter) –

    weight parameter for block_td

  • w1_relu (ReLU) –

    ReLU layer for w1 parameter

  • w2 (Parameter) –

    weight parameter for block_out

  • w2_relu (ReLU) –

    ReLU layer for w2 parameter

initializing BiFpnBlock

Parameters:

  • channel_fpn (int, default: 256 ) –

    input/output channels for all blocks. Defaults to 256.

  • num_feat (int, default: 4 ) –

    number of each blocks. Defaults to 4.

  • epsilon (float, default: 1e-06 ) –

    epsilon value for attribute self.epsilon. Defaults to 1e-6.

Source code in SaigeToolkit/model/neck/fpn.py
def __init__(self, channel_fpn: int = 256, num_feat: int = 4, epsilon: float = 1e-6) -> None:
    """initializing BiFpnBlock

    Args:
        channel_fpn (int, optional): input/output channels for all blocks. Defaults to 256.
        num_feat (int, optional): number of each blocks. Defaults to 4.
        epsilon (float, optional): epsilon value for attribute `self.epsilon`. Defaults to 1e-6.
    """
    super(BiFpnBlock, self).__init__()
    self.epsilon = epsilon

    self.block_td = nn.ModuleList([ConvBlock(channel_fpn, channel_fpn) for _ in range(num_feat - 1)])
    self.block_out = nn.ModuleList(
        [ConvBlock(channel_fpn, channel_fpn) for _ in range(num_feat - 1)]
    )

    self.w1 = nn.Parameter(torch.ones((num_feat - 1, 2)))
    self.w1_relu = nn.ReLU()
    self.w2 = nn.Parameter(torch.ones((num_feat - 1, 3)))
    self.w2_relu = nn.ReLU()
epsilon instance-attribute
epsilon = epsilon
block_td instance-attribute
block_td = ModuleList([ConvBlock(channel_fpn, channel_fpn) for _ in range(num_feat - 1)])
block_out instance-attribute
block_out = ModuleList([ConvBlock(channel_fpn, channel_fpn) for _ in range(num_feat - 1)])
w1 instance-attribute
w1 = Parameter(ones((num_feat - 1, 2)))
w1_relu instance-attribute
w1_relu = ReLU()
w2 instance-attribute
w2 = Parameter(ones((num_feat - 1, 3)))
w2_relu instance-attribute
w2_relu = ReLU()
forward
forward(inputs: List[Tensor]) -> List[Tensor]

forward function for nn.Module

Parameters:

  • inputs (List[Tensor]) –

    list of tensors from former network

Returns:

  • List[Tensor]

    List[torch.Tensor]: output list of tensors from BiFpnBlock

Source code in SaigeToolkit/model/neck/fpn.py
def forward(self, inputs: List[torch.Tensor]) -> List[torch.Tensor]:
    """forward function for nn.Module

    Args:
        inputs (List[torch.Tensor]): list of tensors from former network

    Returns:
        List[torch.Tensor]: output list of tensors from BiFpnBlock
    """

    w1 = self.w1_relu(self.w1)
    w1 /= torch.sum(w1, dim=0) + self.epsilon
    w2 = self.w2_relu(self.w2)
    w2 /= torch.sum(w2, dim=0) + self.epsilon

    p_td = [inputs[-1]]

    for i_td, b_td in enumerate(self.block_td):
        p_td.insert(0, b_td(self._upsample_weighted_sum(inputs[-2 - i_td], p_td[0], w1[i_td])))

    p_out = [p_td.pop(0)]
    for i_out, b_out in enumerate(self.block_out):
        p_out.append(
            b_out(self._downsample_weighted_sum(inputs.pop(1), p_td.pop(0), p_out[-1], w2[i_out]))
        )

    return p_out
_upsample_weighted_sum
_upsample_weighted_sum(x: Tensor, y: Tensor, w: Parameter) -> Tensor

upsample input x and weighted sum with input y

Parameters:

  • x (Tensor) –

    smaller (size) input

  • y (Tensor) –

    larger (size) input

  • w (Parameter) –

    weight parameter

Returns:

  • Tensor

    torch.Tensor: 'upsample_weight_sum'ed output

Source code in SaigeToolkit/model/neck/fpn.py
def _upsample_weighted_sum(self, x: torch.Tensor, y: torch.Tensor, w: nn.Parameter) -> torch.Tensor:
    """upsample `input x` and weighted sum with `input y`

    Args:
        x (torch.Tensor): smaller (size) input
        y (torch.Tensor): larger (size) input
        w (nn.Parameter): weight parameter

    Returns:
        torch.Tensor: 'upsample_weight_sum'ed output
    """
    _, _, H, W = x.size()
    return w[0] * x + w[1] * F.interpolate(y, size=(H, W), mode="bilinear", align_corners=True)
_downsample_weighted_sum
_downsample_weighted_sum(x: Tensor, y: Tensor, z: Tensor, w: Parameter) -> Tensor

downsample input z and weighted sum with input x and input y

Parameters:

  • x (Tensor) –

    smaller (size) input

  • y (Tensor) –

    smaller (size) input

  • z (Tensor) –

    larger (size) input

  • w (Parameter) –

    weight parameter

Returns:

  • Tensor

    torch.Tensor: 'downsample_weight_sum'ed output

Source code in SaigeToolkit/model/neck/fpn.py
def _downsample_weighted_sum(
    self, x: torch.Tensor, y: torch.Tensor, z: torch.Tensor, w: nn.Parameter
) -> torch.Tensor:
    """downsample `input z` and weighted sum with `input x` and `input y`

    Args:
        x (torch.Tensor): smaller (size) input
        y (torch.Tensor): smaller (size) input
        z (torch.Tensor): larger (size) input
        w (nn.Parameter): weight parameter

    Returns:
        torch.Tensor: 'downsample_weight_sum'ed output
    """
    _, _, H, W = x.size()
    return (
        w[0] * x
        + w[1] * y
        + w[2] * F.interpolate(z, size=(H, W), mode="bilinear", align_corners=True)
    )
BiFPN
BiFPN(in_channels: Optional[List[int]] = None, out_channels: int = 64, channel_fpn: int = 256, num_feat: int = 4, num_block: int = 2)

Bases: Module

"BidirectionalFeaturePyramidNetwork".

Attribute

block_in (nn.ModuleList): list of conv layers, which transfers input channels into fixed channel_fpn block_ex (nn.ModuleList): list of ConvBlock, residual blocks block_fpn (nn.ModuleList): list of BiFpnBlocks block_rtn (nn.ModuleList): list of ConvBlock, refining BiFpnBlock output

initializing Bidirectional FPN

Args:

in_channels (List[int], optional): channels from backbone network output tensors. Defaults to None.
out_channels (int, optional): final output channels for FPN. Defaults to 64.
channel_fpn (int, optional): inner channels for BiFpnBlocks. Defaults to 256.
num_feat (int, optional): number of blocks inside of BiFpnBlocks. Defaults to 4.
num_block (int, optional): number of FpnBlocks. Defaults to 1.
Source code in SaigeToolkit/model/neck/fpn.py
def __init__(
    self,
    in_channels: Optional[List[int]] = None,
    out_channels: int = 64,
    channel_fpn: int = 256,
    num_feat: int = 4,
    num_block: int = 2,
) -> None:
    """initializing Bidirectional FPN

    Args:

        in_channels (List[int], optional): channels from backbone network output tensors. Defaults to None.
        out_channels (int, optional): final output channels for FPN. Defaults to 64.
        channel_fpn (int, optional): inner channels for BiFpnBlocks. Defaults to 256.
        num_feat (int, optional): number of blocks inside of BiFpnBlocks. Defaults to 4.
        num_block (int, optional): number of FpnBlocks. Defaults to 1.
    """

    super(BiFPN, self).__init__()

    if in_channels is None:
        in_channels = [64, 128, 256, 512]
    num_feat = len(in_channels) if num_feat is None else num_feat

    self.block_in = []
    for c in in_channels:
        self.block_in.append(nn.Conv2d(c, channel_fpn, kernel_size=1, stride=1, padding=0))
    self.block_in = nn.ModuleList(self.block_in)

    ex_channels = [in_channels[-1]] + [channel_fpn for _ in range(num_feat - 1 - len(in_channels))]
    ex_channels = ex_channels[: num_feat - len(in_channels)]

    self.block_ex = []
    for c in ex_channels:
        self.block_ex.append(ConvBlock(c, channel_fpn, kernel_size=3, stride=2, padding=1))
    self.block_ex = nn.ModuleList(self.block_ex)

    self.block_fpn = nn.ModuleList([BiFpnBlock(channel_fpn, num_feat) for _ in range(num_block)])

    self.block_rtn = []
    if out_channels != channel_fpn:
        for _ in range(num_feat):
            self.block_rtn.append(
                nn.Conv2d(channel_fpn, out_channels, kernel_size=1, stride=1, padding=0)
            )
    self.block_rtn = nn.ModuleList(self.block_rtn)
block_in instance-attribute
block_in = ModuleList(block_in)
block_ex instance-attribute
block_ex = ModuleList(block_ex)
block_fpn instance-attribute
block_fpn = ModuleList([BiFpnBlock(channel_fpn, num_feat) for _ in range(num_block)])
block_rtn instance-attribute
block_rtn = ModuleList(block_rtn)
forward
forward(inputs: List[Tensor]) -> List[Tensor]

forward function for FPN

Parameters:

  • inputs (List[Tensor]) –

    output tensors from backbone network.

Returns:

  • List[Tensor]

    List[torch.Tensor]: output tensors from BiFPN.

Source code in SaigeToolkit/model/neck/fpn.py
def forward(self, inputs: List[torch.Tensor]) -> List[torch.Tensor]:
    """forward function for FPN

    Args:
        inputs (List[torch.Tensor]): output tensors from backbone network.

    Returns:
        List[torch.Tensor]: output tensors from BiFPN.
    """
    assert len(inputs) == len(self.block_in), "channel number does not fit"

    p_ls = []
    for i_p in range(len(inputs)):
        p_ls.append(self.block_in[i_p](inputs[i_p]))

    if self.block_ex:
        p_ls.append(self.block_ex[0](inputs[-1]))
        for b_ex in self.block_ex[1:]:
            p_ls.append(b_ex(p_ls[-1]))

    for b_fpn in self.block_fpn:
        p_ls = b_fpn(p_ls)

    if self.block_rtn:
        for i_br in range(len(self.block_rtn)):
            p_ls[i_br] = self.block_rtn[i_br](p_ls[i_br])

    return p_ls
FPN_DB
FPN_DB(in_channels: Optional[List[int]] = None, inner_channels: int = 256, bias: bool = False, *args, **kwargs)

Bases: Module

bias: Whether conv layers have bias or not.

Source code in SaigeToolkit/model/neck/fpn.py
def __init__(
    self,
    in_channels: Optional[List[int]] = None,
    inner_channels: int = 256,
    bias: bool = False,
    *args,
    **kwargs,
):
    """
    bias: Whether conv layers have bias or not.
    """
    super(FPN_DB, self).__init__()

    if in_channels is None:
        in_channels = [64, 128, 256, 512]

    self.up5 = nn.Upsample(scale_factor=2, mode="nearest")
    self.up4 = nn.Upsample(scale_factor=2, mode="nearest")
    self.up3 = nn.Upsample(scale_factor=2, mode="nearest")

    self.in5 = nn.Conv2d(in_channels[-1], inner_channels, 1, bias=bias)
    self.in4 = nn.Conv2d(in_channels[-2], inner_channels, 1, bias=bias)
    self.in3 = nn.Conv2d(in_channels[-3], inner_channels, 1, bias=bias)
    self.in2 = nn.Conv2d(in_channels[-4], inner_channels, 1, bias=bias)

    self.out5 = nn.Conv2d(inner_channels, inner_channels // 4, 3, padding=1, bias=bias)
    self.out4 = nn.Conv2d(inner_channels, inner_channels // 4, 3, padding=1, bias=bias)
    self.out3 = nn.Conv2d(inner_channels, inner_channels // 4, 3, padding=1, bias=bias)
    self.out2 = nn.Conv2d(inner_channels, inner_channels // 4, 3, padding=1, bias=bias)

    self.in5.apply(self.weights_init)
    self.in4.apply(self.weights_init)
    self.in3.apply(self.weights_init)
    self.in2.apply(self.weights_init)
    self.out5.apply(self.weights_init)
    self.out4.apply(self.weights_init)
    self.out3.apply(self.weights_init)
    self.out2.apply(self.weights_init)
up5 instance-attribute
up5 = Upsample(scale_factor=2, mode='nearest')
up4 instance-attribute
up4 = Upsample(scale_factor=2, mode='nearest')
up3 instance-attribute
up3 = Upsample(scale_factor=2, mode='nearest')
in5 instance-attribute
in5 = Conv2d(in_channels[-1], inner_channels, 1, bias=bias)
in4 instance-attribute
in4 = Conv2d(in_channels[-2], inner_channels, 1, bias=bias)
in3 instance-attribute
in3 = Conv2d(in_channels[-3], inner_channels, 1, bias=bias)
in2 instance-attribute
in2 = Conv2d(in_channels[-4], inner_channels, 1, bias=bias)
out5 instance-attribute
out5 = Conv2d(inner_channels, inner_channels // 4, 3, padding=1, bias=bias)
out4 instance-attribute
out4 = Conv2d(inner_channels, inner_channels // 4, 3, padding=1, bias=bias)
out3 instance-attribute
out3 = Conv2d(inner_channels, inner_channels // 4, 3, padding=1, bias=bias)
out2 instance-attribute
out2 = Conv2d(inner_channels, inner_channels // 4, 3, padding=1, bias=bias)
weights_init
weights_init(m)
Source code in SaigeToolkit/model/neck/fpn.py
def weights_init(self, m):
    classname = m.__class__.__name__
    if classname.find("Conv") != -1:
        nn.init.kaiming_normal_(m.weight.data)
    elif classname.find("BatchNorm") != -1:
        m.weight.data.fill_(1.0)
        m.bias.data.fill_(1e-4)
forward
forward(features, training=True)
Source code in SaigeToolkit/model/neck/fpn.py
def forward(self, features, training=True):
    c2, c3, c4, c5 = features
    in5 = self.in5(c5)
    in4 = self.in4(c4)
    in3 = self.in3(c3)
    in2 = self.in2(c2)

    out4 = self.up5(in5) + in4  # 1/16
    out3 = self.up4(out4) + in3  # 1/8
    out2 = self.up3(out3) + in2  # 1/4

    p5 = self.out5(in5)
    p4 = self.out4(out4)
    p3 = self.out3(out3)
    p2 = self.out2(out2)

    return p2, p3, p4, p5

util

get_last_conv_channels

get_last_conv_channels(model: Module) -> int

WARNING: This will return the number of output channels of the last conv layer defined in model, and it could differ from the actual output channels of the model.

Parameters:

  • model (Module) –

    torch.nn.Module who will return its last conv layer channels

Returns:

  • int ( int ) –

    last conv layer channels

Source code in SaigeToolkit/model/util.py
def get_last_conv_channels(model: torch.nn.Module) -> int:
    """WARNING: This will return the number of output channels of the last conv layer defined in `model`,
    and it could differ from the actual output channels of the model.

    Args:
        model (torch.nn.Module): torch.nn.Module who will return its last conv layer channels

    Returns:
        int: last conv layer channels
    """

    layer_type_list = [torch.nn.Conv2d, torch.nn.ConvTranspose2d]
    return [module for module in model.modules() if type(module) in layer_type_list][-1].out_channels

extend_state_dict_input_channel

extend_state_dict_input_channel(state_dict: Mapping[str, Any], input_weight_key: str, input_conv_layer: Conv2d) -> None

(Multipage) 3채널 이상인 이미지를 사용하기 위해 필요한 기능이며, state_dict의 input conv 채널이 네트워크의 input conv 채널보다 작은 경우 해당 weight의 채널을 늘려줍니다. 현재 네트워크가 가진 input conv 웨이트에서 앞 3 채널을 state_dict의 input conv 웨이트로 치환하는 방식을 사용합니다.

Parameters:

  • state_dict (Mapping[str, Any]) –

    로드하려는 weight

  • input_weight_key (str) –

    input conv weight의 이름

  • input_conv_layer (Conv2d) –

    input conv layer

Source code in SaigeToolkit/model/util.py
def extend_state_dict_input_channel(
    state_dict: Mapping[str, Any],
    input_weight_key: str,
    input_conv_layer: nn.Conv2d,
) -> None:
    """(Multipage) 3채널 이상인 이미지를 사용하기 위해 필요한 기능이며,
    state_dict의 input conv 채널이 네트워크의 input conv 채널보다 작은 경우 해당 weight의 채널을 늘려줍니다.
    현재 네트워크가 가진 input conv 웨이트에서 앞 3 채널을 state_dict의 input conv 웨이트로 치환하는 방식을 사용합니다.

    Args:
        state_dict (Mapping[str, Any]): 로드하려는 weight
        input_weight_key (str): input conv weight의 이름
        input_conv_layer (nn.Conv2d): input conv layer
    """
    state_dict_in_channels = state_dict[input_weight_key].shape[1]
    if state_dict_in_channels != input_conv_layer.in_channels:
        state_dict_in_channels = min(state_dict_in_channels, input_conv_layer.in_channels)
        original_input_conv_weight = input_conv_layer.state_dict()["weight"]
        original_input_conv_weight[:, :state_dict_in_channels] = state_dict[input_weight_key][
            :, :state_dict_in_channels
        ]
        state_dict[input_weight_key] = original_input_conv_weight