Skip to content

Commit fb00f0d

Browse files
committed
Enhance export functionality and documentation for TensorFlow integration
- Updated `pyproject.toml` to include new export package configurations. - Modified GitHub Actions workflow to run tests on the new export directory. - Expanded documentation on tools and usage for the `odet` CLI, including new export modes for Oriented R-CNN. - Added support for exporting Keras detect bundles with ONNX models. - Improved error handling and validation in export scripts. - Introduced new export modes and updated Makefile for streamlined usage.
1 parent 05889a3 commit fb00f0d

31 files changed

Lines changed: 1688 additions & 241 deletions

‎.github/workflows/test.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ jobs:
2323
- name: Check vendored configs match configs/
2424
run: make check-configs
2525
- name: Run tests
26-
run: python -m pytest tests/ -q --tb=short
26+
run: python -m pytest tests/ export/tests/ -q --tb=short

‎docs/getting-started/tools.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ After `uv pip install -e .`, use the **`odet`** CLI (`odet --help`). Script impl
2121
| `viewer` | Gradio app for browsing predictions |
2222
| `playground-csv` | Build Airbus Playground split CSV |
2323
| `playground-to-dota` | Export Playground annotations to DOTA layout |
24-
| `export-onnx` | ONNX export (`export/scripts/export_onnx.py`) |
25-
| `export-tf` | TensorFlow SavedModel via `make -C export export-tf` |
24+
| `export-onnx` | ONNX export (`export.scripts.export_onnx`) |
25+
| `export-tf` | ONNX + Keras detect bundle (`export.scripts.export_tf`; needs `[export]`) |
26+
| `export-detect` | Keras bundle from existing ONNX (`export.scripts.build_faster_rcnn_savedmodel`) |
27+
| `export-preds` | Val inference via Keras bundle (`export.scripts.save_predictions_tf`) |
2628
| `pretrained` | `list` / `download` Hub checkpoints (`hf://` slugs) |
2729
| `labels-to-comma` | Convert DOTA label files to comma-separated format |
2830
| `free-gpu` | Kill GPU processes (dev utility) |

‎export/Makefile‎

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
# Repository root (parent of export/)
44
REPO_ROOT := $(abspath $(CURDIR)/..)
55

6-
# Defaults: published deploy weights (override: make export-tf CONFIG=... CKPT=...)
6+
# Defaults: recipe + Hub pretrained weights (override: make export-tf CONFIG=... CKPT=...)
7+
# Download CKPT if missing: odet pretrained download rotated_faster_rcnn_dota_le90_1x
78
# Override for a trained experiment, e.g. CONFIG=runs/rotated_faster_rcnn/<timestamp>/config.json
8-
CONFIG ?= $(REPO_ROOT)/configs/rotated_faster_rcnn/dota_le90.json
9-
CKPT ?= $(REPO_ROOT)/pretrained/rotated_faster_rcnn_r50_fpn_082.pth
9+
CONFIG ?= $(REPO_ROOT)/configs/rotated_faster_rcnn/dota_le90_1x.json
10+
CKPT ?= $(REPO_ROOT)/pretrained/rotated_faster_rcnn_r50_fpn_dota_le90_1x-0733c506.pth
1011
HEIGHT ?= 1024
1112
WIDTH ?= 1024
1213
ARTIFACTS ?= $(CURDIR)/artifacts
13-
ONNX_OUT ?= $(ARTIFACTS)/faster_rcnn_pre_nms.onnx
14-
DETECT_DIR ?= $(ARTIFACTS)/faster_rcnn_detect
14+
# export-tf / export-detect: faster_rcnn_pre_nms | oriented_rcnn_pre_nms
15+
# export-onnx alone also accepts backbone | retinanet_heads
16+
MODE ?= faster_rcnn_pre_nms
17+
ONNX_OUT ?= $(ARTIFACTS)/pre_nms.onnx
18+
DETECT_DIR ?= $(ARTIFACTS)/detect
1519
DEVICE ?= cpu
20+
OPSET ?= 17
21+
# Set SKIP_ORT=1 to skip ORT validation after ONNX export
22+
SKIP_ORT ?= 0
1623
# ONNX Runtime EP for TF inference (preds / predict): cpu | cuda | auto
1724
ORT_DEVICE ?= auto
1825
# Val preds / metrics (TF export); compare with repo-root make preds / make metrics (PyTorch)
@@ -24,59 +31,79 @@ METRICS_PRED_DIR ?=
2431

2532
.PHONY: help export-tf export-onnx export-detect predict test preds metrics eval-tf
2633

27-
## help: Show export Makefile targets
34+
## help: Show export Makefile targets (thin wrappers around odet)
2835
help:
29-
@echo "oriented-det export — Makefile targets"
36+
@echo "oriented-det export — Makefile targets (call odet; no Make required for pip installs)"
3037
@echo "===================================="
3138
@echo ""
32-
@echo " make export-tf - ONNX (faster_rcnn_pre_nms) + Keras detect bundle"
33-
@echo " make export-onnx - ONNX only"
34-
@echo " make export-detect - Keras bundle from existing ONNX + meta"
39+
@echo " make export-tf - odet export-tf (ONNX + Keras detect bundle)"
40+
@echo " make export-onnx - odet export-onnx"
41+
@echo " make export-detect - odet export-detect"
3542
@echo " make predict - Smoke-test detect bundle (DETECT_DIR, HEIGHT, WIDTH)"
36-
@echo " make preds - Val inference via Keras bundle → $(PREDICTIONS_DIR)/<ts>/"
43+
@echo " make preds - odet export-preds → $(PREDICTIONS_DIR)/<ts>/"
3744
@echo " make metrics - Offline mAP/PR on METRICS_PRED_DIR or newest TF predictions/"
38-
@echo " make eval-tf - preds then metrics (compare with repo-root make eval-val)"
45+
@echo " make eval-tf - preds then metrics"
3946
@echo " make test - pytest export/tests/"
4047
@echo ""
41-
@echo "Defaults (paths relative to repo root):"
48+
@echo "Defaults (checkout paths):"
4249
@echo " CONFIG=$(CONFIG)"
4350
@echo " CKPT=$(CKPT)"
51+
@echo " MODE=$(MODE) (export-tf: pre-NMS modes only)"
4452
@echo " HEIGHT=$(HEIGHT) WIDTH=$(WIDTH)"
45-
@echo " DEVICE=$(DEVICE) (PyTorch trace for export-onnx)"
46-
@echo " ORT_DEVICE=$(ORT_DEVICE) (ONNX Runtime for predict / preds)"
53+
@echo " DEVICE=$(DEVICE) OPSET=$(OPSET) SKIP_ORT=$(SKIP_ORT)"
54+
@echo " ORT_DEVICE=$(ORT_DEVICE)"
4755
@echo " ARTIFACTS=$(ARTIFACTS)"
4856
@echo " REPO_ROOT=$(REPO_ROOT)"
57+
@echo ""
58+
@echo "Pip / no-checkout:"
59+
@echo " odet export-tf --config ... --checkpoint ... --output-dir ./odet_export"
60+
@echo ""
61+
@echo "Oriented R-CNN example:"
62+
@echo " make export-tf MODE=oriented_rcnn_pre_nms \\"
63+
@echo " CONFIG=$(REPO_ROOT)/configs/oriented_rcnn/dota_le90_1x.json \\"
64+
@echo " CKPT=$(REPO_ROOT)/pretrained/oriented_rcnn_r50_fpn_dota_le90_1x-5b128e72.pth"
65+
66+
## export-tf: ONNX + Keras detect bundle via odet
67+
export-tf:
68+
@mkdir -p "$(ARTIFACTS)"
69+
cd "$(REPO_ROOT)" && odet export-tf \
70+
--config "$(CONFIG)" \
71+
--checkpoint "$(CKPT)" \
72+
--mode "$(MODE)" \
73+
--output-dir "$(ARTIFACTS)" \
74+
--height $(HEIGHT) --width $(WIDTH) \
75+
--device "$(DEVICE)" \
76+
--opset $(OPSET) \
77+
$(if $(filter 1 true TRUE yes YES,$(SKIP_ORT)),--skip-ort,)
4978

50-
## export-onnx: PyTorch → ONNX (Rotated Faster R-CNN pre-NMS)
79+
## export-onnx: PyTorch → ONNX only
5180
export-onnx:
5281
@mkdir -p "$(ARTIFACTS)"
5382
cd "$(REPO_ROOT)" && odet export-onnx \
5483
--config "$(CONFIG)" \
5584
--checkpoint "$(CKPT)" \
5685
--output "$(ONNX_OUT)" \
5786
--height $(HEIGHT) --width $(WIDTH) \
58-
--mode faster_rcnn_pre_nms \
59-
--device "$(DEVICE)"
87+
--mode "$(MODE)" \
88+
--device "$(DEVICE)" \
89+
--opset $(OPSET) \
90+
$(if $(filter 1 true TRUE yes YES,$(SKIP_ORT)),--skip-ort,)
6091

61-
## export-detect: Build Keras detect bundle from ONNX + sidecar meta
92+
## export-detect: Keras bundle from existing ONNX + meta
6293
export-detect:
63-
cd "$(REPO_ROOT)" && python export/scripts/build_faster_rcnn_savedmodel.py \
94+
cd "$(REPO_ROOT)" && odet export-detect \
6495
--meta "$(ONNX_OUT:.onnx=.export_meta.json)" \
6596
--onnx "$(ONNX_OUT)" \
6697
--output "$(DETECT_DIR)"
6798

68-
## export-tf: export-onnx then export-detect
69-
export-tf: export-onnx export-detect
70-
@echo "Detect bundle: $(DETECT_DIR)/keras_model.keras"
71-
7299
## predict: Smoke-test keras detect bundle
73100
predict:
74-
cd "$(REPO_ROOT)" && python export/scripts/predict_savedmodel.py \
101+
cd "$(REPO_ROOT)" && python -m export.scripts.predict_savedmodel \
75102
--saved-model "$(DETECT_DIR)" \
76103
--height $(HEIGHT) --width $(WIDTH) \
77104
--ort-device "$(ORT_DEVICE)"
78105

79-
## preds: Val inference with TF/Keras detect bundle (requires make export-tf)
106+
## preds: Val inference with TF/Keras detect bundle
80107
preds:
81108
@if [ ! -f "$(DETECT_DIR)/keras_model.keras" ]; then \
82109
echo "Error: $(DETECT_DIR)/keras_model.keras missing. Run: make export-tf"; \
@@ -86,11 +113,14 @@ preds:
86113
ROOT_ARGS=""; \
87114
if [ -n "$(DATA_ROOT)" ]; then ROOT_ARGS="--data-root $(DATA_ROOT)"; fi; \
88115
if [ -n "$(VAL_DIR)" ]; then ROOT_ARGS="$$ROOT_ARGS --val-dir $(VAL_DIR)"; fi; \
89-
cd "$(REPO_ROOT)" && python export/scripts/save_predictions_tf.py \
116+
TS=$$(date +%Y%m%d_%H%M%S); \
117+
mkdir -p "$(PREDICTIONS_DIR)"; \
118+
cd "$(REPO_ROOT)" && odet export-preds \
90119
--config "$(CONFIG)" \
91120
--detect-dir "$(DETECT_DIR)" \
92121
--reference-checkpoint "$(CKPT)" \
93122
--data-split "$(DATA_SPLIT)" \
123+
--output-dir "$(PREDICTIONS_DIR)/$$TS" \
94124
$$ROOT_ARGS \
95125
--ort-device "$(ORT_DEVICE)" \
96126
--no-diagnostics
@@ -112,9 +142,9 @@ metrics:
112142
echo "TF export metrics: $$PRED"; \
113143
cd "$(REPO_ROOT)" && odet preds --metrics-from-json "$$PRED"
114144

115-
## eval-tf: preds then metrics on newest export/artifacts/predictions/<ts>/
145+
## eval-tf: preds then metrics
116146
eval-tf: preds metrics
117147

118-
## test: Run export unit tests (no TF/ONNX required for most tests)
148+
## test: Run export unit tests
119149
test:
120-
cd "$(REPO_ROOT)" && PYTHONPATH="$(REPO_ROOT):$(REPO_ROOT)/export" pytest export/tests/
150+
cd "$(REPO_ROOT)" && PYTHONPATH="$(REPO_ROOT)" pytest export/tests/

‎export/PARITY.md‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,25 @@ This document records what **matches** PyTorch full inference and what is **inte
2121
| Aspect | Parity |
2222
|--------|--------|
2323
| Backbone, RPN, ROI align, decode (pre-NMS tensors) | Same weights and logic as `RotatedFasterRCNN` via `oriented_det.models.faster_rcnn_inference`; deterministic RPN top-k for export. |
24-
| RPN proposals before ROI | Padded to `max_pre_nms_candidates` (= `rpn_post_nms_top_n` on the loaded model, including `production.*` overrides) so ONNX ROI align traces with fixed shapes. |
24+
| RPN proposals before ROI | Padded to `max_pre_nms_candidates` with **zero-area** xyxy pads; ROI candidates keep only positive-area boxes (dynamic mask). |
2525
| Pre-NMS output padding | `pad_pre_nms_detections` uses `torch.cat` zero-padding (not slice assignment), which avoids invalid ONNX `Expand` when the dynamic candidate count is not exactly 0, 1, or `max_candidates`. |
26+
| Detect bundle layout | `keras_model.keras` + **`model.onnx`** + `export_meta.json` (relative ONNX name; resolve via `load_keras_detect_model`). |
2627
| Final rotated NMS | **Exact** match with deploy when `production.final_nms_use_cpu: true` (polygon CPU path in `rotated_nms`). |
2728
| `production.score_threshold` / per-class floors | Applied in Keras bundle after NMS (`export/postprocess.py`). |
2829
| Sliding-window tiling, margin filter, GeoJSON | **Out of scope** — same as deploy API (see `oriented_det.runtime.inference`). |
2930
| TF SavedModel reload of `tf.py_function` | **Not used**; load `keras_model.keras` with `FasterRCNNDetectLayer` custom object. |
3031
| onnx2tf full graph | May fail on `ScatterND`; bundle uses **ONNX Runtime** for the core graph instead. |
3132

33+
## `oriented_rcnn_pre_nms` + detect bundle (Oriented R-CNN)
34+
35+
| Aspect | Parity |
36+
|--------|--------|
37+
| Backbone, midpoint RPN, OrientedROIAlign, decode | Same weights/logic as `OrientedRCNN` eval via `oriented_det.models.oriented_rcnn_inference`; deterministic midpoint RPN top-k for export. |
38+
| RPN proposals before ROI | Padded oriented proposals with **zero-size** OBBs; ROI candidates keep only `w>0` and `h>0` (pads are not min-clamped to 1). |
39+
| OrientedROIAlign under ONNX | Masked all-N `grid_sample` per FPN level (no indexed ScatterND writes); eager path stays chunked. |
40+
| Final NMS / score floors / Keras bundle | **Same** as Faster R-CNN detect bundle (`export/postprocess.py`). |
41+
| onnx2tf full graph | Same ORT-inside-Keras mitigation; pure TF conversion remains experimental. |
42+
3243
## ONNX → TensorFlow
3344

3445
| Risk | Mitigation |
@@ -45,4 +56,4 @@ This document records what **matches** PyTorch full inference and what is **inte
4556

4657
## Regression tests
4758

48-
`export/tests/test_export_wrappers.py` checks **wrapper forward shapes** against PyTorch without requiring ONNX or TensorFlow. Golden numeric `.npz` dumps are optional and not required for CI.
59+
`export/tests/test_export_wrappers.py` checks **wrapper forward shapes** against PyTorch without requiring ONNX or TensorFlow. Golden numeric `.npz` dumps are optional and not required for CI. Pre-NMS parity: `test_faster_rcnn_export_parity.py`, `test_oriented_rcnn_export_parity.py`.

0 commit comments

Comments
 (0)