Skip to content

Replace PytorchWildlife with sparrow-engine for MegaDetector cropping - #12

Open
Zhongqi Miao (zhmiao) wants to merge 5 commits into
microsoft:mainfrom
zhmiao:feat/sparrow-engine-detection
Open

Zhongqi Miao (zhmiao) wants to merge 5 commits into
microsoft:mainfrom
zhmiao:feat/sparrow-engine-detection

Conversation

@zhmiao

@zhmiao Zhongqi Miao (zhmiao) commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Why

PytorchWildlife is retired, and a fresh pip install -r requirements.txt no longer imports: the resolver picks yolov5==6.0.4, and PytorchWildlife 1.3.1 then fails with ImportError: cannot import name 'scale_boxes' from 'yolov5.utils.general'. PytorchWildlife was used only for the MegaDetector cropping step (src/utils/batch_detection_cropping.py).

What

  • batch_detection_cropping.py calls sparrow_engine.detect and converts results to supervision.Detections in pixel xyxy, so save_crop_images is unchanged.
  • It detects only the images listed in the annotation CSV. The old code walked all of dataset_root, including the cropped_resized/ output folder.
  • New config keys: detector_model (default MDV6-yolov10-e, downloaded on first use) and detection_threshold (default 0.2, the old value).
  • requirements.txt: sparrow-engine replaces PytorchWildlife. Packages PytorchWildlife used to pull in transitively are now listed explicitly: timm, supervision, pandas, Pillow, tqdm, wget and opencv-python-headless.
  • environment.yaml: the stale Python 3.8 / torch 1.10 export is replaced with Python 3.11 plus -r requirements.txt.
  • Docs:
    • Python 3.11+ (required by sparrow-engine)
    • the CPU and GPU sparrow-engine builds
    • the new config keys
    • how the trained weights are loaded. They load with the bundled src.models.classification loaders. Running them in sparrow-engine needs ONNX export plus a manifest; this PR does not add an exporter.
  • Docs: the CSV path examples are corrected. Crops are matched by file name, so imgs/leopard_001.jpg never matched.

Behaviour changes

  • Python ≥ 3.11 is now required.
  • Default detector MDv5 → MDv6 (MDV6-yolov10-e): MDv5a is link-only in the sparrow-engine zoo, so it can't be auto-downloaded. On the test set below, MDv6 gives 250 crops vs 257. Users with MDv5a installed locally can set detector_model: MDV5a. MDv6-yolov10-e is AGPL-3.0 upstream, and the docs say so.
  • Detection runs on the CPU with the default sparrow-engine wheel. Use sparrow-engine-gpu for CUDA detection. Training still uses torch and --gpus.

Verification

I couldn't use the Zenodo demo data from the test machine (HTTP 403), so I used 100 local camera-trap JPGs: 50 train and 50 val, with dummy two-class labels.

Run Crops Train/val CSV Boxes
main + PytorchWildlife 1.3.1 (MDv5, yolov5 pinned to 7.0.14 so it imports) 257 baseline baseline
this PR, detector_model: MDV5a 257 byte-identical to baseline same count and class on all 99 images with detections; per-coordinate diff median 0 px, p95 1 px, max 15 px (≤1.02% of image side); max confidence diff 0.067
this PR, default MDV6-yolov10-e 250 — —

End-to-end, a fresh Python 3.11 venv with only this requirements.txt (no PytorchWildlife) ran python main.py for 1 epoch: it detected, cropped, trained and saved a checkpoint, exiting 0. mkdocs build --strict passes.

Related

This supersedes the dependency workaround in #8 (soundfile, librosa and setuptools<82 were needed only by PytorchWildlife's imports).

Not changed here: crops include person and vehicle boxes, each labelled with the image's species. That was the old behaviour too, and it is now documented. Restricting crops to animals would be a separate change.

End-to-end test (fresh clone, clean Python 3.11 venv, no PytorchWildlife)

100 camera-trap images, 2 classes, 10 locations, split_type: location, 2 epochs.

Run Result
python main.py (split, MDv6 auto-download, crop, train) Pass: 131 train / 43 val crops, checkpoint saved
--evaluate <ckpt> --test Pass
--evaluate <ckpt> --val Pass after 84b37f6 (before it: FileExistsError)
--evaluate <ckpt> --predict --predict-root data/imgs Pass, npz + json written
Second python main.py over existing crops Pass after 84b37f6
sparrow-engine-gpu instead of sparrow-engine Pass, active_device() = cuda:0, same test accuracy (77 vs 76 test crops)

Fixes found by the test:

  • 84b37f6: newer supervision raises FileExistsError in ImageSink for crops that already exist, so any second run crashed. PytorchWildlife had pinned supervision==0.23.0. Crops are now written with cv2.imwrite.
  • 3ce861f: README/usage showed the annotations CSV outside imgs/, but main.py writes the splits next to split_path and reads them from dataset_root. The docs now match config.yaml.

Notes:

  • A machine with no usable GPU crashes at main.py:58-59 (gpus=None). fix: load configs with yaml.safe_load and validate the GPU list #7 fixes that; merge it as well.
  • pip install -r requirements.txt gets the default PyPI torch (CUDA 13.x build). On a host whose driver only supports CUDA 12.x, install torch from the matching index (for example --index-url https://download.pytorch.org/whl/cu128). The old requirements had the same behaviour.

…orchWildlife

PytorchWildlife is retired, and a fresh install currently fails to import
(the resolver picks yolov5 6.0.4, which lacks scale_boxes). Detection now
calls sparrow_engine.detect and converts the normalized results to
supervision.Detections so save_crop_images stays unchanged.

The detector model and threshold are config keys (detector_model,
detection_threshold); the default is MDV6-yolov10-e at 0.2. Only images
listed in the annotation CSV are detected, and the cropped_resized
output folder is never re-scanned.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: b452684d-9b9b-4a61-9af9-28bddaabcf14
The old environment.yaml pinned Python 3.8, torch 1.10 and
pytorchwildlife 1.0.1.1, none of which match requirements.txt.
sparrow-engine needs Python 3.11+, so the env now installs
requirements.txt on top of Python 3.11.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: b452684d-9b9b-4a61-9af9-28bddaabcf14
Python 3.11+, the CPU/GPU sparrow-engine builds, the new detector_model
and detection_threshold keys, and how trained weights are actually
loaded (bundled loaders; sparrow-engine needs ONNX plus a manifest).
Also corrects the CSV path examples: crops are matched by file name,
so imgs/leopard_001.jpg never matched, and states that person and
vehicle boxes are cropped too.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: b452684d-9b9b-4a61-9af9-28bddaabcf14
…ting crops

supervision >= 0.24 makes ImageSink.save_image raise FileExistsError when the
crop already exists. Every non-predict run re-crops into the same
cropped_resized folder, so a second run (for example --evaluate --val)
crashed. PytorchWildlife had pinned supervision 0.23, which overwrote silently.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: b452684d-9b9b-4a61-9af9-28bddaabcf14
…ects

main.py writes the split CSVs next to split_path and reads them from
dataset_root, so the layout with the CSV outside imgs/ could not work.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: b452684d-9b9b-4a61-9af9-28bddaabcf14
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