-
Notifications
You must be signed in to change notification settings - Fork 15
/
dataset.py
140 lines (129 loc) · 4.01 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
import torch
from datasets import (
AdditionProblem,
CopyMemory,
MNIST,
CIFAR10,
SpeechCommands,
CharTrajectories,
PhysioNet,
PennTreeBankChar,
)
import ml_collections
from typing import Tuple
def dataset_constructor(
config: ml_collections.ConfigDict,
) -> 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,
"CIFAR10": CIFAR10,
"SpeechCommands": SpeechCommands,
"CharTrajectories": CharTrajectories,
"PhysioNet": PhysioNet,
"PennTreeBankChar": PennTreeBankChar,
}[config.dataset]
if config.dataset == "PennTreeBankChar":
eval_batch_size = 10
else:
eval_batch_size = config.batch_size
training_set = dataset(
partition="train",
seq_length=config.seq_length,
memory_size=config.memory_size,
mfcc=config.mfcc,
sr=config.sr_train,
dropped_rate=config.drop_rate,
valid_seq_len=config.valid_seq_len,
batch_size=config.batch_size,
)
test_set = dataset(
partition="test",
seq_length=config.seq_length,
memory_size=config.memory_size,
mfcc=config.mfcc,
sr=config.sr_train
if config.sr_test == 0
else config.sr_test, # Test set can be sample differently.
dropped_rate=config.drop_rate,
valid_seq_len=config.valid_seq_len,
batch_size=eval_batch_size,
)
if config.dataset in [
"SpeechCommands",
"CharTrajectories",
"PhysioNet",
"PennTreeBankChar",
]:
validation_set = dataset(
partition="val",
seq_length=config.seq_length,
memory_size=config.memory_size,
mfcc=config.mfcc,
sr=config.sr_train,
dropped_rate=config.drop_rate,
valid_seq_len=config.valid_seq_len,
batch_size=eval_batch_size,
)
else:
validation_set = None
return training_set, validation_set, test_set
def get_dataset(
config: ml_collections.ConfigDict,
num_workers: int = 4,
data_root="./data",
) -> Tuple[dict, torch.utils.data.DataLoader]:
"""
Create datasets loaders for the chosen datasets
:return: Tuple ( dict(train_loader, val_loader) , test_loader)
"""
training_set, validation_set, test_set = dataset_constructor(config)
if config.dataset in ["PennTreeBankChar"]:
with config.unlocked():
config.vocab_size = len(training_set.dictionary)
training_loader = torch.utils.data.DataLoader(
training_set,
batch_sampler=training_set.sampler,
num_workers=num_workers,
)
test_loader = torch.utils.data.DataLoader(
test_set,
batch_sampler=test_set.sampler,
num_workers=num_workers,
)
val_loader = torch.utils.data.DataLoader(
validation_set,
batch_sampler=validation_set.sampler,
num_workers=num_workers,
)
else:
training_loader = torch.utils.data.DataLoader(
training_set,
batch_size=config.batch_size,
shuffle=True,
num_workers=num_workers,
)
test_loader = torch.utils.data.DataLoader(
test_set,
batch_size=config.batch_size,
shuffle=False,
num_workers=num_workers,
)
if validation_set is not None:
val_loader = torch.utils.data.DataLoader(
validation_set,
batch_size=config.batch_size,
shuffle=False,
num_workers=num_workers,
)
else:
val_loader = test_loader
dataloaders = {"train": training_loader, "validation": val_loader}
return dataloaders, test_loader