Dataloaders for meta-learning in Pytorch
Project description
Torchmeta
A collection of extensions and data-loaders for few-shot learning & meta-learning in PyTorch. Torchmeta contains popular meta-learning benchmarks, fully compatible with both torchvision
and PyTorch's DataLoader
.
Features
- A unified interface for both few-shot classification and regression problems, to allow easy benchmarking on multiple problems and reproducibility.
- Helper functions for some popular problems, with default arguments from the literature.
- An thin extension of PyTorch's
Module
, calledMetaModule
, that simplifies the creation of certain meta-learning models (e.g. gradient based meta-learning methods). See the MAML example for an example usingMetaModule
.
Datasets available
- Few-shot regression (toy problems):
- Sine waves (Finn et al., 2017)
- Harmonic functions (Lacoste et al., 2018)
- Sinusoid & lines (Finn et al., 2018)
- Few-shot classification (image classification):
- Omniglot (Lake et al., 2015, 2019)
- Mini-ImageNet (Vinyals et al., 2016, Ravi et al., 2017)
- Tiered-ImageNet (Ren et al., 2018)
- CIFAR-FS (Bertinetto et al., 2018)
- Fewshot-CIFAR100 (Oreshkin et al., 2018)
- Caltech-UCSD Birds (Hilliard et al., 2019, Wah et al., 2019)
- Double MNIST (Sun, 2019)
- Triple MNIST (Sun, 2019)
- Few-shot segmentation (semantic segmentation):
- Pascal5i 1-way Setup
- Few-shot classification (tabular datasets)
- Letter (Frey & Slate, 1991)
- One Hundred Plants (Margin) (Mallah et al. 2013)
- One Hundred Plants (Shape) (Mallah et al. 2013)
- One Hundred Plants (Texture) (Mallah et al. 2013)
- Bach Choral Harmony (Radicioni & Esposito, 2010)
Installation
You can install Torchmeta either using Python's package manager pip, or from source. To avoid any conflict with your existing Python setup, it is suggested to work in a virtual environment with virtualenv
. To install virtualenv
:
pip install --upgrade virtualenv
virtualenv venv
source venv/bin/activate
Requirements
- Python 3.6 or above
- PyTorch 1.4 or above
- Torchvision 0.5 or above
Using pip
This is the recommended way to install Torchmeta:
pip install torchmeta
From source
You can also install Torchmeta from source. This is recommended if you want to contribute to Torchmeta.
git clone https://github.com/tristandeleu/pytorch-meta.git
cd pytorch-meta
python setup.py install
Example
Minimal example
This minimal example below shows how to create a dataloader for the 5-shot 5-way Omniglot dataset with Torchmeta. The dataloader loads a batch of randomly generated tasks, and all the samples are concatenated into a single tensor. For more examples, check the examples folder.
from torchmeta.datasets.helpers import omniglot
from torchmeta.utils.data import BatchMetaDataLoader
dataset = omniglot("data", ways=5, shots=5, test_shots=15, meta_train=True, download=True)
dataloader = BatchMetaDataLoader(dataset, batch_size=16, num_workers=4)
for batch in dataloader:
train_inputs, train_targets = batch["train"]
print('Train inputs shape: {0}'.format(train_inputs.shape)) # (16, 25, 1, 28, 28)
print('Train targets shape: {0}'.format(train_targets.shape)) # (16, 25)
test_inputs, test_targets = batch["test"]
print('Test inputs shape: {0}'.format(test_inputs.shape)) # (16, 75, 1, 28, 28)
print('Test targets shape: {0}'.format(test_targets.shape)) # (16, 75)
Advanced example
Helper functions are only available for some of the datasets available. However, all of them are available through the unified interface provided by Torchmeta. The variable dataset
defined above is equivalent to the following
from torchmeta.datasets import Omniglot
from torchmeta.transforms import Categorical, ClassSplitter, Rotation
from torchvision.transforms import Compose, Resize, ToTensor
from torchmeta.utils.data import BatchMetaDataLoader
dataset = Omniglot("data",
# Number of ways
num_classes_per_task=5,
# Resize the images to 28x28 and converts them to PyTorch tensors (from Torchvision)
transform=Compose([Resize(28), ToTensor()]),
# Transform the labels to integers (e.g. ("Glagolitic/character01", "Sanskrit/character14", ...) to (0, 1, ...))
target_transform=Categorical(num_classes=5),
# Creates new virtual classes with rotated versions of the images (from Santoro et al., 2016)
class_augmentations=[Rotation([90, 180, 270])],
meta_train=True,
download=True)
dataset = ClassSplitter(dataset, shuffle=True, num_train_per_class=5, num_test_per_class=15)
dataloader = BatchMetaDataLoader(dataset, batch_size=16, num_workers=4)
Note that the dataloader, receiving the dataset, remains the same.
Citation
Tristan Deleu, Tobias Würfl, Mandana Samiei, Joseph Paul Cohen, and Yoshua Bengio. Torchmeta: A Meta-Learning library for PyTorch, 2019 [ArXiv]
If you want to cite Torchmeta, use the following Bibtex entry:
@misc{deleu2019torchmeta,
title={{Torchmeta: A Meta-Learning library for PyTorch}},
author={Deleu, Tristan and W\"urfl, Tobias and Samiei, Mandana and Cohen, Joseph Paul and Bengio, Yoshua},
year={2019},
url={https://arxiv.org/abs/1909.06576},
note={Available at: https://github.com/tristandeleu/pytorch-meta}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file torchmeta-1.8.0.tar.gz
.
File metadata
- Download URL: torchmeta-1.8.0.tar.gz
- Upload date:
- Size: 157.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1844abac30c00729190069287687fb5a183f990b98d8755eacf4c8f23ac752f0 |
|
MD5 | 8010556548bbe769809cb77c5d496780 |
|
BLAKE2b-256 | 56f304136f3c976bb2d01dad9ff76a9e03dc9550612faf819ca542356a58e316 |
File details
Details for the file torchmeta-1.8.0-py3-none-any.whl
.
File metadata
- Download URL: torchmeta-1.8.0-py3-none-any.whl
- Upload date:
- Size: 210.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62c0d9813a7883d551701e27de09aea8a89ef9d6c15a443c7552781baaa5ca9a |
|
MD5 | d818138f09f6aaf335b6b3e434da5a36 |
|
BLAKE2b-256 | b968fd6fd811c82c65e180ae211a190e79e10a813b7bd814355c732a56be093a |