Skip to content

Commit 18f5a2d

Browse files
committed
onnx fix
1 parent d09601f commit 18f5a2d

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

src/model_api/models/image_model.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,16 @@ def __init__(self, inference_adapter: InferenceAdapter, configuration: dict = {}
6868
self.embedded_processing: bool
6969
self.labels: list[str]
7070

71-
self._is_dynamic = False
7271
self.nchw_layout = self.inputs[self.image_blob_name].layout == "NCHW"
7372
if self.nchw_layout:
7473
self.n, self.c, self.h, self.w = self.inputs[self.image_blob_name].shape
7574
else:
7675
self.n, self.h, self.w, self.c = self.inputs[self.image_blob_name].shape
76+
77+
self._is_dynamic = False
78+
if self.h == -1 or self.w == -1:
79+
self._is_dynamic = True
80+
7781
self.resize = RESIZE_TYPES[self.resize_type]
7882
self.input_transform = InputTransform(
7983
self.reverse_input_channels,
@@ -84,7 +88,7 @@ def __init__(self, inference_adapter: InferenceAdapter, configuration: dict = {}
8488
layout = self.inputs[self.image_blob_name].layout
8589
if self.embedded_processing:
8690
self.h, self.w = self.orig_height, self.orig_width
87-
else:
91+
elif not self._is_dynamic:
8892
inference_adapter.embed_preprocessing(
8993
layout=layout,
9094
resize_mode=self.resize_type,
@@ -214,11 +218,24 @@ def preprocess(self, inputs: np.ndarray) -> list[dict]:
214218
}
215219
- the input metadata, which might be used in `postprocess` method
216220
"""
221+
if self._is_dynamic:
222+
h, w, c = inputs.shape
223+
resized_shape = (w, h, c)
224+
processed_image = self.input_transform(inputs)
225+
processed_image = self._change_layout(processed_image)
226+
else:
227+
resized_shape = (self.w, self.h, self.c)
228+
if self.embedded_processing:
229+
processed_image = inputs[None]
230+
else:
231+
processed_image = self.input_transform(inputs)
232+
processed_image = self._change_layout(processed_image)
233+
217234
return [
218-
{self.image_blob_name: inputs[None]},
235+
{self.image_blob_name: processed_image},
219236
{
220237
"original_shape": inputs.shape,
221-
"resized_shape": (self.w, self.h, self.c),
238+
"resized_shape": resized_shape,
222239
},
223240
]
224241

@@ -231,9 +248,13 @@ def _change_layout(self, image: np.ndarray) -> np.ndarray:
231248
Returns:
232249
- the image with layout aligned with the model layout
233250
"""
251+
h, w, c = image.shape if self._is_dynamic else (self.h, self.w, self.c)
252+
253+
# For fixed models, use the predefined dimensions
234254
if self.nchw_layout:
235255
image = image.transpose((2, 0, 1)) # HWC->CHW
236-
image = image.reshape((1, self.c, self.h, self.w))
256+
image = image.reshape((1, c, h, w))
237257
else:
238-
image = image.reshape((1, self.h, self.w, self.c))
258+
image = image.reshape((1, h, w, c))
259+
239260
return image

0 commit comments

Comments
 (0)