Skip to content
class

FastRCNNForObjectDetection

extendsObjectDetectionModel
FastRCNNForObjectDetection(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

L=Lcls+λ Lloc,L = L_{\mathrm{cls}} + \lambda\, L_{\mathrm{loc}},

with LclsL_{\mathrm{cls}} the categorical cross-entropy and LlocL_{\mathrm{loc}} the smooth-L1L_1 regression loss applied only to foreground proposals.

Parameters

Frozen architecture spec. Use the fast_rcnn factory for the paper-cited VGG16 configuration (RoI 7x7, stride-16 feature map, 80 COCO classes).

Attributes

Stored copy of the config that built this model.
backbone_VGG16Features
VGG16 conv1_1 .. conv5_3 trunk (pool5 omitted) producing a (B,512,H/16,W/16)(B, 512, H/16, W/16) feature map.
roi_head_FastRCNNHead
RoI feature processor: flatten -> fc6 (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

tx=wxGx−PxPw,ty=wyGy−PyPh,tw=wwlog⁡ ⁣GwPw,th=whlog⁡ ⁣GhPh,t_x = w_x \frac{G_x - P_x}{P_w},\quad t_y = w_y \frac{G_y - P_y}{P_h},\quad t_w = w_w \log\!\frac{G_w}{P_w},\quad t_h = w_h \log\!\frac{G_h}{P_h},

with the default normalisation weights (wx,wy,ww,wh)=(10,10,5,5)(w_x, w_y, w_w, w_h) = (10, 10, 5, 5) 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

1
dunder

__init__

→None
__init__(config: FastRCNNConfig)

Instance methods

2
forward(x: Tensor, proposals: list[Tensor] | None = None, targets: list[dict[str, Tensor]] | None = None)

Run Fast R-CNN on a batch of images.

Parameters

(B, C, H, W) image batch.
proposalslist[Tensor] | None= None
list of B tensors, each (N_i, 4) xyxy proposals.
targetslist[dict[str, Tensor]] | None= None
Optional training targets (list of dicts with "boxes" and "labels").

Returns

ObjectDetectionOutput

ObjectDetectionOutput: 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).

fn

postprocess

→list[dict[str, Tensor]]
postprocess(output: ObjectDetectionOutput, proposals: list[Tensor])

Apply per-class NMS to raw Fast R-CNN output.

Parameters

ObjectDetectionOutput from forward().
proposalslist[Tensor]
Proposal list passed to 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)