Networks Architectures Package

Attention-UNet Architecture Module

Attention-UNet utils module

class model.dim3.attention_unet_utils.AttentionBlock(g_ch, l_ch, int_ch)[source]

Bases: Module

forward(g, x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.attention_unet_utils.attention_up_block(in_ch, out_ch, num_block, block=<class 'model.dim3.conv_layers.BasicBlock'>, kernel_size=[3, 3, 3], up_scale=[2, 2, 2], norm=<class 'torch.nn.modules.batchnorm.BatchNorm3d'>)[source]

Bases: Module

forward(x1, x2)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Layers module

class model.dim3.conv_layers.BasicBlock(in_ch, out_ch, kernel_size=[3, 3, 3], stride=1, norm=<class 'torch.nn.modules.batchnorm.BatchNorm3d'>, act=<class 'torch.nn.modules.activation.ReLU'>, preact=True)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.conv_layers.Bottleneck(in_ch, out_ch, kernel_size=[3, 3, 3], stride=1, groups=1, dilation=1, norm=<class 'torch.nn.modules.batchnorm.BatchNorm3d'>, act=<class 'torch.nn.modules.activation.ReLU'>, preact=True)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.conv_layers.ConvNormAct(in_ch, out_ch, kernel_size=3, stride=1, padding=1, groups=1, dilation=1, bias=False, norm=<class 'torch.nn.modules.batchnorm.BatchNorm3d'>, act=<class 'torch.nn.modules.activation.ReLU'>, preact=False)[source]

Bases: Module

Layer grouping a convolution, normalization and activation function normalization includes BN as IN

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.conv_layers.DepthwiseSeparableConv(in_ch, out_ch, stride=1, kernel_size=3, bias=False)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

MedFormer Architecture Module

class model.dim3.medformer.MedFormer(in_chan, num_classes, base_chan=32, map_size=[4, 8, 8], conv_block='BasicBlock', conv_num=[2, 1, 0, 0, 0, 1, 2, 2], trans_num=[0, 1, 2, 2, 2, 1, 0, 0], chan_num=[64, 128, 256, 320, 256, 128, 64, 32], num_heads=[1, 4, 8, 16, 8, 4, 1, 1], fusion_depth=2, fusion_dim=320, fusion_heads=4, expansion=4, attn_drop=0.0, proj_drop=0.0, proj_type='depthwise', norm='in', act='gelu', kernel_size=[3, 3, 3, 3], scale=[2, 2, 2, 2], aux_loss=False)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

MedFormer utils module

class model.dim3.medformer_utils.BasicLayer(feat_dim, map_dim, out_dim, num_blocks, heads=4, dim_head=64, expansion=4, attn_drop=0.0, proj_drop=0.0, map_size=[8, 8, 8], proj_type='depthwise', norm=<class 'torch.nn.modules.batchnorm.BatchNorm3d'>, act=<class 'torch.nn.modules.activation.GELU'>, kernel_size=[3, 3, 3], no_map_out=False)[source]

Bases: Module

A basic transformer layer for one stage No downsample or upsample operation in this layer, they are wrapped in the down_block of up_block

forward(x, semantic_map)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.medformer_utils.BidirectionAttention(feat_dim, map_dim, out_dim, heads=4, dim_head=64, attn_drop=0.0, proj_drop=0.0, map_size=[8, 8, 8], proj_type='depthwose', kernel_size=[3, 3, 3], no_map_out=False)[source]

Bases: Module

forward(feat, semantic_map)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

rearrange1(x, heads)[source]
rearrange2(x, d, h, w)[source]
class model.dim3.medformer_utils.BidirectionAttentionBlock(feat_dim, map_dim, out_dim, heads, dim_head, norm=<class 'torch.nn.modules.batchnorm.BatchNorm3d'>, act=<class 'torch.nn.modules.activation.ReLU'>, expansion=4, attn_drop=0.0, proj_drop=0.0, map_size=[8, 8, 8], proj_type='depthwise', kernel_size=[3, 3, 3], no_map_out=False)[source]

Bases: Module

forward(x, semantic_map)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.medformer_utils.PatchMerging(dim, out_dim, norm=<class 'torch.nn.modules.batchnorm.BatchNorm3d'>, proj_type='linear', down_scale=[2, 2, 2], kernel_size=[3, 3, 3])[source]

Bases: Module

Modified patch merging layer that works as down-sampling

forward(x)[source]

x: B, C, D, H, W

class model.dim3.medformer_utils.SemanticMapFusion(in_dim_list, dim, heads, depth=1, norm=<class 'torch.nn.modules.batchnorm.BatchNorm3d'>, attn_drop=0.0, proj_drop=0.0)[source]

Bases: Module

forward(map_list)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.medformer_utils.SemanticMapGeneration(feat_dim, map_dim, map_size)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.medformer_utils.down_block(in_ch, out_ch, conv_num, trans_num, down_scale=[2, 2, 2], kernel_size=[3, 3, 3], conv_block=<class 'model.dim3.conv_layers.BasicBlock'>, heads=4, dim_head=64, expansion=1, attn_drop=0.0, proj_drop=0.0, map_size=[8, 8, 8], proj_type='depthwise', norm=<class 'torch.nn.modules.batchnorm.BatchNorm3d'>, act=<class 'torch.nn.modules.activation.GELU'>, map_generate=False, map_dim=None)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.medformer_utils.inconv(in_ch, out_ch, kernel_size=[3, 3, 3], block=<class 'model.dim3.conv_layers.BasicBlock'>, norm=<class 'torch.nn.modules.batchnorm.BatchNorm3d'>, act=<class 'torch.nn.modules.activation.GELU'>)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.medformer_utils.up_block(in_ch, out_ch, conv_num, trans_num, up_scale=[2, 2, 2], kernel_size=[3, 3, 3], conv_block=<class 'model.dim3.conv_layers.BasicBlock'>, heads=4, dim_head=64, expansion=4, attn_drop=0.0, proj_drop=0.0, map_size=[4, 8, 8], proj_type='depthwise', norm=<class 'torch.nn.modules.batchnorm.BatchNorm3d'>, act=<class 'torch.nn.modules.activation.GELU'>, map_dim=None, map_shortcut=False, no_map_out=False)[source]

Bases: Module

forward(x1, x2, map1, map2=None)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

MedFormer utils-2 module

nnFormer Architecture Module

class model.dim3.nnformer.BasicLayer(dim, input_resolution, depth, num_heads, window_size=7, mlp_ratio=4.0, qkv_bias=True, qk_scale=None, drop=0.0, attn_drop=0.0, drop_path=0.0, norm_layer=<class 'torch.nn.modules.normalization.LayerNorm'>, downsample=True)[source]

Bases: Module

forward(x, S, H, W)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.nnformer.BasicLayer_up(dim, input_resolution, depth, num_heads, window_size=7, mlp_ratio=4.0, qkv_bias=True, qk_scale=None, drop=0.0, attn_drop=0.0, drop_path=0.0, norm_layer=<class 'torch.nn.modules.normalization.LayerNorm'>, upsample=True)[source]

Bases: Module

forward(x, skip, S, H, W)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.nnformer.ContiguousGrad(*args, **kwargs)[source]

Bases: Function

static backward(ctx, grad_out)[source]

Defines a formula for differentiating the operation with backward mode automatic differentiation (alias to the vjp function).

This function is to be overridden by all subclasses.

It must accept a context ctx as the first argument, followed by as many outputs as the forward() returned (None will be passed in for non tensor outputs of the forward function), and it should return as many tensors, as there were inputs to forward(). Each argument is the gradient w.r.t the given output, and each returned value should be the gradient w.r.t. the corresponding input. If an input is not a Tensor or is a Tensor not requiring grads, you can just pass None as a gradient for that input.

The context can be used to retrieve tensors saved during the forward pass. It also has an attribute ctx.needs_input_grad as a tuple of booleans representing whether each input needs gradient. E.g., backward() will have ctx.needs_input_grad[0] = True if the first input to forward() needs gradient computated w.r.t. the output.

static forward(ctx, x)[source]

This function is to be overridden by all subclasses. There are two ways to define forward:

Usage 1 (Combined forward and ctx):

@staticmethod
def forward(ctx: Any, *args: Any, **kwargs: Any) -> Any:
    pass
  • It must accept a context ctx as the first argument, followed by any number of arguments (tensors or other types).

  • See combining-forward-context for more details

Usage 2 (Separate forward and ctx):

@staticmethod
def forward(*args: Any, **kwargs: Any) -> Any:
    pass

@staticmethod
def setup_context(ctx: Any, inputs: Tuple[Any, ...], output: Any) -> None:
    pass
  • The forward no longer accepts a ctx argument.

  • Instead, you must also override the torch.autograd.Function.setup_context() staticmethod to handle setting up the ctx object. output is the output of the forward, inputs are a Tuple of inputs to the forward.

  • See extending-autograd for more details

The context can be used to store arbitrary data that can be then retrieved during the backward pass. Tensors should not be stored directly on ctx (though this is not currently enforced for backward compatibility). Instead, tensors should be saved either with ctx.save_for_backward() if they are intended to be used in backward (equivalently, vjp) or ctx.save_for_forward() if they are intended to be used for in jvp.

class model.dim3.nnformer.Decoder(pretrain_img_size, embed_dim, patch_size=4, depths=[2, 2, 2], num_heads=[24, 12, 6], window_size=4, mlp_ratio=4.0, qkv_bias=True, qk_scale=None, drop_rate=0.0, attn_drop_rate=0.0, drop_path_rate=0.2, norm_layer=<class 'torch.nn.modules.normalization.LayerNorm'>)[source]

Bases: Module

forward(x, skips)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.nnformer.Encoder(pretrain_img_size=224, patch_size=4, in_chans=1, embed_dim=96, depths=[2, 2, 2, 2], num_heads=[4, 8, 16, 32], window_size=7, mlp_ratio=4.0, qkv_bias=True, qk_scale=None, drop_rate=0.0, attn_drop_rate=0.0, drop_path_rate=0.2, norm_layer=<class 'torch.nn.modules.normalization.LayerNorm'>, patch_norm=True, out_indices=(0, 1, 2, 3))[source]

Bases: Module

forward(x)[source]

Forward function.

class model.dim3.nnformer.Mlp(in_features, hidden_features=None, out_features=None, act_layer=<class 'torch.nn.modules.activation.GELU'>, drop=0.0)[source]

Bases: Module

Multilayer perceptron.

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.nnformer.PatchEmbed(patch_size=4, in_chans=4, embed_dim=96, norm_layer=None)[source]

Bases: Module

forward(x)[source]

Forward function.

class model.dim3.nnformer.PatchMerging(dim, norm_layer=<class 'torch.nn.modules.normalization.LayerNorm'>)[source]

Bases: Module

forward(x, S, H, W)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.nnformer.Patch_Expanding(dim, norm_layer=<class 'torch.nn.modules.normalization.LayerNorm'>)[source]

Bases: Module

forward(x, S, H, W)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.nnformer.SwinTransformerBlock(dim, input_resolution, num_heads, window_size=7, shift_size=0, mlp_ratio=4.0, qkv_bias=True, qk_scale=None, drop=0.0, attn_drop=0.0, drop_path=0.0, act_layer=<class 'torch.nn.modules.activation.GELU'>, norm_layer=<class 'torch.nn.modules.normalization.LayerNorm'>)[source]

Bases: Module

forward(x, mask_matrix)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.nnformer.SwinTransformerBlock_kv(dim, input_resolution, num_heads, window_size=7, shift_size=0, mlp_ratio=4.0, qkv_bias=True, qk_scale=None, drop=0.0, attn_drop=0.0, drop_path=0.0, act_layer=<class 'torch.nn.modules.activation.GELU'>, norm_layer=<class 'torch.nn.modules.normalization.LayerNorm'>)[source]

Bases: Module

forward(x, mask_matrix, skip=None, x_up=None)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.nnformer.WindowAttention(dim, window_size, num_heads, qkv_bias=True, qk_scale=None, attn_drop=0.0, proj_drop=0.0)[source]

Bases: Module

forward(x, mask=None, pos_embed=None)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.nnformer.WindowAttention_kv(dim, window_size, num_heads, qkv_bias=True, qk_scale=None, attn_drop=0.0, proj_drop=0.0)[source]

Bases: Module

forward(skip, x_up, pos_embed=None, mask=None)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.nnformer.final_patch_expanding(dim, num_class, patch_size)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.nnformer.nnFormer(crop_size=[64, 128, 128], embedding_dim=192, input_channels=1, num_classes=14, conv_op=<class 'torch.nn.modules.conv.Conv3d'>, depths=[2, 2, 2, 2], num_heads=[6, 12, 24, 48], patch_size=[2, 4, 4], window_size=[4, 4, 8, 4], deep_supervision=True)[source]

Bases: SegmentationNetwork

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.nnformer.project(in_dim, out_dim, stride, padding, activate, norm, last=False)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

model.dim3.nnformer.window_partition(x, window_size)[source]
model.dim3.nnformer.window_reverse(windows, window_size, S, H, W)[source]

nnFormer utils module

class model.dim3.nnformer_utils.InitWeights_He(neg_slope=0.01)[source]

Bases: object

class model.dim3.nnformer_utils.InitWeights_XavierUniform(gain=1)[source]

Bases: object

class model.dim3.nnformer_utils.NeuralNetwork[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

get_device()[source]
set_device(device)[source]
class model.dim3.nnformer_utils.SegmentationNetwork[source]

Bases: NeuralNetwork

predict_2D(x, do_mirroring: bool, mirror_axes: tuple = (0, 1, 2), use_sliding_window: bool = False, step_size: float = 0.5, patch_size: tuple | None = None, regions_class_order: tuple | None = None, use_gaussian: bool = False, pad_border_mode: str = 'constant', pad_kwargs: dict | None = None, all_in_gpu: bool = False, verbose: bool = True, mixed_precision: bool = True) Tuple[ndarray, ndarray][source]

Use this function to predict a 2D image. If this is a 3D U-Net it will crash because you cannot predict a 2D image with that (you dummy). When running predictions, you need to specify whether you want to run fully convolutional of sliding window based inference. We very strongly recommend you use sliding window with the default settings. It is the responsibility of the user to make sure the network is in the proper mode (eval for inference!). If the network is not in eval mode it will print a warning. :param x: Your input data. Must be a nd.ndarray of shape (c, x, y). :param do_mirroring: If True, use test time data augmentation in the form of mirroring :param mirror_axes: Determines which axes to use for mirroing. Per default, mirroring is done along all three axes :param use_sliding_window: if True, run sliding window prediction. Heavily recommended! This is also the default :param step_size: When running sliding window prediction, the step size determines the distance between adjacent predictions. The smaller the step size, the denser the predictions (and the longer it takes!). Step size is given as a fraction of the patch_size. 0.5 is the default and means that wen advance by patch_size * 0.5 between predictions. step_size cannot be larger than 1! :param patch_size: The patch size that was used for training the network. Do not use different patch sizes here, this will either crash or give potentially less accurate segmentations :param regions_class_order: Fabian only :param use_gaussian: (Only applies to sliding window prediction) If True, uses a Gaussian importance weighting

to weigh predictions closer to the center of the current patch higher than those at the borders. The reason behind this is that the segmentation accuracy decreases towards the borders. Default (and recommended): True

Parameters:
  • pad_border_mode – leave this alone

  • pad_kwargs – leave this alone

  • all_in_gpu – experimental. You probably want to leave this as is it

  • verbose – Do you want a wall of text? If yes then set this to True

Returns:

predict_3D(x: ndarray, do_mirroring: bool, mirror_axes: Tuple[int, ...] = (0, 1, 2), use_sliding_window: bool = False, step_size: float = 0.5, patch_size: Tuple[int, ...] | None = None, regions_class_order: Tuple[int, ...] | None = None, use_gaussian: bool = False, pad_border_mode: str = 'constant', pad_kwargs: dict | None = None, all_in_gpu: bool = False, verbose: bool = True, mixed_precision: bool = True) Tuple[ndarray, ndarray][source]

Use this function to predict a 3D image. It does not matter whether the network is a 2D or 3D U-Net, it will detect that automatically and run the appropriate code. When running predictions, you need to specify whether you want to run fully convolutional of sliding window based inference. We very strongly recommend you use sliding window with the default settings. It is the responsibility of the user to make sure the network is in the proper mode (eval for inference!). If the network is not in eval mode it will print a warning. :param x: Your input data. Must be a nd.ndarray of shape (c, x, y, z). :param do_mirroring: If True, use test time data augmentation in the form of mirroring :param mirror_axes: Determines which axes to use for mirroing. Per default, mirroring is done along all three axes :param use_sliding_window: if True, run sliding window prediction. Heavily recommended! This is also the default :param step_size: When running sliding window prediction, the step size determines the distance between adjacent predictions. The smaller the step size, the denser the predictions (and the longer it takes!). Step size is given as a fraction of the patch_size. 0.5 is the default and means that wen advance by patch_size * 0.5 between predictions. step_size cannot be larger than 1! :param patch_size: The patch size that was used for training the network. Do not use different patch sizes here, this will either crash or give potentially less accurate segmentations :param regions_class_order: Fabian only :param use_gaussian: (Only applies to sliding window prediction) If True, uses a Gaussian importance weighting

to weigh predictions closer to the center of the current patch higher than those at the borders. The reason behind this is that the segmentation accuracy decreases towards the borders. Default (and recommended): True

Parameters:
  • pad_border_mode – leave this alone

  • pad_kwargs – leave this alone

  • all_in_gpu – experimental. You probably want to leave this as is it

  • verbose – Do you want a wall of text? If yes then set this to True

  • mixed_precision – if True, will run inference in mixed precision with autocast()

Returns:

predict_3D_pseudo3D_2Dconv(x: ndarray, min_size: Tuple[int, int], do_mirroring: bool, mirror_axes: tuple = (0, 1), regions_class_order: tuple | None = None, pseudo3D_slices: int = 5, all_in_gpu: bool = False, pad_border_mode: str = 'constant', pad_kwargs: dict | None = None, verbose: bool = True) Tuple[ndarray, ndarray][source]
model.dim3.nnformer_utils.maybe_to_torch(d)[source]
class model.dim3.nnformer_utils.no_op[source]

Bases: object

model.dim3.nnformer_utils.softmax_helper(x)
model.dim3.nnformer_utils.to_cuda(data, non_blocking=True, gpu_id=0)[source]

SegFormer Architecture Module

SwinUNETR Architecture Module

class model.dim3.swin_unetr.BasicLayer(dim: int, depth: int, num_heads: int, window_size: ~collections.abc.Sequence[int], drop_path: list, mlp_ratio: float = 4.0, qkv_bias: bool = False, drop: float = 0.0, attn_drop: float = 0.0, norm_layer: type[~torch.nn.modules.normalization.LayerNorm] = <class 'torch.nn.modules.normalization.LayerNorm'>, downsample: ~torch.nn.modules.module.Module | None = None, use_checkpoint: bool = False)[source]

Bases: Module

Basic Swin Transformer layer in one stage based on: “Liu et al., Swin Transformer: Hierarchical Vision Transformer using Shifted Windows <https://arxiv.org/abs/2103.14030>” https://github.com/microsoft/Swin-Transformer

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.swin_unetr.PatchMerging(dim: int, norm_layer: type[~torch.nn.modules.normalization.LayerNorm] = <class 'torch.nn.modules.normalization.LayerNorm'>, spatial_dims: int = 3)[source]

Bases: PatchMergingV2

The PatchMerging module previously defined in v0.9.0.

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.swin_unetr.PatchMergingV2(dim: int, norm_layer: type[~torch.nn.modules.normalization.LayerNorm] = <class 'torch.nn.modules.normalization.LayerNorm'>, spatial_dims: int = 3)[source]

Bases: Module

Patch merging layer based on: “Liu et al., Swin Transformer: Hierarchical Vision Transformer using Shifted Windows <https://arxiv.org/abs/2103.14030>” https://github.com/microsoft/Swin-Transformer

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.swin_unetr.SwinTransformer(in_chans: int, embed_dim: int, window_size: ~collections.abc.Sequence[int], patch_size: ~collections.abc.Sequence[int], depths: ~collections.abc.Sequence[int], num_heads: ~collections.abc.Sequence[int], mlp_ratio: float = 4.0, qkv_bias: bool = True, drop_rate: float = 0.0, attn_drop_rate: float = 0.0, drop_path_rate: float = 0.0, norm_layer: type[~torch.nn.modules.normalization.LayerNorm] = <class 'torch.nn.modules.normalization.LayerNorm'>, patch_norm: bool = False, use_checkpoint: bool = False, spatial_dims: int = 3, downsample='merging', use_v2=False)[source]

Bases: Module

Swin Transformer based on: “Liu et al., Swin Transformer: Hierarchical Vision Transformer using Shifted Windows <https://arxiv.org/abs/2103.14030>” https://github.com/microsoft/Swin-Transformer

forward(x, normalize=True)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

proj_out(x, normalize=False)[source]
class model.dim3.swin_unetr.SwinTransformerBlock(dim: int, num_heads: int, window_size: ~collections.abc.Sequence[int], shift_size: ~collections.abc.Sequence[int], mlp_ratio: float = 4.0, qkv_bias: bool = True, drop: float = 0.0, attn_drop: float = 0.0, drop_path: float = 0.0, act_layer: str = 'GELU', norm_layer: type[~torch.nn.modules.normalization.LayerNorm] = <class 'torch.nn.modules.normalization.LayerNorm'>, use_checkpoint: bool = False)[source]

Bases: Module

Swin Transformer block based on: “Liu et al., Swin Transformer: Hierarchical Vision Transformer using Shifted Windows <https://arxiv.org/abs/2103.14030>” https://github.com/microsoft/Swin-Transformer

forward(x, mask_matrix)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

forward_part1(x, mask_matrix)[source]
forward_part2(x)[source]
load_from(weights, n_block, layer)[source]
class model.dim3.swin_unetr.SwinUNETR(img_size: Sequence[int] | int, in_channels: int, out_channels: int, depths: Sequence[int] = (2, 2, 2, 2), num_heads: Sequence[int] = (3, 6, 12, 24), feature_size: int = 24, norm_name: tuple | str = 'instance', drop_rate: float = 0.0, attn_drop_rate: float = 0.0, dropout_path_rate: float = 0.0, normalize: bool = True, use_checkpoint: bool = False, spatial_dims: int = 3, downsample='merging', use_v2=False)[source]

Bases: Module

Swin UNETR based on: “Hatamizadeh et al., Swin UNETR: Swin Transformers for Semantic Segmentation of Brain Tumors in MRI Images <https://arxiv.org/abs/2201.01266>”

forward(x_in)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

load_from(weights)[source]
patch_size: Final[int] = 2
class model.dim3.swin_unetr.WindowAttention(dim: int, num_heads: int, window_size: Sequence[int], qkv_bias: bool = False, attn_drop: float = 0.0, proj_drop: float = 0.0)[source]

Bases: Module

Window based multi-head self attention module with relative position bias based on: “Liu et al., Swin Transformer: Hierarchical Vision Transformer using Shifted Windows <https://arxiv.org/abs/2103.14030>” https://github.com/microsoft/Swin-Transformer

forward(x, mask)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

model.dim3.swin_unetr.window_partition(x, window_size)[source]

window partition operation based on: “Liu et al., Swin Transformer: Hierarchical Vision Transformer using Shifted Windows <https://arxiv.org/abs/2103.14030>” https://github.com/microsoft/Swin-Transformer

Args:

x: input tensor. window_size: local window size.

model.dim3.swin_unetr.window_reverse(windows, window_size, dims)[source]

window reverse operation based on: “Liu et al., Swin Transformer: Hierarchical Vision Transformer using Shifted Windows <https://arxiv.org/abs/2103.14030>” https://github.com/microsoft/Swin-Transformer

Args:

windows: windows tensor. window_size: local window size. dims: dimension values.

trans-layers module

class model.dim3.trans_layers.Attention(dim, heads, dim_head, attn_drop=0.0, proj_drop=0.0)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

rearrange1(x, heads)[source]
rearrange2(x)[source]
class model.dim3.trans_layers.LayerNorm(normalized_shape, eps=1e-05, data_format='channels_first')[source]

Bases: Module

LayerNorm that supports two data formats: channels_last (default) or channels_first. The ordering of the dimensions in the inputs. channels_last corresponds to inputs with shape (batch_size, height, width, channels) while channels_first corresponds to inputs with shape (batch_size, channels, height, width).

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.trans_layers.Mlp(in_dim, hid_dim=None, out_dim=None, act=<class 'torch.nn.modules.activation.GELU'>, drop=0.0)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.trans_layers.TransformerBlock(dim, depth, heads, dim_head, mlp_dim, attn_drop=0.0, proj_drop=0.0)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

UNet Architecture Module

class model.dim3.unet.UNet(in_ch, base_ch, scale=[2, 2, 2, 2], kernel_size=[3, 3, 3, 3], num_classes=1, block='ConvNormAct', pool=True, norm='bn')[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

UNet utils module

class model.dim3.unet_utils.down_block(in_ch, out_ch, num_block, block=<class 'model.dim3.conv_layers.BasicBlock'>, kernel_size=[3, 3, 3], down_scale=[2, 2, 2], pool=True, norm=<class 'torch.nn.modules.batchnorm.BatchNorm3d'>)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.unet_utils.inconv(in_ch, out_ch, kernel_size=[3, 3, 3], block=<class 'model.dim3.conv_layers.BasicBlock'>, norm=<class 'torch.nn.modules.batchnorm.BatchNorm3d'>)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.unet_utils.up_block(in_ch, out_ch, num_block, block=<class 'model.dim3.conv_layers.BasicBlock'>, kernel_size=[3, 3, 3], up_scale=[2, 2, 2], norm=<class 'torch.nn.modules.batchnorm.BatchNorm3d'>)[source]

Bases: Module

forward(x1, x2)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

UNet++ Architecture Module

class model.dim3.unetpp.UNetPlusPlus(in_ch, base_ch, scale, kernel_size, num_classes=1, block='SingleConv', norm='bn')[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

make_layer(in_ch, out_ch, num_block, block, kernel_size, norm)[source]

UNETR Architecture Module

class model.dim3.unetr.UNETR(in_channels: int, out_channels: int, img_size: Tuple[int, int, int], feature_size: int = 16, hidden_size: int = 768, mlp_dim: int = 3072, num_heads: int = 12, pos_embed: str = 'perceptron', norm_name: Tuple | str = 'instance', conv_block: bool = True, res_block: bool = True, dropout_rate: float = 0.0)[source]

Bases: Module

UNETR based on: “Hatamizadeh et al., UNETR: Transformers for 3D Medical Image Segmentation <https://arxiv.org/abs/2103.10504>”

forward(x_in)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

load_from(weights)[source]
proj_feat(x, hidden_size, feat_size)[source]

utils module

model.dim3.utils.get_act(name)[source]
model.dim3.utils.get_block(name)[source]
model.dim3.utils.get_norm(name)[source]

VNet Architecture Module

class model.dim3.vnet.ContBatchNorm3d(num_features: int, eps: float = 1e-05, momentum: float = 0.1, affine: bool = True, track_running_stats: bool = True, device=None, dtype=None)[source]

Bases: _BatchNorm

forward(input)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.vnet.DownTransition(inChans, nConvs, elu, scale=2, dropout=False)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

model.dim3.vnet.ELUCons(elu, nchan)[source]
class model.dim3.vnet.InputTransition(inChans, outChans, elu)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.vnet.LUConv(nchan, elu)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.vnet.OutputTransition(inChans, outChans, elu, nll)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.vnet.UpTransition(inChans, outChans, nConvs, elu, scale=2, dropout=False)[source]

Bases: Module

forward(x, skipx)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class model.dim3.vnet.VNet(inChans, outChans, scale, baseChans=16, elu=True, nll=False)[source]

Bases: Module

forward(x)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

model.dim3.vnet.passthrough(x, **kwargs)[source]

VTUNet Architecture Module

VTUNet utils module

Architecture selection module