-
Notifications
You must be signed in to change notification settings - Fork 7
/
dataset_utils.py
29 lines (26 loc) · 1020 Bytes
/
dataset_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import os
import torch
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms.functional as F
from torchvision import transforms, utils
from PIL import Image
class resized_dataset(Dataset):
def __init__(self, dataset, transform=None, start=None, end=None, resize=None):
self.data=[]
if start == None: start = 0
if end == None: end = dataset.__len__()
if resize==None:
for i in range(start, end):
self.data.append((*dataset.__getitem__(i)))
else:
for i in range(start, end):
item=dataset.__getitem__(i)
self.data.append((F.center_crop(F.resize(item[0],resize,Image.BILINEAR),resize),item[1]))
self.transform = transform
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
if self.transform:
return (self.transform(self.data[idx][0]), self.data[idx][1])
else:
return self.data[idx]