mmselfsup/openselfsup/datasets/contrastive.py

29 lines
972 B
Python
Raw Normal View History

2020-06-16 00:05:18 +08:00
import torch
from PIL import Image
2020-06-16 00:05:18 +08:00
from .registry import DATASETS
from .base import BaseDataset
@DATASETS.register_module
class ContrastiveDataset(BaseDataset):
2020-09-02 18:49:39 +08:00
"""Dataset for contrastive learning methods that forward
two views of the image at a time (MoCo, SimCLR).
2020-06-16 00:05:18 +08:00
"""
def __init__(self, data_source, pipeline):
super(ContrastiveDataset, self).__init__(data_source, pipeline)
def __getitem__(self, idx):
2020-06-17 01:31:59 +08:00
img = self.data_source.get_sample(idx)
assert isinstance(img, Image.Image), \
'The output from the data source must be an Image, got: {}. \
Please ensure that the list file does not contain labels.'.format(
type(img))
2020-06-16 00:05:18 +08:00
img1 = self.pipeline(img)
img2 = self.pipeline(img)
img_cat = torch.cat((img1.unsqueeze(0), img2.unsqueeze(0)), dim=0)
return dict(img=img_cat)
def evaluate(self, scores, keyword, logger=None):
raise NotImplemented