|
| 1 | +import numpy as np |
| 2 | +import torch.nn as nn |
| 3 | +from mmcv.cnn import ConvModule |
| 4 | + |
| 5 | +from mmseg.ops import resize |
| 6 | +from ..builder import HEADS |
| 7 | +from .decode_head import BaseDecodeHead |
| 8 | + |
| 9 | + |
| 10 | +@HEADS.register_module() |
| 11 | +class FPNHead(BaseDecodeHead): |
| 12 | + """Panoptic Feature Pyramid Networks. |
| 13 | +
|
| 14 | + This head is the implementation of `Semantic FPN |
| 15 | + <https://arxiv.org/abs/1901.02446>`_. |
| 16 | +
|
| 17 | + Args: |
| 18 | + feature_strides (tuple[int]): The strides for input feature maps. |
| 19 | + stack_lateral. All strides suppose to be power of 2. The first |
| 20 | + one is of largest resolution. |
| 21 | + """ |
| 22 | + |
| 23 | + def __init__(self, feature_strides, **kwargs): |
| 24 | + super(FPNHead, self).__init__( |
| 25 | + input_transform='multiple_select', **kwargs) |
| 26 | + assert len(feature_strides) == len(self.in_channels) |
| 27 | + assert min(feature_strides) == feature_strides[0] |
| 28 | + self.feature_strides = feature_strides |
| 29 | + |
| 30 | + self.scale_heads = nn.ModuleList() |
| 31 | + for i in range(len(feature_strides)): |
| 32 | + head_length = max( |
| 33 | + 1, |
| 34 | + int(np.log2(feature_strides[i]) - np.log2(feature_strides[0]))) |
| 35 | + scale_head = [] |
| 36 | + for k in range(head_length): |
| 37 | + scale_head.append( |
| 38 | + ConvModule( |
| 39 | + self.in_channels[i] if k == 0 else self.channels, |
| 40 | + self.channels, |
| 41 | + 3, |
| 42 | + padding=1, |
| 43 | + conv_cfg=self.conv_cfg, |
| 44 | + norm_cfg=self.norm_cfg, |
| 45 | + act_cfg=self.act_cfg)) |
| 46 | + if feature_strides[i] != feature_strides[0]: |
| 47 | + scale_head.append( |
| 48 | + nn.Upsample( |
| 49 | + scale_factor=2, |
| 50 | + mode='bilinear', |
| 51 | + align_corners=self.align_corners)) |
| 52 | + self.scale_heads.append(nn.Sequential(*scale_head)) |
| 53 | + |
| 54 | + def forward(self, inputs): |
| 55 | + |
| 56 | + x = self._transform_inputs(inputs) |
| 57 | + |
| 58 | + output = self.scale_heads[0](x[0]) |
| 59 | + for i in range(1, len(self.feature_strides)): |
| 60 | + # non inplace |
| 61 | + output = output + resize( |
| 62 | + self.scale_heads[i](x[i]), |
| 63 | + size=output.shape[2:], |
| 64 | + mode='bilinear', |
| 65 | + align_corners=self.align_corners) |
| 66 | + |
| 67 | + output = self.cls_seg(output) |
| 68 | + return output |
0 commit comments