Example #1
0
    def build(self):
        super().build()
        self._class_ids_to_names_mapping = {}
        self._colors = {}
        for each_attribute_name, each_mapping in (
                self.attributes_class_names_to_labels_mappings.items()):
            each_mapping = io_utils.maybe_load_json(each_mapping)
            class_id_to_name_mapping = {
                each_item["class_id"]: each_class_name
                for each_class_name, each_item in each_mapping.items()
            }
            self._class_ids_to_names_mapping[each_attribute_name] = (
                class_id_to_name_mapping)
            if self._colors is None:
                continue

            try:
                colors = {
                    each_item["class_id"]: each_item["color"]
                    for each_class_name, each_item in each_mapping.items()
                }
                self._colors[each_attribute_name] = colors
            except KeyError:
                self._colors = None
        return self
Example #2
0
 def build(self):
     super().build()
     self.rgb_class_mapping = io_utils.maybe_load_json(
         self.rgb_class_mapping)
     self._rgb_class_ids_mapping_hashed = dict(
         zip(map(image_utils.rgb_hash_fun, self.rgb_class_mapping['rgb']),
             self.rgb_class_mapping['class_ids']))
     return self
Example #3
0
def build_class_labels_to_names_mapping(
        num_classes: Optional[int] = None,
        class_names_to_labels_mapping: Optional[dict] = None,
        class_id_offset: int = 0):
    """
    Build mapping from class id to its names and vice-verse from that mapping or
    from num_classes only. If no mapping was provided, class names are
    "class_{class_id}".

    Parameters
    ----------
    num_classes
        number of classes
    class_names_to_labels_mapping
        file name or mapping itself; mapping should be in format
        {"class name": {"class_id": 1}, "other class_name": {"class_id": 2}},
        where class_id is an unique integer; if multiple class names have same
        class id, then the last name inside of json file will be used as class
        name
    class_id_offset
        offset of class id

    Returns
    -------
    num_classes
        number of classes
    class_names_to_labels_mapping
        mapping in format
        {"class name": {"class_id": 1}, "other class_name": {"class_id": 2}}
    class_labels_to_names_mapping
        mapping from class id to class name
    """
    assert (num_classes is not None
            or class_names_to_labels_mapping is not None
            ), "Provide num_classes or class_names_to_labels_mapping!"

    if not class_names_to_labels_mapping:
        class_labels_to_names_mapping = {
            class_label: "class_{}".format(class_label)
            for class_label in range(class_id_offset, num_classes +
                                     class_id_offset)
        }
        return (num_classes, class_names_to_labels_mapping,
                class_labels_to_names_mapping)

    class_names_to_labels_mapping = io_utils.maybe_load_json(
        class_names_to_labels_mapping, as_ordereddict=True)
    class_labels_to_names_mapping = {
        v['class_id']: k
        for k, v in reversed(class_names_to_labels_mapping.items())
    }
    num_classes = len(class_labels_to_names_mapping)

    return (num_classes, class_names_to_labels_mapping,
            class_labels_to_names_mapping)
Example #4
0
def _get_configs_from_single_files(config_dir: str,
                                   single_config_fnames: list) -> dict:
    logger = logging.getLogger(__name__)
    configs = {}
    for fname in single_config_fnames:
        config_fname = os.path.join(config_dir, fname + '.json')
        if os.path.isfile(config_fname):
            config_name = fname + '_config'
            logger.info('Found %s with config %s', config_name, config_fname)
            configs[config_name] = io_utils.maybe_load_json(config_fname)
    return configs
def maybe_create_category_index(num_classes: int,
                                class_names_to_labels_mapping: Union[str,
                                                                     dict],
                                class_offset: int = 1) -> Tuple[int, dict]:
    """
    Create category index from num_classes or from class_names to labels
    mapping. This is the same category index as is used in object_detection API

    Parameters
    ----------
    num_classes
        number of classes
    class_names_to_labels_mapping
        either str with file name to mapping or dict with mapping itself;
        mapping should be in format {"class name": {"class_id": 1}, },
        where class_id is an unique integer
    class_offset
        class id to start it from 0 (for classification) or 1 (for object
        detection)

    Returns
    -------
    num_classes
        number of classes
    category_index
        category index in format {"class name": {"id": , {"name": ,}}, ...}
    """
    if class_names_to_labels_mapping is not None:
        class_names_to_labels_mapping = io_utils.maybe_load_json(
            class_names_to_labels_mapping, as_ordereddict=True)
        if isinstance(class_names_to_labels_mapping, OrderedDict):
            categories = {
                v['class_id']: k
                for k, v in reversed(class_names_to_labels_mapping.items())
            }
        else:
            categories = {
                v['class_id']: k
                for k, v in class_names_to_labels_mapping.items()
            }
        category_index = {
            k: {
                'id': k,
                'name': v
            }
            for k, v in sorted(categories.items())
        }
    else:
        assert num_classes is not None, (
            "Either num_classes or class_names_to_labels_mapping "
            "should be provided!!!")
        category_index = get_category_index(num_classes, class_offset)
    num_classes = len(category_index)
    return num_classes, category_index
Example #6
0
def _read_from_json_or_txt(fname_or_data):
    if not isinstance(fname_or_data, str):
        return fname_or_data
    if not os.path.exists(fname_or_data):
        raise FileNotFoundError("file {} not found!".format(
            os.path.realpath(fname_or_data)))

    if os.path.splitext(fname_or_data)[-1] == ".txt":
        with open(fname_or_data, "r") as file:
            data = file.read().splitlines()
        return data

    data = io_utils.maybe_load_json(fname_or_data)
    return data
Example #7
0
 def build(self):
     super().build()
     connection_map = io_utils.maybe_load_json(self.connection_map)
     self._connection_map_only = {
         each_name: each_item["connection"]
         for each_name, each_item in connection_map.items()
     }
     try:
         self._colors = {
             each_name: each_item["color"]
             for each_name, each_item in connection_map.items()
         }
     except KeyError:
         self._colors = None
     return self
    def __init__(self, *,
                 rgb_class_mapping: Optional[dict] = None,
                 save_file_extension: str = ".png",
                 save_probabilities: bool = False,
                 **callback_kwargs):
        super(SemanticSegmentationSaver, self).__init__(
            **callback_kwargs)

        if save_file_extension.startswith("."):
            save_file_extension = save_file_extension[1:]
        self.save_file_extension = save_file_extension
        self.rgb_class_mapping = io_utils.maybe_load_json(rgb_class_mapping)
        if (self.rgb_class_mapping is not None and
                'class_ids' in self.rgb_class_mapping):
            self.rgb_class_mapping = dict(zip(
                self.rgb_class_mapping['class_ids'],
                self.rgb_class_mapping['rgb']))
        self.save_probabilities = save_probabilities
Example #9
0
def add_global_project_config_from_file(fname: str):
    """
    Add project configuration from json file

    Parameters
    ----------
    fname
        file name of project configuration .json file

    See Also
    --------
    :func:`add_global_project_config`

    """
    logger = logging.getLogger(__name__)
    if not os.path.isfile(fname):
        logger.info("No project configuration file was found")
        return
    config = io_utils.maybe_load_json(fname)
    logger.info("Add project global config from %s", fname)
    add_global_project_config(config)
Example #10
0
def _read_components_configs(config_fnames: list) -> list:
    """
    Read multiple config files

    Parameters
    ----------
    config_fnames
        list with file names with config json files

    Returns
    -------
    config_components
        list of read configurations from single files
    """
    config_components = []
    for config_fname in config_fnames:
        config_component = io_utils.maybe_load_json(config_fname)
        if isinstance(config_component, list):
            config_components.extend(config_component)
        else:
            config_components.append(config_component)
    return config_components