Skip to content

Commit 17e4eab

Browse files
authored
Merge branch 'develop' into feature/coral-tpu-detection
2 parents 92aad77 + bd8da83 commit 17e4eab

File tree

11 files changed

+570
-200
lines changed

11 files changed

+570
-200
lines changed

skills/detection/yolo-detection-2026-coral-tpu/deploy.bat

Lines changed: 135 additions & 80 deletions
Large diffs are not rendered by default.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# syntax=docker/dockerfile:1
2+
# ─────────────────────────────────────────────────────────────────────────────
3+
# Coral Edge TPU Model Compiler
4+
#
5+
# Compiles YOLO 2026 nano to Google Coral Edge TPU .tflite format using
6+
# ultralytics' built-in format="edgetpu" export pipeline.
7+
#
8+
# Per https://docs.ultralytics.com/guides/coral-edge-tpu-on-raspberry-pi/:
9+
# model.export(format="edgetpu") handles the full pipeline:
10+
# .pt → ONNX → onnx2tf SavedModel → TFLite INT8 → edgetpu_compiler
11+
#
12+
# MUST run on linux/amd64 — edgetpu_compiler is x86_64 Linux only.
13+
# On Apple Silicon or Windows, Docker Desktop handles QEMU emulation.
14+
# ─────────────────────────────────────────────────────────────────────────────
15+
16+
FROM --platform=linux/amd64 python:3.11-slim
17+
18+
LABEL maintainer="Aegis-AI / DeepCamera"
19+
LABEL description="Compiles YOLO 2026 to Google Coral Edge TPU .tflite"
20+
21+
# ── System deps ───────────────────────────────────────────────────────────────
22+
RUN apt-get update && apt-get install -y --no-install-recommends \
23+
curl \
24+
gnupg \
25+
apt-transport-https \
26+
ca-certificates \
27+
libgl1 \
28+
libglib2.0-0 \
29+
&& rm -rf /var/lib/apt/lists/*
30+
31+
# ── edgetpu_compiler from Google Coral apt (x86_64 only) ─────────────────────
32+
RUN curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg \
33+
| gpg --dearmor -o /usr/share/keyrings/coral-edgetpu.gpg \
34+
&& echo "deb [signed-by=/usr/share/keyrings/coral-edgetpu.gpg] \
35+
https://packages.cloud.google.com/apt coral-edgetpu-stable main" \
36+
> /etc/apt/sources.list.d/coral-edgetpu.list \
37+
&& apt-get update \
38+
&& apt-get install -y --no-install-recommends edgetpu-compiler \
39+
&& rm -rf /var/lib/apt/lists/*
40+
41+
# ── Python: ultralytics handles all TF/onnx2tf version management ─────────────
42+
# ultralytics auto-installs: onnx2tf, tensorflow-cpu, onnxslim, etc.
43+
RUN pip install --no-cache-dir \
44+
"ultralytics>=8.3.0" \
45+
"numpy>=1.24.0,<2.0"
46+
47+
# ── Copy compile entrypoint ───────────────────────────────────────────────────
48+
WORKDIR /compile
49+
COPY scripts/compile_model.py /compile/compile_model.py
50+
51+
# ── Output volume (mount skill's models/ directory here) ──────────────────────
52+
VOLUME ["/compile/output"]
53+
54+
# ── Entrypoint ────────────────────────────────────────────────────────────────
55+
ENTRYPOINT ["python", "/compile/compile_model.py"]
56+
CMD ["--model", "yolo26n", "--size", "320", "--output", "/compile/output"]
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Coral Edge TPU Model Compiler — Docker
2+
3+
Converts the YOLO 2026 nano model to Google Coral Edge TPU format using
4+
a Docker container (so `edgetpu_compiler` runs on Linux x86_64, even from
5+
Windows or Apple Silicon machines).
6+
7+
## Pipeline
8+
9+
```
10+
yolo26n.pt → [ultralytics export INT8] → yolo26n_int8.tflite
11+
→ [edgetpu_compiler] → yolo26n_int8_edgetpu.tflite
12+
```
13+
14+
The compiled `.tflite` file is written to `../models/` and then committed
15+
to the git repository so `deploy.bat` / `deploy.sh` can pick it up without
16+
needing to compile again.
17+
18+
## Requirements
19+
20+
- Docker Desktop (Windows / macOS) or Docker Engine (Linux)
21+
- Internet access on first run (downloads `yolo26n.pt` from ultralytics + base image)
22+
- On Windows: Docker Desktop with WSL2 backend recommended
23+
24+
## Quick Start
25+
26+
### Option A — Shell script (Linux / macOS / Git Bash on Windows)
27+
28+
```bash
29+
bash docker/compile.sh
30+
```
31+
32+
### Option B — Docker Compose
33+
34+
```bash
35+
# From the skill root (yolo-detection-2026-coral-tpu/)
36+
docker compose -f docker/docker-compose.yml run --rm coral-compiler
37+
```
38+
39+
### Option C — Raw Docker commands
40+
41+
```bash
42+
# Build
43+
docker build --platform linux/amd64 -t coral-tpu-compiler -f docker/Dockerfile .
44+
45+
# Run (mounts models/ as output)
46+
docker run --rm --platform linux/amd64 \
47+
-v "$(pwd)/models:/compile/output" \
48+
coral-tpu-compiler \
49+
--model yolo26n --size 320 --output /compile/output
50+
```
51+
52+
## Output Files
53+
54+
After compilation, `models/` will contain:
55+
56+
| File | Size | Notes |
57+
|------|------|-------|
58+
| `yolo26n_int8.tflite` | ~3–4 MB | Full-integer quantized (CPU fallback) |
59+
| `yolo26n_int8_edgetpu.tflite` | ~3–4 MB | Compiled for Edge TPU (primary model) |
60+
61+
> **Note**: `edgetpu_compiler` may warn that some YOLO operations are not mapped
62+
> to the Edge TPU and will fall back to CPU. This is expected for larger YOLO
63+
> architectures with complex postprocessing. The 320×320 nano model achieves
64+
> ~100% on-chip mapping.
65+
66+
## Committing the Model
67+
68+
```bash
69+
git add models/*.tflite
70+
git commit -m "feat(coral-tpu): add compiled yolo26n edgetpu model (320x320 INT8)"
71+
git push
72+
```
73+
74+
## Recompiling
75+
76+
If you update the YOLO model or want a 640×640 version:
77+
78+
```bash
79+
# 640×640 version
80+
bash docker/compile.sh --model yolo26n --size 640
81+
82+
# Small model
83+
bash docker/compile.sh --model yolo26s --size 320
84+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env bash
2+
# compile.sh — Build and run the Coral EdgeTPU model compiler Docker image.
3+
#
4+
# Converts yolo26n.pt → TFLite INT8 → yolo26n_edgetpu.tflite
5+
# Output lands in ../models/ (tracked in the git repo).
6+
#
7+
# Usage:
8+
# bash docker/compile.sh # 320×320 nano (default)
9+
# bash docker/compile.sh --size 640 # 640×640 nano
10+
# bash docker/compile.sh --model yolo26s # small model
11+
#
12+
# Requirements:
13+
# - Docker with buildx / multi-platform support
14+
# - Internet access (downloads yolo26n.pt from ultralytics on first run)
15+
#
16+
# On Apple Silicon or Windows, Docker Desktop handles linux/amd64 emulation
17+
# via Rosetta / QEMU automatically. First run will be slower (emulation).
18+
19+
set -euo pipefail
20+
21+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
22+
SKILL_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
23+
MODELS_DIR="$SKILL_DIR/models"
24+
IMAGE_NAME="coral-tpu-compiler"
25+
26+
# ── Parse args (pass-through to compile_model.py) ────────────────────────────
27+
COMPILE_ARGS=("$@")
28+
if [[ ${#COMPILE_ARGS[@]} -eq 0 ]]; then
29+
COMPILE_ARGS=(--model yolo26n --size 320 --output /compile/output)
30+
fi
31+
32+
log() { echo "[compile.sh] $*" >&2; }
33+
34+
log "Skill dir : $SKILL_DIR"
35+
log "Models out: $MODELS_DIR"
36+
log "Args : ${COMPILE_ARGS[*]}"
37+
38+
# ── Ensure models dir exists ──────────────────────────────────────────────────
39+
mkdir -p "$MODELS_DIR"
40+
41+
# ── Build image (linux/amd64 required for edgetpu_compiler) ──────────────────
42+
log "Building Docker image: $IMAGE_NAME (linux/amd64)..."
43+
docker build \
44+
--platform linux/amd64 \
45+
--tag "$IMAGE_NAME:latest" \
46+
--file "$SCRIPT_DIR/Dockerfile" \
47+
"$SKILL_DIR"
48+
49+
log "Build complete. Running model compiler..."
50+
51+
# ── Run compiler, mount models/ as output volume ──────────────────────────────
52+
docker run --rm \
53+
--platform linux/amd64 \
54+
--name coral-tpu-compile-run \
55+
-v "$MODELS_DIR:/compile/output" \
56+
"$IMAGE_NAME:latest" \
57+
"${COMPILE_ARGS[@]}"
58+
59+
echo ""
60+
log "✓ Compilation complete. Output files in: $MODELS_DIR"
61+
log ""
62+
log "Files produced:"
63+
ls -lh "$MODELS_DIR"/*.tflite 2>/dev/null || log " (no .tflite files yet — check compile output above)"
64+
65+
echo ""
66+
log "Next steps:"
67+
log " 1. Verify the model: ls -lh $MODELS_DIR/*_edgetpu.tflite"
68+
log " 2. Commit to git: git -C '$SKILL_DIR' add models/*.tflite && git commit -m 'feat(coral-tpu): add compiled yolo26n edgetpu model'"
69+
log " 3. Run deploy.bat on your Windows machine to install the skill."
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
services:
2+
# ─────────────────────────────────────────────────────────────────────────
3+
# coral-compiler
4+
#
5+
# Compiles YOLO 2026 nano (.pt) → TFLite INT8 → Edge TPU (.tflite).
6+
# Output files land in the skill's models/ directory.
7+
#
8+
# Usage:
9+
# docker compose run --rm coral-compiler # default 320×320
10+
# docker compose run --rm coral-compiler --size 640 # 640×640
11+
# docker compose run --rm coral-compiler --model yolo26s # small model
12+
#
13+
# Build only:
14+
# docker compose build coral-compiler
15+
# ─────────────────────────────────────────────────────────────────────────
16+
coral-compiler:
17+
build:
18+
context: ..
19+
dockerfile: docker/Dockerfile
20+
platforms:
21+
- linux/amd64
22+
platform: linux/amd64
23+
image: coral-tpu-compiler:latest
24+
volumes:
25+
# Mount the skill's models/ directory as the compiler output
26+
- ../models:/compile/output
27+
command:
28+
- --model
29+
- yolo26n
30+
- --size
31+
- "320"
32+
- --output
33+
- /compile/output
34+
# Don't restart — this is a one-shot build job
35+
restart: "no"
Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
1-
# Pre-compiled YOLO 2026 Nano model for Google Coral Edge TPU
2-
#
3-
# Place your compiled model here:
4-
# yolo26n_320_edgetpu.tflite (320×320 input, fully on-chip)
5-
# yolo26n_640_edgetpu.tflite (640×640 input, partially on CPU)
6-
#
7-
# To compile your own:
8-
# python scripts/compile_model.py --model yolo26n --size 320 --output models/
9-
#
10-
# Note: edgetpu_compiler only runs on x86_64 Linux.
11-
# Use Docker with --platform linux/amd64 on other architectures.
1+
# Pre-compiled YOLO 2026 Nano models for Google Coral Edge TPU
2+
3+
Place compiled `.tflite` files here. They are committed to the repository
4+
so `deploy.bat` and `deploy.sh` can use them without needing a Linux machine.
5+
6+
## Files Expected
7+
8+
| File | Size | Notes |
9+
|------|------|-------|
10+
| `yolo26n_int8_edgetpu.tflite` | ~3–4 MB | Edge TPU compiled (primary) |
11+
| `yolo26n_int8.tflite` | ~3–4 MB | CPU fallback |
12+
13+
## How to Compile
14+
15+
The `edgetpu_compiler` only runs on x86_64 Linux. Use the included Docker
16+
setup to compile from any OS (Windows, macOS, Linux):
17+
18+
```bash
19+
# From the yolo-detection-2026-coral-tpu/ root:
20+
bash docker/compile.sh
21+
```
22+
23+
Or with Docker Compose:
24+
```bash
25+
docker compose -f docker/docker-compose.yml run --rm coral-compiler
26+
```
27+
28+
See `docker/README.md` for full instructions.
29+
30+
## After Compiling
31+
32+
```bash
33+
git add models/*.tflite
34+
git commit -m "feat(coral-tpu): add compiled yolo26n edgetpu model (320x320 INT8)"
35+
git push
36+
```
37+
38+
## Alternative: CPU Fallback
39+
40+
If no EdgeTPU model is present, `deploy.bat` / `deploy.sh` will download
41+
`ssd_mobilenet_v2_coco_quant_postprocess_edgetpu.tflite` as a functional
42+
fallback. This is SSD MobileNet (not YOLO 2026), but confirms the TPU
43+
pipeline works before the YOLO model is compiled.
Binary file not shown.
Binary file not shown.

skills/detection/yolo-detection-2026-coral-tpu/requirements.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
# Dependencies for Coral TPU Detection Skill
1+
# Coral TPU Detection Skill — Python Dependencies
22
#
3-
# Runtime: ai-edge-litert (LiteRT) — modern successor to tflite-runtime
4-
# Hardware: libedgetpu must be installed separately as a system library
5-
# See SKILL.md → Platform Setup for installation instructions
3+
# Runtime: ai-edge-litert (LiteRT) — Google's modern TFLite runtime
4+
# Supports Python 3.9–3.13. No pycoral required.
5+
# detect.py uses the low-level ai-edge-litert API directly with
6+
# libedgetpu as a delegate — this is faster and simpler than pycoral.
7+
#
8+
# Hardware driver: libedgetpu (system library, installed separately)
9+
# Linux: libedgetpu.so.1 (via apt: libedgetpu1-std)
10+
# macOS: libedgetpu.1.dylib (via deploy-macos.sh)
11+
# Windows: edgetpu.dll (via deploy.bat UAC install)
612

7-
# LiteRT runtime with Edge TPU delegate support (Python 3.9–3.13)
13+
# LiteRT runtime — loads Edge TPU delegate on all platforms
814
ai-edge-litert>=2.1.0
915

1016
# Image processing

0 commit comments

Comments
 (0)