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.2.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.2-py3-none-any.whl (39.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fewlearn-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 5b253a654f7f6a308d60e291209a79293c8bba16decefc8b4e4570c169690359
MD5 56364f8ab38ad0e9f6fc80b3ad00dc27
BLAKE2b-256 c470b741e30b173473b788832313a192a5ee24585a05c474f3ab213c5f584ce3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fewlearn-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 39.1 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e2bb559840b91a5d3fedbbb11c0230a3281ab40e91c68dbc96bb03708f0384eb
MD5 1a27ea3efb4a66aabfa4d338db4b87e0
BLAKE2b-256 9c4728d6c46292e858687770e61f870a7f723cf9ce497873982dc6e94f0dc937

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