22
22
class ImageModel (Model ):
23
23
'''An abstract wrapper for an image-based model
24
24
25
- An image-based model is a model which has one or more inputs with image - 4D tensors with NHWC or NCHW layout.
26
- Also it may support additional inputs - 2D tensor.
27
- Implements basic preprocessing for image: resizing and aligning to model input.
25
+ The ImageModel has 1 or more inputs with images - 4D tensors with NHWC or NCHW layout.
26
+ It may support additional inputs - 2D tensors.
27
+
28
+ The ImageModel implements basic preprocessing for an image provided as model input.
29
+ See `preprocess` description.
30
+
31
+ The `postprocess` method must be implemented in a specific inherited wrapper.
28
32
29
33
Attributes:
30
- resize_type(str): one of the preimplemented resize types
31
- image_blob_names(List[str]): names of all image-like inputs (4D tensors)
32
- image_info_blob_names(List[str]): names of all secondary inputs (2D tensors)
33
- image_blob_name(str): name of image input (None, if they are many)
34
+ image_blob_names (List[str]): names of all image-like inputs (4D tensors)
35
+ image_info_blob_names (List[str]): names of all secondary inputs (2D tensors)
36
+ image_blob_name (str): name of the first image input
37
+ nchw_layout (bool): a flag whether the model input layer has NCHW layout
38
+ resize_type (str): the type for image resizing (see `RESIZE_TYPE` for info)
39
+ resize (function): resizing function corresponding to the `resize_type`
40
+ input_transform (InputTransform): instance of the `InputTransform` for image normalization
34
41
'''
35
42
36
43
def __init__ (self , model_adapter , configuration = None , preload = False ):
37
44
'''Image model constructor
38
45
39
- Calls the `Model` constructor first
46
+ It extends the `Model` constructor.
40
47
41
48
Args:
42
- model_adapter(ModelAdapter): allows working with the specified executor
43
- resize_type(str): sets the type for image resizing (see ``RESIZE_TYPE`` for info)
49
+ model_adapter (ModelAdapter): allows working with the specified executor
50
+ configuration (dict, optional): it contains values for parameters accepted by specific
51
+ wrapper (`confidence_threshold`, `labels` etc.) which are set as data attributes
52
+ preload (bool, optional): a flag whether the model is loaded to device while
53
+ initialization. If `preload=False`, the model must be loaded via `load` method before inference
54
+
55
+ Raises:
56
+ WrapperError: if the wrapper failed to define appropriate inputs for images
44
57
'''
45
58
super ().__init__ (model_adapter , configuration , preload )
46
59
self .image_blob_names , self .image_info_blob_names = self ._get_inputs ()
@@ -75,6 +88,15 @@ def parameters(cls):
75
88
return parameters
76
89
77
90
def _get_inputs (self ):
91
+ '''Defines the model inputs for images and additional info.
92
+
93
+ Raises:
94
+ WrapperError: if the wrapper failed to define appropriate inputs for images
95
+
96
+ Returns:
97
+ - list of inputs names for images
98
+ - list of inputs names for additional info
99
+ '''
78
100
image_blob_names , image_info_blob_names = [], []
79
101
for name , metadata in self .inputs .items ():
80
102
if len (metadata .shape ) == 4 :
@@ -90,24 +112,27 @@ def _get_inputs(self):
90
112
def preprocess (self , inputs ):
91
113
'''Data preprocess method
92
114
93
- Performs some basic preprocessing with single image:
94
- - resizing to net input size
95
- - applying tranform orerations: mean and scale values, BGR-RGB conversions
96
- - changing layout according to net input layout
115
+ It performs basic preprocessing of a single image:
116
+ - Resizes the image to fit the model input size via the defined resize type
117
+ - Normalizes the image: subtracts means, divides by scales, switch channels BGR-RGB
118
+ - Changes the image layout according to the model input layout
97
119
98
- Adds the size of initial image and after resizing to metadata as `original_shape` and `resized_shape`
99
- correspondenly .
120
+ Also, it keeps the size of original image and resized one as `original_shape` and `resized_shape`
121
+ in the metadata dictionary .
100
122
101
123
Note:
102
- This method supports only models with single image input. If model has more image inputs
103
- or has additional support inputs, their preprocessing should be implemented in concrete class
124
+ It supports only models with single image input. If the model has more image inputs or has
125
+ additional supported inputs, the `preprocess` should be overloaded in a specific wrapper.
104
126
105
127
Args:
106
- inputs: single image as 3D array in HWC layout
128
+ inputs (ndarray): a single image as 3D array in HWC layout
107
129
108
130
Returns:
109
- - the dict with preprocessed image data
110
- - The dict with metadata
131
+ - the preprocessed image in the following format:
132
+ {
133
+ 'input_layer_name': preprocessed_image
134
+ }
135
+ - the input metadata, which might be used in `postprocess` method
111
136
'''
112
137
image = inputs
113
138
meta = {'original_shape' : image .shape }
@@ -121,8 +146,16 @@ def preprocess(self, inputs):
121
146
return dict_inputs , meta
122
147
123
148
def _change_layout (self , image ):
149
+ '''Changes the input image layout to fit the layout of the model input layer.
150
+
151
+ Args:
152
+ inputs (ndarray): a single image as 3D array in HWC layout
153
+
154
+ Returns:
155
+ - the image with layout aligned with the model layout
156
+ '''
124
157
if self .nchw_layout :
125
- image = image .transpose ((2 , 0 , 1 )) # Change data layout from HWC to CHW
158
+ image = image .transpose ((2 , 0 , 1 )) # HWC-> CHW
126
159
image = image .reshape ((1 , self .c , self .h , self .w ))
127
160
else :
128
161
image = image .reshape ((1 , self .h , self .w , self .c ))
0 commit comments