mirror of
https://github.com/open-mmlab/mmselfsup.git
synced 2025-06-03 14:59:38 +08:00
34 lines
1.2 KiB
Python
34 lines
1.2 KiB
Python
import torch
|
|
from PIL import Image
|
|
from .registry import DATASETS
|
|
from .base import BaseDataset
|
|
from .utils import to_numpy
|
|
|
|
|
|
@DATASETS.register_module
|
|
class ContrastiveDataset(BaseDataset):
|
|
"""Dataset for contrastive learning methods that forward
|
|
two views of the image at a time (MoCo, SimCLR).
|
|
"""
|
|
|
|
def __init__(self, data_source, pipeline, prefetch=False):
|
|
data_source['return_label'] = False
|
|
super(ContrastiveDataset, self).__init__(data_source, pipeline, prefetch)
|
|
|
|
def __getitem__(self, idx):
|
|
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))
|
|
img1 = self.pipeline(img)
|
|
img2 = self.pipeline(img)
|
|
if self.prefetch:
|
|
img1 = torch.from_numpy(to_numpy(img1))
|
|
img2 = torch.from_numpy(to_numpy(img2))
|
|
img_cat = torch.cat((img1.unsqueeze(0), img2.unsqueeze(0)), dim=0)
|
|
return dict(img=img_cat)
|
|
|
|
def evaluate(self, scores, keyword, logger=None, **kwargs):
|
|
raise NotImplemented
|