EfficientNet based on `Rethinking Model Scaling for Convolutional Neural Networks `_. Adapted from `EfficientNet-PyTorch `_. Args: blocks_args_str: block definitions.
(
self,
blocks_args_str: list[str],
spatial_dims: int = 2,
in_channels: int = 3,
num_classes: int = 1000,
width_coefficient: float = 1.0,
depth_coefficient: float = 1.0,
dropout_rate: float = 0.2,
image_size: int = 224,
norm: str | tuple = ("batch", {"eps": 1e-3, "momentum": 0.01}),
drop_connect_rate: float = 0.2,
depth_divisor: int = 8,
)
| 230 | class EfficientNet(nn.Module): |
| 231 | |
| 232 | def __init__( |
| 233 | self, |
| 234 | blocks_args_str: list[str], |
| 235 | spatial_dims: int = 2, |
| 236 | in_channels: int = 3, |
| 237 | num_classes: int = 1000, |
| 238 | width_coefficient: float = 1.0, |
| 239 | depth_coefficient: float = 1.0, |
| 240 | dropout_rate: float = 0.2, |
| 241 | image_size: int = 224, |
| 242 | norm: str | tuple = ("batch", {"eps": 1e-3, "momentum": 0.01}), |
| 243 | drop_connect_rate: float = 0.2, |
| 244 | depth_divisor: int = 8, |
| 245 | ) -> None: |
| 246 | """ |
| 247 | EfficientNet based on `Rethinking Model Scaling for Convolutional Neural Networks <https://arxiv.org/pdf/1905.11946.pdf>`_. |
| 248 | Adapted from `EfficientNet-PyTorch <https://github.com/lukemelas/EfficientNet-PyTorch>`_. |
| 249 | |
| 250 | Args: |
| 251 | blocks_args_str: block definitions. |
| 252 | spatial_dims: number of spatial dimensions. |
| 253 | in_channels: number of input channels. |
| 254 | num_classes: number of output classes. |
| 255 | width_coefficient: width multiplier coefficient (w in paper). |
| 256 | depth_coefficient: depth multiplier coefficient (d in paper). |
| 257 | dropout_rate: dropout rate for dropout layers. |
| 258 | image_size: input image resolution. |
| 259 | norm: feature normalization type and arguments. |
| 260 | drop_connect_rate: dropconnect rate for drop connection (individual weights) layers. |
| 261 | depth_divisor: depth divisor for channel rounding. |
| 262 | |
| 263 | """ |
| 264 | super().__init__() |
| 265 | |
| 266 | if spatial_dims not in (1, 2, 3): |
| 267 | raise ValueError("spatial_dims can only be 1, 2 or 3.") |
| 268 | |
| 269 | # select the type of N-Dimensional layers to use |
| 270 | # these are based on spatial dims and selected from MONAI factories |
| 271 | conv_type: type[nn.Conv1d | nn.Conv2d | nn.Conv3d] = Conv["conv", spatial_dims] |
| 272 | adaptivepool_type: type[nn.AdaptiveAvgPool1d | nn.AdaptiveAvgPool2d | nn.AdaptiveAvgPool3d] = Pool[ |
| 273 | "adaptiveavg", spatial_dims |
| 274 | ] |
| 275 | |
| 276 | # decode blocks args into arguments for MBConvBlock |
| 277 | blocks_args = [BlockArgs.from_string(s) for s in blocks_args_str] |
| 278 | |
| 279 | # checks for successful decoding of blocks_args_str |
| 280 | if not isinstance(blocks_args, list): |
| 281 | raise ValueError("blocks_args must be a list") |
| 282 | |
| 283 | if blocks_args == []: |
| 284 | raise ValueError("block_args must be non-empty") |
| 285 | |
| 286 | self._blocks_args = blocks_args |
| 287 | self.num_classes = num_classes |
| 288 | self.in_channels = in_channels |
| 289 | self.drop_connect_rate = drop_connect_rate |
nothing calls this directly
no test coverage detected