_utils.py 1.17 KB
Newer Older
1
from collections import OrderedDict
2
from typing import Optional, Dict
3

4
from torch import nn, Tensor
5
6
from torch.nn import functional as F

7
from ...utils import _log_api_usage_once
8

9
10

class _SimpleSegmentationModel(nn.Module):
11
12
13
    __constants__ = ["aux_classifier"]

    def __init__(self, backbone: nn.Module, classifier: nn.Module, aux_classifier: Optional[nn.Module] = None) -> None:
14
        super().__init__()
Kai Zhang's avatar
Kai Zhang committed
15
        _log_api_usage_once(self)
16
17
18
19
        self.backbone = backbone
        self.classifier = classifier
        self.aux_classifier = aux_classifier

20
    def forward(self, x: Tensor) -> Dict[str, Tensor]:
21
22
23
24
25
26
27
        input_shape = x.shape[-2:]
        # contract: features is a dict of tensors
        features = self.backbone(x)

        result = OrderedDict()
        x = features["out"]
        x = self.classifier(x)
28
        x = F.interpolate(x, size=input_shape, mode="bilinear", align_corners=False)
29
30
31
32
33
        result["out"] = x

        if self.aux_classifier is not None:
            x = features["aux"]
            x = self.aux_classifier(x)
34
            x = F.interpolate(x, size=input_shape, mode="bilinear", align_corners=False)
35
36
37
            result["aux"] = x

        return result