    def forward(
        self,
        images: FloatTensor,
        sizes: IntTensor,
        image_features: "OrderedDict[str, FloatTensor]",
    ) -> RegionDetectorOutput:
        """
        Extract regions and region features from the given images.

        In most cases `image_features` should come directly from the `ResnetBackbone`
        `GridEmbedder`. The `images` themselves should be standardized and resized
        using the default settings for the `TorchImageLoader`.
        """
        if self.training:
            raise RuntimeError(
                "FasterRcnnRegionDetector can not be used for training at the moment"
            )

        # Adapted from https://github.com/pytorch/vision/blob/
        # 4521f6d152875974e317fa247a633e9ad1ea05c8/torchvision/models/detection/generalized_rcnn.py#L45
        # We re-implement essentially the same forward eval pass except that we
        # skip calling the backbone since we already have the `image_features`,
        # and we also unpack the call to `roi_heads` so that we can keep the `box_features`
        # that are created here:
        # https://github.com/pytorch/vision/blob/
        # 4521f6d152875974e317fa247a633e9ad1ea05c8/torchvision/models/detection/roi_heads.py#L752-L753

        image_shapes: List[Tuple[int, int]] = list((int(h), int(w)) for (h, w) in sizes)
        image_list = torchvision.models.detection.image_list.ImageList(images, image_shapes)

        # `proposals` is a list of tensors, one tensor per image, each representing a
        # fixed number of proposed regions/boxes.
        # shape (proposals[i]): (proposals_per_image, 4)
        proposals: List[Tensor]
        proposals, _ = self.detector.rpn(image_list, image_features)

        # shape: (batch_size * proposals_per_image, *)
        box_features = self.detector.roi_heads.box_roi_pool(image_features, proposals, image_shapes)

        # shape: (batch_size * proposals_per_image, *)
        box_features = self.detector.roi_heads.box_head(box_features)

        # shape (class_logits): (batch_size * proposals_per_image, num_classes)
        # shape (box_regression): (batch_size * proposals_per_image, regression_output_size)
        class_logits, box_regression = self.detector.roi_heads.box_predictor(box_features)

        # This step filters down the `proposals` to only detections that reach
        # a certain threshold.
        # Each of these is a list of tensors, one for each image in the batch.
        # shape (boxes[i]): (num_predicted_boxes, 4)
        # shape (features[i]): (num_predicted_boxes, feature_size)
        # shape (scores[i]): (num_predicted_classes,)
        # shape (labels[i]): (num_predicted_classes,)
        boxes, features, scores, labels = self._postprocess_detections(
            class_logits, box_features, box_regression, proposals, image_shapes
        )

        return RegionDetectorOutput(features, boxes, scores, labels)
Extract regions and region features from the given images.

In most cases `image_features` should come directly from the `ResnetBackbone`
`GridEmbedder`. The `images` themselves should be standardized and resized
using the default settings for the `TorchImageLoader`.
allennlp/modules/vision/region_detector.py