Team Ai
Apppublic

Arulkumar03/Wheat_HEAD_Detection_Counting_ComputerVision_Model

sourceHugging Facemitupdated 3y agoView on Hugging Face
0likes
catalog.py237 linesDownload Raw Back to data
1# Copyright (c) Facebook, Inc. and its affiliates.2import copy3import logging4import types5from collections import UserDict6from typing import List7 8from detectron2.utils.logger import log_first_n9 10__all__ = ["DatasetCatalog", "MetadataCatalog", "Metadata"]11 12 13class _DatasetCatalog(UserDict):14    """15    A global dictionary that stores information about the datasets and how to obtain them.16 17    It contains a mapping from strings18    (which are names that identify a dataset, e.g. "coco_2014_train")19    to a function which parses the dataset and returns the samples in the20    format of `list[dict]`.21 22    The returned dicts should be in Detectron2 Dataset format (See DATASETS.md for details)23    if used with the data loader functionalities in `data/build.py,data/detection_transform.py`.24 25    The purpose of having this catalog is to make it easy to choose26    different datasets, by just using the strings in the config.27    """28 29    def register(self, name, func):30        """31        Args:32            name (str): the name that identifies a dataset, e.g. "coco_2014_train".33            func (callable): a callable which takes no arguments and returns a list of dicts.34                It must return the same results if called multiple times.35        """36        assert callable(func), "You must register a function with `DatasetCatalog.register`!"37        assert name not in self, "Dataset '{}' is already registered!".format(name)38        self[name] = func39 40    def get(self, name):41        """42        Call the registered function and return its results.43 44        Args:45            name (str): the name that identifies a dataset, e.g. "coco_2014_train".46 47        Returns:48            list[dict]: dataset annotations.49        """50        try:51            f = self[name]52        except KeyError as e:53            raise KeyError(54                "Dataset '{}' is not registered! Available datasets are: {}".format(55                    name, ", ".join(list(self.keys()))56                )57            ) from e58        return f()59 60    def list(self) -> List[str]:61        """62        List all registered datasets.63 64        Returns:65            list[str]66        """67        return list(self.keys())68 69    def remove(self, name):70        """71        Alias of ``pop``.72        """73        self.pop(name)74 75    def __str__(self):76        return "DatasetCatalog(registered datasets: {})".format(", ".join(self.keys()))77 78    __repr__ = __str__79 80 81DatasetCatalog = _DatasetCatalog()82DatasetCatalog.__doc__ = (83    _DatasetCatalog.__doc__84    + """85    .. automethod:: detectron2.data.catalog.DatasetCatalog.register86    .. automethod:: detectron2.data.catalog.DatasetCatalog.get87"""88)89 90 91class Metadata(types.SimpleNamespace):92    """93    A class that supports simple attribute setter/getter.94    It is intended for storing metadata of a dataset and make it accessible globally.95 96    Examples:97    ::98        # somewhere when you load the data:99        MetadataCatalog.get("mydataset").thing_classes = ["person", "dog"]100 101        # somewhere when you print statistics or visualize:102        classes = MetadataCatalog.get("mydataset").thing_classes103    """104 105    # the name of the dataset106    # set default to N/A so that `self.name` in the errors will not trigger getattr again107    name: str = "N/A"108 109    _RENAMED = {110        "class_names": "thing_classes",111        "dataset_id_to_contiguous_id": "thing_dataset_id_to_contiguous_id",112        "stuff_class_names": "stuff_classes",113    }114 115    def __getattr__(self, key):116        if key in self._RENAMED:117            log_first_n(118                logging.WARNING,119                "Metadata '{}' was renamed to '{}'!".format(key, self._RENAMED[key]),120                n=10,121            )122            return getattr(self, self._RENAMED[key])123 124        # "name" exists in every metadata125        if len(self.__dict__) > 1:126            raise AttributeError(127                "Attribute '{}' does not exist in the metadata of dataset '{}'. Available "128                "keys are {}.".format(key, self.name, str(self.__dict__.keys()))129            )130        else:131            raise AttributeError(132                f"Attribute '{key}' does not exist in the metadata of dataset '{self.name}': "133                "metadata is empty."134            )135 136    def __setattr__(self, key, val):137        if key in self._RENAMED:138            log_first_n(139                logging.WARNING,140                "Metadata '{}' was renamed to '{}'!".format(key, self._RENAMED[key]),141                n=10,142            )143            setattr(self, self._RENAMED[key], val)144 145        # Ensure that metadata of the same name stays consistent146        try:147            oldval = getattr(self, key)148            assert oldval == val, (149                "Attribute '{}' in the metadata of '{}' cannot be set "150                "to a different value!\n{} != {}".format(key, self.name, oldval, val)151            )152        except AttributeError:153            super().__setattr__(key, val)154 155    def as_dict(self):156        """157        Returns all the metadata as a dict.158        Note that modifications to the returned dict will not reflect on the Metadata object.159        """160        return copy.copy(self.__dict__)161 162    def set(self, **kwargs):163        """164        Set multiple metadata with kwargs.165        """166        for k, v in kwargs.items():167            setattr(self, k, v)168        return self169 170    def get(self, key, default=None):171        """172        Access an attribute and return its value if exists.173        Otherwise return default.174        """175        try:176            return getattr(self, key)177        except AttributeError:178            return default179 180 181class _MetadataCatalog(UserDict):182    """183    MetadataCatalog is a global dictionary that provides access to184    :class:`Metadata` of a given dataset.185 186    The metadata associated with a certain name is a singleton: once created, the187    metadata will stay alive and will be returned by future calls to ``get(name)``.188 189    It's like global variables, so don't abuse it.190    It's meant for storing knowledge that's constant and shared across the execution191    of the program, e.g.: the class names in COCO.192    """193 194    def get(self, name):195        """196        Args:197            name (str): name of a dataset (e.g. coco_2014_train).198 199        Returns:200            Metadata: The :class:`Metadata` instance associated with this name,201            or create an empty one if none is available.202        """203        assert len(name)204        r = super().get(name, None)205        if r is None:206            r = self[name] = Metadata(name=name)207        return r208 209    def list(self):210        """211        List all registered metadata.212 213        Returns:214            list[str]: keys (names of datasets) of all registered metadata215        """216        return list(self.keys())217 218    def remove(self, name):219        """220        Alias of ``pop``.221        """222        self.pop(name)223 224    def __str__(self):225        return "MetadataCatalog(registered metadata: {})".format(", ".join(self.keys()))226 227    __repr__ = __str__228 229 230MetadataCatalog = _MetadataCatalog()231MetadataCatalog.__doc__ = (232    _MetadataCatalog.__doc__233    + """234    .. automethod:: detectron2.data.catalog.MetadataCatalog.get235"""236)237