@@ -51,6 +51,7 @@ def __attrs_post_init__(self):
51
51
self ._is_single_task : bool = len (self .project .get_trainable_tasks ()) == 1
52
52
self ._are_models_loaded : bool = False
53
53
self ._inference_converters : Dict [str , Any ] = {}
54
+ self ._alternate_inference_converters : Dict [str , Any ] = {}
54
55
self ._empty_labels : Dict [str , Label ] = {}
55
56
56
57
@property
@@ -131,6 +132,7 @@ def load_inference_models(self, device: str = "CPU"):
131
132
"""
132
133
try :
133
134
from ote_sdk .usecases .exportable_code .prediction_to_annotation_converter import (
135
+ DetectionBoxToAnnotationConverter ,
134
136
IPredictionToAnnotationConverter ,
135
137
create_converter ,
136
138
)
@@ -159,6 +161,16 @@ def load_inference_models(self, device: str = "CPU"):
159
161
converter_type = task .type .to_ote_domain (), labels = model .ote_label_schema
160
162
)
161
163
inference_converters .update ({task .title : inference_converter })
164
+
165
+ # This is a workaround for a backwards incompatible change in later ote
166
+ # versions
167
+ if task .type .is_detection :
168
+ alternate_inference_converter = DetectionBoxToAnnotationConverter (
169
+ labels = model .ote_label_schema
170
+ )
171
+ self ._alternate_inference_converters .update (
172
+ {task .title : alternate_inference_converter }
173
+ )
162
174
empty_label = next ((label for label in task .labels if label .is_empty ), None )
163
175
empty_labels .update ({task .title : empty_label })
164
176
@@ -274,9 +286,23 @@ def _infer_task(self, image: np.ndarray, task: Task) -> Prediction:
274
286
height : int = image .shape [0 ]
275
287
276
288
if len (postprocessing_results ) != 0 :
277
- annotation_scene_entity = converter .convert_to_annotation (
278
- predictions = postprocessing_results , metadata = metadata
279
- )
289
+ # The try/except is a workaround to handle different detection inference
290
+ # results by different ote sdk versions
291
+ try :
292
+ annotation_scene_entity = converter .convert_to_annotation (
293
+ predictions = postprocessing_results , metadata = metadata
294
+ )
295
+ except TypeError as error :
296
+ if task .type .is_detection :
297
+ converter = self ._alternate_inference_converters [task .title ]
298
+ annotation_scene_entity = converter .convert_to_annotation (
299
+ predictions = postprocessing_results , metadata = metadata
300
+ )
301
+ # Make sure next time we get it right in one shot
302
+ self ._inference_converters .update ({task .title : converter })
303
+ else :
304
+ raise error
305
+
280
306
prediction = Prediction .from_ote (
281
307
annotation_scene_entity , image_width = width , image_height = height
282
308
)
0 commit comments