-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdataset_constructor.py
154 lines (141 loc) · 4.51 KB
/
dataset_constructor.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
import torch
from datasets import (
# 1D
AdditionProblem,
CopyMemory,
SpeechCommands,
CharTrajectories,
# 2D classification
MNIST,
CIFAR10,
CIFAR100,
STL10,
ImagenetDownsampled,
)
# typing
from omegaconf import OmegaConf
from typing import Dict, Tuple
from torch.utils.data import DataLoader
DATASET_RESOLUTIONS = {
"CIFAR10": 32,
"CIFAR100": 32,
}
def dataset_constructor(
cfg: OmegaConf,
) -> Tuple[
torch.utils.data.Dataset, torch.utils.data.Dataset, torch.utils.data.Dataset
]:
"""
Create datasets loaders for the chosen datasets
:return: Tuple (training_set, validation_set, test_set)
"""
dataset = {
"AddProblem": AdditionProblem,
"CopyMemory": CopyMemory,
"MNIST": MNIST,
"sMNIST": MNIST,
"CIFAR10": CIFAR10,
"sCIFAR10": CIFAR10,
"CIFAR100": CIFAR100,
"Imagenet64": ImagenetDownsampled,
"Imagenet32": ImagenetDownsampled,
"Imagenet16": ImagenetDownsampled,
"Imagenet8": ImagenetDownsampled,
"Imagenet-k": ImagenetDownsampled,
"STL10": STL10,
"SpeechCommands": SpeechCommands,
"CharTrajectories": CharTrajectories,
}[cfg.dataset]
test_partition = "test"
# Custom settings for some datasets, passed by keyword args
kwargs = {}
if cfg.dataset in ["Imagenet64", "Imagenet32", "Imagenet16", "Imagenet8"]:
kwargs["root"] = cfg.dataset_params.root
kwargs["use_ram"] = cfg.dataset_params.in_ram
kwargs["use_cache"] = cfg.dataset_params.from_cache
kwargs["img_size"] = int(cfg.dataset.split("Imagenet")[1])
test_partition = "val"
elif cfg.dataset == "CIFAR10":
kwargs["blur"] = cfg.cross_res.blur
training_set = dataset(
partition="train",
seq_length=cfg.dataset_params.seq_length,
memory_size=cfg.dataset_params.memory_size,
mfcc=cfg.dataset_params.mfcc,
dropped_rate=cfg.dataset_params.drop_rate,
augment=cfg.train.augment,
resize=cfg.cross_res.resize,
resize_interpolation=cfg.cross_res.interpolation,
resize_blur=cfg.cross_res.resize_blur,
resize_blur_sigma=cfg.cross_res.resize_blur_sigma,
**kwargs,
)
test_set = dataset(
partition=test_partition,
seq_length=cfg.dataset_params.seq_length,
memory_size=cfg.dataset_params.memory_size,
mfcc=cfg.dataset_params.mfcc,
dropped_rate=cfg.dataset_params.drop_rate,
augment="None",
resize=cfg.cross_res.resize,
resize_interpolation=cfg.cross_res.interpolation,
resize_blur=cfg.cross_res.resize_blur,
resize_blur_sigma=cfg.cross_res.resize_blur_sigma,
**kwargs,
)
if cfg.dataset in ["SpeechCommands", "CharTrajectories"]:
validation_set = dataset(
partition="val",
seq_length=cfg.dataset_params.seq_length,
memory_size=cfg.dataset_params.memory_size,
mfcc=cfg.dataset_params.mfcc,
dropped_rate=cfg.dataset_params.drop_rate,
augment="None",
resize=cfg.cross_res.resize,
resize_interpolation=cfg.cross_res.interpolation,
resize_blur=cfg.cross_res.resize_blur,
resize_blur_sigma=cfg.cross_res.resize_blur_sigma,
**kwargs,
)
else:
validation_set = None
return training_set, validation_set, test_set
def construct_dataloaders(
cfg: OmegaConf,
num_workers: int = 4,
) -> Dict[str, DataLoader]:
"""
Create datasets loaders for the chosen datasets
:return: dict(train_loader, val_loader, test_loader)
"""
training_set, validation_set, test_set = dataset_constructor(cfg)
training_loader = torch.utils.data.DataLoader(
training_set,
batch_size=cfg.train.batch_size,
shuffle=True,
num_workers=num_workers,
pin_memory=True,
)
test_loader = torch.utils.data.DataLoader(
test_set,
batch_size=cfg.train.batch_size,
shuffle=False,
num_workers=num_workers,
pin_memory=True,
)
if validation_set is not None:
val_loader = torch.utils.data.DataLoader(
validation_set,
batch_size=cfg.train.batch_size,
shuffle=False,
num_workers=num_workers,
pin_memory=True,
)
else:
val_loader = test_loader
dataloaders = {
"train": training_loader,
"validation": val_loader,
"test": test_loader,
}
return dataloaders