DL-Art-School/codes/data/torch_dataset.py

57 lines
1.8 KiB
Python
Raw Normal View History

2020-12-03 22:32:21 +00:00
import torch
from torch.utils.data import Dataset
import torchvision.transforms as T
from torchvision import datasets
# Wrapper for basic pytorch datasets which re-wraps them into a format usable by ExtensibleTrainer.
class TorchDataset(Dataset):
def __init__(self, opt):
DATASET_MAP = {
"mnist": datasets.MNIST,
"fmnist": datasets.FashionMNIST,
"cifar10": datasets.CIFAR10,
2021-01-12 03:09:16 +00:00
"imagenet": datasets.ImageNet,
"imagefolder": datasets.ImageFolder
2020-12-03 22:32:21 +00:00
}
2021-01-12 03:09:16 +00:00
normalize = T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
if opt['train']:
transforms = [
T.RandomResizedCrop(opt['image_size']),
T.RandomHorizontalFlip(),
T.ToTensor(),
normalize,
]
else:
transforms = [
T.Resize(opt['val_resize']),
T.CenterCrop(opt['image_size']),
T.ToTensor(),
normalize,
]
2020-12-03 22:32:21 +00:00
transforms = T.Compose(transforms)
2021-01-12 03:09:16 +00:00
self.dataset = DATASET_MAP[opt['dataset']](transform=transforms, **opt['kwargs'])
2020-12-03 22:32:21 +00:00
self.len = opt['fixed_len'] if 'fixed_len' in opt.keys() else len(self.dataset)
def __getitem__(self, item):
2021-01-12 03:09:16 +00:00
underlying_item, lbl = self.dataset[item]
return {'lq': underlying_item, 'hq': underlying_item, 'labels': lbl,
2020-12-03 22:32:21 +00:00
'LQ_path': str(item), 'GT_path': str(item)}
def __len__(self):
return self.len
2021-01-12 03:09:16 +00:00
if __name__ == '__main__':
opt = {
'flip': True,
'crop_sz': None,
'dataset': 'imagefolder',
'resize': 256,
'center_crop': 224,
'normalize': True,
'kwargs': {
'root': 'F:\\4k6k\\datasets\\images\\imagenet_2017\\val',
}
}
set = TorchDataset(opt)
j = set[0]