-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
167 lines (146 loc) · 5.88 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from typing import List, Optional, Sequence, Union
from pytorch_lightning import LightningDataModule
from torch.utils.data import DataLoader
from torchvision import transforms
from datasets.celeba_dataset import MyCelebA, TCeleba
from datasets.disent_dataset import MyCars3D, MyDSprites, MySmallNORB, MyShapes3D, MySprites, TCars3D, TDSprites, TSmallNORB, TShapes3D, TSprites
from datasets.oxford_dataset import OxfordPets
from datasets.transition import TransitionDataset, TransitionBatchSampler
DATASETS = {
"Celeba" : MyCelebA,
"TCeleba": TCeleba,
"Cars3D" : MyCars3D,
"TCars3D": TCars3D,
"DSprites" : MyDSprites,
"TDSprites": TDSprites,
"SmallNORB" : MySmallNORB,
"TSmallNORB": TSmallNORB,
"Shapes3D" : MyShapes3D,
"TShapes3D": TShapes3D,
"Sprites" : MySprites,
"TSprites": TSprites,
}
class VAEDataset(LightningDataModule):
"""
PyTorch Lightning data module
Args:
data_path: root directory of your dataset.
train_batch_size: the batch size to use during training.
val_batch_size: the batch size to use during validation.
patch_size: the size of the crop to take from the original images.
num_workers: the number of parallel workers to create to load data
items (see PyTorch's Dataloader documentation for more details).
pin_memory: whether prepared items should be loaded into pinned memory
or not. This can improve performance on GPUs.
"""
def __init__(
self,
data_path: str,
dataset_name: str,
train_batch_size: int = 8,
val_batch_size: int = 8,
patch_size: Union[int, Sequence[int]] = (256, 256),
num_workers: int = 0,
pin_memory: bool = False,
limit: Optional[int] = None,
distributed: bool = True,
**kwargs,
):
super().__init__()
self.data_dir = data_path
self.dataset_name = dataset_name
self.train_batch_size = train_batch_size
self.val_batch_size = val_batch_size
self.patch_size = patch_size
self.num_workers = num_workers
self.pin_memory = pin_memory
self.limit = limit
self.distributed = distributed
def setup(self, stage: Optional[str] = None) -> None:
train_transforms = transforms.Compose([transforms.ToTensor(),
# transforms.RandomHorizontalFlip(),
transforms.CenterCrop(148),
transforms.Resize(self.patch_size)])
val_transforms = transforms.Compose([transforms.ToTensor(),
# transforms.RandomHorizontalFlip(),
transforms.CenterCrop(148),
transforms.Resize(self.patch_size),])
self.train_dataset = DATASETS[self.dataset_name](
self.data_dir,
split='train',
transform=train_transforms,
download=False,
)
self.val_dataset = DATASETS[self.dataset_name](
self.data_dir,
split='test',
transform=val_transforms,
download=False,
)
def train_dataloader(self) -> DataLoader:
if isinstance(self.train_dataset, TransitionDataset):
return DataLoader(
self.train_dataset,
batch_sampler=TransitionBatchSampler(
self.train_dataset,
shuffle=True,
batch_size=self.train_batch_size,
drop_last=True,
distributed=self.distributed,
limit=self.limit
),
num_workers=self.num_workers,
pin_memory=self.pin_memory,
)
else:
return DataLoader(
self.train_dataset,
batch_size=self.train_batch_size,
num_workers=self.num_workers,
shuffle=True,
pin_memory=self.pin_memory,
)
def val_dataloader(self) -> Union[DataLoader, List[DataLoader]]:
if isinstance(self.val_dataset, TransitionDataset):
return DataLoader(
self.val_dataset,
batch_sampler=TransitionBatchSampler(
self.val_dataset,
shuffle=False,
batch_size=self.val_batch_size,
drop_last=True,
distributed=self.distributed
),
num_workers=self.num_workers,
pin_memory=self.pin_memory,
)
else:
return DataLoader(
self.val_dataset,
batch_size=self.val_batch_size,
num_workers=self.num_workers,
shuffle=False,
pin_memory=self.pin_memory,
)
def test_dataloader(self) -> Union[DataLoader, List[DataLoader]]:
if isinstance(self.val_dataset, TransitionDataset):
return DataLoader(
self.val_dataset,
batch_sampler=TransitionBatchSampler(
self.val_dataset,
shuffle=True,
batch_size=self.val_batch_size,
drop_last=True,
distributed=self.distributed
),
num_workers=self.num_workers,
pin_memory=self.pin_memory,
)
else:
return DataLoader(
self.val_dataset,
batch_size=self.val_batch_size,
num_workers=self.num_workers,
shuffle=True,
pin_memory=self.pin_memory,
)