-
Notifications
You must be signed in to change notification settings - Fork 223
fix: pass an explicit factor to smart_resize for Qwen2.5-VL #241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
roshaninfordham
wants to merge
2
commits into
roboflow:develop
Choose a base branch
from
roshaninfordham:fix/qwen-smart-resize-factor
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
101 changes: 101 additions & 0 deletions
101
test/meastro/trainer/models/qwen_2_5_vl/test_detection.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| # `qwen_vl_utils` ships with the optional `qwen_2_5_vl` extra. Without this guard the | ||
| # import below aborts collection for the whole run, taking unrelated suites with it. | ||
| pytest.importorskip("qwen_vl_utils", reason="requires the optional `qwen_2_5_vl` extra") | ||
|
|
||
| from maestro.trainer.models.qwen_2_5_vl.detection import ( | ||
| QWEN_2_5_VL_IMAGE_FACTOR, | ||
| detections_to_suffix_formatter, | ||
| ) | ||
|
|
||
| MIN_PIXELS = 256 * 28 * 28 | ||
| MAX_PIXELS = 1280 * 28 * 28 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("xyxy", "class_id", "classes", "resolution_wh", "image_factor", "expected"), | ||
| [ | ||
| # 1. Single box on a 640x480 image. smart_resize gives (h=476, w=644), so the | ||
| # box is scaled by 644/640 horizontally and 476/480 vertically. | ||
| ( | ||
| np.array([[10.0, 20.0, 110.0, 120.0]]), | ||
| np.array([0]), | ||
| ["cat", "dog"], | ||
| (640, 480), | ||
| QWEN_2_5_VL_IMAGE_FACTOR, | ||
| '```json\n[\n\t{"bbox_2d": [10, 19, 110, 119], "label": "cat"}\n]\n```', | ||
| ), | ||
| # 2. Two boxes, second class -> both scaled, labels resolved by class_id. | ||
| ( | ||
| np.array([[0.0, 0.0, 640.0, 480.0], [320.0, 240.0, 640.0, 480.0]]), | ||
| np.array([0, 1]), | ||
| ["cat", "dog"], | ||
| (640, 480), | ||
| QWEN_2_5_VL_IMAGE_FACTOR, | ||
| '```json\n[\n\t{"bbox_2d": [0, 0, 644, 476], "label": "cat"},\n' | ||
| '\t{"bbox_2d": [322, 238, 644, 476], "label": "dog"}\n]\n```', | ||
| ), | ||
| # 3. Larger source resolution -> (h=756, w=1036). | ||
| ( | ||
| np.array([[0.0, 0.0, 1024.0, 768.0]]), | ||
| np.array([0]), | ||
| ["cat"], | ||
| (1024, 768), | ||
| QWEN_2_5_VL_IMAGE_FACTOR, | ||
| '```json\n[\n\t{"bbox_2d": [0, 0, 1036, 756], "label": "cat"}\n]\n```', | ||
| ), | ||
| # 4. A non-default factor is honoured: 640x480 at factor 56 gives (h=504, w=616). | ||
| ( | ||
| np.array([[0.0, 0.0, 640.0, 480.0]]), | ||
| np.array([0]), | ||
| ["cat"], | ||
| (640, 480), | ||
| 56, | ||
| '```json\n[\n\t{"bbox_2d": [0, 0, 616, 504], "label": "cat"}\n]\n```', | ||
| ), | ||
| # 5. No detections -> an empty JSON block rather than an error. | ||
| ( | ||
| np.zeros((0, 4), dtype=np.float32), | ||
| np.zeros((0,), dtype=np.int32), | ||
| ["cat"], | ||
| (640, 480), | ||
| QWEN_2_5_VL_IMAGE_FACTOR, | ||
| "```json\n[\n\n]\n```", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_detections_to_suffix_formatter( | ||
| xyxy: np.ndarray, | ||
| class_id: np.ndarray, | ||
| classes: list[str], | ||
| resolution_wh: tuple[int, int], | ||
| image_factor: int, | ||
| expected: str, | ||
| ) -> None: | ||
| result = detections_to_suffix_formatter( | ||
| xyxy=xyxy, | ||
| class_id=class_id, | ||
| classes=classes, | ||
| resolution_wh=resolution_wh, | ||
| min_pixels=MIN_PIXELS, | ||
| max_pixels=MAX_PIXELS, | ||
| image_factor=image_factor, | ||
| ) | ||
| assert result == expected | ||
|
|
||
|
|
||
| def test_resized_side_lengths_are_multiples_of_the_image_factor() -> None: | ||
| """The whole point of `factor`: Qwen2.5-VL cannot patch a side it cannot divide.""" | ||
| result = detections_to_suffix_formatter( | ||
| xyxy=np.array([[0.0, 0.0, 640.0, 480.0]]), | ||
| class_id=np.array([0]), | ||
| classes=["cat"], | ||
| resolution_wh=(640, 480), | ||
| min_pixels=MIN_PIXELS, | ||
| max_pixels=MAX_PIXELS, | ||
| ) | ||
| x1, y1, x2, y2 = (int(value) for value in result.split("[")[2].split("]")[0].split(",")) | ||
| assert x2 % QWEN_2_5_VL_IMAGE_FACTOR == 0 | ||
| assert y2 % QWEN_2_5_VL_IMAGE_FACTOR == 0 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the default test environment this new test is collected unconditionally, but
tox.inionly installspytestplus the package’s normal dependencies, whileqwen-vl-utilslives under the optionalqwen_2_5_vlextra inpyproject.toml. On a standardtox/dev run without that extra, importingmaestro.trainer.models.qwen_2_5_vl.detectionfails during collection before any tests can run; gate this withpytest.importorskip("qwen_vl_utils")or install the qwen extra in the test env.Useful? React with 👍 / 👎.