Class NeckBase<T>
- Namespace
- AiDotNet.ComputerVision.Detection.Necks
- Assembly
- AiDotNet.dll
Base class for neck modules that perform multi-scale feature fusion.
public abstract class NeckBase<T>
Type Parameters
TThe numeric type used for calculations.
- Inheritance
-
NeckBase<T>
- Derived
- Inherited Members
Remarks
For Beginners: The "neck" sits between the backbone and the detection head. It takes multi-scale features from the backbone and fuses them together so that each feature level contains information from both higher and lower resolutions. This helps detect objects of various sizes more accurately.
Common neck architectures: - FPN (Feature Pyramid Network): Top-down feature fusion - PANet (Path Aggregation Network): Top-down + bottom-up paths - BiFPN (Bidirectional FPN): Weighted bidirectional fusion
Constructors
NeckBase()
Creates a new neck module.
protected NeckBase()
Fields
IsTrainingMode
Whether the neck is in training mode.
protected bool IsTrainingMode
Field Value
NumOps
Numeric operations for type T.
protected readonly INumericOperations<T> NumOps
Field Value
- INumericOperations<T>
Properties
Name
Name of this neck architecture.
public abstract string Name { get; }
Property Value
NumLevels
Number of feature levels output by the neck.
public abstract int NumLevels { get; }
Property Value
OutputChannels
Number of output channels for all feature levels.
public abstract int OutputChannels { get; }
Property Value
Remarks
Necks typically project all feature levels to the same number of channels (e.g., 256) to simplify the detection head.
Methods
Add(Tensor<T>, Tensor<T>)
Adds two feature maps element-wise.
protected Tensor<T> Add(Tensor<T> a, Tensor<T> b)
Parameters
aTensor<T>First feature map.
bTensor<T>Second feature map.
Returns
- Tensor<T>
Element-wise sum.
Conv1x1(Tensor<T>, Tensor<T>, Tensor<T>?)
Applies a 1x1 convolution to change the number of channels.
protected Tensor<T> Conv1x1(Tensor<T> input, Tensor<T> weights, Tensor<T>? bias = null)
Parameters
inputTensor<T>Input feature map.
weightsTensor<T>Convolution weights [out_channels, in_channels].
biasTensor<T>Optional bias [out_channels].
Returns
- Tensor<T>
Feature map with new channel count.
Downsample2x(Tensor<T>)
Downsample a feature map by a factor of 2 using max pooling.
protected Tensor<T> Downsample2x(Tensor<T> input)
Parameters
inputTensor<T>Input feature map.
Returns
- Tensor<T>
Downsampled feature map.
Forward(List<Tensor<T>>)
Performs multi-scale feature fusion.
public abstract List<Tensor<T>> Forward(List<Tensor<T>> features)
Parameters
featuresList<Tensor<T>>List of feature maps from the backbone, ordered from highest to lowest resolution.
Returns
- List<Tensor<T>>
Fused feature maps at multiple scales.
Remarks
For Beginners: This method takes the raw features from the backbone and combines them across scales. After fusion, each feature level "knows about" features from other scales, making detection more accurate.
GetParameterCount()
Gets the total number of parameters in the neck.
public abstract long GetParameterCount()
Returns
- long
Number of trainable parameters.
ReadParameters(BinaryReader)
Reads parameters from a binary reader for deserialization.
public abstract void ReadParameters(BinaryReader reader)
Parameters
readerBinaryReaderThe binary reader to read from.
SetTrainingMode(bool)
Sets whether the neck is in training mode.
public virtual void SetTrainingMode(bool training)
Parameters
trainingboolTrue for training, false for inference.
Upsample2x(Tensor<T>)
Upsample a feature map by a factor of 2 using nearest neighbor interpolation.
protected Tensor<T> Upsample2x(Tensor<T> input)
Parameters
inputTensor<T>Input feature map.
Returns
- Tensor<T>
Upsampled feature map.
ValidateFeatures(List<Tensor<T>>, int[])
Validates that the input features are compatible with this neck.
protected void ValidateFeatures(List<Tensor<T>> features, int[] expectedInputChannels)
Parameters
featuresList<Tensor<T>>Features to validate.
expectedInputChannelsint[]Expected input channels at each level.
Exceptions
- ArgumentException
Thrown if features are incompatible.
WriteParameters(BinaryWriter)
Writes all parameters to a binary writer for serialization.
public abstract void WriteParameters(BinaryWriter writer)
Parameters
writerBinaryWriterThe binary writer to write to.