-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathtest_h5dataset.py
425 lines (314 loc) · 12.4 KB
/
test_h5dataset.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import os
import time
import h5py
import numpy as np
import pytest
import torch
from torch.utils.data import DataLoader
import torchvision.transforms as trsf
from continuum.scenarios import ContinualScenario, ClassIncremental, Permutations
from continuum.datasets import H5Dataset, CIFAR100, MNIST
from continuum.tasks.h5_task_set import H5TaskSet
from continuum.tasks import split_train_val
from continuum.scenarios import create_subscenario
DATA_PATH = os.environ.get("CONTINUUM_DATA_PATH")
@pytest.fixture
def data():
x_ = np.random.randint(0, 255, size=(20, 32, 32, 3))
y_ = []
for i in range(10):
y_.append(np.ones(2) * i)
y_ = np.concatenate(y_)
t_ = np.copy(y_) // 5
return x_, y_.astype(int), t_.astype(int)
# yapf: disable
def test_creation_h5dataset(data, tmpdir):
filename_h5 = os.path.join(tmpdir, "test_h5.hdf5")
x_, y_, t_ = data
h5dataset = H5Dataset(x_, y_, t_, data_path=filename_h5)
x_0, y_0, t_0 = h5dataset.get_data()
assert isinstance(x_0, str) # x is only the path to the file
assert len(y_0) == len(y_)
assert len(t_0) == len(t_)
def test_concatenate_h5dataset(data, tmpdir):
filename_h5 = os.path.join(tmpdir, "test_h5.hdf5")
x_, y_, t_ = data
h5dataset = H5Dataset(x_, y_, t_, data_path=filename_h5)
h5dataset.add_data(x_, y_, t_)
assert len(h5dataset.get_class_vector()) == 2 * len(y_)
def test_create_subscenario_h5dataset(data, tmpdir):
from continuum.scenarios import create_subscenario
filename_h5 = os.path.join(tmpdir, "test_h5.hdf5")
x_, y_, t_ = data
h5dataset = H5Dataset(x_, y_, t_, data_path=filename_h5)
nb_task = len(np.unique(t_))
scenario = ContinualScenario(h5dataset)
sub_scenario = create_subscenario(scenario, np.arange(nb_task - 1))
for task_set in sub_scenario:
loader = DataLoader(task_set)
for _ in loader:
pass
assert sub_scenario.nb_tasks == nb_task - 1
def test_create_subscenario_suffle_h5dataset(data, tmpdir):
filename_h5 = os.path.join(tmpdir, "test_h5.hdf5")
x_, y_, t_ = data
h5dataset = H5Dataset(x_, y_, t_, data_path=filename_h5)
nb_task = len(np.unique(t_))
scenario = ContinualScenario(h5dataset)
task_order = np.arange(nb_task)
np.random.shuffle(task_order)
sub_scenario = create_subscenario(scenario, task_order)
for task_set in sub_scenario:
loader = DataLoader(task_set)
for _ in loader:
pass
assert sub_scenario.nb_tasks == nb_task
def test_h5dataset_ContinualScenario(data, tmpdir):
filename_h5 = os.path.join(tmpdir, "test_h5.hdf5")
x_, y_, t_ = data
h5dataset = H5Dataset(x_, y_, t_, data_path=filename_h5)
nb_task = len(np.unique(t_))
scenario = ContinualScenario(h5dataset)
assert scenario.nb_tasks == nb_task
data_indexes = np.where(t_ == 0)[0]
assert len(data_indexes) == len(scenario[0])
def test_h5dataset_add_data(data, tmpdir):
filename_h5 = os.path.join(tmpdir, "test_h5.hdf5")
x_, y_, t_ = data
h5dataset = H5Dataset(x_, y_, t_, data_path=filename_h5)
h5dataset.add_data(x_, y_, t_)
nb_task = len(np.unique(t_))
scenario = ContinualScenario(h5dataset)
assert scenario.nb_tasks == nb_task
def test_h5dataset_IncrementalScenario(data, tmpdir):
filename_h5 = os.path.join(tmpdir, "test_h5.hdf5")
x_, y_, t_ = data
nb_task = 2
h5dataset = H5Dataset(x_, y_, None, data_path=filename_h5)
scenario = ClassIncremental(h5dataset, nb_tasks=nb_task)
assert scenario.nb_tasks == nb_task
tot_len = 0
for task_set in scenario:
tot_len += len(task_set)
loader = DataLoader(task_set)
for _ in loader:
pass
assert tot_len == len(y_)
def test_h5dataset_loading(data, tmpdir):
filename_h5 = os.path.join(tmpdir, "test_h5.hdf5")
x_, y_, t_ = data
h5dataset = H5Dataset(x_, y_, t_, data_path=filename_h5)
nb_task = len(np.unique(t_))
scenario = ContinualScenario(h5dataset)
for task_set in scenario:
loader = DataLoader(task_set)
for _ in loader:
pass
assert scenario.nb_tasks == nb_task
def test_h5dataset_get_raw(data, tmpdir):
filename_h5 = os.path.join(tmpdir, "test_h5.hdf5")
x_, y_, t_ = data
h5dataset = H5Dataset(x_, y_, t_, data_path=filename_h5)
nb_task = len(np.unique(t_))
scenario = ContinualScenario(h5dataset)
for task_set in scenario:
indexes = np.random.randint(len(task_set), size=len(task_set) // 2)
_, _, _ = task_set.get_raw_samples(indexes.sort())
# test with no indexes
_, _, _ = task_set.get_raw_samples()
assert scenario.nb_tasks == nb_task
def test_h5dataset_split_train_test(data, tmpdir):
filename_h5 = os.path.join(tmpdir, "test_h5.hdf5")
x_, y_, t_ = data
h5dataset = H5Dataset(x_, y_, t_, data_path=filename_h5)
nb_task = len(np.unique(t_))
scenario = ContinualScenario(h5dataset)
for task_set in scenario:
task_set_tr, task_set_val = split_train_val(task_set)
loader_tr = DataLoader(task_set_tr)
for _ in loader_tr:
pass
loader_val = DataLoader(task_set_val)
for _ in loader_val:
pass
assert scenario.nb_tasks == nb_task
def test_h5dataset_reloading(data, tmpdir):
filename_h5 = os.path.join(tmpdir, "test_h5.hdf5")
x_, y_, t_ = data
# create dataset
h5dataset = H5Dataset(x_, y_, t_, data_path=filename_h5)
# destroy object
del h5dataset
# reload data set
h5dataset_reloaded = H5Dataset(x=None, y=None, t=None, data_path=filename_h5)
nb_task = len(np.unique(t_))
scenario = ContinualScenario(h5dataset_reloaded)
for task_set in scenario:
loader = DataLoader(task_set)
for _ in loader:
pass
assert scenario.nb_tasks == nb_task
def test_h5dataset_to_taskset(data, tmpdir):
filename_h5 = os.path.join(tmpdir, "test_h5.hdf5")
x_, y_, t_ = data
h5dataset = H5Dataset(x_, y_, t_, data_path=filename_h5)
task_set = h5dataset.to_taskset()
loader = DataLoader(task_set)
for _ in loader:
pass
@pytest.mark.slow
def test_time(tmpdir):
global DATA_PATH
cl_dataset = CIFAR100(data_path=DATA_PATH,
download=False,
train=True,
labels_type="category",
task_labels="lifelong")
# in practice the construction is part by part to reduce data load but here we do it at once
x, y, t = cl_dataset.get_data()
h5_filename = os.path.join(tmpdir, "test_time_h5.hdf5")
h5dataset = H5Dataset(x, y, t, data_path=h5_filename)
task_set = H5TaskSet(h5_filename, y=h5dataset.get_class_vector(), t=h5dataset.get_task_indexes(), trsf=None)
start = time.time()
for i in range(10000):
a = task_set[5]
end = time.time()
print(f"normal __getitem__ {end - start}")
start = time.time()
with h5py.File(h5_filename, 'r') as hf:
for i in range(10000):
x = hf['x'][5]
y = hf['y'][5]
if 't' in hf.keys():
t = hf['t'][5]
else:
t = -1
end = time.time()
print(f"open only once __getitem__ {end - start}")
@pytest.mark.slow
def test_on_array_dataset_incremental(tmpdir):
filename_h5 = os.path.join(tmpdir, "test_CIFAR100_h5.hdf5")
nb_tasks = 10
cl_dataset = CIFAR100(data_path=DATA_PATH,
download=False,
train=True)
# in practice the construction is part by part to reduce data load but here we do it at once
x, y, t = cl_dataset.get_data()
h5dataset = H5Dataset(x, y, t, data_path=filename_h5)
scenario = ClassIncremental(h5dataset, nb_tasks=nb_tasks)
for task_set in scenario:
loader = DataLoader(task_set, batch_size=64)
for x, y, t in loader:
assert x.shape == torch.Size([64, 3, 32, 32])
break
assert scenario.nb_tasks == nb_tasks # number of task of CIFAR100Lifelong
@pytest.mark.slow
def test_h5dataset_reloading_slow(tmpdir):
filename_h5 = os.path.join(tmpdir, "test_h5.hdf5")
nb_tasks = 5
cl_dataset = CIFAR100(data_path=DATA_PATH,
download=False,
train=True,
labels_type="category",
task_labels="lifelong")
x, y, t = cl_dataset.get_data()
# create dataset
h5dataset = H5Dataset(x, y, t, data_path=filename_h5)
# destroy object
del h5dataset
# reload data set
h5dataset_reloaded = H5Dataset(x=None, y=None, t=None, data_path=filename_h5)
scenario = ContinualScenario(h5dataset_reloaded)
for task_set in scenario:
loader = DataLoader(task_set)
for _ in loader:
pass
assert scenario.nb_tasks == nb_tasks
task_order = np.arange(nb_tasks)
sub_scenario = create_subscenario(scenario, task_order[:-1])
assert sub_scenario.nb_tasks == nb_tasks-1
np.random.shuffle(task_order)
sub_scenario = create_subscenario(scenario, task_order)
assert sub_scenario.nb_tasks == nb_tasks
@pytest.mark.slow
def test_on_array_dataset(tmpdir):
filename_h5 = os.path.join(tmpdir, "test_CIFAR100_h5.hdf5")
cl_dataset = CIFAR100(data_path=DATA_PATH,
download=False,
train=True,
labels_type="category",
task_labels="lifelong")
# in practice the construction is part by part to reduce data load but here we do it at once
x, y, t = cl_dataset.get_data()
h5dataset = H5Dataset(x, y, t, data_path=filename_h5)
scenario = ContinualScenario(h5dataset)
for task_set in scenario:
loader = DataLoader(task_set, batch_size=64)
for x, y, t in loader:
assert x.shape == torch.Size([64, 3, 32, 32])
break
assert scenario.nb_tasks == 5 # number of task of CIFAR100Lifelong
# Not compatible at the moment (it is less necessary to use h5 with transform incremental scenarios.)
# @pytest.mark.slow
# def test_on_transform_scenario():
# filename_h5 = "test_permutation.hdf5"
# if os.path.exists(filename_h5):
# os.remove(filename_h5)
#
# cl_dataset = MNIST(data_path=DATA_PATH,
# download=False,
# train=True)
# # in practice the construction is part by part to reduce data load but here we do it at once
# x, y, t = cl_dataset.get_data()
# h5dataset = H5Dataset(x, y, t, data_path=filename_h5)
#
# scenario = Permutations(h5dataset, nb_tasks=3, shared_label_space=True)
#
# for task_set in scenario:
# loader = DataLoader(task_set, batch_size=64)
# for x, y, t in loader:
# break
#
# # SECOND TEST WITH A LABEL TRANSFORMATION
#
# scenario = Permutations(h5dataset, nb_tasks=3, shared_label_space=False)
#
# for task_set in scenario:
# loader = DataLoader(task_set, batch_size=64)
# for x, y, t in loader:
# break
#
# assert scenario.nb_tasks == 5 # number of task of CIFAR100Lifelong
# os.remove(filename_h5)
# Not compatible at the moment (it is not really necessary to use h5 when images are referenced by a path.)
# @pytest.mark.slow
# def test_on_path_dataset():
# filename_h5 = "test_CIFAR100_h5.hdf5"
# if os.path.exists(filename_h5):
# os.remove(filename_h5)
#
# cl_dataset = Core50(data_path=DATA_PATH,
# download=False,
# train=True,
# scenario="domains",
# classification="category")
# # in practice the construction is part by part to reduce data load but here we do it at once
# x, y, t = cl_dataset.get_data()
# h5dataset = H5Dataset(x, y, t, data_path=filename_h5)
#
# normalize = trsf.Normalize(mean=[0.485, 0.456, 0.406],
# std=[0.229, 0.224, 0.225])
# resize = trsf.Resize(size=(224, 224))
# transform = trsf.Compose([resize, trsf.ToTensor(), normalize])
# list_transform = [transform]
#
# scenario = ContinualScenario(h5dataset, transformations=list_transform)
#
# for task_set in scenario:
# loader = DataLoader(task_set, batch_size=64)
# for x, y, t in loader:
# assert x.shape == torch.Size([64, 3, 32, 32])
# break
#
# assert scenario.nb_tasks == 5 # number of task of CIFAR100Lifelong
# os.remove(filename_h5)