Team Ai
Apppublic

Arulkumar03/Fox_Sheep_Detector_Computer_Vision_model

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
backbone.py75 linesDownload Raw Back to backbone
1# Copyright (c) Facebook, Inc. and its affiliates.2from abc import ABCMeta, abstractmethod3from typing import Dict4import torch.nn as nn5 6from detectron2.layers import ShapeSpec7 8__all__ = ["Backbone"]9 10 11class Backbone(nn.Module, metaclass=ABCMeta):12    """13    Abstract base class for network backbones.14    """15 16    def __init__(self):17        """18        The `__init__` method of any subclass can specify its own set of arguments.19        """20        super().__init__()21 22    @abstractmethod23    def forward(self):24        """25        Subclasses must override this method, but adhere to the same return type.26 27        Returns:28            dict[str->Tensor]: mapping from feature name (e.g., "res2") to tensor29        """30        pass31 32    @property33    def size_divisibility(self) -> int:34        """35        Some backbones require the input height and width to be divisible by a36        specific integer. This is typically true for encoder / decoder type networks37        with lateral connection (e.g., FPN) for which feature maps need to match38        dimension in the "bottom up" and "top down" paths. Set to 0 if no specific39        input size divisibility is required.40        """41        return 042 43    @property44    def padding_constraints(self) -> Dict[str, int]:45        """46        This property is a generalization of size_divisibility. Some backbones and training47        recipes require specific padding constraints, such as enforcing divisibility by a specific48        integer (e.g., FPN) or padding to a square (e.g., ViTDet with large-scale jitter49        in :paper:vitdet). `padding_constraints` contains these optional items like:50        {51            "size_divisibility": int,52            "square_size": int,53            # Future options are possible54        }55        `size_divisibility` will read from here if presented and `square_size` indicates the56        square padding size if `square_size` > 0.57 58        TODO: use type of Dict[str, int] to avoid torchscipt issues. The type of padding_constraints59        could be generalized as TypedDict (Python 3.8+) to support more types in the future.60        """61        return {}62 63    def output_shape(self):64        """65        Returns:66            dict[str->ShapeSpec]67        """68        # this is a backward-compatible default69        return {70            name: ShapeSpec(71                channels=self._out_feature_channels[name], stride=self._out_feature_strides[name]72            )73            for name in self._out_features74        }75