Table of Contents

Class AnchorGenerator<T>

Namespace
AiDotNet.ComputerVision.Detection.Anchors
Assembly
AiDotNet.dll

Generates anchor boxes for object detection models.

public class AnchorGenerator<T>

Type Parameters

T

The 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

baseSizes double[]

Base anchor sizes at each feature level.

aspectRatios double[]

Anchor aspect ratios (height/width).

scales double[]

Scales to apply to base sizes.

strides int[]

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

int

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

anchors double[,]

Array of (width, height) anchor pairs for each level.

strides int[]

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

featureSizes List<(int, int)>

List of (height, width) for each feature level.

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

imageHeight int

Image height in pixels.

imageWidth int

Image width in pixels.

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

featureHeight int

Height of the feature map.

featureWidth int

Width of the feature map.

stride int

Stride (downsampling factor) of this level.

baseSize double

Base 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

imageHeight int

Image height.

imageWidth int

Image width.

Returns

int

Total anchor count.