Note

This page is a reference documentation. It only explains the class signature, and not how to use it. Please refer to the user guide for the big picture.

nidl.backbones.volume.utils.Patched3DInputAdapter

class nidl.backbones.volume.utils.Patched3DInputAdapter(num_channels, stride_level, patch_size_full, dim_tokens=None, sincos_pos_emb=True, learnable_pos_emb=False, volume_size=128)[source]

Bases: Module

Adapter turning a 3D volume or feature map (B, C, H, W, D) into a sequence of tokens (B, N, dim_tokens).

The input is split into non-overlapping patches, each linearly projected to a token, and 3D positional embeddings are added. The input may be downsampled from a full volume, e.g. the feature map of a CNN: the patch size on the input is then patch_size_full // stride_level.

Adapted from the 2D PatchedInputAdapter of https://github.com/Duplums/CoMM.

Parameters:
num_channelsint

Number of channels C of the input.

stride_levelint

Downsampling factor of the input relative to the full volume, e.g. 1 for a scan, or 32 for the feature map of a ResNet.

patch_size_fullint or tuple of (int, int, int)

Patch size in voxels of the full volume.

dim_tokensint or None, default=None

Dimension of the tokens. If None, call init before use.

sincos_pos_embbool, default=True

Whether the positional embeddings are initialized with 3D sin-cos functions. Otherwise, they are initialized randomly and learned.

learnable_pos_embbool, default=False

Whether the sin-cos positional embeddings are learned (fixed otherwise).

volume_sizeint or tuple of (int, int, int), default=128

Size of the full volume, which sets the grid of the positional embeddings (interpolated to the grid of the input).

__init__(num_channels, stride_level, patch_size_full, dim_tokens=None, sincos_pos_emb=True, learnable_pos_emb=False, volume_size=128)[source]

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Forward pass through the input adapter, transforming a volume into a sequence of tokens and adding positional encodings.

Parameters:
xtorch.Tensor

Input volume tensor of shape (B, C, H, W, D).

Returns:
xtorch.Tensor

Tokens of shape (B, N_H * N_W * N_D, dim_tokens).

init(dim_tokens=768)[source]

Initialize parts of the adapter that depend on the dimension of tokens.

Parameters:
dim_tokensint, default=768

Dimension of tokens.

no_weight_decay()[source]