-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasetUtils.py
41 lines (31 loc) · 1.21 KB
/
datasetUtils.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
30
31
32
33
34
35
36
37
38
39
40
41
from torch.utils.data import DataLoader, Dataset
import os
from PIL import Image
from skimage import io
import torchvision
class imageDataset(Dataset):
def __init__(self, root_dir, transform=None):
"""
Args:
root_dir (string): Directory with all the images.
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.root_dir = root_dir
self.transform = transform
self.imgPaths= [os.path.join(self.root_dir,f) for f in os.listdir(self.root_dir) if os.path.isfile(os.path.join(root_dir, f))]
self.imgPaths=[image for image in self.imgPaths if Image.open(image).mode == "RGB"]
def __len__(self):
return len(self.imgPaths)
def __getitem__(self, idx):
sample = Image.open(self.imgPaths[idx])
if self.transform:
sample = self.transform(sample)
return sample
def showSample(self,idx,transformed=True):
sample = Image.open(self.imgPaths[idx])
if transformed:
sample = self.transform(sample)
torchvision.transforms.ToPILImage()(sample).show()
else:
sample.show()