detectionmetrics.utils package
Submodules
detectionmetrics.utils.conversion module
- detectionmetrics.utils.conversion.get_ontology_conversion_lut(old_ontology: dict, new_ontology: dict, ontology_translation: dict | None = None) ndarray
Build a LUT that links old ontology and new ontology indices. If class names don’t match between the provided ontologies, user must provide an ontology translation dictionary with old and new class names as keys and values, respectively
- Parameters:
old_ontology (dict) – Origin ontology definition
new_ontology (dict) – Target ontology definition
ontology_translation (Optional[dict], optional) – Ontology translation dictionary, defaults to None
- Returns:
numpy array associating old and new ontology indices
- Return type:
np.ndarray
- detectionmetrics.utils.conversion.hex_to_rgb(hex: str) Tuple[int, ...]
Convert HEX color code to sRGB
- Parameters:
hex (str) – HEX color code
- Returns:
sRGB color value
- Return type:
Tuple[int, …]
- detectionmetrics.utils.conversion.label_to_rgb(label: Image, ontology: dict) Image
Convert an image with raw label indices to RGB mask
- Parameters:
label (Image.Image) – Raw label indices as PIL image
ontology (dict) – Ontology definition
- Returns:
RGB mask
- Return type:
Image.Image
- detectionmetrics.utils.conversion.ontology_to_rgb_lut(ontology: dict) ndarray
Given an ontology definition, build a LUT that links indices and RGB values
- Parameters:
ontology (dict) – Ontology definition
- Returns:
numpy array containing RGB values per index
- Return type:
np.ndarray
detectionmetrics.utils.io module
- detectionmetrics.utils.io.get_image_mode(fname: str) str
Given an image retrieve its color mode using PIL
- Parameters:
fname (str) – Input image
- Returns:
PIL color image mode
- Return type:
str
- detectionmetrics.utils.io.read_json(fname: str) dict
Read a JSON file
- Parameters:
fname (str) – JSON filename
- Returns:
Dictionary containing JSON file data
- Return type:
dict
- detectionmetrics.utils.io.read_txt(fname: str) List[str]
Read a .txt file line by line
- Parameters:
fname (str) – .txt filename
- Returns:
List of lines found in the .txt file
- Return type:
List[str]
- detectionmetrics.utils.io.read_yaml(fname: str) dict
Read a YAML file
- Parameters:
fname (str) – YAML filename
- Returns:
Dictionary containing YAML file data
- Return type:
dict
- detectionmetrics.utils.io.write_json(fname: str, data: dict)
Write a JSON file properly indented
- Parameters:
fname (str) – Target JSON filename
data (dict) – Dictionary containing data to be dumped as a JSON file
detectionmetrics.utils.metrics module
- class detectionmetrics.utils.metrics.Accuracy(n_classes: int)
Bases:
Metric
Compute accuracy as:
\[\text{Accuracy} = \frac{1}{N}\sum_i^N 1(y_i = \hat{y}_i)\]Accuracy per sample and class is accumulated and then the average per class is computed.
- Parameters:
n_classes (int) – Number of classes to evaluate
- compute() Tuple[float, ndarray]
Get accuracy (global and per class)
- Returns:
per class accuracy, and global accuracy
- Return type:
Tuple[float, np.ndarray]
- update(pred: ndarray, gt: ndarray)
Accumulate accuracy values for a new set of samples
- Parameters:
pred (np.ndarray) – label encoded prediction array (batch, width, height)
gt (np.ndarray) – label encoded ground truth array (batch, width, height)
- class detectionmetrics.utils.metrics.IoU(n_classes: int)
Bases:
Metric
Compute Intersection over Union (IoU). IoU per sample and class is accumulated and then the average per class is computed.
- Parameters:
n_classes (int) – Number of classes to evaluate
- compute() ndarray
Get IoU (global and per class)
- Returns:
per class IoU, and global IoU
- Return type:
Tuple[float, np.ndarray]
- update(pred: ndarray, gt: ndarray)
Accumulate IoU values for a new set of samples
- Parameters:
pred (np.ndarray) – one-hot encoded prediction array (batch, class, width, height)
gt (np.ndarray) – one-hot encoded ground truth array (batch, class, width, height)
- class detectionmetrics.utils.metrics.Metric(n_classes: int)
Bases:
ABC
Abstract class for metrics
- Parameters:
n_classes (int) – Number of classes to evaluate
- abstract compute() ndarray
Get final values
- Returns:
Array containing final values
- Return type:
np.ndarray
- abstract update(pred: ndarray, gt: ndarray)
Accumulate results for a new batch
- Parameters:
pred (np.ndarray) – Array containing prediction
gt (np.ndarray) – Array containing ground truth