Skip to content

refactor: standardize naming and imports of Detections across modules#1091

Open
Borda wants to merge 2 commits into
developfrom
fix/imports
Open

refactor: standardize naming and imports of Detections across modules#1091
Borda wants to merge 2 commits into
developfrom
fix/imports

Conversation

@Borda

@Borda Borda commented Jun 4, 2026

Copy link
Copy Markdown
Member

This pull request refactors the codebase to use direct imports of specific classes and functions from the supervision library instead of importing the entire module as sv. This change improves code clarity and reduces unnecessary namespace usage. Additionally, the code now consistently uses PIL.Image for image I/O instead of cv2, standardizing image handling across the project. Some docstrings and type annotations have also been updated to reflect these changes.

Refactor: Use direct imports from supervision instead of module alias

Image handling standardization

  • Replaced all uses of cv2.imread and OpenCV BGR-to-RGB conversion with PIL.Image.open and direct RGB handling, ensuring consistency and correctness in image loading and saving. [1] [2] [3] [4]

Documentation and type annotation updates

  • Updated docstrings and type annotations to refer to the correct classes and usage patterns after the refactor, improving code readability and maintainability. [1] [2] [3]

These changes collectively improve code clarity, maintainability, and consistency across the project.

Borda and others added 2 commits June 3, 2026 17:40
- `yolo.py`: replace `cv2.imread` with `PIL.Image.open(...).convert("RGB")`; removes implicit BGR→RGB flip; rename `cv2_image` → `rgb_image`/`image_array` throughout
- `synthetic.py`: replace `cv2.imwrite` with `Image.fromarray(img[..., ::-1]).save()`; explicit BGR→RGB flip preserved since supervision draws in BGR order
- `test_yolo.py`: update mock targets from `cv2.imread` to `Image.Image.convert`; replace return-None mock with on-disk corruption for more realistic failure test

---
Co-authored-by: Claude Code <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 4, 2026 12:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors RF-DETR’s integrations with the supervision library to use direct symbol imports (e.g., Detections, BoxAnnotator) instead of import supervision as sv, and standardizes image I/O on PIL.Image (replacing OpenCV-based loading where applicable). It also updates related type annotations/docstrings and adjusts tests to assert that lazy datasets don’t decode pixel buffers during initialization.

Changes:

  • Replaced sv.* usages with direct supervision imports across visualization, export inference helpers, datasets, and the main RFDETR.predict typing surface.
  • Switched YOLO lazy image loading from cv2.imread to PIL.Image.open(...).convert("RGB"), and updated tests to validate “no pixel decode during init” behavior.
  • Updated synthetic dataset generation to save images via PIL while accounting for channel-order differences.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/datasets/test_yolo.py Updates lazy-mask tests to assert no pixel-buffer decode during dataset init and to simulate unreadable images with PIL.
src/rfdetr/visualize/data.py Converts visualization helpers to direct supervision imports and removes sv alias usage.
src/rfdetr/models/backbone/projector.py Renames internal conv module attributes (cv1/cv2conv1/conv2) in backbone projector blocks.
src/rfdetr/export/_tflite/inference.py Uses direct Detections import and updates return typing/construction accordingly.
src/rfdetr/export/_onnx/inference.py Uses direct Detections import and updates return typing/construction accordingly.
src/rfdetr/detr.py Updates type-checking imports and runtime usage to remove sv alias for Detections.
src/rfdetr/datasets/yolo.py Replaces cv2-based image reads with PIL, keeps metadata-only init behavior, and updates Detections typing/usage.
src/rfdetr/datasets/synthetic.py Switches image writing from OpenCV to PIL and updates supervision imports/types.
src/rfdetr/datasets/save_grids.py Refactors supervision usage to direct imports for annotators/colors/detections.

Comment on lines 129 to 133
c_ = int(c2 * e) # hidden channels
self.cv1 = ConvX(c1, c_, k[0], 1, act=act, layer_norm=layer_norm, rms_norm=rms_norm)
self.cv2 = ConvX(c_, c2, k[1], 1, groups=g, act=act, layer_norm=layer_norm, rms_norm=rms_norm)
self.conv1 = ConvX(c1, c_, k[0], 1, act=act, layer_norm=layer_norm, rms_norm=rms_norm)
self.conv2 = ConvX(c_, c2, k[1], 1, groups=g, act=act, layer_norm=layer_norm, rms_norm=rms_norm)
self.add = shortcut and c1 == c2

Comment on lines 145 to 149
self.c = int(c2 * e) # hidden channels
self.cv1 = ConvX(c1, 2 * self.c, 1, 1, act=act, layer_norm=layer_norm, rms_norm=rms_norm)
self.cv2 = ConvX(
self.conv1 = ConvX(c1, 2 * self.c, 1, 1, act=act, layer_norm=layer_norm, rms_norm=rms_norm)
self.conv2 = ConvX(
(2 + n) * self.c, c2, 1, act=act, layer_norm=layer_norm, rms_norm=rms_norm
) # optional act=FReLU(c2)
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.

2 participants