示例#1
0
    def init_processors(self):
        if not hasattr(self.config, "processors"):
            return
        extra_params = {"data_dir": self.config.data_dir}
        for processor_key, processor_params in self.config.processors.items():
            if not processor_params:
                continue
            reg_key = f"{self._dataset_name}_{processor_key}"
            reg_check = registry.get(reg_key, no_warning=True)

            if reg_check is None:
                processor_object = Processor(processor_params, **extra_params)
                setattr(self, processor_key, processor_object)
                registry.register(reg_key, processor_object)
            else:
                setattr(self, processor_key, reg_check)
示例#2
0
def build_processors(processors_config: mmf_typings.DictConfig,
                     registry_key: str = None,
                     *args,
                     **kwargs) -> ProcessorDict:
    """Given a processor config, builds the processors present and returns back
    a dict containing processors mapped to keys as per the config

    Args:
        processors_config (mmf_typings.DictConfig): OmegaConf DictConfig describing
            the parameters and type of each processor passed here

        registry_key (str, optional): If passed, function would look into registry for
            this particular key and return it back. .format with processor_key will
            be called on this string. Defaults to None.

    Returns:
        ProcessorDict: Dictionary containing key to
            processor mapping
    """
    from mmf.datasets.processors.processors import Processor

    processor_dict = {}

    for processor_key, processor_params in processors_config.items():
        if not processor_params:
            continue

        processor_instance = None
        if registry_key is not None:
            full_key = registry_key.format(processor_key)
            processor_instance = registry.get(full_key, no_warning=True)

        if processor_instance is None:
            processor_instance = Processor(processor_params, *args, **kwargs)
            # We don't register back here as in case of hub interface, we
            # want the processors to be instantiate every time. BaseDataset
            # can register at its own end
        processor_dict[processor_key] = processor_instance

    return processor_dict
示例#3
0
 def test_processor_class_None(self):
     config = OmegaConf.create({"type": "UndefinedType"})
     with self.assertRaises(ValueError):
         Processor(config)