detectionmetrics.models package

Submodules

detectionmetrics.models.model module

class detectionmetrics.models.model.ImageSegmentationModel(model: Any, model_type: str, model_cfg: str, ontology_fname: str, model_fname: str | None = None)

Bases: SegmentationModel

Parent image segmentation model class

Parameters:
  • model (Any) – Image segmentation model object

  • model_type (str) – Model type (e.g. scripted, compiled, etc.)

  • model_cfg (str) – JSON file containing model configuration (e.g. image size or normalization parameters)

  • ontology_fname (str) – JSON file containing model output ontology

  • model_fname (Optional[str], optional) – Model file or directory

abstract eval(dataset: ImageSegmentationDataset, split: str | List[str] = 'test', ontology_translation: str | None = None, predictions_outdir: str | None = None, results_per_sample: bool = False) DataFrame

Perform evaluation for an image segmentation dataset

Parameters:
  • dataset (ImageSegmentationDataset) – Image segmentation dataset for which the evaluation will be performed

  • split (str | List[str], optional) – Split or splits to be used from the dataset, defaults to “test”

  • ontology_translation (str, optional) – JSON file containing translation between dataset and model output ontologies

  • predictions_outdir (Optional[str], optional) – Directory to save predictions per sample, defaults to None. If None, predictions are not saved.

  • results_per_sample (bool, optional) – Whether to store results per sample or not, defaults to False. If True, predictions_outdir must be provided.

Returns:

DataFrame containing evaluation results

Return type:

pd.DataFrame

abstract inference(image: Image) Image

Perform inference for a single image

Parameters:

image (Image.Image) – PIL image.

Returns:

Segmenation result as PIL image

Return type:

Image.Image

class detectionmetrics.models.model.LiDARSegmentationModel(model: Any, model_type: str, model_cfg: str, ontology_fname: str, model_fname: str | None = None)

Bases: SegmentationModel

Parent LiDAR segmentation model class

Parameters:
  • model (Any) – LiDAR segmentation model object

  • model_type (str) – Model type (e.g. scripted, compiled, etc.)

  • model_cfg (str) – JSON file containing model configuration (e.g. sampling method, input format, etc.)

  • ontology_fname (str) – JSON file containing model output ontology

  • model_fname (Optional[str], optional) – Model file or directory

abstract eval(dataset: LiDARSegmentationDataset, split: str | List[str] = 'test', ontology_translation: str | None = None, predictions_outdir: str | None = None, results_per_sample: bool = False) DataFrame

Perform evaluation for a LiDAR segmentation dataset

Parameters:
  • dataset (LiDARSegmentationDataset) – LiDAR segmentation dataset for which the evaluation will be performed

  • split (str | List[str], optional) – Split or splits to be used from the dataset, defaults to “test”

  • ontology_translation (str, optional) – JSON file containing translation between dataset and model output ontologies

  • predictions_outdir (Optional[str], optional) – Directory to save predictions per sample, defaults to None. If None, predictions are not saved.

  • results_per_sample (bool, optional) – Whether to store results per sample or not, defaults to False. If True, predictions_outdir must be provided.

Returns:

DataFrame containing evaluation results

Return type:

pd.DataFrame

abstract inference(points: ndarray) ndarray

Perform inference for a single image

Parameters:

image (np.ndarray) – Point cloud xyz array

Returns:

Segmenation result as a point cloud with label indices

Return type:

np.ndarray

class detectionmetrics.models.model.SegmentationModel(model: Any, model_type: str, model_cfg: str, ontology_fname: str, model_fname: str | None = None)

Bases: ABC

Parent segmentation model class

Parameters:
  • model (Any) – Segmentation model object

  • model_type (str) – Model type (e.g. scripted, compiled, etc.)

  • model_cfg (str) – JSON file containing model configuration

  • ontology_fname (str) – JSON file containing model output ontology

  • model_fname (Optional[str], optional) – Model file or directory, defaults to None

abstract eval(dataset: SegmentationDataset, split: str | List[str] = 'test', ontology_translation: str | None = None, predictions_outdir: str | None = None, results_per_sample: bool = False) DataFrame

Perform evaluation for an image segmentation dataset

Parameters:
  • dataset (ImageSegmentationDataset) – Segmentation dataset for which the evaluation will be performed

  • split (str | List[str], optional) – Split or splits to be used from the dataset, defaults to “test”

  • ontology_translation (str, optional) – JSON file containing translation between dataset and model output ontologies

  • predictions_outdir (Optional[str], optional) – Directory to save predictions per sample, defaults to None. If None, predictions are not saved.

  • results_per_sample (bool, optional) – Whether to store results per sample or not, defaults to False. If True, predictions_outdir must be provided.

Returns:

DataFrame containing evaluation results

Return type:

pd.DataFrame

abstract get_computational_cost(runs: int = 30, warm_up_runs: int = 5) dict

Get different metrics related to the computational cost of the model

Parameters:
  • runs (int, optional) – Number of runs to measure inference time, defaults to 30

  • warm_up_runs (int, optional) – Number of warm-up runs, defaults to 5

Returns:

Dictionary containing computational cost information

get_lut_ontology(dataset_ontology: dict, ontology_translation: str | None = None)

Build ontology lookup table (leave empty if ontologies match)

Parameters:
  • dataset_ontology (dict) – Image or LiDAR dataset ontology

  • ontology_translation (Optional[str], optional) – JSON file containing translation between model and dataset ontologies, defaults to None

abstract inference(points: ndarray | Image) ndarray | Image

Perform inference for a single image or point cloud

Parameters:

image (Union[np.ndarray, Image.Image]) – Either a numpy array (LiDAR point cloud) or a PIL image

Returns:

Segmenation result as a point cloud or image with label indices

Return type:

Union[np.ndarray, Image.Image]

detectionmetrics.models.onnx module

class detectionmetrics.models.onnx.OnnxImageSegmentationModel(model, model_type, ontology_fname, model_cfg, model_fname)

Bases: ImageSegmentationModel

detectionmetrics.models.tensorflow module

class detectionmetrics.models.tensorflow.ImageSegmentationTensorflowDataset(dataset: ImageSegmentationDataset, image_size: Tuple[int, int], batch_size: int = 1, splits: List[str] = ['test'], lut_ontology: dict | None = None, normalization: dict | None = None, keep_aspect: bool = False)

Bases: object

Dataset for image segmentation Tensorflow models

Parameters:
  • dataset (ImageSegmentationDataset) – Image segmentation dataset

  • image_size (Tuple[int, int]) – Image size in pixels (width, height)

  • batch_size (int, optional) – Batch size, defaults to 1

  • splits (str, optional) – Splits to be used from the dataset, defaults to [“test”]

  • lut_ontology (dict, optional) – LUT to transform label classes, defaults to None

  • normalization (dict, optional) – Parameters for normalizing input images, defaults to None

  • keep_aspect (bool, optional) – Whether to keep aspect ratio when resizing images. If true, resize to match smaller sides size and crop center. Defaults to False

load_data(idx: str, images_fnames: List[str], labels_fnames: List[str]) Tuple[tensorflow.Tensor, tensorflow.Tensor]

Function for loading data for each dataset sample

Parameters:
  • idx (str) – Sample index

  • images_fnames (List[str]) – List containing all image filenames

  • labels_fnames (List[str]) – List containing all corresponding label filenames

Returns:

Image and label tensor pairs

Return type:

Tuple[tf.Tensor, tf.Tensor]

read_image(fname: str, label=False) tensorflow.Tensor

Read a single image or label

Parameters:
  • fname (str) – Input image or label filename

  • label (bool, optional) – Whether the input data is a label or not, defaults to False

Returns:

Tensorflow tensor containing read image or label

Return type:

tf.Tensor

class detectionmetrics.models.tensorflow.TensorflowImageSegmentationModel(model: str | tensorflow.Module | tensorflow.keras.Model, model_cfg: str, ontology_fname: str)

Bases: ImageSegmentationModel

Image segmentation model for Tensorflow framework

Parameters:
  • model (Union[str, torch.nn.Module]) – Either the filename of a Tensorflow model in SavedModel format or the model already loaded into an arbitrary Tensorflow or Keras model.

  • model_cfg (str) – JSON file containing model configuration

  • ontology_fname (str) – JSON file containing model output ontology

eval(dataset: ImageSegmentationDataset, split: str | List[str] = 'test', ontology_translation: str | None = None, predictions_outdir: str | None = None, results_per_sample: bool = False) DataFrame

Perform evaluation for an image segmentation dataset

Parameters:
  • dataset (ImageSegmentationDataset) – Image segmentation dataset for which the evaluation will be performed

  • split (str | List[str], optional) – Split to be used from the dataset, defaults to “test”

  • ontology_translation (str, optional) – JSON file containing translation between dataset and model output ontologies

  • predictions_outdir (Optional[str], optional) – Directory to save predictions per sample, defaults to None. If None, predictions are not saved.

  • results_per_sample (bool, optional) – Whether to store results per sample or not, defaults to False. If True, predictions_outdir must be provided.

Returns:

DataFrame containing evaluation results

Return type:

pd.DataFrame

get_computational_cost(runs: int = 30, warm_up_runs: int = 5) dict

Get different metrics related to the computational cost of the model

Parameters:
  • runs (int, optional) – Number of runs to measure inference time, defaults to 30

  • warm_up_runs (int, optional) – Number of warm-up runs, defaults to 5

Returns:

Dictionary containing computational cost information

inference(image: Image) Image

Perform inference for a single image

Parameters:

image (Image.Image) – PIL image

Returns:

segmenation result as PIL image

Return type:

Image.Image

detectionmetrics.models.tensorflow.get_computational_cost(model: tensorflow.Module, dummy_input: tensorflow.Tensor, model_fname: str | None = None, runs: int = 30, warm_up_runs: int = 5) dict

Get different metrics related to the computational cost of the model

Parameters:
  • model (tf.Module) – Loaded TensorFlow SavedModel

  • dummy_input (tf.Tensor) – Dummy input data for the model

  • model_fname (Optional[str], optional) – Model filename used to measure model size, defaults to None

  • runs (int, optional) – Number of runs to measure inference time, defaults to 30

  • warm_up_runs (int, optional) – Number of warm-up runs, defaults to 5

Returns:

Dictionary containing computational cost information

detectionmetrics.models.tensorflow.resize_image(image: tensorflow.Tensor, target_size: Tuple[int, int], method: str, keep_aspect: bool = False) tensorflow.Tensor

Resize tensorflow image to target size

Parameters:
  • image (tf.Tensor) – Input image tensor

  • target_size (Tuple[int, int]) – Target size for the image

  • method (str) – Resizing method (e.g. bilinear, nearest)

  • keep_aspect (bool, optional) – Whether to keep aspect ratio when resizing images. If true, resize to match smaller sides size and crop center. Defaults to False

Returns:

Resized image tensor

Return type:

tf.Tensor

detectionmetrics.models.torch module

class detectionmetrics.models.torch.CustomResize(*args: Any, **kwargs: Any)

Bases: Module

Custom rescale transformation for PyTorch

Parameters:
  • target_size (Tuple[int, int]) – Target size for the image

  • keep_aspect (bool, defaults to False) – Flag to keep aspect ratio

  • interpolation (F.InterpolationMode, defaults to F.InterpolationMode.BILINEAR) – Interpolation mode for resizing (e.g. NEAREST, BILINEAR)

forward(image: Image) Image
class detectionmetrics.models.torch.ImageSegmentationTorchDataset(*args: Any, **kwargs: Any)

Bases: Dataset

Dataset for image segmentation PyTorch models

Parameters:
  • dataset (ImageSegmentationDataset) – Image segmentation dataset

  • transform (transforms.Compose) – Transformation to be applied to images

  • target_transform (transforms.Compose) – Transformation to be applied to labels

  • splits (str, optional) – Splits to be used from the dataset, defaults to [“test”]

class detectionmetrics.models.torch.LiDARSegmentationTorchDataset(*args: Any, **kwargs: Any)

Bases: Dataset

Dataset for LiDAR segmentation PyTorch models

Parameters:
  • dataset (LiDARSegmentationDataset) – LiDAR segmentation dataset

  • model_cfg (dict) – Dictionary containing model configuration

  • preprocess (callable) – Function for preprocessing point clouds

  • n_classes (int) – Number of classes estimated by the model

  • splits (str, optional) – Splits to be used from the dataset, defaults to [“test”]

class detectionmetrics.models.torch.TorchImageSegmentationModel(model: str | torch.nn.Module, model_cfg: str, ontology_fname: str)

Bases: ImageSegmentationModel

eval(dataset: ImageSegmentationDataset, split: str | List[str] = 'test', ontology_translation: str | None = None, predictions_outdir: str | None = None, results_per_sample: bool = False) DataFrame

Perform evaluation for an image segmentation dataset

Parameters:
  • dataset (ImageSegmentationDataset) – Image segmentation dataset for which the evaluation will be performed

  • split (str | List[str], optional) – Split or splits to be used from the dataset, defaults to “test”

  • ontology_translation (str, optional) – JSON file containing translation between dataset and model output ontologies

  • predictions_outdir (Optional[str], optional) – Directory to save predictions per sample, defaults to None. If None, predictions are not saved.

  • results_per_sample (bool, optional) – Whether to store results per sample or not, defaults to False. If True, predictions_outdir must be provided.

Returns:

DataFrame containing evaluation results

Return type:

pd.DataFrame

get_computational_cost(runs: int = 30, warm_up_runs: int = 5) dict

Get different metrics related to the computational cost of the model

Parameters:
  • runs (int, optional) – Number of runs to measure inference time, defaults to 30

  • warm_up_runs (int, optional) – Number of warm-up runs, defaults to 5

Returns:

Dictionary containing computational cost information

inference(image: Image) Image

Perform inference for a single image

Parameters:

image (Image.Image) – PIL image

Returns:

segmenation result as PIL image

Return type:

Image.Image

class detectionmetrics.models.torch.TorchLiDARSegmentationModel(model: str | torch.nn.Module, model_cfg: str, ontology_fname: str)

Bases: LiDARSegmentationModel

eval(dataset: LiDARSegmentationDataset, split: str | List[str] = 'test', ontology_translation: str | None = None, predictions_outdir: str | None = None, results_per_sample: bool = False) DataFrame

Perform evaluation for a LiDAR segmentation dataset

Parameters:
  • dataset (LiDARSegmentationDataset) – LiDAR segmentation dataset for which the evaluation will be performed

  • split (str | List[str], optional) – Split or splits to be used from the dataset, defaults to “test”

  • ontology_translation (str, optional) – JSON file containing translation between dataset and model output ontologies

  • predictions_outdir (Optional[str], optional) – Directory to save predictions per sample, defaults to None. If None, predictions are not saved.

  • results_per_sample (bool, optional) – Whether to store results per sample or not, defaults to False. If True, predictions_outdir must be provided.

Returns:

DataFrame containing evaluation results

Return type:

pd.DataFrame

get_computational_cost(runs: int = 30, warm_up_runs: int = 5) dict

Get different metrics related to the computational cost of the model

Parameters:
  • runs (int, optional) – Number of runs to measure inference time, defaults to 30

  • warm_up_runs (int, optional) – Number of warm-up runs, defaults to 5

Returns:

Dictionary containing computational cost information

inference(points: ndarray) ndarray

Perform inference for a single point cloud

Parameters:

points (np.ndarray) – Point cloud xyz array

Returns:

Segmenation result as a point cloud with label indices

Return type:

np.ndarray

detectionmetrics.models.torch.data_to_device(data: tuple | list, device: torch.device) tuple | list

Move provided data to given device (CPU or GPU)

Parameters:
  • data (Union[tuple, list]) – Data provided (it can be a single or multiple tensors)

  • device (torch.device) – Device to move data to

Returns:

Data moved to device

Return type:

Union[tuple, list]

detectionmetrics.models.torch.get_computational_cost(model: Any, dummy_input: torch.Tensor, model_fname: str | None = None, runs: int = 30, warm_up_runs: int = 5) dict

Get different metrics related to the computational cost of the model

Parameters:
  • model (Any) – Either a TorchScript model or an arbitrary PyTorch module

  • dummy_input (torch.Tensor) – Dummy input data for the model

  • model_fname (Optional[str], optional) – Model filename used to measure model size, defaults to None

  • runs (int, optional) – Number of runs to measure inference time, defaults to 30

  • warm_up_runs (int, optional) – Number of warm-up runs, defaults to 5

Returns:

Dictionary containing computational cost information

detectionmetrics.models.torch.get_data_shape(data: tuple | list) tuple | list

Get the shape of the provided data

Parameters:

data (Union[tuple, list]) – Data provided (it can be a single or multiple tensors)

Returns:

Data shape

Return type:

Union[tuple, list]

detectionmetrics.models.torch.unsqueeze_data(data: tuple | list, dim: int = 0) tuple | list

Unsqueeze provided data along given dimension

Parameters:
  • data (Union[tuple, list]) – Data provided (it can be a single or multiple tensors)

  • dim (int, optional) – Dimension that will be unsqueezed, defaults to 0

Returns:

Unsqueezed data

Return type:

Union[tuple, list]

Module contents