-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
51 lines (40 loc) · 1.69 KB
/
dataset.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
42
43
44
45
46
47
48
49
50
51
from torch.utils.data import Dataset
from PIL import Image
import os
import torch
class StandfordCarsDataset(Dataset):
def __init__(self, data_df, transforms):
image_paths = []
for idx, row in data_df.iterrows():
image_path = row["image_path"]
image_paths.append(image_path)
self.image_paths = image_paths
self.transforms = transforms
def __len__(self):
return len(self.image_paths)
def __getitem__(self, idx):
image_path = self.image_paths[idx]
image = Image.open(image_path).convert("RGB")
image = self.transforms(image)
return {"image": image}
class DiffusionDataset(Dataset):
def __init__(self, root_dir, split='train', transform=None):
"""
Args:
root_dir (string): Directory with all the images.
split (string): One of 'train' or 'test' to specify the split.
transform (callable, optional): Optional transform to be applied on a sample.
"""
self.root_dir = os.path.join(root_dir, split)
self.transform = transform
self.image_paths = [os.path.join(self.root_dir, fname) for fname in os.listdir(self.root_dir) if os.path.isfile(os.path.join(self.root_dir, fname))]
def __len__(self):
return len(self.image_paths)
def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
img_name = self.image_paths[idx]
image = Image.open(img_name).convert('RGB')
if self.transform:
image = self.transform(image)
return {'image': image}