Skip to main content

Dataloaders for meta-learning in Pytorch

Project description

torchmeta

A collection of extensions and data-loaders for few-shot learning & meta-learning in PyTorch. The package contains popular meta-learning benchmarks, fully compatible with both torchvision and PyTorch's DataLoader.

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. For more examples, check the examples folder.

from torchmeta.datasets import Omniglot
from torchmeta.transforms import Categorical, ClassSplitter
from torchvision.transforms import Resize, ToTensor, Compose

from torchmeta.utils.data import BatchMetaDataLoader

dataset = Omniglot('data', num_classes_per_task=5,
                   transform=Compose([Resize(28), ToTensor()]),
                   target_transform=Categorical(num_classes=5),
                   meta_train=True, download=True)
dataset = ClassSplitter(dataset, num_train_per_class=5, num_test_per_class=15)

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))
    print('Train targets shape: {0}'.format(train_targets.shape))
    # Train inputs shape: torch.Size([16, 25, 1, 28, 28])
    # Train targets shape: torch.Size([16, 25])

    test_inputs, test_targets = batch['test']
    print('Test inputs shape: {0}'.format(test_inputs.shape))
    print('Test targets shape: {0}'.format(test_targets.shape))
    # Test inputs shape: torch.Size([16, 75, 1, 28, 28])
    # Test targets shape: torch.Size([16, 75])

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

torchmeta-1.1.0rc2.tar.gz (105.8 kB view hashes)

Uploaded Source

Built Distribution

torchmeta-1.1.0rc2-py3-none-any.whl (129.7 kB view hashes)

Uploaded Python 3

Supported by

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