-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
32 lines (23 loc) · 834 Bytes
/
dataset.py
File metadata and controls
32 lines (23 loc) · 834 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
from torch.utils.data import Dataset
import os
from PIL import Image
class ImageDataset(Dataset):
"""
dataset for images
"""
def __init__(self, root, transform=None):
self.root = root
files = os.listdir(root)
self.image_files = [file for file in files if self.is_image(file)]# store image file names in a list
self.transform = transform
@staticmethod
def is_image(file):
return file.lower().endswith(('.jpg', '.png', '.jpeg'))
def __len__(self):
return len(self.image_files)
def __getitem__(self, idx):
image_full_path = os.path.join(self.root, self.image_files[idx])
img = Image.open(image_full_path).convert('RGB')
if self.transform:
img = self.transform(img)
return img