def from_labels_text_file(cls, label_text_file: Union[str, Path], labels: Optional[List[str]] = None): label_text_file = str(label_text_file) if not os.path.exists(label_text_file): raise FileNotFoundError(label_text_file) labels_classes = np.loadtxt(label_text_file).astype(np.float32) if labels_classes.shape[1] < 3: raise ValueError( "Text label file did not contain enough information to be colors." ) colors = [ parse_and_validate_color(color) for color in labels_classes[:, :3] ] if labels_classes.shape[1] == 3: return cls(colors, labels=labels) else: one_hot_encoding = [ parse_validate_one_hot(one_hot) for one_hot in labels_classes[:, 3:] ] return cls(colors, one_hot_encoding, labels)
def from_colors_multilabel( cls, colors: List[Union[str, Tuple[int, int, int]]], labels: Optional[List[str]] = None, ): colors = [parse_and_validate_color(color) for color in colors] one_hot_encoding, colors = all_one_hot_and_colors(colors) return cls(colors, one_hot_encoding, labels)
def from_colors( cls, colors: List[Union[str, Tuple[int, int, int]]], labels: Optional[List[str]] = None, ): colors = [parse_and_validate_color(color) for color in colors] colors = [(0, 0, 0)] + colors if labels: labels = ["background"] + labels return cls(colors, labels=labels)
def from_list_of_color_labels(cls, color_labels: List[Dict[str, Any]], labels: Optional[List[str]] = None): colors: List[Tuple[int, int, int]] = [] one_hot_encoding: Optional[List[List[Union[int, float]]]] = None labels: Optional[List[str]] = labels has_one_hot = None has_labels = None one_hot_size = None for label in color_labels: if "color" not in label: raise ValueError("Need at least a color to define a label.") color = parse_and_validate_color(label["color"]) colors.append(color) if "one_hot" in label: if has_one_hot is None: has_one_hot = True one_hot_encoding = [] one_hot = parse_validate_one_hot(label["one_hot"]) if one_hot_size is None: one_hot_size = len(one_hot) if not has_one_hot: raise ValueError( "Some labels have one hot defined, others not.") if one_hot_size != len(one_hot): raise ValueError( "Some labels have different one hot sizes.") one_hot_encoding.append(one_hot) else: has_one_hot = False if "label" in label: if has_labels is None: has_labels = True labels = [] if not has_labels: raise ValueError("Some labels have a name, others not.") labels.append(label["label"]) else: has_labels = False return cls(colors, one_hot_encoding, labels)