-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
33 lines (24 loc) · 956 Bytes
/
Copy pathdata.py
File metadata and controls
33 lines (24 loc) · 956 Bytes
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
import os
from PIL import Image
from torch.utils.data import Dataset
class FolderDataset(Dataset):
"""
Creates a PyTorch dataset from folder, returning two tensor images.
Args:
main_dir : directory where images are stored.
transform (optional) : torchvision transforms to be applied while making dataset
"""
def __init__(self, main_dir, transform=None):
print(main_dir)
self.main_dir = main_dir
self.transform = transform
self.all_imgs = os.listdir(main_dir)
def __len__(self):
return len(self.all_imgs)
def __getitem__(self, idx):
img_loc = os.path.join(self.main_dir, self.all_imgs[idx])
image = Image.open(img_loc).convert("RGB")
if self.transform is not None:
tensor_image = self.transform(image)
return tensor_image, tensor_image,self.all_imgs[idx]
return image,image