Classification#

Description#

The ClassificationModel is the OpenVINO wrapper for models exported from OpenVINO Training Extensions. It supports multi-label classification as well as hierarchical classification.

Model Specifications#

Inputs#

A single input image of shape (H, W, 3) where H and W are the height and width of the image, respectively.

Outputs#

  • top_labels: List of tuples containing the top labels of the classification model.

  • saliency_map: Saliency map of the input image.

  • feature_vector: Feature vector of the input image. This is useful for Active Learning.

  • raw_scores: Raw scores of the classification model.

class model_api.models.classification.ClassificationModel(inference_adapter, configuration={}, preload=False)#

Bases: ImageModel

Classification Model.

Parameters:
  • inference_adapter (InferenceAdapter) – Inference adapter

  • configuration (dict, optional) – Configuration parameters. Defaults to {}.

  • preload (bool, optional) – Whether to preload the model. Defaults to False.

Example

>>> from model_api.models import ClassificationModel
>>> import cv2
>>> model = ClassificationModel.create_model("./path_to_model.xml")
>>> image = cv2.imread("path_to_image.jpg")
>>> result = model.predict(image)
ClassificationResult(
    top_labels=[(1, 'bicycle', 0.90176445), (6, 'car', 0.85433626), (7, 'cat', 0.60699755)],
    saliency_map=array([], dtype=float64),
    feature_vector=array([], dtype=float64),
    raw_scores=array([], dtype=float64)
)

Image model constructor

It extends the Model constructor.

Parameters:
  • inference_adapter (InferenceAdapter) – allows working with the specified executor

  • configuration (dict, optional) – it contains values for parameters accepted by specific wrapper (confidence_threshold, labels etc.) which are set as data attributes

  • preload (bool, optional) – a flag whether the model is loaded to device while initialization. If preload=False, the model must be loaded via load method before inference

Raises:

WrapperError – if the wrapper failed to define appropriate inputs for images

get_all_probs(logits)#
Return type:

ndarray

get_hierarchical_predictions(logits)#
get_multiclass_predictions(outputs)#
Return type:

list[tuple[int, str, float]]

get_multilabel_predictions(logits)#
Return type:

List[Tuple[int, str, float32]]

get_saliency_maps(outputs)#

Returns saliency map model output. In hierarchical case reorders saliency maps to match the order of labels in .XML meta.

Return type:

ndarray

classmethod parameters()#

Defines the description and type of configurable data parameters for the wrapper.

See types.py to find available types of the data parameter. For each parameter the type, default value and description must be provided.

The example of possible data parameter:
‘confidence_threshold’: NumericalValue(

default_value=0.5, description=”Threshold value for detection box confidence”

)

The method must be implemented in each specific inherited wrapper.

Return type:

dict

Returns:

  • the dictionary with defined wrapper data parameters

postprocess(outputs, meta)#

Interface for postprocess method.

Parameters:
  • outputs (dict) –

    model raw output in the following format: {

    ’output_layer_name_1’: raw_result_1, ‘output_layer_name_2’: raw_result_2, …

    }

  • meta (dict) – the input metadata obtained from preprocess method

Return type:

ClassificationResult

Returns:

  • postprocessed data in the format defined by wrapper

class model_api.models.classification.GreedyLabelsResolver(hierarchical_config)#

Bases: object

resolve_labels(predictions)#

Resolves hierarchical labels and exclusivity based on a list of ScoredLabels (labels with probability). The following two steps are taken: - select the most likely label from each label group - add it and it’s predecessors if they are also most likely labels (greedy approach).

Parameters:

predictions (list[tuple]) – a list of tuples (label name, score)

Return type:

list

class model_api.models.classification.ProbabilisticLabelsResolver(hierarchical_config, warmup_cache=True)#

Bases: GreedyLabelsResolver

resolve_labels(predictions)#

Resolves hierarchical labels and exclusivity based on a list of ScoredLabels (labels with probability).

The following two steps are taken:

  • selects the most likely label from an exclusive (multiclass) group

  • removes children of “not-most-likely” (non-max) parents in an exclusive group (top-down approach)

Parameters:

predictions (list[tuple[str, float]]) – a list of tuples (label name, score)

Return type:

list[tuple[int, str, float]]

class model_api.models.classification.SimpleLabelsGraph(vertices)#

Bases: object

Class representing a tree. It implements basic operations like adding edges, getting children and parents.

add_edge(parent, child)#
Return type:

None

clear_topological_cache()#
Return type:

None

get_ancestors(label)#

Returns all the ancestors of the input label, including self.

Return type:

list[str]

get_children(label)#
Return type:

list

get_labels_in_topological_order()#
Return type:

list

get_parent(label)#
Return type:

str | None

topological_sort()#
Return type:

list

model_api.models.classification.addOrFindSoftmaxAndTopkOutputs(inference_adapter, topk, output_raw_scores)#
Return type:

None

model_api.models.classification.sigmoid_numpy(x)#
Return type:

ndarray