Skip to main content

A metric learning toolkit

Project description

BioEncoder

BioEncoder is a toolkit for supervised metric learning to i) learn and extract features from images, ii) enhance biological image classification, and iii) identify the features most relevant to classification. Designed for diverse and complex datasets, the package and the available metric losses can handle unbalanced classes and subtle phenotypic differences more effectively than non-metric approaches. The package includes taxon-agnostic data loaders, custom augmentation techniques, hyperparameter tuning through YAML configuration files, and rich model visualizations, providing a comprehensive solution for high-throughput analysis of biological images.

Read the paper: https://onlinelibrary.wiley.com/doi/10.1111/ele.14495

Functionality

>> Full list of available model architectures, losses, optimizers, schedulers, and augmentations <<

  • Taxon-agnostic dataloaders (making it applicable to any dataset - not just biological ones)
  • Support of timm models, and pytorch-optimizer
  • Access to state-of-the-art metric losses, such as Supcon and Sub-center ArcFace.
  • Exponential Moving Average for stable training, and Stochastic Moving Average for better generalization and performance.
  • LRFinder for the second stage of the training.
  • Easy customization of hyperparameters, including augmentations, through YAML configs (check the config-templates folder for examples)
  • Custom augmentations techniques via albumentations
  • TensorBoard logs and checkpoints (soon to come: WandB integration)
  • Streamlit app with rich model visualizations (e.g., Grad-CAM and timm-vis)
  • Interactive t-SNE and PCA plots using Bokeh

Quickstart

>> Comprehensive help files <<

1. Install BioEncoder (into a virtual environment with pytorch/CUDA):

pip install bioencoder

2. Get the example dataset from the data repo and the config files by downloading the git repo, and extract both.

3. Start interactive session (e.g., in Spyder or VS code) and run the following commands one by one:

## use "overwrite=True to redo a step

import bioencoder

## global setup (pick a target directory for all output that bioencoder generates, e.g. training dataset, model weights, etc.)
bioencoder.configure(root_dir=r"bioencoder_wd", run_name="v1")

## split dataset (the dataset you downloaded)
bioencoder.split_dataset(image_dir=r"damselflies-aligned-trai_val", max_ratio=6, random_seed=42, val_percent=0.1, min_per_class=20)

## train stage 1 
bioencoder.train(config_path=r"bioencoder_configs/train_stage1.yml")
bioencoder.swa(config_path=r"bioencoder_configs/swa_stage1.yml")

## explore embedding space and model from stage 1
bioencoder.interactive_plots(config_path=r"bioencoder_configs/plot_stage1.yml")
bioencoder.model_explorer(config_path=r"bioencoder_configs/explore_stage1.yml")

## (optional) learning rate finder for stage 2
bioencoder.lr_finder(config_path=r"bioencoder_configs/lr_finder.yml")

## train stage 2
bioencoder.train(config_path=r"bioencoder_configs/train_stage2.yml")
bioencoder.swa(config_path=r"bioencoder_configs/swa_stage2.yml")

## explore model from stage 2
bioencoder.model_explorer(config_path=r"bioencoder_configs/explore_stage2.yml")

## inference (stage 1 = embeddings, stage 2 = classification)
bioencoder.inference(config_path="bioencoder_configs/inference.yml", image="path/to/image.jpg" / np.array)

4. Alternatively, you can directly use the command line interface:

## use the flag "--overwrite" to redo a step

bioencoder_configure --root-dir "~/bioencoder_wd" --run-name v1
bioencoder_split_dataset --image-dir "damselflies-aligned-trai_val" --max-ratio 6 --random-seed 42
bioencoder_train --config-path "bioencoder_configs/train_stage1.yml"
bioencoder_swa --config-path "bioencoder_configs/swa_stage1.yml"
bioencoder_interactive_plots --config-path "bioencoder_configs/plot_stage1.yml"
bioencoder_model_explorer --config-path "bioencoder_configs/explore_stage1.yml"
bioencoder_lr_finder --config-path "bioencoder_configs/lr_finder.yml"
bioencoder_train --config-path "bioencoder_configs/train_stage2.yml"
bioencoder_swa --config-path "bioencoder_configs/swa_stage2.yml"
bioencoder_model_explorer --config-path "bioencoder_configs/explore_stage2.yml"
bioencoder_inference --config-path "bioencoder_configs/inference.yml" --path "path/to/image.jpg"

Citation

Please cite BioEncoder as follows:

@article{https://doi.org/10.1111/ele.14495,
    author = {Lürig, Moritz D. and Di Martino, Emanuela and Porto, Arthur},
    title = {BioEncoder: A metric learning toolkit for comparative organismal biology},
    journal = {Ecology Letters},
    volume = {27},
    number = {8},
    pages = {e14495},
    keywords = {biodiversity, deep metric learning, feature space, machine learning, phenotypic differences, python package, species identification},
    doi = {https://doi.org/10.1111/ele.14495},
    url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/ele.14495},
    eprint = {https://onlinelibrary.wiley.com/doi/pdf/10.1111/ele.14495},
    year = {2024}
}

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

bioencoder-1.0.3.tar.gz (43.9 kB view details)

Uploaded Source

Built Distribution

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

bioencoder-1.0.3-py3-none-any.whl (54.1 kB view details)

Uploaded Python 3

File details

Details for the file bioencoder-1.0.3.tar.gz.

File metadata

  • Download URL: bioencoder-1.0.3.tar.gz
  • Upload date:
  • Size: 43.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for bioencoder-1.0.3.tar.gz
Algorithm Hash digest
SHA256 322ac081de7c668e4b00c36d8c7e6819aa8a721800b7b0532609fe0f22f2f20d
MD5 256504f75ee2e1910436b5fad1b40ca3
BLAKE2b-256 297cd1652e1829ac12f516784ce95c61e3a20db07d2b7cd15d8219d1ee5588cd

See more details on using hashes here.

File details

Details for the file bioencoder-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: bioencoder-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 54.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for bioencoder-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ac4093f2ed8a7e11f92a4fa10c23cc5883f2b75ca882847dc7050b86bec81a7c
MD5 d51f261c733c1fdfa133bdf088c215b7
BLAKE2b-256 812a614bab040f1c5fc9be0e02c4afaa22d766f85f1a1c01552bf2e16ccf6987

See more details on using hashes here.

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