Skip to content

Commit 83e5d85

Browse files
authored
Merge pull request #1159 from roboflow/fix-rfdetr-postporccess
Fix rfdetr postporccess
2 parents 73eaec9 + dc290a8 commit 83e5d85

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

inference/models/rfdetr/rfdetr.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,34 @@ def postprocess(
282282
xy_max = cxcy + 0.5 * wh
283283
boxes_xyxy = np.concatenate([xy_min, xy_max], axis=1)
284284

285-
scale_fct = np.array([orig_w, orig_h, orig_w, orig_h], dtype=np.float32)
286-
boxes_xyxy *= scale_fct
285+
if self.resize_method == "Stretch to":
286+
scale_fct = np.array([orig_w, orig_h, orig_w, orig_h], dtype=np.float32)
287+
boxes_xyxy *= scale_fct
288+
else:
289+
input_h, input_w = self.img_size_h, self.img_size_w
290+
291+
scale = min(input_w / orig_w, input_h / orig_h)
292+
scaled_w = int(orig_w * scale)
293+
scaled_h = int(orig_h * scale)
294+
295+
pad_x = (input_w - scaled_w) / 2
296+
pad_y = (input_h - scaled_h) / 2
297+
298+
boxes_input = boxes_xyxy * np.array(
299+
[input_w, input_h, input_w, input_h], dtype=np.float32
300+
)
301+
302+
boxes_input[:, 0] -= pad_x
303+
boxes_input[:, 1] -= pad_y
304+
boxes_input[:, 2] -= pad_x
305+
boxes_input[:, 3] -= pad_y
306+
307+
boxes_xyxy = boxes_input / scale
308+
309+
boxes_xyxy[:, 0] = np.clip(boxes_xyxy[:, 0], 0, orig_w)
310+
boxes_xyxy[:, 1] = np.clip(boxes_xyxy[:, 1], 0, orig_h)
311+
boxes_xyxy[:, 2] = np.clip(boxes_xyxy[:, 2], 0, orig_w)
312+
boxes_xyxy[:, 3] = np.clip(boxes_xyxy[:, 3], 0, orig_h)
287313

288314
batch_predictions = np.column_stack(
289315
(

0 commit comments

Comments
 (0)