mmselfsup.models.necks.linear_neck 源代码
# Copyright (c) OpenMMLab. All rights reserved.
from typing import List, Optional, Tuple, Union
import torch
import torch.nn as nn
from mmengine.model import BaseModule
from mmselfsup.registry import MODELS
[文档]@MODELS.register_module()
class LinearNeck(BaseModule):
"""The linear neck: fc only.
Args:
in_channels (int): Number of input channels.
out_channels (int): Number of output channels.
with_avg_pool (bool): Whether to apply the global
average pooling after backbone. Defaults to True.
init_cfg (dict or list[dict], optional): Initialization config dict.
Defaults to None.
"""
def __init__(self,
in_channels: int,
out_channels: int,
with_avg_pool: bool = True,
init_cfg: Optional[Union[dict, List[dict]]] = None) -> None:
super().__init__(init_cfg)
self.with_avg_pool = with_avg_pool
if with_avg_pool:
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(in_channels, out_channels)
[文档] def forward(self, x: Tuple[torch.Tensor]) -> List[torch.Tensor]:
"""Forward function.
Args:
x (List[torch.Tensor]): The feature map of backbone.
Returns:
List[torch.Tensor]: The output features.
"""
assert len(x) == 1
x = x[0]
if self.with_avg_pool:
x = self.avgpool(x)
return [self.fc(x.view(x.size(0), -1))]