unetrpp¶
UnetR++ Vision transformer based on: “Shaker et al., Adapted from https://github.com/Amshaker/unetr_plus_plus Added 2d support and Bilinear interpolation for upsampling.
- class mfai.pytorch.models.unetrpp.EPA(input_size, hidden_size, num_heads=4, qkv_bias=False, channel_attn_drop=0.1, spatial_attn_drop=0.1, proj_size=64, attention_code='torch')[source]¶
Bases:
ModuleEfficient Paired Attention Block, based on: “Shaker et al., UNETR++: Delving into Efficient and Accurate 3D Medical Image Segmentation” Modifications : - adds compatibility with 2d inputs - adds an option to use torch’s scaled dot product instead of the original implementation This should enable the use of flash attention in the future.
- Parameters:
- forward(x)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
TensorNote
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mfai.pytorch.models.unetrpp.LayerNorm(normalized_shape, eps=1e-06, data_format='channels_last')[source]¶
Bases:
Module- forward(x)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
TensorNote
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mfai.pytorch.models.unetrpp.TransformerBlock(input_size, hidden_size, num_heads, dropout_rate=0.0, pos_embed=False, spatial_dims=2, proj_size=64, attention_code='torch')[source]¶
Bases:
ModuleA transformer block, based on: “Shaker et al., UNETR++: Delving into Efficient and Accurate 3D Medical Image Segmentation” Modified to work with both 2d and 3d data (spatial_dims).
- Parameters:
- forward(x)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
TensorNote
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mfai.pytorch.models.unetrpp.UNetRPP(in_channels, out_channels, input_shape, settings=UNetRPPSettings(hidden_size=256, num_heads_encoder=4, num_heads_decoder=4, pos_embed='perceptron', norm_name='instance', dropout_rate=0.0, depths=(3, 3, 3, 3), conv_op='Conv2d', linear_upsampling=False, downsampling_rate=4, decoder_proj_size=64, encoder_proj_sizes=(64, 64, 64, 32), autopad_enabled=False, add_skip_connections=True, attention_code='torch'))[source]¶
Bases:
BaseModel,AutoPaddingModelUNetR++ based on: “Shaker et al., UNETR++: Delving into Efficient and Accurate 3D Medical Image Segmentation”.
- Parameters:
in_channels (int)
out_channels (int)
settings (UNetRPPSettings)
- forward(x)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
Tensor|list[Tensor]Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- model_type = 3¶
- onnx_supported = False¶
- property settings: UNetRPPSettings¶
Returns the settings instance used to configure for this model.
- settings_kls¶
alias of
UNetRPPSettings
- supported_num_spatial_dims = (2, 3)¶
- validate_input_shape(input_shape)[source]¶
- Given an input shape, verifies whether the inputs fit with the
calling model’s specifications.
- Parameters:
input_shape (Size) – The shape of the input data, excluding any batch dimension and channel dimension. For example, for a batch of 2D tensors of shape [B,C,W,H], [W,H] should be passed. For 3D data instead of shape [B,C,W,H,D], instead, [W,H,D] should be passed.
- Returns:
- Returns a tuple where the first element is a boolean signaling whether the given input shape
already fits the model’s requirements. If that value is False, the second element contains the closest shape that fits the model, otherwise it will be None.
- Return type:
- class mfai.pytorch.models.unetrpp.UNetRPPEncoder(input_size=[32768, 4096, 512, 64], dims=[32, 64, 128, 256], depths=[3, 3, 3, 3], num_heads=4, spatial_dims=2, in_channels=4, dropout=0.0, transformer_dropout_rate=0.1, downsampling_rate=4, proj_sizes=(64, 64, 64, 32), attention_code='torch')[source]¶
Bases:
Module- Parameters:
- forward(x)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
tuple[Tensor,list[Tensor]]Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mfai.pytorch.models.unetrpp.UNetRPPSettings(hidden_size=256, num_heads_encoder=4, num_heads_decoder=4, pos_embed='perceptron', norm_name='instance', dropout_rate=0.0, depths=(3, 3, 3, 3), conv_op='Conv2d', linear_upsampling=False, downsampling_rate=4, decoder_proj_size=64, encoder_proj_sizes=(64, 64, 64, 32), autopad_enabled=False, add_skip_connections=True, attention_code='torch')[source]¶
Bases:
objectSettings dataclass for UNetRPP. Contains all the hyperparameters needed to initialize the model.
- Parameters:
hidden_size (
int) – dimensions of the last encoder.num_heads – number of attention heads.
pos_embed (
str) – position embedding layer type.norm_name (
Union[tuple,str]) – feature normalization type and arguments.dropout_rate (
float) – faction of the input units to drop.conv_op (
str) – type of convolution operation.do_ds – use deep supervision to compute the loss.
num_heads_encoder (int)
num_heads_decoder (int)
linear_upsampling (bool)
downsampling_rate (int)
decoder_proj_size (int)
autopad_enabled (bool)
add_skip_connections (bool)
attention_code (str)
- do_ds = False¶
- classmethod from_dict(kvs, *, infer_missing=False)¶
- classmethod from_json(s, *, parse_float=None, parse_int=None, parse_constant=None, infer_missing=False, **kw)¶
- classmethod schema(*, infer_missing=False, only=None, exclude=(), many=False, context=None, load_only=(), dump_only=(), partial=False, unknown=None)¶
- spatial_dims = 2¶
- to_json(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, indent=None, separators=None, default=None, sort_keys=False, **kw)¶
- class mfai.pytorch.models.unetrpp.UNetRUpBlock(spatial_dims, in_channels, out_channels, kernel_size, upsample_kernel_size, norm_name, num_heads=4, out_size=0, depth=3, conv_decoder=False, linear_upsampling=False, proj_size=64, attention_code='torch')[source]¶
Bases:
Module
- mfai.pytorch.models.unetrpp.trunc_normal_(tensor, mean=0.0, std=1.0, a=-2.0, b=2.0)[source]¶
Fills the input Tensor with values drawn from a truncated normal distribution. The values are effectively drawn from the normal distribution \(\mathcal{N}(\text{mean}, \text{std}^2)\) with values outside \([a, b]\) redrawn until they are within the bounds. The method used for generating the random values works best when \(a \leq \text{mean} \leq b\).
NOTE: this impl is similar to the PyTorch trunc_normal_, the bounds [a, b] are applied while sampling the normal with mean/std applied, therefore a, b args should be adjusted to match the range of mean, std args.
- Parameters:
- Return type:
Examples
>>> w = torch.empty(3, 5) >>> nn.init.trunc_normal_(w)