|
| 1 | +from functools import partial |
| 2 | +from typing import Any, Callable, Dict, List, Optional, Sequence |
| 3 | + |
| 4 | +import torch |
| 5 | +from torch import nn, Tensor |
| 6 | +from torch.nn import functional as F |
| 7 | + |
| 8 | +from .._internally_replaced_utils import load_state_dict_from_url |
| 9 | +from ..ops.misc import ConvNormActivation |
| 10 | +from ..ops.stochastic_depth import StochasticDepth |
| 11 | +from ..utils import _log_api_usage_once |
| 12 | + |
| 13 | + |
| 14 | +__all__ = [ |
| 15 | + "ConvNeXt", |
| 16 | + "convnext_tiny", |
| 17 | + "convnext_small", |
| 18 | + "convnext_base", |
| 19 | + "convnext_large", |
| 20 | +] |
| 21 | + |
| 22 | + |
| 23 | +_MODELS_URLS: Dict[str, Optional[str]] = { |
| 24 | + "convnext_tiny": "https://download.pytorch.org/models/convnext_tiny-983f1562.pth", |
| 25 | + "convnext_small": "https://download.pytorch.org/models/convnext_small-0c510722.pth", |
| 26 | + "convnext_base": "https://download.pytorch.org/models/convnext_base-6075fbad.pth", |
| 27 | + "convnext_large": "https://download.pytorch.org/models/convnext_large-ea097f82.pth", |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +class LayerNorm2d(nn.LayerNorm): |
| 32 | + def forward(self, x: Tensor) -> Tensor: |
| 33 | + x = x.permute(0, 2, 3, 1) |
| 34 | + x = F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps) |
| 35 | + x = x.permute(0, 3, 1, 2) |
| 36 | + return x |
| 37 | + |
| 38 | + |
| 39 | +class Permute(nn.Module): |
| 40 | + def __init__(self, dims: List[int]): |
| 41 | + super().__init__() |
| 42 | + self.dims = dims |
| 43 | + |
| 44 | + def forward(self, x): |
| 45 | + return torch.permute(x, self.dims) |
| 46 | + |
| 47 | + |
| 48 | +class CNBlock(nn.Module): |
| 49 | + def __init__( |
| 50 | + self, |
| 51 | + dim, |
| 52 | + layer_scale: float, |
| 53 | + stochastic_depth_prob: float, |
| 54 | + norm_layer: Optional[Callable[..., nn.Module]] = None, |
| 55 | + ) -> None: |
| 56 | + super().__init__() |
| 57 | + if norm_layer is None: |
| 58 | + norm_layer = partial(nn.LayerNorm, eps=1e-6) |
| 59 | + |
| 60 | + self.block = nn.Sequential( |
| 61 | + nn.Conv2d(dim, dim, kernel_size=7, padding=3, groups=dim, bias=True), |
| 62 | + Permute([0, 2, 3, 1]), |
| 63 | + norm_layer(dim), |
| 64 | + nn.Linear(in_features=dim, out_features=4 * dim, bias=True), |
| 65 | + nn.GELU(), |
| 66 | + nn.Linear(in_features=4 * dim, out_features=dim, bias=True), |
| 67 | + Permute([0, 3, 1, 2]), |
| 68 | + ) |
| 69 | + self.layer_scale = nn.Parameter(torch.ones(dim, 1, 1) * layer_scale) |
| 70 | + self.stochastic_depth = StochasticDepth(stochastic_depth_prob, "row") |
| 71 | + |
| 72 | + def forward(self, input: Tensor) -> Tensor: |
| 73 | + result = self.layer_scale * self.block(input) |
| 74 | + result = self.stochastic_depth(result) |
| 75 | + result += input |
| 76 | + return result |
| 77 | + |
| 78 | + |
| 79 | +class CNBlockConfig: |
| 80 | + # Stores information listed at Section 3 of the ConvNeXt paper |
| 81 | + def __init__( |
| 82 | + self, |
| 83 | + input_channels: int, |
| 84 | + out_channels: Optional[int], |
| 85 | + num_layers: int, |
| 86 | + ) -> None: |
| 87 | + self.input_channels = input_channels |
| 88 | + self.out_channels = out_channels |
| 89 | + self.num_layers = num_layers |
| 90 | + |
| 91 | + def __repr__(self) -> str: |
| 92 | + s = self.__class__.__name__ + "(" |
| 93 | + s += "input_channels={input_channels}" |
| 94 | + s += ", out_channels={out_channels}" |
| 95 | + s += ", num_layers={num_layers}" |
| 96 | + s += ")" |
| 97 | + return s.format(**self.__dict__) |
| 98 | + |
| 99 | + |
| 100 | +class ConvNeXt(nn.Module): |
| 101 | + def __init__( |
| 102 | + self, |
| 103 | + block_setting: List[CNBlockConfig], |
| 104 | + stochastic_depth_prob: float = 0.0, |
| 105 | + layer_scale: float = 1e-6, |
| 106 | + num_classes: int = 1000, |
| 107 | + block: Optional[Callable[..., nn.Module]] = None, |
| 108 | + norm_layer: Optional[Callable[..., nn.Module]] = None, |
| 109 | + **kwargs: Any, |
| 110 | + ) -> None: |
| 111 | + super().__init__() |
| 112 | + _log_api_usage_once(self) |
| 113 | + |
| 114 | + if not block_setting: |
| 115 | + raise ValueError("The block_setting should not be empty") |
| 116 | + elif not (isinstance(block_setting, Sequence) and all([isinstance(s, CNBlockConfig) for s in block_setting])): |
| 117 | + raise TypeError("The block_setting should be List[CNBlockConfig]") |
| 118 | + |
| 119 | + if block is None: |
| 120 | + block = CNBlock |
| 121 | + |
| 122 | + if norm_layer is None: |
| 123 | + norm_layer = partial(LayerNorm2d, eps=1e-6) |
| 124 | + |
| 125 | + layers: List[nn.Module] = [] |
| 126 | + |
| 127 | + # Stem |
| 128 | + firstconv_output_channels = block_setting[0].input_channels |
| 129 | + layers.append( |
| 130 | + ConvNormActivation( |
| 131 | + 3, |
| 132 | + firstconv_output_channels, |
| 133 | + kernel_size=4, |
| 134 | + stride=4, |
| 135 | + padding=0, |
| 136 | + norm_layer=norm_layer, |
| 137 | + activation_layer=None, |
| 138 | + bias=True, |
| 139 | + ) |
| 140 | + ) |
| 141 | + |
| 142 | + total_stage_blocks = sum(cnf.num_layers for cnf in block_setting) |
| 143 | + stage_block_id = 0 |
| 144 | + for cnf in block_setting: |
| 145 | + # Bottlenecks |
| 146 | + stage: List[nn.Module] = [] |
| 147 | + for _ in range(cnf.num_layers): |
| 148 | + # adjust stochastic depth probability based on the depth of the stage block |
| 149 | + sd_prob = stochastic_depth_prob * stage_block_id / (total_stage_blocks - 1.0) |
| 150 | + stage.append(block(cnf.input_channels, layer_scale, sd_prob)) |
| 151 | + stage_block_id += 1 |
| 152 | + layers.append(nn.Sequential(*stage)) |
| 153 | + if cnf.out_channels is not None: |
| 154 | + # Downsampling |
| 155 | + layers.append( |
| 156 | + nn.Sequential( |
| 157 | + norm_layer(cnf.input_channels), |
| 158 | + nn.Conv2d(cnf.input_channels, cnf.out_channels, kernel_size=2, stride=2), |
| 159 | + ) |
| 160 | + ) |
| 161 | + |
| 162 | + self.features = nn.Sequential(*layers) |
| 163 | + self.avgpool = nn.AdaptiveAvgPool2d(1) |
| 164 | + |
| 165 | + lastblock = block_setting[-1] |
| 166 | + lastconv_output_channels = ( |
| 167 | + lastblock.out_channels if lastblock.out_channels is not None else lastblock.input_channels |
| 168 | + ) |
| 169 | + self.classifier = nn.Sequential( |
| 170 | + norm_layer(lastconv_output_channels), nn.Flatten(1), nn.Linear(lastconv_output_channels, num_classes) |
| 171 | + ) |
| 172 | + |
| 173 | + for m in self.modules(): |
| 174 | + if isinstance(m, (nn.Conv2d, nn.Linear)): |
| 175 | + nn.init.trunc_normal_(m.weight, std=0.02) |
| 176 | + if m.bias is not None: |
| 177 | + nn.init.zeros_(m.bias) |
| 178 | + |
| 179 | + def _forward_impl(self, x: Tensor) -> Tensor: |
| 180 | + x = self.features(x) |
| 181 | + x = self.avgpool(x) |
| 182 | + x = self.classifier(x) |
| 183 | + return x |
| 184 | + |
| 185 | + def forward(self, x: Tensor) -> Tensor: |
| 186 | + return self._forward_impl(x) |
| 187 | + |
| 188 | + |
| 189 | +def _convnext( |
| 190 | + arch: str, |
| 191 | + block_setting: List[CNBlockConfig], |
| 192 | + stochastic_depth_prob: float, |
| 193 | + pretrained: bool, |
| 194 | + progress: bool, |
| 195 | + **kwargs: Any, |
| 196 | +) -> ConvNeXt: |
| 197 | + model = ConvNeXt(block_setting, stochastic_depth_prob=stochastic_depth_prob, **kwargs) |
| 198 | + if pretrained: |
| 199 | + if arch not in _MODELS_URLS: |
| 200 | + raise ValueError(f"No checkpoint is available for model type {arch}") |
| 201 | + state_dict = load_state_dict_from_url(_MODELS_URLS[arch], progress=progress) |
| 202 | + model.load_state_dict(state_dict) |
| 203 | + return model |
| 204 | + |
| 205 | + |
| 206 | +def convnext_tiny(*, pretrained: bool = False, progress: bool = True, **kwargs: Any) -> ConvNeXt: |
| 207 | + r"""ConvNeXt Tiny model architecture from the |
| 208 | + `"A ConvNet for the 2020s" <https://arxiv.org/abs/2201.03545>`_ paper. |
| 209 | + Args: |
| 210 | + pretrained (bool): If True, returns a model pre-trained on ImageNet |
| 211 | + progress (bool): If True, displays a progress bar of the download to stderr |
| 212 | + """ |
| 213 | + block_setting = [ |
| 214 | + CNBlockConfig(96, 192, 3), |
| 215 | + CNBlockConfig(192, 384, 3), |
| 216 | + CNBlockConfig(384, 768, 9), |
| 217 | + CNBlockConfig(768, None, 3), |
| 218 | + ] |
| 219 | + stochastic_depth_prob = kwargs.pop("stochastic_depth_prob", 0.1) |
| 220 | + return _convnext("convnext_tiny", block_setting, stochastic_depth_prob, pretrained, progress, **kwargs) |
| 221 | + |
| 222 | + |
| 223 | +def convnext_small(*, pretrained: bool = False, progress: bool = True, **kwargs: Any) -> ConvNeXt: |
| 224 | + r"""ConvNeXt Small model architecture from the |
| 225 | + `"A ConvNet for the 2020s" <https://arxiv.org/abs/2201.03545>`_ paper. |
| 226 | + Args: |
| 227 | + pretrained (bool): If True, returns a model pre-trained on ImageNet |
| 228 | + progress (bool): If True, displays a progress bar of the download to stderr |
| 229 | + """ |
| 230 | + block_setting = [ |
| 231 | + CNBlockConfig(96, 192, 3), |
| 232 | + CNBlockConfig(192, 384, 3), |
| 233 | + CNBlockConfig(384, 768, 27), |
| 234 | + CNBlockConfig(768, None, 3), |
| 235 | + ] |
| 236 | + stochastic_depth_prob = kwargs.pop("stochastic_depth_prob", 0.4) |
| 237 | + return _convnext("convnext_small", block_setting, stochastic_depth_prob, pretrained, progress, **kwargs) |
| 238 | + |
| 239 | + |
| 240 | +def convnext_base(*, pretrained: bool = False, progress: bool = True, **kwargs: Any) -> ConvNeXt: |
| 241 | + r"""ConvNeXt Base model architecture from the |
| 242 | + `"A ConvNet for the 2020s" <https://arxiv.org/abs/2201.03545>`_ paper. |
| 243 | + Args: |
| 244 | + pretrained (bool): If True, returns a model pre-trained on ImageNet |
| 245 | + progress (bool): If True, displays a progress bar of the download to stderr |
| 246 | + """ |
| 247 | + block_setting = [ |
| 248 | + CNBlockConfig(128, 256, 3), |
| 249 | + CNBlockConfig(256, 512, 3), |
| 250 | + CNBlockConfig(512, 1024, 27), |
| 251 | + CNBlockConfig(1024, None, 3), |
| 252 | + ] |
| 253 | + stochastic_depth_prob = kwargs.pop("stochastic_depth_prob", 0.5) |
| 254 | + return _convnext("convnext_base", block_setting, stochastic_depth_prob, pretrained, progress, **kwargs) |
| 255 | + |
| 256 | + |
| 257 | +def convnext_large(*, pretrained: bool = False, progress: bool = True, **kwargs: Any) -> ConvNeXt: |
| 258 | + r"""ConvNeXt Large model architecture from the |
| 259 | + `"A ConvNet for the 2020s" <https://arxiv.org/abs/2201.03545>`_ paper. |
| 260 | + Args: |
| 261 | + pretrained (bool): If True, returns a model pre-trained on ImageNet |
| 262 | + progress (bool): If True, displays a progress bar of the download to stderr |
| 263 | + """ |
| 264 | + block_setting = [ |
| 265 | + CNBlockConfig(192, 384, 3), |
| 266 | + CNBlockConfig(384, 768, 3), |
| 267 | + CNBlockConfig(768, 1536, 27), |
| 268 | + CNBlockConfig(1536, None, 3), |
| 269 | + ] |
| 270 | + stochastic_depth_prob = kwargs.pop("stochastic_depth_prob", 0.5) |
| 271 | + return _convnext("convnext_large", block_setting, stochastic_depth_prob, pretrained, progress, **kwargs) |
0 commit comments