-
Notifications
You must be signed in to change notification settings - Fork 9
/
data_loader.py
214 lines (188 loc) · 6.63 KB
/
data_loader.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import numpy as np
import tensorflow.compat.v1 as tf
import tensorflow_datasets as tfds
class MineRL:
def __init__(self, batch_size, epochs, train=True, seq_len=None, data_root=None):
self._train = train
self._batch_size = batch_size
self._epochs = epochs
self._data_seq_len = 500
self._seq_len = seq_len
if self._train:
ds = tfds.load("minerl_navigate", data_dir=data_root, shuffle_files=True)[
"train"
]
else:
ds = tfds.load("minerl_navigate", data_dir=data_root, shuffle_files=False)[
"test"
]
ds = ds.map(lambda vid: vid["video"]).flat_map(
lambda x: tf.data.Dataset.from_tensor_slices(self._process_seq(x))
)
ds = ds.prefetch(tf.data.experimental.AUTOTUNE)
ds = ds.repeat(self._epochs)
if self._train:
ds = ds.shuffle(10 * self._batch_size)
ds = ds.batch(self._batch_size)
ds = ds.prefetch(tf.data.experimental.AUTOTUNE)
self.batch = tf.data.make_one_shot_iterator(ds).get_next()
def get_batch(self):
return self.batch
def _process_seq(self, seq):
if self._seq_len:
seq_len_tr = self._data_seq_len - (self._data_seq_len % self._seq_len)
seq = seq[:seq_len_tr]
seq = tf.reshape(
seq,
tf.concat(
[[seq_len_tr // self._seq_len, self._seq_len], tf.shape(seq)[1:]],
-1,
),
)
else:
seq = tf.expand_dims(seq, 0)
seq = tf.cast(seq, tf.float32) / 255.0
return seq
class GQNMazes:
def __init__(self, batch_size, epochs, train=True, seq_len=None, data_root=None):
self._train = train
self._batch_size = batch_size
self._epochs = epochs
self._data_seq_len = 300
self._seq_len = seq_len
if self._train:
ds = tfds.load("gqn_mazes", data_dir=data_root, shuffle_files=True)["train"]
else:
ds = tfds.load("gqn_mazes", data_dir=data_root, shuffle_files=False)["test"]
ds = ds.map(lambda vid: vid["video"]).flat_map(
lambda x: tf.data.Dataset.from_tensor_slices(self._process_seq(x))
)
ds = ds.prefetch(tf.data.experimental.AUTOTUNE)
ds = ds.repeat(self._epochs)
if self._train:
ds = ds.shuffle(10 * self._batch_size)
ds = ds.batch(self._batch_size)
ds = ds.prefetch(tf.data.experimental.AUTOTUNE)
self.batch = tf.data.make_one_shot_iterator(ds).get_next()
def get_batch(self):
return self.batch
def _process_seq(self, seq):
if self._seq_len:
seq_len_tr = self._data_seq_len - (self._data_seq_len % self._seq_len)
seq = seq[:seq_len_tr]
seq = tf.reshape(
seq,
tf.concat(
[[seq_len_tr // self._seq_len, self._seq_len], tf.shape(seq)[1:]],
-1,
),
)
else:
seq = tf.expand_dims(seq, 0)
seq = tf.cast(seq, tf.float32) / 255.0
return seq
class MovingMNIST:
def __init__(self, batch_size, epochs, train=True, seq_len=None, data_root=None):
self._train = train
self._batch_size = batch_size
self._epochs = epochs
if self._train:
self._data_seq_len = 100
else:
self._data_seq_len = 1000
self._seq_len = seq_len
if self._train:
ds = tfds.load(
"moving_mnist_2digit", data_dir=data_root, shuffle_files=True
)["train"]
else:
ds = tfds.load(
"moving_mnist_2digit", data_dir=data_root, shuffle_files=False
)["test"]
ds = ds.map(lambda vid: vid["video"]).flat_map(
lambda x: tf.data.Dataset.from_tensor_slices(self._process_seq(x))
)
ds = ds.prefetch(tf.data.experimental.AUTOTUNE)
ds = ds.repeat(self._epochs)
if self._train:
ds = ds.shuffle(10 * self._batch_size)
ds = ds.batch(self._batch_size)
ds = ds.prefetch(tf.data.experimental.AUTOTUNE)
self.batch = tf.data.make_one_shot_iterator(ds).get_next()
def get_batch(self):
return self.batch
def _process_seq(self, seq):
if self._seq_len:
seq_len_tr = self._data_seq_len - (self._data_seq_len % self._seq_len)
seq = seq[:seq_len_tr]
seq = tf.reshape(
seq,
tf.concat(
[[seq_len_tr // self._seq_len, self._seq_len], tf.shape(seq)[1:]],
-1,
),
)
else:
seq = tf.expand_dims(seq, 0)
seq = tf.cast(seq, tf.float32) / 255.0
return seq
def load_dataset(cfg, **kwargs):
if cfg.dataset == "minerl":
import minerl_navigate
train_data_batch = MineRL(
cfg.batch_size,
cfg.num_epochs,
train=True,
seq_len=cfg.seq_len,
data_root=cfg.datadir,
).get_batch()
test_data_batch = MineRL(
cfg.batch_size,
1,
train=False,
seq_len=cfg.eval_seq_len,
data_root=cfg.datadir,
).get_batch()
elif cfg.dataset == "mmnist":
import datasets.moving_mnist
train_data_batch = MovingMNIST(
cfg.batch_size,
cfg.num_epochs,
train=True,
seq_len=cfg.seq_len,
data_root=cfg.datadir,
).get_batch()
test_data_batch = MovingMNIST(
cfg.batch_size,
1,
train=False,
seq_len=cfg.eval_seq_len,
data_root=cfg.datadir,
).get_batch()
elif cfg.dataset == "mazes":
import datasets.gqn_mazes
train_data_batch = GQNMazes(
cfg.batch_size,
cfg.num_epochs,
train=True,
seq_len=cfg.seq_len,
data_root=cfg.datadir,
).get_batch()
test_data_batch = GQNMazes(
cfg.batch_size,
1,
train=False,
seq_len=cfg.eval_seq_len,
data_root=cfg.datadir,
).get_batch()
else:
raise ValueError("Dataset {} not supported.".format(cfg.dataset))
return train_data_batch, test_data_batch
def get_multiple_batches(batch_op, num_batches, sess):
batches = []
for _ in range(num_batches):
batches.append(sess.run(batch_op))
batches = np.concatenate(batches, 0)
return batches
def get_single_batch(batch_op, sess):
return sess.run(batch_op)