Class AnchorGenerator<T>
- Namespace
- AiDotNet.ComputerVision.Detection.Anchors
- Assembly
- AiDotNet.dll
Generates anchor boxes for object detection models.
public class AnchorGenerator<T>
Type Parameters
TThe numeric type used for calculations.
- Inheritance
-
AnchorGenerator<T>
- Inherited Members
Remarks
For Beginners: Anchor boxes (also called prior boxes) are pre-defined boxes of various sizes and aspect ratios placed at each location in the feature map. The detector predicts how to adjust these anchors to fit actual objects. Using anchors helps the model handle objects of different sizes and shapes.
For example, a feature map of 80x80 with 3 anchors per location generates 80 x 80 x 3 = 19,200 anchor boxes, each representing a potential object location.
Constructors
AnchorGenerator()
Creates a new anchor generator with default YOLO-style anchors.
public AnchorGenerator()
AnchorGenerator(double[], double[], double[], int[])
Creates a new anchor generator with custom settings.
public AnchorGenerator(double[] baseSizes, double[] aspectRatios, double[] scales, int[] strides)
Parameters
baseSizesdouble[]Base anchor sizes at each feature level.
aspectRatiosdouble[]Anchor aspect ratios (height/width).
scalesdouble[]Scales to apply to base sizes.
stridesint[]Feature map strides.
Properties
AspectRatios
Aspect ratios for anchors (height/width).
public double[] AspectRatios { get; }
Property Value
- double[]
BaseSizes
Base sizes for anchors at each feature level.
public double[] BaseSizes { get; }
Property Value
- double[]
NumAnchorsPerLocation
Gets the number of anchors per feature map location.
public int NumAnchorsPerLocation { get; }
Property Value
Scales
Scales to apply to base sizes.
public double[] Scales { get; }
Property Value
- double[]
Strides
Strides (downsampling factors) at each feature level.
public int[] Strides { get; }
Property Value
- int[]
Methods
CreateFasterRCNNAnchors()
Creates an anchor generator with Faster R-CNN style anchors.
public static AnchorGenerator<T> CreateFasterRCNNAnchors()
Returns
- AnchorGenerator<T>
Anchor generator for Faster R-CNN.
CreateRetinaNetAnchors()
Creates an anchor generator with RetinaNet style anchors.
public static AnchorGenerator<T> CreateRetinaNetAnchors()
Returns
- AnchorGenerator<T>
Anchor generator for RetinaNet.
CreateYOLOAnchors(double[,], int[])
Creates an anchor generator with YOLO-style anchors.
public static AnchorGenerator<T> CreateYOLOAnchors(double[,] anchors, int[] strides)
Parameters
anchorsdouble[,]Array of (width, height) anchor pairs for each level.
stridesint[]Feature map strides.
Returns
- AnchorGenerator<T>
Configured anchor generator.
GenerateAnchors(List<(int Height, int Width)>)
Generates anchors for all feature levels.
public List<List<BoundingBox<T>>> GenerateAnchors(List<(int Height, int Width)> featureSizes)
Parameters
Returns
- List<List<BoundingBox<T>>>
List of anchors for each level.
GenerateAnchorsForImage(int, int)
Generates anchors for an image of specified size.
public List<BoundingBox<T>> GenerateAnchorsForImage(int imageHeight, int imageWidth)
Parameters
Returns
- List<BoundingBox<T>>
Flattened list of all anchors across all levels.
GenerateAnchorsForLevel(int, int, int, double)
Generates anchors for a single feature map level.
public List<BoundingBox<T>> GenerateAnchorsForLevel(int featureHeight, int featureWidth, int stride, double baseSize)
Parameters
featureHeightintHeight of the feature map.
featureWidthintWidth of the feature map.
strideintStride (downsampling factor) of this level.
baseSizedoubleBase anchor size for this level.
Returns
- List<BoundingBox<T>>
List of anchor boxes centered at each feature map location.
Remarks
For Beginners: For each location (x, y) in the feature map, this method creates multiple anchor boxes of different sizes and aspect ratios. The anchor centers are converted to image coordinates using the stride.
GetTotalAnchorCount(int, int)
Gets the total number of anchors for an image.
public int GetTotalAnchorCount(int imageHeight, int imageWidth)
Parameters
Returns
- int
Total anchor count.