FastRCNNForObjectDetection
PretrainedModelFastRCNNForObjectDetection(config: FastRCNNConfig)Fast R-CNN object detector (Girshick, ICCV 2015).
The successor to R-CNN that fixes its main bottleneck: rather than running
the CNN once per proposal, the backbone is applied once to the whole
image, and per-proposal features are extracted via roi_pool over
the shared feature map (7x7 at stride 16 for the VGG16 default). The
pooled tensor is flattened, passed through two FC layers
(fc6, fc7), and split into sibling class (cls_score) and
class-specific bounding-box (bbox_pred) heads. At training time the
model computes the paper's multi-task loss
with the categorical cross-entropy and the smooth- regression loss applied only to foreground proposals.
Parameters
configFastRCNNConfigfast_rcnn factory for
the paper-cited VGG16 configuration (RoI 7x7, stride-16 feature
map, 80 COCO classes).Attributes
configFastRCNNConfigbackbone_VGG16Featuresroi_head_FastRCNNHeadfc6 (4096) -> fc7 (4096)
-> sibling cls_score (K + 1 logits) and bbox_pred
(4K class-specific deltas).Notes
See Girshick, "Fast R-CNN", ICCV 2015 (arXiv:1504.08083). Bounding-box targets follow the paper's parameterisation
with the default normalisation weights matching the Fast R-CNN Caffe reference. RoI Pool (not
RoI Align) is used to preserve faithfulness — see MaskRCNNForObjectDetection
for the RoI Align successor. Per-class boxes are decoded for all classes
and per-class NMS is applied by postprocess.
Examples
Inference with externally-supplied proposals:
>>> import lucid
>>> from lucid.models.vision.fast_rcnn import fast_rcnn
>>> model = fast_rcnn(num_classes=20)
>>> x = lucid.randn(1, 3, 600, 800)
>>> proposals = [lucid.tensor(
... [[10.0, 10.0, 200.0, 200.0],
... [50.0, 60.0, 300.0, 280.0]])]
>>> out = model(x, proposals)
>>> out.logits.shape
(2, 21)
>>> out.loss is None
True
Training with ground-truth targets to compute the multi-task loss:
>>> targets = [{
... "boxes": lucid.tensor([[20.0, 20.0, 180.0, 180.0]]),
... "labels": lucid.tensor([3], dtype=lucid.int64),
... }]
>>> out = model(x, proposals, targets=targets)
>>> out.loss.shape
()Used by 2
Constructors
1Instance methods
2forward(x: Tensor, proposals: list[Tensor] | None = None, targets: list[dict[str, Tensor]] | None = None)Run Fast R-CNN on a batch of images.
Parameters
Returns
ObjectDetectionOutputObjectDetectionOutput:
logits : (Σ N_i, num_classes + 1) raw class logits.
pred_boxes : (Σ N_i, 4) decoded xyxy top-class boxes.
loss : scalar multi-task loss (only with targets).
postprocess(output: ObjectDetectionOutput, proposals: list[Tensor])Apply per-class NMS to raw Fast R-CNN output.
Parameters
outputObjectDetectionOutputObjectDetectionOutput from forward().proposalslist[Tensor]forward() (for shape info).Returns
list[dict[str, Tensor]]Per-image list of result dicts:
"boxes" : (K_det, 4) kept xyxy detections
"scores" : (K_det,) class confidence scores
"labels" : (K_det,) class indices (1-based)