TransUNet<T>

Models & Types · AiDotNet.ComputerVision.Segmentation.Medical

TransUNet: Transformers make strong encoders for medical segmentation.

Inheritance: NeuralNetworkBase<T> → SegmentationModelBase<T> → MedicalSegmentationBase<T> → TransUNet<T>

For Beginners

Multi-organ segmentation from CT scans. Cardiac segmentation from MRI.

Common use cases:

  • Multi-organ segmentation from CT scans
  • Cardiac segmentation from MRI
  • Medical image analysis requiring global context
  • Hybrid CNN-Transformer medical segmentation

How It Works

Technical Details:

  • Hybrid CNN-Transformer encoder (ResNet + ViT)
  • CNN captures local features, Transformer captures global context
  • Cascaded upsampler decoder with skip connections
  • Tokenized image patches enable self-attention over full image

Reference: Chen et al., “TransUNet: Transformers Make Strong Encoders for Medical Image Segmentation”, arXiv 2021.

Example

// Create a TransUNet model for multi-organ CT segmentation
var architecture = new NeuralNetworkArchitecture<double>(
    inputType: InputType.ThreeDimensional,
    taskType: NeuralNetworkTaskType.MultiClassClassification,
    inputHeight: 224, inputWidth: 224, inputDepth: 1, outputSize: 9);
var model = new TransUNet<double>(architecture);
// Every tunable is now set through the options object; these are the defaults:
var custom = new TransUNet<double>(architecture,
    options: new TransUNetOptions { NumClasses = 9, DropRate = 0.1, ModelSize = TransUNetModelSize.Base });
            
// Or load a pre-trained ONNX model for cardiac MRI segmentation
var onnxModel = new TransUNet<double>(architecture, "transunet.onnx");

Constructors

Constructor Summary
TransUNet(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, TransUNetOptions options) Initializes TransUNet in native (trainable) mode.
TransUNet(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, TransUNetOptions options) Initializes TransUNet in ONNX (inference-only) mode.

Properties

Property Summary
bool Supports3D { get; } TransUNet is a 2D model; volumetric input is not supported.

Methods

Method Summary
MedicalSegmentationResult<T> SegmentFewShot(Tensor<T> queryImage, Tensor<T> supportImages, Tensor<T> supportMasks)
MedicalSegmentationResult<T> SegmentSlice(Tensor<T> slice) Segments a single 2D medical slice.
MedicalSegmentationResult<T> SegmentVolume(Tensor<T> volume)
ModelMetadata<T> GetModelMetadata() Collects metadata describing this model’s configuration.