Example #1
0
    def from_config(cls: Type[T],
                    config_dict: Dict,
                    merge_default: bool = True) -> T:
        if merge_default:
            config_dict = merge_dict(cls.get_default_config(), config_dict)

        data_elem_impl_set = DataElement.get_impls()

        # Translate prototext and model sub-configs into DataElement instances.
        config_dict['network_prototxt'] = \
            from_config_dict(config_dict['network_prototxt'],
                             data_elem_impl_set)
        config_dict['network_model'] = \
            from_config_dict(config_dict['network_model'],
                             data_elem_impl_set)

        # Translate optionally provided image mean sub-config into a
        # DataElement instance. May have been provided as ``None`` or a
        # configuration dictionary with type ``None`.
        # None, dict[type=None], dict[type=str]
        if config_dict['image_mean'] is None \
                or config_dict['image_mean'].get('type', None) is None:
            config_dict['image_mean'] = None
        else:
            config_dict['image_mean'] = \
                from_config_dict(config_dict['image_mean'], data_elem_impl_set)

        return super(CaffeDescriptorGenerator,
                     cls).from_config(config_dict, merge_default=False)
Example #2
0
    def from_config(cls: Type[T_IF],
                    config_dict: Dict,
                    merge_default: bool = True) -> T_IF:
        """
        Instantiate a new instance of this class given the JSON-compliant
        configuration dictionary encapsulating initialization arguments.

        :param config_dict: JSON compliant dictionary encapsulating
            a configuration.
        :param merge_default: Merge the given configuration on top of the
            default provided by ``get_default_config``.

        :return: Constructed instance from the provided config.

        """
        if merge_default:
            config_dict = merge_dict(cls.get_default_config(), config_dict)

        data_element_impls = DataElement.get_impls()
        # Mean vector cache element.
        mean_vec_cache = None
        if config_dict['mean_vec_cache'] and \
                config_dict['mean_vec_cache']['type']:
            mean_vec_cache = from_config_dict(config_dict['mean_vec_cache'],
                                              data_element_impls)
        config_dict['mean_vec_cache'] = mean_vec_cache
        # Rotation matrix cache element.
        rotation_cache = None
        if config_dict['rotation_cache'] and \
                config_dict['rotation_cache']['type']:
            rotation_cache = from_config_dict(config_dict['rotation_cache'],
                                              data_element_impls)
        config_dict['rotation_cache'] = rotation_cache

        return super(ItqFunctor, cls).from_config(config_dict, False)
Example #3
0
def test_from_config_dict_assertion_error() -> None:
    """
    Test that assertion error is raised when a class is provided AND specified
    that does not descend from the Configurable interface.
    """
    class NotConfigurable(object):
        """ Not a configurable class. """

    test_class_set = T_CLASS_SET | {NotConfigurable}

    test_config: Dict[str, Any] = {
        'type': 'tests.test_configuration.NotConfigurable',
        'tests.test_configuration.T1': {
            'foo': 256,
            'bar': 'Some string value'
        },
        'tests.test_configuration.T2': {
            'child': {
                'foo': -1,
                'bar': 'some other value'
            },
            'alpha': 1.0,
            'beta': 'euclidean',
        },
        'tests.test_configuration.NotConfigurable': {}
    }
    with pytest.raises(
            AssertionError,
            match="Configured class type 'NotConfigurable' does not "
            r"descend from `Configurable`\."):
        from_config_dict(test_config, test_class_set)  # type: ignore
Example #4
0
    def from_config(
        cls: Type[T_LSH],
        config_dict: Dict,
        merge_default: bool = True
    ) -> T_LSH:
        """
        Instantiate a new instance of this class given the configuration
        JSON-compliant dictionary encapsulating initialization arguments.

        This method should not be called via super unless and instance of the
        class is desired.

        :param config_dict: JSON compliant dictionary encapsulating
            a configuration.
        :type config_dict: dict

        :param merge_default: Merge the given configuration on top of the
            default provided by ``get_default_config``.
        :type merge_default: bool

        :return: Constructed instance from the provided config.
        :rtype: LSHNearestNeighborIndex

        """
        # Controlling merge here so we can control known comment stripping from
        # default config.
        if merge_default:
            merged = cls.get_default_config()
            merge_dict(merged, config_dict)
        else:
            merged = config_dict

        merged['lsh_functor'] = \
            from_config_dict(merged['lsh_functor'], LshFunctor.get_impls())
        merged['descriptor_set'] = \
            from_config_dict(merged['descriptor_set'],
                             DescriptorSet.get_impls())

        # Hash index may be None for a default at-query-time linear indexing
        if merged['hash_index'] and merged['hash_index']['type']:
            merged['hash_index'] = \
                from_config_dict(merged['hash_index'],
                                 HashIndex.get_impls())
        else:
            LOG.debug("No HashIndex impl given. Passing ``None``.")
            merged['hash_index'] = None

        # remove possible comment added by default generator
        if 'hash_index_comment' in merged:
            del merged['hash_index_comment']

        merged['hash2uuids_kvstore'] = \
            from_config_dict(merged['hash2uuids_kvstore'],
                             KeyValueStore.get_impls())

        return super(LSHNearestNeighborIndex, cls).from_config(merged, False)
Example #5
0
 def from_config(
     cls: Type[C],
     config_dict: Dict,
     merge_default: bool = True
 ) -> C:
     config_dict = dict(config_dict)  # shallow-copy
     config_dict['perturber'] = from_config_dict(
         config_dict['perturber'],
         PerturbImage.get_impls()
     )
     config_dict['generator'] = from_config_dict(
         config_dict['generator'],
         GenerateClassifierConfidenceSaliency.get_impls()
     )
     return super().from_config(config_dict, merge_default=merge_default)
Example #6
0
    def from_config(
        cls: Type[MDS],
        config_dict: Dict,
        merge_default: bool = True
    ) -> MDS:
        """
        Instantiate a new instance of this class given the configuration
        JSON-compliant dictionary encapsulating initialization arguments.

        :param config_dict: JSON compliant dictionary encapsulating
            a configuration.

        :param merge_default: Merge the given configuration on top of the
            default provided by ``get_default_config``.

        :return: Constructed instance from the provided config.

        """
        if merge_default:
            config_dict = merge_dict(cls.get_default_config(), config_dict)

        # Optionally construct cache element from sub-config.
        if config_dict['cache_element'] \
                and config_dict['cache_element']['type']:
            e = from_config_dict(config_dict['cache_element'],
                                 DataElement.get_impls())
            config_dict['cache_element'] = e
        else:
            config_dict['cache_element'] = None

        return super(MemoryDescriptorSet, cls).from_config(config_dict, False)
Example #7
0
    def from_config(cls: Type[T],
                    config_dict: Dict,
                    merge_default: bool = True) -> T:
        """
        Instantiate a new instance of this class given the configuration
        JSON-compliant dictionary encapsulating initialization arguments.

        :param config_dict: JSON compliant dictionary encapsulating
            a configuration.
        :type config_dict: dict

        :param merge_default: Merge the given configuration on top of the
            default provided by ``get_default_config``.
        :type merge_default: bool

        :return: Constructed instance from the provided config.
        :rtype: KVSDataSet
        """
        if merge_default:
            config_dict = merge_dict(cls.get_default_config(), config_dict)

        # Convert KVStore config to instance for constructor.
        kvs_inst = from_config_dict(config_dict['kvstore'],
                                    KeyValueStore.get_impls())
        config_dict['kvstore'] = kvs_inst

        return super(KVSDataSet, cls).from_config(config_dict, False)
Example #8
0
def test_from_config_dict() -> None:
    """
    Test that ``from_config_dict`` correctly creates an instance of the class
    requested by the configuration.
    """
    test_config = {
        'type': 'tests.test_configuration.T1',
        'tests.test_configuration.T1': {
            'foo': 256,
            'bar': 'Some string value'
        },
        'tests.test_configuration.T2': {
            'child': {
                'foo': -1,
                'bar': 'some other value'
            },
            'alpha': 1.0,
            'beta': 'euclidean',
        },
        'NotAnImpl': {}
    }

    #: :type: DummyAlgo1
    i = from_config_dict(test_config, T_CLASS_SET)
    assert isinstance(i, T1)
    assert i.foo == 256
    assert i.bar == "Some string value"
Example #9
0
    def from_config(cls: Type[T],
                    config_dict: Dict,
                    merge_default: bool = True) -> T:
        """
        Instantiate a new instance of this class given the configuration
        JSON-compliant dictionary encapsulating initialization arguments.

        :param config_dict: JSON compliant dictionary encapsulating
            a configuration.
        :type config_dict: dict

        :param merge_default: Merge the given configuration on top of the
            default provided by ``get_default_config``.
        :type merge_default: bool

        :return: Constructed instance from the provided config.
        :rtype: MemoryKeyValueStore

        """
        # Copy top-level of config in order to not modify input instance.
        c = config_dict.copy()
        # Simplify specification for "no cache element"
        if 'cache_element' not in c or \
                c['cache_element'] is None or \
                c['cache_element']['type'] is None:
            c['cache_element'] = None
        else:
            # Create from nested config.
            c['cache_element'] = \
                from_config_dict(config_dict['cache_element'],
                                 DataElement.get_impls())
        return super(MemoryKeyValueStore, cls).from_config(c)
Example #10
0
 def __setstate__(self, state: Mapping[str, Any]) -> None:
     # This ``__dict__.update`` works because configuration parameters
     # exactly match up with instance attributes currently.
     self.__dict__.update(state)
     # Translate nested Configurable instance configurations into actual
     # object instances.
     self.network_prototxt = from_config_dict(state["network_prototxt"],
                                              DataElement.get_impls())
     # noinspection PyTypeChecker
     self.network_model = from_config_dict(state["network_model"],
                                           DataElement.get_impls())
     state_image_mean = state["image_mean"]
     if state_image_mean is not None:
         # noinspection PyTypeChecker
         self.image_mean = from_config_dict(state_image_mean,
                                            DataElement.get_impls())
     self._setup_network()
Example #11
0
    def from_config(cls: Type[T_FNNI],
                    config_dict: Dict,
                    merge_default: bool = True) -> T_FNNI:
        """
        Instantiate a new instance of this class given the configuration
        JSON-compliant dictionary encapsulating initialization arguments.

        This method should not be called via super unless and instance of the
        class is desired.

        :param config_dict: JSON compliant dictionary encapsulating
            a configuration.
        :param merge_default: Merge the given configuration on top of the
            default provided by ``get_default_config``.

        :return: Constructed instance from the provided config.

        """
        if merge_default:
            cfg = cls.get_default_config()
            merge_dict(cfg, config_dict)
        else:
            cfg = config_dict

        cfg['descriptor_set'] = from_config_dict(cfg['descriptor_set'],
                                                 DescriptorSet.get_impls())
        cfg['uid2idx_kvs'] = from_config_dict(cfg['uid2idx_kvs'],
                                              KeyValueStore.get_impls())
        cfg['idx2uid_kvs'] = from_config_dict(cfg['idx2uid_kvs'],
                                              KeyValueStore.get_impls())

        if (cfg['index_element'] and cfg['index_element']['type']):
            index_element = from_config_dict(cfg['index_element'],
                                             DataElement.get_impls())
            cfg['index_element'] = index_element
        else:
            cfg['index_element'] = None

        if (cfg['index_param_element'] and cfg['index_param_element']['type']):
            index_param_element = from_config_dict(cfg['index_param_element'],
                                                   DataElement.get_impls())
            cfg['index_param_element'] = index_param_element
        else:
            cfg['index_param_element'] = None

        return super(FaissNearestNeighborsIndex, cls).from_config(cfg, False)
Example #12
0
 def from_config(cls: Type[T],
                 config_dict: Dict,
                 merge_default: bool = True) -> T:
     config_dict = dict(config_dict)  # shallow copy to write to input dict
     config_dict['classifier_inst'] = \
         from_config_dict(config_dict.get('classifier_inst', {}),
                          ClassifyDescriptorSupervised.get_impls())
     return super(SupervisedClassifierRelevancyIndex,
                  cls).from_config(config_dict, merge_default=merge_default)
Example #13
0
 def from_config(cls: Type[T],
                 config_dict: Dict[str, Any],
                 merge_default: bool = True) -> T:
     config_dict = dict(config_dict,
                        rank_relevancy=from_config_dict(
                            config_dict['rank_relevancy'],
                            RankRelevancy.get_impls(),
                        ))
     return super().from_config(config_dict, merge_default=merge_default)
Example #14
0
    def from_config(cls: Type[T],
                    config_dict: Dict,
                    merge_default: bool = True) -> T:
        if merge_default:
            cfg = cls.get_default_config()
            merge_dict(cfg, config_dict)
        else:
            cfg = config_dict

        cfg['descriptor_set'] = \
            from_config_dict(cfg['descriptor_set'],
                             DescriptorSet.get_impls())

        return super(MRPTNearestNeighborsIndex, cls).from_config(cfg, False)
Example #15
0
    def from_config(cls: Type[T],
                    config_dict: Dict,
                    merge_default: bool = True) -> T:
        if merge_default:
            config_dict = merge_dict(cls.get_default_config(), config_dict)

        cache_element = None
        if config_dict['cache_element'] and config_dict['cache_element'][
                'type']:
            cache_element = from_config_dict(config_dict['cache_element'],
                                             DataElement.get_impls())
        config_dict['cache_element'] = cache_element

        return super(DataMemorySet, cls).from_config(config_dict, False)
    def from_config(
            cls,
            config_dict: Dict,
            merge_default: bool = True) -> "ClassifyDescriptorCollection":
        if merge_default:
            config_dict = merge_dict(cls.get_default_config(), config_dict)

        classifier_map = {}

        # Copying list of keys so we can update the dictionary as we loop.
        for label in list(config_dict.keys()):
            # Skip the example section.
            if label == cls.EXAMPLE_KEY:
                continue

            classifier_config = config_dict[label]
            classifier = from_config_dict(classifier_config,
                                          ClassifyDescriptor.get_impls())
            classifier_map[label] = classifier

        return cls(classifiers=classifier_map)
    def from_config(cls, config_dict, merge_default=True):
        """
        Instantiate a new instance of this class given the configuration
        JSON-compliant dictionary encapsulating initialization arguments.

        This method should not be called via super unless an instance of the
        class is desired.

        :param config_dict: JSON compliant dictionary encapsulating
            a configuration.
        :type config_dict: dict

        :param merge_default: Merge the given configuration on top of the
            default provided by ``get_default_config``.
        :type merge_default: bool

        :return: Constructed instance from the provided config.
        :rtype: ClassifierCollection

        """
        if merge_default:
            config_dict = merge_dict(cls.get_default_config(), config_dict)

        classifier_map = {}

        # Copying list of keys so we can update the dictionary as we loop.
        for label in list(config_dict.keys()):
            # Skip the example section.
            if label == cls.EXAMPLE_KEY:
                continue

            classifier_config = config_dict[label]
            classifier = from_config_dict(classifier_config,
                                          Classifier.get_impls())
            classifier_map[label] = classifier

        # Don't merge back in "example" default
        return super(ClassifierCollection,
                     cls).from_config({'classifiers': classifier_map},
                                      merge_default=False)
Example #18
0
    def from_config(
        cls: Type[T],
        config_dict: Dict,
        merge_default: bool = True
    ) -> T:
        """
        Instantiate a new instance of this class given the configuration
        JSON-compliant dictionary encapsulating initialization arguments.

        This method should not be called via super unless an instance of the
        class is desired.

        :param config_dict: JSON compliant dictionary encapsulating
            a configuration.
        :type config_dict: dict

        :param merge_default: Merge the given configuration on top of the
            default provided by ``get_default_config``.
        :type merge_default: bool

        :return: Constructed instance from the provided config.
        :rtype: SkLearnBallTreeHashIndex

        """
        if merge_default:
            config_dict = merge_dict(cls.get_default_config(), config_dict)

        # Parse ``cache_element`` configuration if set.
        cache_element = None
        if config_dict['cache_element'] and \
                config_dict['cache_element']['type']:
            cache_element = \
                from_config_dict(config_dict['cache_element'],
                                 DataElement.get_impls())
        config_dict['cache_element'] = cache_element

        return super(SkLearnBallTreeHashIndex, cls).from_config(config_dict,
                                                                False)