Spaces:
Sleeping
Sleeping
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# Copyright (c) Facebook, Inc. and its affiliates. | |
import torch.nn as nn | |
from detectron2.modeling import ShapeSpec | |
# from ..layers import ShapeSpec | |
__all__ = ["Backbone"] | |
class Backbone(nn.Module): | |
""" | |
Abstract base class for network backbones. | |
""" | |
def __init__(self): | |
""" | |
The `__init__` method of any subclass can specify its own set of arguments. | |
""" | |
super().__init__() | |
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 | |
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 | |
} | |