Segmentation#
The SegmentationModel
is the OpenVINO wrapper for models exported from OpenVINO Training Extensions. It produces a segmentation mask for the input image.
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#
resultImage
: Image with the segmentation mask.soft_prediction
: Soft prediction of the segmentation model.saliency_map
: Saliency map of the input image.feature_vector
: Feature vector of the input image. This is useful for Active Learning.
- class model_api.models.segmentation.SalientObjectDetectionModel(inference_adapter, configuration={}, preload=False)#
Bases:
SegmentationModel
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
- 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:
Mat
- Returns:
postprocessed data in the format defined by wrapper
- class model_api.models.segmentation.SegmentationModel(inference_adapter, configuration={}, preload=False)#
Bases:
ImageModel
Segmentation 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 SegmentationModel >>> import cv2 >>> model = SegmentationModel.create_model("./path_to_model.xml") >>> image = cv2.imread("path_to_image.jpg") >>> result = model.predict(image) ImageResultWithSoftPrediction( ... )
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_contours(prediction)#
- Return type:
list
- 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:
ImageResultWithSoftPrediction
|Mat
- Returns:
postprocessed data in the format defined by wrapper
- model_api.models.segmentation.create_hard_prediction_from_soft_prediction(soft_prediction, soft_threshold, blur_strength)#
Creates a hard prediction containing the final label index per pixel.
- Parameters:
soft_prediction (
ndarray
) – Output from segmentation network. Assumes floating point values, between 0.0 and 1.0. Can be a per-class segmentation logits of shape (height, width, num_classes)soft_threshold (
float
) – minimum class confidence for each pixel. The higher the value, the more strict the segmentation is (usually set to 0.5)blur_strength (
int
) – The higher the value, the smoother the segmentation output will be, but less accurate
- Return type:
ndarray
- Returns:
Numpy array of the hard prediction