Skip to content

feat(tflite): add private inference helpers and tests - #1011

Merged
Borda merged 11 commits into
developfrom
tflite/inf
Apr 29, 2026
Merged

Borda merged 11 commits into
developfrom
tflite/inf

Conversation

@Borda

@Borda Borda commented Apr 29, 2026

Copy link
Copy Markdown
Member

This pull request introduces a new TFLite inference helper module for RF-DETR exported models, along with a comprehensive test suite. The main focus is to provide lightweight, dependency-minimal utilities for running inference on TFLite models outside the training stack, and to ensure robust and reliable operation through detailed unit tests.

Key additions and improvements:

TFLite Inference Utilities

  • Added src/rfdetr/export/_tflite/inference.py, which provides helper functions for TFLite model inference:
    • _create_interpreter: Loads and prepares a TFLite interpreter, supporting both tflite-runtime and tensorflow.lite as backends, and prints input/output tensor shapes.
    • _run_inference: Handles image preprocessing (including normalization and resizing), runs inference, and decodes detection outputs into pixel-space bounding boxes using the supervision library. Supports both RGB and grayscale images and is robust to output tensor ordering.
    • Includes a numerically stable _softmax function for post-processing logits.

Comprehensive Testing

  • Added tests/export/test_tflite_inference.py with detailed unit tests for the new inference helpers:
    • Tests for interpreter creation, backend fallback logic, tensor allocation, and correct handling of both string and Path model paths.
    • Tests for inference correctness, including detection thresholding, pixel-space box scaling, handling of RGB and grayscale images, and robustness to output tensor reordering.
    • Uses extensive mocking to simulate TFLite interpreters and image inputs, ensuring tests do not require actual TFLite models or hardware.

These changes make it easy to deploy and test RF-DETR TFLite models in environments where only minimal dependencies are available, and ensure correctness and robustness through thorough testing.

- `_create_interpreter`, `_run_inference`, `_softmax` in `export/_tflite/inference.py`
- 14 mock-based tests in `tests/export/test_tflite_inference.py`

---
Co-authored-by: Claude Code <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 29, 2026 21:09
@codecov

codecov Bot commented Apr 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.11111% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 80%. Comparing base (3036069) to head (9863df9).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff           @@
##           develop   #1011   +/-   ##
=======================================
  Coverage       80%     80%           
=======================================
  Files          100     101    +1     
  Lines         8447    8519   +72     
=======================================
+ Hits          6774    6836   +62     
- Misses        1673    1683   +10     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Adds lightweight, dependency-minimal TFLite inference helpers under rfdetr.export._tflite and introduces a dedicated unit test suite to validate interpreter creation and output decoding behavior for exported RF-DETR models.

Changes:

  • Added src/rfdetr/export/_tflite/inference.py with private helper functions for interpreter creation, image preprocessing, and decoding TFLite outputs into supervision.Detections.
  • Added tests/export/test_tflite_inference.py with mocked-interpreter tests covering backend fallback, preprocessing, thresholding, grayscale handling, and output reordering.

Reviewed changes

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

File Description
src/rfdetr/export/_tflite/inference.py Implements private TFLite interpreter + inference helpers (softmax, preprocessing, decoding).
tests/export/test_tflite_inference.py Adds unit tests using mocks/temp images to validate the new inference helpers.

Comment thread src/rfdetr/export/_tflite/inference.py Outdated
Comment thread src/rfdetr/export/_tflite/inference.py
Comment thread src/rfdetr/export/_tflite/inference.py Outdated
Comment thread src/rfdetr/export/_tflite/inference.py Outdated
Comment thread tests/export/test_tflite_inference.py Outdated
Comment thread src/rfdetr/export/_tflite/inference.py Outdated
Borda and others added 8 commits April 29, 2026 23:27
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…reter

[resolve #1] Review comment by @Copilot (PR #1011):
"These unconditional print() calls will emit output in any library usag..."

---
Co-authored-by: Claude Code <noreply@anthropic.com>
…kends missing

[resolve #6] Review comment by @Copilot (PR #1011):
"If neither tflite_runtime nor tensorflow is installed, this will raise..."

---
Co-authored-by: Claude Code <noreply@anthropic.com>
[resolve #2] Review comment by @Copilot (PR #1011):
"_run_inference() always produces a float32 input tensor, but TFLite mo..."

---
Co-authored-by: Claude Code <noreply@anthropic.com>
[resolve #5] Review comment by @Copilot (PR #1011):
"using types.SimpleNamespace is less faithful to real import semantics t..."

---
Co-authored-by: Claude Code <noreply@anthropic.com>
…py ignores

- Use od.get("name", "<unnamed>") in debug log (W2: KeyError guard)
- Fix test_logs_input_and_output_shapes to mock logger.debug (propagate=False blocks caplog)
- Add test_raises_for_non_float32_input_dtype covering new ValueError branch
- Add tflite_runtime/tensorflow to mypy ignore_missing_imports (optional deps)

---
Co-authored-by: Claude Code <noreply@anthropic.com>
Comment thread src/rfdetr/export/_tflite/inference.py Outdated
Comment thread src/rfdetr/export/_tflite/inference.py Outdated
Comment thread src/rfdetr/export/_tflite/inference.py Outdated
Borda and others added 2 commits April 30, 2026 00:02
Co-authored-by: Jirka Borovec <6035284+Borda@users.noreply.github.com>
…tch.dict

---
Co-authored-by: Claude Code <noreply@anthropic.com>
@Borda
Borda merged commit 1a17f6f into develop Apr 29, 2026
24 checks passed
@Borda
Borda deleted the tflite/inf branch April 29, 2026 22:52
@Borda Borda added the enhancement New feature or request label Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants