generateEfficientDetAnchors function

List<List<double>> generateEfficientDetAnchors({
  1. required int imageSize,
  2. int minLevel = 3,
  3. int maxLevel = 7,
  4. int numScales = 3,
  5. List<double> aspectRatios = const [1.0, 2.0, 0.5],
  6. double anchorScale = 4.0,
})

Generates EfficientDet RetinaNet-style multi-scale anchors.

EfficientDet uses 5 feature pyramid levels (P3-P7) with numScales (3) × aspectRatios.length (3) = 9 anchors per spatial location. Anchors are returned in normalized image coordinates as [cx, cy, w, h].

For Lite0 with imageSize=320, total anchors = 19 206. For Lite2 with imageSize=448, total anchors = 37 629.

Implementation

List<List<double>> generateEfficientDetAnchors({
  required int imageSize,
  int minLevel = 3,
  int maxLevel = 7,
  int numScales = 3,
  List<double> aspectRatios = const [1.0, 2.0, 0.5],
  double anchorScale = 4.0,
}) {
  final anchors = <List<double>>[];
  for (int level = minLevel; level <= maxLevel; level++) {
    final int stride = 1 << level;
    final int featureSize = (imageSize / stride).ceil();
    final double baseAnchorSize = anchorScale * stride.toDouble();
    for (int y = 0; y < featureSize; y++) {
      for (int x = 0; x < featureSize; x++) {
        final double cy = (y + 0.5) * stride / imageSize;
        final double cx = (x + 0.5) * stride / imageSize;
        for (int s = 0; s < numScales; s++) {
          final double scale = math.pow(2, s / numScales).toDouble();
          for (final aspect in aspectRatios) {
            final double sqAspect = math.sqrt(aspect);
            final double w = baseAnchorSize * scale * sqAspect / imageSize;
            final double h = baseAnchorSize * scale / sqAspect / imageSize;
            anchors.add([cx, cy, w, h]);
          }
        }
      }
    }
  }
  return anchors;
}