Skip to main content

Hugging Face Transformers image embedding adapter with a scikit-learn KNN classification head.

Project description

transformers-knn-adapter

Hugging Face image embeddings with a scikit-learn KNN head.

tests release-please publish

transformers_knn_adapter extends Hugging Face image models by attaching a scikit-learn KNN classifier on top of transformer embeddings.

Requirements

  • Python 3.11+
  • uv

Setup

uv sync --dev

Run Tests

uv run pytest

Train

uv run python -m transformers_knn_adapter.knn_image_pipeline train \
  --model microsoft/resnet-50 \
  --knn-model-path /tmp/knn/dinov2_small_mini_imagenet_full.joblib \
  --dataset timm/mini-imagenet \
  --split train \
  --max-samples 1000 \
  --shuffle \
  --grid-search \
  --grid-search-splits 3 \
  --grid-search-repeats 2 \
  --grid-search-scoring f1_macro

Evaluate

uv run python -m transformers_knn_adapter.knn_image_pipeline eval \
  --model microsoft/resnet-50 \
  --knn-model-path /tmp/knn/dinov2_small_mini_imagenet_full.joblib \
  --dataset timm/mini-imagenet \
  --split test \
  --stratified \
  --max-samples 100 \
  --shuffle \
  --batch-size 100

Inference

uv run python -m transformers_knn_adapter.knn_image_pipeline infer \
  --model microsoft/resnet-50 \
  --knn-model-path /tmp/knn/dinov2_small_mini_imagenet_full.joblib \
  --image https://picsum.photos/200 \
  --inference-batch-size 5

Python API

from transformers_knn_adapter.knn_image_pipeline import pipeline

clf = pipeline(
    "image-classification",
    model_path="microsoft/resnet-50",
    knn_model_path="/tmp/knn/model.joblib",
)

Train from Python

clf.train(
    dataset="timm/mini-imagenet",
    split="train",
    max_samples=1000,
    shuffle=True,
    grid_search=True,
    grid_search_splits=3,
    grid_search_repeats=2,
    grid_search_scoring="f1_macro",
)

Evaluate from Python

metrics = clf.evaluate(
    dataset="timm/mini-imagenet",
    split="test",
    max_samples=100,
    shuffle=True,
    batch_size=100,
)
print(metrics["top1_accuracy"])

Inference from Python

single = clf("https://picsum.photos/200")
batch = clf(["https://picsum.photos/200"] * 5)
print(single)
print(batch)

Notes

Real train/eval runs can download model and dataset artifacts from Hugging Face.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

transformers_knn_adapter-0.2.1.tar.gz (230.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

transformers_knn_adapter-0.2.1-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file transformers_knn_adapter-0.2.1.tar.gz.

File metadata

  • Download URL: transformers_knn_adapter-0.2.1.tar.gz
  • Upload date:
  • Size: 230.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for transformers_knn_adapter-0.2.1.tar.gz
Algorithm Hash digest
SHA256 89d19dea57e50b358169a47a77aa66ff6c12200c48df75b44a3b609143d3852b
MD5 e914c0b1cefe605d6c459658de8b3824
BLAKE2b-256 6eaa22e9af031c8952d2d5b3123918dbca31ba3571afb9bc2efac89a874dcf8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for transformers_knn_adapter-0.2.1.tar.gz:

Publisher: publish.yml on dimidagd/transformers-knn-adapter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file transformers_knn_adapter-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for transformers_knn_adapter-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 be54e658e98500bbab30401354f7972104680ca3b0ef5427f8a6be931edf6d61
MD5 63144c7013100722c53ea217d2eb6713
BLAKE2b-256 dc3c62bd62c3bf393f5670afc56d7c80c1025552507864a87f4049ae9a6c1273

See more details on using hashes here.

Provenance

The following attestation bundles were made for transformers_knn_adapter-0.2.1-py3-none-any.whl:

Publisher: publish.yml on dimidagd/transformers-knn-adapter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page