API Reference: Models & Components¶
Architectural components including Backbones, Necks, Heads, and Detector compositions.
Object Detectors¶
corecv.models.detector.CoreObjectDetector(backbone, neck, head)
¶
Bases: Module
End-to-end object detection model.
Composes a backbone, optional neck, and detection head into a single module. The forward pass flows through each component sequentially:
input -> backbone -> (neck) -> head -> output
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backbone
|
Module
|
Feature extractor implementing
:class: |
required |
neck
|
Module | None
|
Optional feature pyramid network (FPN, PANet, etc.).
Pass |
required |
head
|
Module
|
Detection head producing class logits and box predictions. |
required |
Initialise the detector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backbone
|
Module
|
Feature extractor. |
required |
neck
|
Module | None
|
Optional neck ( |
required |
head
|
Module
|
Detection head. |
required |
Source code in src/corecv/models/detector.py
forward(x)
¶
Run the full detection pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
object
|
Output from the detection head (typically a |
object
|
|
Source code in src/corecv/models/detector.py
Backbones¶
ResNet¶
corecv.models.backbones.resnet
¶
ResNet backbone with multi-scale feature extraction.
Wraps :mod:torchvision.models ResNet variants (18, 34, 50, 101) and
extracts intermediate feature maps from layer1 through layer4 at
strides 4, 8, 16, and 32 respectively.
Extracted feature levels (verified against torchvision output shapes):
.. list-table:: :header-rows: 1 :widths: 20 20 20 15 15 15 15
-
- Model
- Level
- Stride
- ResNet-18
- ResNet-34
- ResNet-50
- ResNet-101
-
- layer1
- stride4
- 4
- 64
- 64
- 256
- 256
-
- layer2
- stride8
- 8
- 128
- 128
- 512
- 512
-
- layer3
- stride16
- 16
- 256
- 256
- 1024
- 1024
-
- layer4
- stride32
- 32
- 512
- 512
- 2048
- 2048
Example
from corecv.models.backbones.resnet import ResNet50Backbone backbone = ResNet50Backbone(pretrained=False) backbone.feature_info.channels {'stride4': 256, 'stride8': 512, 'stride16': 1024, 'stride32': 2048}
ResNet101Backbone(pretrained=True, **kwargs)
¶
Bases: _ResNetBackbone
ResNet-101 backbone.
Feature levels
stride4: 256 channelsstride8: 512 channelsstride16: 1024 channelsstride32: 2048 channels
Source code in src/corecv/models/backbones/resnet.py
ResNet18Backbone(pretrained=True, **kwargs)
¶
Bases: _ResNetBackbone
ResNet-18 backbone.
Feature levels
stride4: 64 channelsstride8: 128 channelsstride16: 256 channelsstride32: 512 channels
Source code in src/corecv/models/backbones/resnet.py
ResNet34Backbone(pretrained=True, **kwargs)
¶
Bases: _ResNetBackbone
ResNet-34 backbone.
Feature levels
stride4: 64 channelsstride8: 128 channelsstride16: 256 channelsstride32: 512 channels
Source code in src/corecv/models/backbones/resnet.py
ResNet50Backbone(pretrained=True, **kwargs)
¶
Bases: _ResNetBackbone
ResNet-50 backbone.
Feature levels
stride4: 256 channelsstride8: 512 channelsstride16: 1024 channelsstride32: 2048 channels
Source code in src/corecv/models/backbones/resnet.py
MobileNetV3¶
corecv.models.backbones.mobilenetv3
¶
MobileNetV3 backbone with multi-scale feature extraction.
Wraps :mod:torchvision.models.mobilenet_v3_small and
:mod:torchvision.models.mobilenet_v3_large to expose four feature levels
at strides 4, 8, 16, and 32. Feature maps are extracted at the last block
of each spatial resolution stage.
Extracted feature levels (verified against torchvision output shapes):
- MobileNetV3-Small (
featuresSequential, 224x224 input):
============ ===== ======= =========================
Level Index Spatial Channel Count
============ ===== ======= =========================
stride4 1 56x56 16
stride8 3 28x28 24
stride16 8 14x14 48
stride32 12 7x7 576
============ ===== ======= =========================
- MobileNetV3-Large (
featuresSequential, 224x224 input):
============ ===== ======= =========================
Level Index Spatial Channel Count
============ ===== ======= =========================
stride4 3 56x56 24
stride8 6 28x28 40
stride16 12 14x14 112
stride32 16 7x7 960
============ ===== ======= =========================
Example
from corecv.models.backbones.mobilenetv3 import MobileNetV3SmallBackbone backbone = MobileNetV3SmallBackbone(pretrained=False) backbone.feature_info.channels {'stride4': 16, 'stride8': 24, 'stride16': 48, 'stride32': 576}
MobileNetV3LargeBackbone(pretrained=True, **kwargs)
¶
Bases: _MobileNetV3Backbone
MobileNetV3-Large backbone with four-level feature extraction.
Wraps :func:torchvision.models.mobilenet_v3_large. Feature maps are
extracted from features Sequential at indices 3, 6, 12, and 16,
corresponding to strides 4, 8, 16, and 32 respectively.
Feature levels
stride4: 24 channels, 56x56 spatial (index 3)stride8: 40 channels, 28x28 spatial (index 6)stride16: 112 channels, 14x14 spatial (index 12)stride32: 960 channels, 7x7 spatial (index 16)
Source code in src/corecv/models/backbones/mobilenetv3.py
MobileNetV3SmallBackbone(pretrained=True, **kwargs)
¶
Bases: _MobileNetV3Backbone
MobileNetV3-Small backbone with four-level feature extraction.
Wraps :func:torchvision.models.mobilenet_v3_small. Feature maps are
extracted from features Sequential at indices 1, 3, 8, and 12,
corresponding to strides 4, 8, 16, and 32 respectively.
Feature levels
stride4: 16 channels, 56x56 spatial (index 1)stride8: 24 channels, 28x28 spatial (index 3)stride16: 48 channels, 14x14 spatial (index 8)stride32: 576 channels, 7x7 spatial (index 12)
Source code in src/corecv/models/backbones/mobilenetv3.py
ConvNeXt¶
corecv.models.backbones.convnext
¶
ConvNeXt backbone with multi-scale feature extraction.
Wraps :mod:torchvision.models ConvNeXt variants (Tiny, Small, Base, Large)
and extracts intermediate feature maps at four spatial scales. Each
ConvNeXt features Sequential contains eight :class:torch.nn.Sequential
stages paired into four resolution groups; the last stage of each group is
used as the extracted feature level.
Extracted feature levels (verified against torchvision output shapes, 224x224 input):
.. list-table:: :header-rows: 1 :widths: 16 12 10 14 14 14 14
-
- Level
- Index
- Stride
- Tiny
- Small
- Base
- Large
-
stride4- 1
- 4
- 96
- 96
- 128
- 192
-
stride8- 3
- 8
- 192
- 192
- 256
- 384
-
stride16- 5
- 16
- 384
- 384
- 512
- 768
-
stride32- 7
- 32
- 768
- 768
- 1024
- 1536
Example
from corecv.models.backbones.convnext import ConvNeXtTinyBackbone backbone = ConvNeXtTinyBackbone(pretrained=False) backbone.feature_info.channels {'stride4': 96, 'stride8': 192, 'stride16': 384, 'stride32': 768}
ConvNeXtBaseBackbone(pretrained=True, **kwargs)
¶
Bases: _ConvNeXtBackbone
ConvNeXt-Base backbone.
Feature levels
stride4: 128 channelsstride8: 256 channelsstride16: 512 channelsstride32: 1024 channels
Source code in src/corecv/models/backbones/convnext.py
ConvNeXtLargeBackbone(pretrained=True, **kwargs)
¶
Bases: _ConvNeXtBackbone
ConvNeXt-Large backbone.
Feature levels
stride4: 192 channelsstride8: 384 channelsstride16: 768 channelsstride32: 1536 channels
Source code in src/corecv/models/backbones/convnext.py
ConvNeXtSmallBackbone(pretrained=True, **kwargs)
¶
Bases: _ConvNeXtBackbone
ConvNeXt-Small backbone.
Feature levels
stride4: 96 channelsstride8: 192 channelsstride16: 384 channelsstride32: 768 channels
Source code in src/corecv/models/backbones/convnext.py
ConvNeXtTinyBackbone(pretrained=True, **kwargs)
¶
Bases: _ConvNeXtBackbone
ConvNeXt-Tiny backbone.
Feature levels
stride4: 96 channelsstride8: 192 channelsstride16: 384 channelsstride32: 768 channels
Source code in src/corecv/models/backbones/convnext.py
Vision Transformer (ViT)¶
corecv.models.backbones.vit
¶
Vision Transformer (ViT) backbone with multi-scale pyramid adapter.
Implements ViT-Tiny, ViT-Small, and ViT-Base variants from scratch to
enable intermediate feature extraction. A :class:SimplePyramidAdapter
converts the flat patch-token sequence output into a multi-scale feature
pyramid with strides 8, 16, and 32, making the ViT transparently comply
with the :class:~corecv.core.contract.BaseBackbone contract.
Architecture overview::
Image (B, 3, 224, 224)
|
v
PatchEmbedding -> (B, N, embed_dim) N = (H/P)*(W/P) = 14*14 = 196
|
v
TransformerBlock x depth
|
v
LayerNorm -> (B, 1+N, embed_dim)
|
v (drop CLS token)
patch_tokens: (B, N, embed_dim)
|
v
SimplePyramidAdapter -> [f8, f16, f32]
- f8: (B, embed_dim, 28, 28) stride 8
- f16: (B, embed_dim, 14, 14) stride 16
- f32: (B, embed_dim, 7, 7) stride 32
Variant configurations:
.. list-table:: :header-rows: 1
-
- Variant
- embed_dim
- depth
- num_heads
- mlp_ratio
-
- ViT-Tiny
- 192
- 12
- 3
- 4.0
-
- ViT-Small
- 384
- 12
- 6
- 4.0
-
- ViT-Base
- 768
- 12
- 12
- 4.0
Example
from corecv.models.backbones.vit import ViTBaseBackbone backbone = ViTBaseBackbone(pretrained=False) backbone.feature_info.channels {'stride8': 768, 'stride16': 768, 'stride32': 768}
SimplePyramidAdapter(in_channels, out_channels)
¶
Bases: Module
Convert flat ViT patch tokens into a multi-scale feature pyramid.
Takes the 2D-reshaped patch tokens from the ViT encoder and produces three feature maps at strides 8, 16, and 32 using transposed convolution (upsample), pointwise projection (identity), and strided convolution (downsample).
For a 224x224 input with patch_size=16, the spatial grid is 14x14. The adapter produces: - stride 8: 28x28 via transposed convolution (2x upsample) - stride 16: 14x14 via pointwise projection - stride 32: 7x7 via strided convolution (2x downsample)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Number of input channels (ViT embed_dim). |
required |
out_channels
|
int
|
Number of output channels for each pyramid level. |
required |
Initialise the pyramid adapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_channels
|
int
|
Number of input channels (ViT embed_dim). |
required |
out_channels
|
int
|
Number of output channels for each pyramid level. |
required |
Source code in src/corecv/models/backbones/vit.py
forward(patch_tokens, grid_size)
¶
Reshape patch tokens to 2D and create the feature pyramid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patch_tokens
|
Tensor
|
Patch token sequence of shape
|
required |
grid_size
|
int
|
Spatial side length of the patch grid
( |
required |
Returns:
| Type | Description |
|---|---|
list[Tensor]
|
A list of three feature tensors at strides 8, 16, and 32. |
Source code in src/corecv/models/backbones/vit.py
ViTBaseBackbone(pretrained=False, img_size=224, patch_size=16, in_channels=3, drop=0.0, attn_drop=0.0)
¶
Bases: _ViTBackbone
ViT-Base backbone (embed_dim=768, depth=12, heads=12).
Feature levels (via :class:SimplePyramidAdapter):
- stride8: 768 channels, 28x28 spatial
- stride16: 768 channels, 14x14 spatial
- stride32: 768 channels, 7x7 spatial
Source code in src/corecv/models/backbones/vit.py
ViTSmallBackbone(pretrained=False, img_size=224, patch_size=16, in_channels=3, drop=0.0, attn_drop=0.0)
¶
Bases: _ViTBackbone
ViT-Small backbone (embed_dim=384, depth=12, heads=6).
Feature levels (via :class:SimplePyramidAdapter):
- stride8: 384 channels, 28x28 spatial
- stride16: 384 channels, 14x14 spatial
- stride32: 384 channels, 7x7 spatial
Source code in src/corecv/models/backbones/vit.py
ViTTinyBackbone(pretrained=False, img_size=224, patch_size=16, in_channels=3, drop=0.0, attn_drop=0.0)
¶
Bases: _ViTBackbone
ViT-Tiny backbone (embed_dim=192, depth=12, heads=3).
Feature levels (via :class:SimplePyramidAdapter):
- stride8: 192 channels, 28x28 spatial
- stride16: 192 channels, 14x14 spatial
- stride32: 192 channels, 7x7 spatial
Source code in src/corecv/models/backbones/vit.py
Necks¶
Feature Pyramid Network (FPN)¶
corecv.models.necks.fpn
¶
Feature Pyramid Network (FPN) neck implementation.
Implements the FPN architecture from "Feature Pyramid Networks for Object Detection" (Lin et al., 2017). The FPN augments a backbone's multi-scale feature maps with a top-down pathway and lateral connections, producing pyramid feature maps at every scale with a uniform channel dimension.
Architecture¶
Given backbone feature levels [C2, C3, C4, C5] at strides
[4, 8, 16, 32]:
- Lateral 1x1 convolutions project each level to
out_channels. - Top-down pathway merges coarse features into finer scales via nearest-neighbour upsampling and element-wise addition.
- Output 3x3 convolutions reduce aliasing artifacts from upsampling.
The result is [P2, P3, P4, P5] -- one feature map per backbone level,
all with out_channels channels.
Dynamic Channel Projection¶
The neck accepts any :class:~corecv.core.contract.FeatureInfo and
automatically instantiates the correct number of 1x1 lateral convolutions
with input channels derived from the backbone metadata. This means the
same :class:FPN class works with ResNet, MobileNetV3, ConvNeXt, ViT,
or any future backbone that implements :class:BaseBackbone.
Example
from corecv.models.backbones.resnet import ResNet50Backbone from corecv.models.necks.fpn import FPN backbone = ResNet50Backbone(pretrained=False) neck = FPN(feature_info=backbone.feature_info, out_channels=256) features = backbone(torch.randn(1, 3, 224, 224)) pyramid = neck(features) tuple(f.shape for f in pyramid) (torch.Size([1, 256, 56, 56]), torch.Size([1, 256, 28, 28]), torch.Size([1, 256, 14, 14]), torch.Size([1, 256, 7, 7]))
FPN(feature_info, out_channels=256)
¶
Bases: Module
Feature Pyramid Network neck with dynamic channel projection.
Produces a multi-scale feature pyramid from a backbone's intermediate
feature maps. The number of lateral convolutions, their input channels,
and the interpolation targets are all derived at construction time from
the provided :class:FeatureInfo, making this neck backbone-agnostic.
Attributes:
| Name | Type | Description |
|---|---|---|
out_channels |
Uniform channel count for all output feature levels. |
|
levels |
Sorted list of |
|
lateral_convs |
Module mapping level name -> 1x1 conv for channel alignment in the top-down pathway. |
|
fpn_convs |
Module mapping level name -> 3x3 conv applied after the top-down merge to reduce upsampling aliasing. |
Initialise the FPN neck.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_info
|
FeatureInfo
|
Metadata describing the backbone's feature levels. Used to determine the number of levels and their input channel counts for lateral convolution construction. |
required |
out_channels
|
int
|
Output channel dimension for every pyramid level. All lateral 1x1 convolutions project to this width. |
256
|
Source code in src/corecv/models/necks/fpn.py
forward(features)
¶
Apply the FPN top-down pathway to backbone features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Sequence[Tensor]
|
Sequence of backbone feature tensors in stride-
ascending order (i.e. highest resolution first). The
length must match the number of levels in the
:class: |
required |
Returns:
| Type | Description |
|---|---|
list[Tensor]
|
A list of pyramid feature tensors in stride-ascending order |
list[Tensor]
|
(smallest stride first), each with shape |
list[Tensor]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of input features does not match the number of registered backbone levels. |
Source code in src/corecv/models/necks/fpn.py
Path Aggregation Network (PANet)¶
corecv.models.necks.panet
¶
Path Aggregation Network (PANet) neck implementation.
Implements the PANet architecture from "Path Aggregation Network for Instance Segmentation" (Liu et al., 2018). PANet extends FPN by adding a bottom-up path augmentation on top of the FPN's top-down pyramid, providing a shorter information path from low-level features to the deepest layers and improving feature fusion across all scales.
Architecture¶
Given backbone feature levels [C2, C3, C4, C5] at strides
[4, 8, 16, 32]:
- FPN top-down pathway (identical to :class:
~corecv.models.necks.fpn.FPN) produces[P2, P3, P4, P5]. - Bottom-up path augmentation merges FPN outputs from shallowest to
deepest via stride-2 convolutions for spatial downsampling and
element-wise addition, producing
[N2, N3, N4, N5]. - Output 3x3 convolutions are applied after each bottom-up merge.
The result is [N2, N3, N4, N5] -- one feature map per backbone level,
all with out_channels channels.
Dynamic Channel Projection¶
Like :class:~corecv.models.necks.fpn.FPN, the PANet accepts any
:class:~corecv.core.contract.FeatureInfo and dynamically instantiates
all 1x1 and 3x3 convolutions based on backbone metadata.
Example
from corecv.models.backbones.resnet import ResNet50Backbone from corecv.models.necks.panet import PANet backbone = ResNet50Backbone(pretrained=False) neck = PANet(feature_info=backbone.feature_info, out_channels=256) features = backbone(torch.randn(1, 3, 224, 224)) pyramid = neck(features) tuple(f.shape for f in pyramid) (torch.Size([1, 256, 56, 56]), torch.Size([1, 256, 28, 28]), torch.Size([1, 256, 14, 14]), torch.Size([1, 256, 7, 7]))
PANet(feature_info, out_channels=256)
¶
Bases: Module
Path Aggregation Network neck with dynamic channel projection.
Combines a top-down FPN pathway with a bottom-up path augmentation, yielding richer multi-scale features for detection and segmentation heads.
Attributes:
| Name | Type | Description |
|---|---|---|
out_channels |
Uniform channel count for all output feature levels. |
|
levels |
Sorted list of |
|
lateral_convs |
1x1 convolutions for FPN top-down lateral connections. |
|
fpn_convs |
3x3 convolutions after FPN top-down merges. |
|
panet_reduce_convs |
3x3 stride-2 convolutions for bottom-up spatial downsampling between adjacent PANet levels. |
|
panet_lateral_convs |
1x1 convolutions that project FPN outputs into the PANet bottom-up pathway. |
|
panet_convs |
3x3 convolutions applied after each bottom-up addition. |
Initialise the PANet neck.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_info
|
FeatureInfo
|
Metadata describing the backbone's feature levels. Used to determine the number of levels and their input channel counts for all convolution construction. |
required |
out_channels
|
int
|
Output channel dimension for every pyramid level. All lateral and projection convolutions target this width. |
256
|
Source code in src/corecv/models/necks/panet.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
forward(features)
¶
Apply FPN top-down then PANet bottom-up pathways.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Sequence[Tensor]
|
Sequence of backbone feature tensors in stride-
ascending order (highest resolution first). The length
must match the number of levels in the
:class: |
required |
Returns:
| Type | Description |
|---|---|
list[Tensor]
|
A list of feature tensors in stride-ascending order (smallest |
list[Tensor]
|
stride first), each with shape |
list[Tensor]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of input features does not match the number of registered backbone levels. |
Source code in src/corecv/models/necks/panet.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
Heads¶
Classification Head¶
corecv.models.heads.classification
¶
Classification head modules for CoreCV.
Provides a simple linear classification head that consumes the coarsest feature level from a backbone or neck and produces per-class logits.
Available classification heads¶
.. list-table:: :header-rows: 1 :widths: 25 30 45
-
- Class
- Registry Key
- Description
-
- :class:
LinearClassificationHead linear_classification- Global average pooling + fully-connected layer for image-level classification.
- :class:
Example
from corecv.models.heads.classification import LinearClassificationHead from corecv.core.contract import FeatureInfo fi = FeatureInfo(channels={"feat": 2048}, strides={"feat": 32}) head = LinearClassificationHead(feature_info=fi, num_classes=1000)
LinearClassificationHead(feature_info, num_classes, dropout=0.0)
¶
Bases: Module
Image-level classification head with global average pooling.
Consumes the coarsest (last) feature map from a backbone or neck,
applies adaptive average pooling to 1x1, and passes through a
1x1 convolution (equivalent to nn.Linear on a 1x1 spatial map)
to produce per-class logits.
A 1x1 convolution is used instead of nn.Linear to ensure
compatibility with device='meta' shape propagation.
The head dynamically inspects the :class:FeatureInfo to determine
the input channel count, making it compatible with any backbone
that implements :class:~corecv.core.contract.BaseBackbone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_info
|
FeatureInfo
|
Metadata from the upstream backbone, used to determine input channel count from the coarsest feature level. |
required |
num_classes
|
int
|
Number of output classes. |
required |
dropout
|
float
|
Optional dropout probability applied before the
classifier (default |
0.0
|
Initialise the classification head.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_info
|
FeatureInfo
|
Backbone feature metadata. |
required |
num_classes
|
int
|
Number of output classes. |
required |
dropout
|
float
|
Dropout probability (default 0.0). |
0.0
|
Source code in src/corecv/models/heads/classification/linear_head.py
feature_info
property
¶
Return the feature metadata used at construction time.
Returns:
| Name | Type | Description |
|---|---|---|
The |
FeatureInfo
|
class: |
forward(features)
¶
Produce classification logits from multi-scale feature maps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
dict[str, Tensor] | Sequence[Tensor]
|
Feature maps from a backbone or neck. Can be a
|
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Logit tensor of shape |
Source code in src/corecv/models/heads/classification/linear_head.py
Detection Head¶
corecv.models.heads.detection
¶
Detection head modules for CoreCV.
Provides :class:~corecv.core.contract.FeatureInfo-aware detection heads
for anchor-free and query-based object detection.
Available heads¶
.. list-table:: :header-rows: 1 :widths: 25 20 30
-
- Class
- Registry Key
- Description
-
- :class:
DecoupledAnchorFreeHead decoupled_anchor_free- FCOS/YOLOX-style pure-conv head with decoupled cls/reg branches.
- :class:
-
- :class:
QueryDetectionHead query_detection- RT-DETR/D-FINE-style transformer-decoder head with learnable queries and NMS-free inference.
- :class:
Example
from corecv.models.heads.detection import ( ... DecoupledAnchorFreeHead, ... QueryDetectionHead, ... )
DecoupledAnchorFreeHead(feature_info, num_classes, feat_channels=256, num_convs=4)
¶
Bases: Module
FCOS/YOLOX-style decoupled anchor-free detection head.
Dynamically adapts to an upstream :class:FeatureInfo by constructing
per-level branches whose input channels and count match the feature
map metadata. The head is fully stride-aware: a per-level stride
buffer is registered so that downstream loss and post-processing can
map spatial predictions back to the original image coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_info
|
FeatureInfo
|
Feature metadata from the backbone or neck. The head
builds one branch per key in |
required |
num_classes
|
int
|
Number of foreground object classes (excluding background). |
required |
feat_channels
|
int
|
Intermediate channel dimension inside each per-level
conv stack. Default |
256
|
num_convs
|
int
|
Number of 3x3 |
4
|
Initialise the decoupled anchor-free detection head.
Builds per-level branches matching the FeatureInfo metadata and
registers per-level stride buffers for coordinate mapping.
Source code in src/corecv/models/heads/detection/decoupled_anchor_free.py
level_names
property
¶
Return the ordered list of feature level names.
level_strides
property
¶
Return the ordered list of per-level stride values.
num_levels
property
¶
Return the number of feature levels this head operates on.
forward(features)
¶
Run detection on all feature levels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
list[Tensor]
|
List of feature tensors from the backbone or neck,
one per level, ordered from finest to coarsest spatial
resolution. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, list[Tensor]]
|
Dictionary with keys:
- |
Source code in src/corecv/models/heads/detection/decoupled_anchor_free.py
get_stride_tensors(device)
¶
Return per-level stride tensors on the given device.
Used by downstream loss modules and post-processors that need to map per-cell spatial predictions back to image coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device
|
Target device for the stride tensors. |
required |
Returns:
| Type | Description |
|---|---|
list[Tensor]
|
List of scalar |
Source code in src/corecv/models/heads/detection/decoupled_anchor_free.py
QueryDetectionHead(feature_info, num_classes, d_model=256, num_queries=300, num_decoder_layers=6, num_heads=8, dim_feedforward=2048, dropout=0.1, num_reg_fcs=3, return_intermediate=False)
¶
Bases: Module
RT-DETR / D-FINE style query-based detection head.
Learns a set of fixed object queries that attend to multi-scale features
via a transformer decoder. Each query directly regresses a normalised
(cx, cy, w, h) bounding box and produces class logits, enabling
NMS-free inference.
The head dynamically inspects FeatureInfo.channels to construct
per-level input projections, making it transparent to backbone and neck
architecture changes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_info
|
FeatureInfo
|
Feature metadata from the backbone or neck. Per-level
projections are built from |
required |
num_classes
|
int
|
Number of foreground object classes (excluding background). |
required |
d_model
|
int
|
Transformer hidden dimension. Default |
256
|
num_queries
|
int
|
Number of learnable object queries. Default |
300
|
num_decoder_layers
|
int
|
Depth of the transformer decoder. Default |
6
|
num_heads
|
int
|
Number of attention heads in the decoder. Default |
8
|
dim_feedforward
|
int
|
FFN hidden dimension in the decoder. Default
|
2048
|
dropout
|
float
|
Dropout probability. Default |
0.1
|
num_reg_fcs
|
int
|
Number of FC layers in the box regression MLP.
Default |
3
|
return_intermediate
|
bool
|
If |
False
|
Initialise the query-based detection head.
Builds multi-scale feature projections, learnable object queries, a transformer decoder stack, and classification / regression heads.
Source code in src/corecv/models/heads/detection/query_detection_head.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |
level_names
property
¶
Return the ordered list of feature level names.
level_strides
property
¶
Return the ordered list of per-level stride values.
num_levels
property
¶
Return the number of feature levels.
forward(features)
¶
Run query-based detection on multi-scale features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
list[Tensor]
|
List of feature tensors from the backbone or neck,
one per level, ordered from finest to coarsest spatial
resolution. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict[str, Tensor | list[Tensor]]
|
Dictionary with keys:
- |
|
dict[str, Tensor | list[Tensor]]
|
If |
|
contains |
dict[str, Tensor | list[Tensor]]
|
|
Source code in src/corecv/models/heads/detection/query_detection_head.py
get_reference_points(features)
¶
Generate normalised reference points for each spatial location.
Produces a grid of cx, cy coordinates in [0, 1] for each
feature level. This is useful for downstream box refinement (e.g.
D-FINE style iterative decoding).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
list[Tensor]
|
Feature tensors (used only for spatial dimensions). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Reference points of shape |
Tensor
|
|
Source code in src/corecv/models/heads/detection/query_detection_head.py
Segmentation Head¶
corecv.models.heads.segmentation
¶
Segmentation head modules for CoreCV.
Provides decoder implementations for semantic segmentation tasks. All heads
dynamically adapt to backbone
:class:~corecv.core.contract.FeatureInfo metadata and are registered in
:func:~corecv.core.registry.HEAD_REGISTRY.
Available segmentation heads¶
.. list-table:: :header-rows: 1 :widths: 25 30 45
-
- Class
- Registry Key
- Description
-
- :class:
ResUNetDecoder resunet_decoder- U-Net style decoder with residual blocks and skip connections
- :class:
-
- :class:
ASPPDecoder aspp_decoder- DeepLabV3+ style decoder with ASPP context module
- :class:
Example
from corecv.models.backbones.resnet import ResNet50Backbone from corecv.models.heads.segmentation import ASPPDecoder backbone = ResNet50Backbone(pretrained=False) decoder = ASPPDecoder( ... feature_info=backbone.feature_info, ... out_channels=256, ... num_classes=21, ... ) feats = backbone(torch.randn(1, 3, 224, 224)) logits = decoder(feats)
ASPPDecoder(feature_info, out_channels=256, num_classes=21, atrous_rates=(6, 12, 18), dropout=0.5)
¶
Bases: Module
DeepLabV3+ style segmentation decoder with ASPP and lightweight decoder.
Applies ASPP (Atrous Spatial Pyramid Pooling) to the coarsest feature level for multi-scale context aggregation, then fuses with low-level encoder features through a lightweight decoder path.
Input projections are created dynamically from
:class:~corecv.core.contract.FeatureInfo metadata, making this
decoder compatible with any backbone that exposes multi-scale features.
For backbones with 4 levels (stride 4, 8, 16, 32): - ASPP is applied to stride-32 features. - Low-level features come from stride-8. - Decoder upsamples 32->8 (4x) then 8->2 (4x) for 8x total.
For backbones with 3 levels (stride 8, 16, 32): - ASPP is applied to stride-32 features. - Low-level features come from stride-8 (the finest available). - Decoder upsamples 32->8 (4x) then 8->2 (4x) for 8x total.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_info
|
FeatureInfo
|
Feature metadata from the backbone or neck, containing channel counts and strides for each feature level. |
required |
out_channels
|
int
|
Internal channel dimension throughout the decoder. |
256
|
num_classes
|
int
|
Number of output segmentation classes. |
21
|
atrous_rates
|
Sequence[int]
|
Dilation rates for the ASPP module. |
(6, 12, 18)
|
dropout
|
float
|
Dropout probability in the ASPP and classification head. |
0.5
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Initialise the ASPP decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_info
|
FeatureInfo
|
Feature metadata from the backbone or neck. |
required |
out_channels
|
int
|
Internal channel dimension. |
256
|
num_classes
|
int
|
Number of segmentation classes. |
21
|
atrous_rates
|
Sequence[int]
|
ASPP dilation rates. |
(6, 12, 18)
|
dropout
|
float
|
Dropout probability. |
0.5
|
Source code in src/corecv/models/heads/segmentation/aspp_decoder.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | |
forward(features)
¶
Forward pass through the ASPP decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Sequence[Tensor]
|
Ordered sequence of feature tensors from the backbone,
sorted by ascending stride (finest to coarsest). Each
tensor has shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Segmentation logits of shape |
Tensor
|
where |
Tensor
|
feature's spatial dimensions (or 4x if a single decoder stage |
Tensor
|
suffices). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of features does not match the number
of levels in |
Source code in src/corecv/models/heads/segmentation/aspp_decoder.py
ResUNetDecoder(feature_info, out_channels=256, num_classes=21, dropout=0.1)
¶
Bases: Module
U-Net style segmentation decoder with residual blocks and skip connections.
Consumes an ordered list of multi-scale feature maps (finest to
coarsest) and produces per-pixel class logits at the original input
resolution. Input projections are created dynamically from
:class:~corecv.core.contract.FeatureInfo metadata.
The decoder expects features sorted by ascending stride (finest first). For backbones with 4 levels (stride 4, 8, 16, 32), the full decoder path is used. For backbones with 3 levels (stride 8, 16, 32), the stride-4 stage is skipped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_info
|
FeatureInfo
|
Feature metadata from the backbone or neck, containing channel counts and strides for each feature level. |
required |
out_channels
|
int
|
Number of channels in the bottleneck and decoder internal feature maps. |
256
|
num_classes
|
int
|
Number of output segmentation classes. |
21
|
dropout
|
float
|
Dropout probability applied to the final classification
head (default |
0.1
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Initialise the ResUNet decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_info
|
FeatureInfo
|
Feature metadata from the backbone or neck. |
required |
out_channels
|
int
|
Internal channel dimension. |
256
|
num_classes
|
int
|
Number of segmentation classes. |
21
|
dropout
|
float
|
Dropout probability in the classification head. |
0.1
|
Source code in src/corecv/models/heads/segmentation/resunet_decoder.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
forward(features)
¶
Forward pass through the ResUNet decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Sequence[Tensor]
|
Ordered sequence of feature tensors from the backbone,
sorted by ascending stride (finest to coarsest). Each
tensor has shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Segmentation logits of shape |
Tensor
|
where |
Tensor
|
finest-scale input feature. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of features does not match the number
of levels in |