Skip to content

fix(detection): scale from_tensorflow boxes by correct axes#2360

Open
RubenHaisma wants to merge 1 commit into
roboflow:developfrom
RubenHaisma:fix/from-tensorflow-axis-scaling
Open

fix(detection): scale from_tensorflow boxes by correct axes#2360
RubenHaisma wants to merge 1 commit into
roboflow:developfrom
RubenHaisma:fix/from-tensorflow-axis-scaling

Conversation

@RubenHaisma

Copy link
Copy Markdown
Contributor

Description

Detections.from_tensorflow scaled the normalized box coordinates by the wrong image dimensions. Tensorflow Hub object-detection models emit detection_boxes as normalized [ymin, xmin, ymax, xmax] (the connector's own [1, 0, 3, 2] reorder confirms this layout), so the y coordinates must scale by height and x by width. The code did the opposite:

boxes[:, [0, 2]] *= resolution_wh[0]   # ymin, ymax scaled by WIDTH  (should be height)
boxes[:, [1, 3]] *= resolution_wh[1]   # xmin, xmax scaled by HEIGHT (should be width)

The bug is masked on square images (width == height) but corrupts every coordinate on the common non-square case.

Reproduction

A box normalized to [ymin=0.1, xmin=0.2, ymax=0.5, xmax=0.6] on a 1000×500 image (resolution_wh=(1000, 500)):

xmin ymin xmax ymax
before (wrong) 100 100 300 500
after (correct) 200 50 600 250

Correct values: xmin = 0.2·1000, ymin = 0.1·500, xmax = 0.6·1000, ymax = 0.5·500.

Fix

Swap the two multipliers so y scales by resolution_wh[1] (height) and x by resolution_wh[0] (width). No behavior change on square images.

Tests

Adds test_from_tensorflow_scales_axes_on_non_square_image (the connector was previously untested — no references in tests/ or docs/). It fails on develop ([100,100,300,500]) and passes with this fix ([200,50,600,250]). ruff check/format clean.

`Detections.from_tensorflow` scaled the normalized box coordinates by the
wrong image dimensions: the y coordinates (ymin/ymax, columns 0 and 2) were
multiplied by width and the x coordinates (xmin/xmax, columns 1 and 3) by
height. Tensorflow Hub object-detection models emit `detection_boxes` as
normalized `[ymin, xmin, ymax, xmax]`, so y must scale by height and x by
width.

The bug is masked on square images (width == height) but corrupts every
coordinate on the common non-square case — e.g. a box normalized to
`[0.1, 0.2, 0.5, 0.6]` on a 1000x500 image came out as
`[100, 100, 300, 500]` instead of the correct `[200, 50, 600, 250]`.

Swap the two multipliers so y scales by `resolution_wh[1]` (height) and x by
`resolution_wh[0]` (width). Adds a non-square regression test (the connector
was previously untested).
@RubenHaisma RubenHaisma requested a review from SkalskiP as a code owner June 27, 2026 13:17
@codecov

codecov Bot commented Jun 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82%. Comparing base (09b2199) to head (3ea9854).

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #2360   +/-   ##
=======================================
  Coverage       82%     82%           
=======================================
  Files           68      68           
  Lines         9560    9560           
=======================================
+ Hits          7881    7886    +5     
+ Misses        1679    1674    -5     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant