Skip to content

[Draft] Serve the BioCLIP 2.5 + LogReg classifier as a pipeline - #174

Draft
mohamedelabbas1996 wants to merge 1 commit into
mainfrom
feat/bioclip-classifier
Draft

mohamedelabbas1996 wants to merge 1 commit into
mainfrom
feat/bioclip-classifier

Conversation

@mohamedelabbas1996

Copy link
Copy Markdown

Summary

This adds the BioCLIP 2.5 + logistic-regression species classifiers to the processing service as pipelines. A frozen BioCLIP 2.5 ViT-H/14 backbone carries a linear head fit as an sklearn LogisticRegression over L2-normalised embeddings, so a softmax over the head reproduces sklearn's predict_proba exactly. Two heads ship: one over the Newfoundland species list and one over the Panama list.

Only serving is included here. Retraining a head from verified labels builds on top of this and is kept for a separate change (RolnickLab/antenna#1407 and #167), so this PR is small and reviewable on its own.

List of Changes

# What it does How
1 Two BioCLIP 2.5 + LogReg classifiers BioCLIP25NewfoundlandClassifier and BioCLIP25PanamaClassifier in trapdata/ml/models/bioclip.py, loading the backbone and a head from the Hub
2 They are offered as pipelines bioclip_2_5_newfoundland and bioclip_2_5_panama in CLASSIFIER_CHOICES, with MothClassifierBioCLIP25* API wrappers
3 A classification carries the embedding it was made from features on ClassificationResponse and ClassifierResult; the forward pass returns (logits, features)
4 open-clip-torch is available added to pyproject.toml and uv.lock

Detailed Description

The backbone is frozen, so a crop's embedding never changes. The forward pass returns that embedding alongside the logits, and post_process_batch carries it through to ClassificationResponse.features. Storing the vector means the backbone never has to run over the same crop twice, which is what makes a later head-retrain cheap; a model that returns logits alone is unaffected and its features stays None.

The Newfoundland head is read from a public Hugging Face repository; the Panama head is read from a directory the deployment provides via BIOCLIP_PANAMA_HEAD_DIR. The label vocabulary for the Panama head travels inside the npz, so it is normalised through labels_from / BioCLIPVocabularyInHeadClassifier.

scripts/run_bioclip_on_remote_gpu.sh is the helper used to serve these on a remote GPU.

Verification

trapdata/tests/test_bioclip_classifier.py (6 tests) covers: both classifiers are registered under their slugs, they extend APIMothClassifier and the BioCLIP bases, they declare a backbone and a head, the forward pass returns an L2-normalised embedding matching the logits, post_process_batch keeps the embedding on the result, and ClassificationResponse carries features. These check the wiring without downloading the backbone or a head.

Run locally against this branch: the new tests and test_registry.py pass; black, isort, and flake8 are clean on the changed files.

Adds two species classifiers built on a frozen BioCLIP 2.5 ViT-H/14 backbone with a
linear logistic-regression head on top, one over the Newfoundland species list and one
over the Panama list, and registers them as the bioclip_2_5_newfoundland and
bioclip_2_5_panama pipelines. The head is an sklearn LogisticRegression exported to a
single Linear layer, so a softmax over its output reproduces sklearn's predict_proba.

The backbone is frozen, so a crop's embedding never changes. The forward pass returns
that embedding alongside the logits, and a classification now carries it as `features`,
so whatever consumes the classification can keep the vector that produced it without
running the backbone over the same crop twice.

Only serving is included here. Retraining a head from verified labels builds on this and
is a separate change.

Co-Authored-By: Claude <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Sep 17, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

mohamedelabbas1996 added a commit that referenced this pull request Sep 17, 2026
Brings the standalone BioCLIP 2.5 + LogReg serving change (#174) in as the base this
retraining work builds on, so the model that gets retrained is the one that PR reviews and
merges on its own. The serving code was already here; this records the relationship and
adds the serving-only registration tests.

Co-Authored-By: Claude <noreply@anthropic.com>
mohamedelabbas1996 added a commit that referenced this pull request Sep 28, 2026
Brings the standalone BioCLIP 2.5 + LogReg serving change (#174) in as the base this
retraining work builds on, so the model that gets retrained is the one that PR reviews and
merges on its own. The serving code was already here; this records the relationship and
adds the serving-only registration tests.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CnKz4AS4iFrYj1GrgkZQbq
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