generalized_rcnn.py 4.4 KB
Newer Older
1
2
3
4
"""
Implements the Generalized R-CNN framework
"""

5
import warnings
6
from collections import OrderedDict
7
8
from typing import Tuple, List, Dict, Optional, Union

9
import torch
10
from torch import nn, Tensor
11

12
13
from ...utils import _log_api_usage_once

14
15
16
17
18

class GeneralizedRCNN(nn.Module):
    """
    Main class for Generalized R-CNN.

19
    Args:
20
21
        backbone (nn.Module):
        rpn (nn.Module):
22
        roi_heads (nn.Module): takes the features + the proposals from the RPN and computes
23
24
25
26
27
            detections / masks from it.
        transform (nn.Module): performs the data transformation from the inputs to feed into
            the model
    """

28
    def __init__(self, backbone: nn.Module, rpn: nn.Module, roi_heads: nn.Module, transform: nn.Module) -> None:
29
        super().__init__()
Kai Zhang's avatar
Kai Zhang committed
30
        _log_api_usage_once(self)
31
32
33
34
        self.transform = transform
        self.backbone = backbone
        self.rpn = rpn
        self.roi_heads = roi_heads
35
36
        # used only on torchscript mode
        self._has_warned = False
37

eellison's avatar
eellison committed
38
    @torch.jit.unused
39
40
    def eager_outputs(self, losses, detections):
        # type: (Dict[str, Tensor], List[Dict[str, Tensor]]) -> Union[Dict[str, Tensor], List[Dict[str, Tensor]]]
eellison's avatar
eellison committed
41
42
43
44
45
        if self.training:
            return losses

        return detections

46
47
    def forward(self, images, targets=None):
        # type: (List[Tensor], Optional[List[Dict[str, Tensor]]]) -> Tuple[Dict[str, Tensor], List[Dict[str, Tensor]]]
48
        """
49
        Args:
50
            images (list[Tensor]): images to be processed
51
            targets (list[Dict[str, Tensor]]): ground-truth boxes present in the image (optional)
52
53
54
55
56
57
58
59
60
61

        Returns:
            result (list[BoxList] or dict[Tensor]): the output from the model.
                During training, it returns a dict[Tensor] which contains the losses.
                During testing, it returns list[BoxList] contains additional fields
                like `scores`, `labels` and `mask` (for Mask R-CNN models).

        """
        if self.training and targets is None:
            raise ValueError("In training mode, targets should be passed")
62
63
64
65
66
67
        if self.training:
            assert targets is not None
            for target in targets:
                boxes = target["boxes"]
                if isinstance(boxes, torch.Tensor):
                    if len(boxes.shape) != 2 or boxes.shape[-1] != 4:
68
                        raise ValueError(f"Expected target boxes to be a tensor of shape [N, 4], got {boxes.shape}.")
69
                else:
70
                    raise ValueError(f"Expected target boxes to be of type Tensor, got {type(boxes)}.")
71

72
        original_image_sizes: List[Tuple[int, int]] = []
eellison's avatar
eellison committed
73
74
75
76
77
        for img in images:
            val = img.shape[-2:]
            assert len(val) == 2
            original_image_sizes.append((val[0], val[1]))

78
        images, targets = self.transform(images, targets)
79
80
81
82
83
84
85
86

        # Check for degenerate boxes
        # TODO: Move this to a function
        if targets is not None:
            for target_idx, target in enumerate(targets):
                boxes = target["boxes"]
                degenerate_boxes = boxes[:, 2:] <= boxes[:, :2]
                if degenerate_boxes.any():
87
                    # print the first degenerate box
88
                    bb_idx = torch.where(degenerate_boxes.any(dim=1))[0][0]
89
                    degen_bb: List[float] = boxes[bb_idx].tolist()
90
91
                    raise ValueError(
                        "All bounding boxes should have positive height and width."
92
                        f" Found invalid box {degen_bb} for target at index {target_idx}."
93
                    )
94

95
96
        features = self.backbone(images.tensors)
        if isinstance(features, torch.Tensor):
97
            features = OrderedDict([("0", features)])
98
99
        proposals, proposal_losses = self.rpn(images, features, targets)
        detections, detector_losses = self.roi_heads(features, proposals, images.image_sizes, targets)
100
        detections = self.transform.postprocess(detections, images.image_sizes, original_image_sizes)  # type: ignore[operator]
101
102
103
104
105

        losses = {}
        losses.update(detector_losses)
        losses.update(proposal_losses)

eellison's avatar
eellison committed
106
        if torch.jit.is_scripting():
107
108
109
            if not self._has_warned:
                warnings.warn("RCNN always returns a (Losses, Detections) tuple in scripting")
                self._has_warned = True
110
            return losses, detections
eellison's avatar
eellison committed
111
112
        else:
            return self.eager_outputs(losses, detections)