Skip to main content

A Python module for few-shot learning with pretrained models

Project description

FewLearn

A Python module for few-shot learning with pretrained models, enabling efficient model evaluation and comparison through prototypical networks.

Overview

FewLearn provides tools for comparing and evaluating multiple pretrained models in parallel using few-shot learning techniques like Prototypical Networks. The framework allows you to:

  • Efficiently evaluate multiple backbone models in parallel
  • Analyze model performance across various metrics
  • Visualize embeddings, confusion matrices, and model comparisons
  • Export models for deployment

Key Components

  • MINDS: Main framework for coordinating few-shot learning evaluations
  • Prototypical Networks: Implementation of the few-shot learning algorithm
  • Backbones: Support for various pre-trained model architectures
  • Evaluation: Protocols and metrics for comparing model performance
  • Visualization: Tools for visualizing embeddings and results

Installation

# Basic installation
pip install fewlearn

# With optional dependencies
pip install fewlearn[dev,easyfsl,demo]

Quick Start

import torch
from torchvision.datasets import Omniglot
from torchvision import transforms
from fewlearn import MINDS, PrototypicalNetworks, Evaluator, EpisodicProtocol

# 1. Initialize the MINDS framework
minds = MINDS()

# 2. Add different backbone models to evaluate
minds.add_model("resnet18", PrototypicalNetworks(backbone="resnet18"))
minds.add_model("mobilenet_v2", PrototypicalNetworks(backbone="mobilenet_v2"))
minds.add_model("efficientnet_b0", PrototypicalNetworks(backbone="efficientnet_b0"))

# 3. Prepare dataset
transform = transforms.Compose([
    transforms.Resize((84, 84)),
    transforms.ToTensor(),
    transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])

dataset = Omniglot(root='./data', download=True, transform=transform)

# 4. Create an evaluation protocol
protocol = EpisodicProtocol(n_way=5, n_shot=1, n_query=15, episodes=100)

# 5. Create an evaluator
evaluator = Evaluator(
    protocol=protocol,
    metrics=["accuracy", "f1"],
    parallel=True  # Enable parallel evaluation
)

# 6. Run the evaluation
results = evaluator.evaluate(
    models={name: model for name, model in minds.models.items()},
    dataset=dataset
)

# 7. Get a summary of the results
summary = evaluator.summary()
print(summary)

# 8. Get the best model
best_model_name, best_model = minds.get_best_model(results)
print(f"Best model: {best_model_name}")

# 9. Export the best model for deployment
export_path = minds.export_model(best_model_name, format="onnx")
print(f"Model exported to: {export_path}")

Advanced Features

Custom Backbone Models

from torch import nn
from fewlearn.backbones import register_backbone

# Define a custom backbone
def my_custom_backbone(pretrained=True):
    # Create your custom model here
    model = nn.Sequential(
        # ...layers
    )
    return model

# Register the backbone
register_backbone("my_custom_model", my_custom_backbone)

# Use it in a few-shot model
model = PrototypicalNetworks(backbone="my_custom_model")

Visualization

from fewlearn.visualization import (
    plot_prototype_embeddings,
    plot_confusion_matrix,
    plot_performance_comparison
)

# Plot model performance comparison
fig = plot_performance_comparison(results)
fig.savefig("model_comparison.png")

# Plot embedding space visualizations
fig = plot_prototype_embeddings(
    support_embeddings,
    support_labels,
    query_embeddings,
    query_labels
)
fig.savefig("embeddings.png")

Development

Setting Up Development Environment

# Clone the repository
git clone https://github.com/AdityaSharma2485/fewlearn.git
cd fewlearn

# Install development dependencies
pip install -e ".[dev]"

Running Tests

FewLearn comes with a suite of unit tests to ensure the functionality works as expected:

# Run all tests
pytest tests/

# Run specific test modules
pytest tests/test_minds.py
pytest tests/test_prototypical.py

Demo Application

A Streamlit-based demo application is available in the demoapp.py file. To run it:

# Install demo dependencies
pip install -e ".[demo]"

# Run the demo app
streamlit run demoapp.py

The demo allows you to:

  • Compare different backbone models
  • Test on the Omniglot dataset
  • Upload and test your own custom datasets
  • Visualize model performance and predictions

Contributing

Contributions to FewLearn are welcome! To contribute:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Run tests to ensure they pass (pytest tests/)
  5. Commit your changes (git commit -m 'Add amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

Please ensure your code follows the project's style guidelines and includes appropriate tests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgements

  • PyTorch team for the excellent deep learning framework
  • Authors of the paper "Prototypical Networks for Few-shot Learning" for the foundational algorithm
  • Contributors and users of the FewLearn library

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

fewlearn-0.1.1.tar.gz (1.6 MB view details)

Uploaded Source

Built Distribution

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

fewlearn-0.1.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file fewlearn-0.1.1.tar.gz.

File metadata

  • Download URL: fewlearn-0.1.1.tar.gz
  • Upload date:
  • Size: 1.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.20

File hashes

Hashes for fewlearn-0.1.1.tar.gz
Algorithm Hash digest
SHA256 241f63b26d7cbe5055e1f63826a2b66bcad28847b4092410fbf00232cbcbe0dd
MD5 c101c43ba4ccf13d0a48fb938a6b82fb
BLAKE2b-256 b15237365fc292cd10f903c7c862ee7121ba275cfcb45dcf1e3128bae6e8f914

See more details on using hashes here.

File details

Details for the file fewlearn-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: fewlearn-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.20

File hashes

Hashes for fewlearn-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9e52abed9c044a0a29ad765cf9f0456ebe43bb3ad7b34af31afa260c393fd8ba
MD5 ccc47a9518f0ad680268819d8e101b5a
BLAKE2b-256 d84396bc71afa8444aeabee2a4c84d192f381801e584735d1397f3820e9be616

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