ViTAdapter<T>

Models & Types · AiDotNet.ComputerVision.Segmentation.Semantic

ViT-Adapter: Vision Transformer Adapter for Dense Predictions.

Inheritance: NeuralNetworkBase<T> → SegmentationModelBase<T> → SemanticSegmentationBase<T> → ViTAdapter<T>

For Beginners

ViT-Adapter enables plain Vision Transformers (ViTs) to handle dense prediction tasks like semantic segmentation without changing the ViT architecture. It adds lightweight “adapter” modules that inject multi-scale spatial information into the ViT, letting it produce features at different resolutions needed for pixel-level predictions.

Common use cases:

  • Semantic segmentation using pre-trained ViT backbones (DINOv2, MAE, BEiT)
  • Object detection with ViT encoders
  • Any dense prediction task where you want to leverage powerful ViT pretraining

How It Works

Technical Details:

  • Spatial Prior Module: injects multi-scale spatial priors into plain ViT
  • Spatial Interaction Module: enables feature interaction between adapter and ViT
  • Works with any plain ViT (ViT-S, ViT-B, ViT-L) without modifying the backbone
  • Adds only ~3% extra parameters on top of the base ViT
  • SOTA on ADE20K with BEiT-L backbone (58.0 mIoU)

Reference: Chen et al., “Vision Transformer Adapter for Dense Predictions”, ICLR 2023 Spotlight.

Example

// Create a ViT-Adapter model for dense prediction with a plain ViT backbone
var architecture = new NeuralNetworkArchitecture<double>(
    inputType: InputType.ThreeDimensional,
    taskType: NeuralNetworkTaskType.ImageClassification,
    inputHeight: 512, inputWidth: 512, inputDepth: 3, outputSize: 150);
var model = new ViTAdapter<double>(architecture);
// Every tunable is now set through the options object; these are the defaults:
var custom = new ViTAdapter<double>(architecture,
    options: new ViTAdapterOptions { NumClasses = 150, DropRate = 0.1, ModelSize = ViTAdapterModelSize.Base });
            
// Or load a pre-trained ONNX model for inference
var onnxModel = new ViTAdapter<double>(architecture, "vitadapter.onnx");

Constructors

Constructor Summary
ViTAdapter(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction, ViTAdapterOptions options) Initializes a new instance of ViT-Adapter in native (trainable) mode.
ViTAdapter(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, ViTAdapterOptions options) Initializes a new instance of ViT-Adapter in ONNX (inference-only) mode.

Methods

Method Summary
ModelMetadata<T> GetModelMetadata() Collects model metadata.