Skip to content

Migrate off pycoral to ai-edge-litert#2

Merged
VIDGuide merged 1 commit into
mainfrom
litert-migration
Jul 10, 2026
Merged

Migrate off pycoral to ai-edge-litert#2
VIDGuide merged 1 commit into
mainfrom
litert-migration

Conversation

@VIDGuide

Copy link
Copy Markdown
Owner

Replaces pycoral/tflite_runtime (abandoned upstream, cp39-only) with
ai-edge-litert (Google's actively maintained LiteRT runtime), removing the
Python 3.9 pin and the downstream numpy 1.x / opencv-python-headless 4.9.x
version ceiling it forced. See #1 for the full background on why this was
needed.

What changed

  • detector.py rewritten against ai_edge_litert.interpreter directly —
    reimplements the small pure-Python surface area pycoral used to wrap
    (input tensor resize/pad, output tensor parsing). Public API unchanged.
  • Dockerfile: python:3.9python:3.12, current stable versions of
    numpy/opencv/requests/paho-mqtt/shapely (no more forced pins).
  • tests/test_detector.py (new, 8 tests) covering the tensor-parsing logic.
  • CI bumped to Python 3.12.
  • README's "Known limitations" rewritten for the new stack.

Verified so far (this machine, no Coral hardware)

  • Full linux/amd64 Docker build succeeds.
  • All modules import inside the built container.
  • Ran the actual ssd_mobilenet_v2_coco_quant_postprocess model (CPU/XNNPACK
    interpreter, since the Edge TPU delegate needs real hardware) against a
    known two-dog test photo — got two correct 93%-confidence dog detections
    with correct bounding boxes, confirming the reimplemented tensor parsing
    matches pycoral's behavior exactly.
  • Full local test suite (55 tests) passes on Python 3.12 with the real
    ai-edge-litert package installed.

Not yet verified

  • The actual Edge TPU delegate against real Coral hardware (/dev/apex_0).
    This is the next step — will verify on the physical server before
    merging.

pycoral/tflite_runtime are abandoned upstream and only ever shipped cp39
wheels, which pinned this whole project to Python 3.9 and, downstream of
that, to numpy 1.x (pycoral's compiled bindings are built against the
numpy 1.x C ABI). Migrating to ai-edge-litert (Google's actively maintained
LiteRT runtime, wheels through Python 3.14, no numpy ceiling) removes both
constraints.

- detector.py: rewritten to use ai_edge_litert.interpreter directly
  (Interpreter + load_delegate), reimplementing pycoral's small pure-Python
  surface area (input tensor resize/pad, output tensor parsing for
  SSD-style detection models, including the same 'legacy tensor order'
  fallback pycoral used for older model exports). Public API
  (DogDetector.__init__/.detect()) is unchanged, so camera_pipeline.py and
  dogwatch.py needed no changes.
- Dockerfile: python:3.9-slim-bookworm -> python:3.12-slim-bookworm,
  ai-edge-litert==2.1.6 replacing the pycoral/tflite_runtime wheel URLs,
  numpy/opencv-python-headless/requests/paho-mqtt/shapely all bumped to
  current stable releases (no more version ceiling forcing them behind).
- Verified locally: full amd64 Docker build succeeds; all modules import
  inside the container; ran the real ssd_mobilenet_v2_coco_quant_postprocess
  model (CPU/XNNPACK, since the Edge TPU delegate needs real hardware)
  against a known two-dog test photo and got two correct 93%-confidence dog
  detections with sane bounding boxes, confirming the reimplemented tensor
  parsing produces identical results to pycoral's.
- Added tests/test_detector.py (8 tests) covering the tensor-parsing logic
  with fake interpreter objects, including both output-tensor-order
  branches pycoral supported.
- requirements-test.txt and .github/workflows/ci.yml bumped to Python 3.12
  and the same current dependency versions as the Dockerfile.
- README: rewrote the pycoral-specific 'Known limitations' entries to
  describe the new ai-edge-litert + feranick/libedgetpu stack and its own
  (much smaller) residual risk.
- .gitignore: add .ssh (homelab SSH credentials file, was untracked but
  unignored).

NOT yet verified: the actual Edge TPU delegate against real Coral hardware
(/dev/apex_0) — that requires the physical server and is the next step
before merging to main.
@cursor

cursor Bot commented Jul 10, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

@VIDGuide

Copy link
Copy Markdown
Owner Author

Hardware verification: passed ✅

Verified on the real Coral PCIe M.2 TPU (`/dev/apex_0`) via SSH to the homelab server:

  • Built the image natively on the server (Debian, x86_64) — succeeds cleanly.
  • `load_delegate("libedgetpu.so.1")` loads successfully against `/dev/apex_0`.
  • Ran `DogDetector` against the real Edge-TPU-compiled model
    (`ssd_mobilenet_v2_coco_quant_postprocess_edgetpu.tflite`) with the actual
    hardware attached:
    • Inference time: ~9ms mean over 20 runs — consistent with genuine Edge TPU
      acceleration (CPU-only inference for this model is typically 100-300ms).
    • Ran against a known two-dog test photo: correctly detected both dogs at
      93% confidence with sane bounding boxes, matching the CPU/XNNPACK dry-run
      from the PR description almost exactly (small bbox differences expected —
      different quantized/compiled model variant).
  • Rebuilt and restarted the actual production container via
    `docker compose build && docker compose up -d`. Both camera pipelines
    (`camera`, `rear-east`) came up clean, MQTT connected on both, zero
    Python errors/tracebacks over several minutes of live operation, container
    RestartCount stayed at 0.

Merging.

@VIDGuide VIDGuide merged commit c6803a2 into main Jul 10, 2026
3 checks passed
@VIDGuide VIDGuide deleted the litert-migration branch July 10, 2026 07:05
VIDGuide pushed a commit that referenced this pull request Jul 12, 2026
…e of missed alerts

User reported missed dog-at-fence alerts and suspected the ai-edge-litert
migration (PR #2). Investigated live on the server:

- Confirmed the Coral TPU, MQTT pipeline, and camera streams were all
  healthy — no infrastructure issue.
- Used 5 real past-detection sample images (4 rear-east alert photos +
  1 live fence-camera capture with a confirmed dog in frame) to test the
  actual production detector against real hardware.
- All 5 samples showed weak (~0.20) or zero detection scores at the full
  frame — but a direct A/B test against the pre-migration pycoral/
  tflite_runtime stack, run on the same hardware with the same samples,
  produced IDENTICAL scores and bounding boxes. This rules out the
  ai-edge-litert migration as the cause.
- Root cause: ssd_mobilenet_v2's fixed 300x300 input resolution struggles
  with small/distant dogs in full uncropped wide-angle frames (confirmed:
  cropping the same images to the region containing the dog raised scores
  from 0.00 to 0.72-0.80). This is a pre-existing model limitation, not a
  regression — it just hadn't been exercised by dogs at this distance
  before, or was previously borderline-passing and tipped over by normal
  score jitter.
- Actionable fix identified: the fence 'camera' config has no crop_roi set
  (unlike rear-east, which already uses one), which is the most direct
  lever to improve small-object detection sensitivity on that camera.

Added:
- samples/ (5 real images + README.md documenting ground truth, measured
  baseline scores, and the A/B test methodology)
- tests/hardware_smoke_test.py: runs the real DogDetector against real
  Coral TPU hardware using these samples, comparing against tracked
  baseline scores to catch future regressions (vs. a prior naive version
  that would have falsely flagged all 5 known-weak samples as failures
  every run)
- README: documented the smoke test, and flagged crop_roi as the
  recommended fix for the fence camera in the config reference table
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