Skip to content

squeezenet

model.backbone.torchvision.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)

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)

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

_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)