54 lines
1.5 KiB
Python
Raw Normal View History

2024-03-19 01:50:17 +08:00
# Copyright (c) Facebook, Inc. and its affiliates.
2024-03-19 10:31:00 +08:00
from abc import ABCMeta, abstractmethod
2024-03-19 01:50:17 +08:00
import torch.nn as nn
2024-03-19 10:31:00 +08:00
from detectron2.layers import ShapeSpec
2024-03-19 01:50:17 +08:00
__all__ = ["Backbone"]
2024-03-19 10:31:00 +08:00
class Backbone(nn.Module, metaclass=ABCMeta):
2024-03-19 01:50:17 +08:00
"""
Abstract base class for network backbones.
"""
def __init__(self):
"""
The `__init__` method of any subclass can specify its own set of arguments.
"""
super().__init__()
2024-03-19 10:31:00 +08:00
@abstractmethod
2024-03-19 01:50:17 +08:00
def forward(self):
"""
Subclasses must override this method, but adhere to the same return type.
Returns:
dict[str->Tensor]: mapping from feature name (e.g., "res2") to tensor
"""
pass
@property
def size_divisibility(self) -> int:
"""
Some backbones require the input height and width to be divisible by a
specific integer. This is typically true for encoder / decoder type networks
with lateral connection (e.g., FPN) for which feature maps need to match
dimension in the "bottom up" and "top down" paths. Set to 0 if no specific
input size divisibility is required.
"""
return 0
def output_shape(self):
"""
Returns:
dict[str->ShapeSpec]
"""
# this is a backward-compatible default
return {
name: ShapeSpec(
channels=self._out_feature_channels[name], stride=self._out_feature_strides[name]
)
for name in self._out_features
}