Image Model#
- class model_api.models.image_model.ImageModel(inference_adapter, configuration={}, preload=False)#
Bases:
Model
An abstract wrapper for an image-based model
The ImageModel has 1 or more inputs with images - 4D tensors with NHWC or NCHW layout. It may support additional inputs - 2D tensors.
The ImageModel implements basic preprocessing for an image provided as model input. See preprocess description.
The postprocess method must be implemented in a specific inherited wrapper.
- image_blob_names#
names of all image-like inputs (4D tensors)
- Type:
List[str]
- image_info_blob_names#
names of all secondary inputs (2D tensors)
- Type:
List[str]
- image_blob_name#
name of the first image input
- Type:
str
- nchw_layout#
a flag whether the model input layer has NCHW layout
- Type:
bool
- resize_type#
the type for image resizing (see RESIZE_TYPE for info)
- Type:
str
- resize#
resizing function corresponding to the resize_type
- Type:
function
- input_transform#
instance of the InputTransform for image normalization
- Type:
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_label_name(label_id)#
- Return type:
str
- 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
[str
,Any
]- Returns:
the dictionary with defined wrapper data parameters
- preprocess(inputs)#
Data preprocess method
- It performs basic preprocessing of a single image:
Resizes the image to fit the model input size via the defined resize type
Normalizes the image: subtracts means, divides by scales, switch channels BGR-RGB
Changes the image layout according to the model input layout
Also, it keeps the size of original image and resized one as original_shape and resized_shape in the metadata dictionary.
Note
It supports only models with single image input. If the model has more image inputs or has additional supported inputs, the preprocess should be overloaded in a specific wrapper.
- Parameters:
inputs (ndarray) – a single image as 3D array in HWC layout
- Returns:
- {
‘input_layer_name’: preprocessed_image
}
the input metadata, which might be used in postprocess method
- Return type:
the preprocessed image in the following format