forked from MIC-DKFZ/batchgenerators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_DataLoader.py
262 lines (211 loc) · 10.1 KB
/
test_DataLoader.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
# Copyright 2021 Division of Medical Image Computing, German Cancer Research Center (DKFZ)
# and Applied Computer Vision Lab, Helmholtz Imaging Platform
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from copy import deepcopy
import numpy as np
from batchgenerators.dataloading.data_loader import DataLoader
from batchgenerators.dataloading.multi_threaded_augmenter import MultiThreadedAugmenter
class DummyDataLoader(DataLoader):
def __init__(self, data, batch_size, num_threads_in_multithreaded, seed_for_shuffle=1, return_incomplete=False,
shuffle=True, infinite=False):
super(DummyDataLoader, self).__init__(data, batch_size, num_threads_in_multithreaded, seed_for_shuffle, return_incomplete, shuffle,
infinite)
self.indices = data
def generate_train_batch(self):
idx = self.get_indices()
return idx
class TestDataLoader(unittest.TestCase):
def test_return_all_indices_single_threaded_shuffle_False(self):
data = list(range(123))
batch_sizes = [1, 3, 75, 12, 23]
for b in batch_sizes:
dl = DummyDataLoader(deepcopy(data), b, 1, 1, return_incomplete=True, shuffle=False, infinite=False)
for _ in range(3):
idx = []
for i in dl:
idx += i
self.assertTrue(len(idx) == len(data))
self.assertTrue(all([i == j for i,j in zip(idx, data)]))
def test_return_all_indices_single_threaded_shuffle_True(self):
data = list(range(123))
batch_sizes = [1, 3, 75, 12, 23]
np.random.seed(1234)
for b in batch_sizes:
dl = DummyDataLoader(deepcopy(data), b, 1, 1, return_incomplete=True, shuffle=True, infinite=False)
for _ in range(3):
idx = []
for i in dl:
idx += i
self.assertTrue(len(idx) == len(data))
self.assertTrue(not all([i == j for i, j in zip(idx, data)]))
idx.sort()
self.assertTrue(all([i == j for i,j in zip(idx, data)]))
def test_infinite_single_threaded(self):
data = list(range(123))
dl = DummyDataLoader(deepcopy(data), 12, 1, 1, return_incomplete=True, shuffle=True, infinite=False)
# this should raise a StopIteration
with self.assertRaises(StopIteration):
for i in range(1000):
idx = next(dl)
dl = DummyDataLoader(deepcopy(data), 12, 1, 1, return_incomplete=True, shuffle=True, infinite=True)
# this should now not raise a StopIteration anymore
for i in range(1000):
idx = next(dl)
def test_return_incomplete_single_threaded(self):
data = list(range(123))
batch_size = 12
dl = DummyDataLoader(deepcopy(data), batch_size, 1, 1, return_incomplete=False, shuffle=False, infinite=False)
# this should now not raise a StopIteration anymore
total = 0
ctr = 0
for i in dl:
ctr += 1
assert len(i) == batch_size
total += batch_size
self.assertTrue(total == 120)
self.assertTrue(ctr == 10)
dl = DummyDataLoader(deepcopy(data), batch_size, 1, 1, return_incomplete=True, shuffle=False, infinite=False)
# this should now not raise a StopIteration anymore
total = 0
ctr = 0
for i in dl:
ctr += 1
total += len(i)
self.assertTrue(total == 123)
self.assertTrue(ctr == 11)
def test_return_all_indices_multi_threaded_shuffle_False(self):
data = list(range(123))
batch_sizes = [1, 3, 75, 12, 23]
num_workers = 3
for b in batch_sizes:
dl = DummyDataLoader(deepcopy(data), b, num_workers, 1, return_incomplete=True, shuffle=False, infinite=False)
mt = MultiThreadedAugmenter(dl, None, num_workers, 1, None, False)
for _ in range(3):
idx = []
for i in mt:
idx += i
self.assertTrue(len(idx) == len(data))
self.assertTrue(all([i == j for i,j in zip(idx, data)]))
def test_return_all_indices_multi_threaded_shuffle_True(self):
data = list(range(123))
batch_sizes = [1, 3, 75, 12, 23]
num_workers = 3
for b in batch_sizes:
dl = DummyDataLoader(deepcopy(data), b, num_workers, 1, return_incomplete=True, shuffle=True, infinite=False)
mt = MultiThreadedAugmenter(dl, None, num_workers, 1, None, False)
for _ in range(3):
idx = []
for i in mt:
idx += i
self.assertTrue(len(idx) == len(data))
self.assertTrue(not all([i == j for i, j in zip(idx, data)]))
idx.sort()
self.assertTrue(all([i == j for i,j in zip(idx, data)]))
def test_infinite_multi_threaded(self):
data = list(range(123))
num_workers = 3
dl = DummyDataLoader(deepcopy(data), 12, num_workers, 1, return_incomplete=True, shuffle=True, infinite=False)
mt = MultiThreadedAugmenter(dl, None, num_workers, 1, None, False)
# this should raise a StopIteration
with self.assertRaises(StopIteration):
for i in range(1000):
idx = next(mt)
dl = DummyDataLoader(deepcopy(data), 12, num_workers, 1, return_incomplete=True, shuffle=True, infinite=True)
mt = MultiThreadedAugmenter(dl, None, num_workers, 1, None, False)
# this should now not raise a StopIteration anymore
for i in range(1000):
idx = next(mt)
def test_return_incomplete_multi_threaded(self):
data = list(range(123))
batch_size = 12
num_workers = 3
dl = DummyDataLoader(deepcopy(data), batch_size, num_workers, 1, return_incomplete=False, shuffle=False, infinite=False)
mt = MultiThreadedAugmenter(dl, None, num_workers, 1, None, False)
all_return = []
total = 0
ctr = 0
for i in mt:
ctr += 1
assert len(i) == batch_size
total += len(i)
all_return += i
self.assertTrue(total == 120)
self.assertTrue(ctr == 10)
self.assertTrue(len(np.unique(all_return)) == total)
dl = DummyDataLoader(deepcopy(data), batch_size, num_workers, 1, return_incomplete=True, shuffle=False, infinite=False)
mt = MultiThreadedAugmenter(dl, None, num_workers, 1, None, False)
all_return = []
total = 0
ctr = 0
for i in mt:
ctr += 1
total += len(i)
all_return += i
self.assertTrue(total == 123)
self.assertTrue(ctr == 11)
self.assertTrue(len(np.unique(all_return)) == len(data))
def test_thoroughly(self):
data_list = [list(range(123)),
list(range(1243)),
list(range(1)),
list(range(7)),
]
worker_list = (1, 4, 7)
batch_size_list = (1, 3, 32)
seed_list = [318, None]
epochs = 3
for data in data_list:
#print('data', len(data))
for num_workers in worker_list:
#print('num_workers', num_workers)
for batch_size in batch_size_list:
#print('batch_size', batch_size)
for return_incomplete in [True, False]:
#print('return_incomplete', return_incomplete)
for shuffle in [True, False]:
#print('shuffle', shuffle)
for seed_for_shuffle in seed_list:
#print('seed_for_shuffle', seed_for_shuffle)
if return_incomplete:
if len(data) % batch_size == 0:
expected_num_batches = len(data) // batch_size
else:
expected_num_batches = len(data) // batch_size + 1
else:
expected_num_batches = len(data) // batch_size
expected_num_items = len(data) if return_incomplete else expected_num_batches * batch_size
print("init")
dl = DummyDataLoader(deepcopy(data), batch_size, num_workers, seed_for_shuffle,
return_incomplete=return_incomplete, shuffle=shuffle,
infinite=False)
mt = MultiThreadedAugmenter(dl, None, num_workers, 5, None, False, wait_time=0)
mt._start()
for epoch in range(epochs):
print("sampling")
all_return = []
total = 0
ctr = 0
for i in mt:
ctr += 1
total += len(i)
all_return += i
print('asserting')
self.assertTrue(total == expected_num_items)
self.assertTrue(ctr == expected_num_batches)
self.assertTrue(len(np.unique(all_return)) == expected_num_items)
if __name__ == "__main__":
from multiprocessing import freeze_support
freeze_support()
unittest.main()