MCPcopy Create free account
hub / github.com/PeizeSun/SparseR-CNN / MapDataset

Class MapDataset

detectron2/data/common.py:16–59  ·  view source on GitHub ↗

Map a function over the elements in a dataset. Args: dataset: a dataset where map function is applied. map_func: a callable which maps the element in dataset. map_func is responsible for error handling, when error happens, it needs to return None so

Source from the content-addressed store, hash-verified

14
15
16class MapDataset(data.Dataset):
17 """
18 Map a function over the elements in a dataset.
19
20 Args:
21 dataset: a dataset where map function is applied.
22 map_func: a callable which maps the element in dataset. map_func is
23 responsible for error handling, when error happens, it needs to
24 return None so the MapDataset will randomly use other
25 elements from the dataset.
26 """
27
28 def __init__(self, dataset, map_func):
29 self._dataset = dataset
30 self._map_func = PicklableWrapper(map_func) # wrap so that a lambda will work
31
32 self._rng = random.Random(42)
33 self._fallback_candidates = set(range(len(dataset)))
34
35 def __len__(self):
36 return len(self._dataset)
37
38 def __getitem__(self, idx):
39 retry_count = 0
40 cur_idx = int(idx)
41
42 while True:
43 data = self._map_func(self._dataset[cur_idx])
44 if data is not None:
45 self._fallback_candidates.add(cur_idx)
46 return data
47
48 # _map_func fails for this idx, use a random new index from the pool
49 retry_count += 1
50 self._fallback_candidates.discard(cur_idx)
51 cur_idx = self._rng.sample(self._fallback_candidates, k=1)[0]
52
53 if retry_count >= 3:
54 logger = logging.getLogger(__name__)
55 logger.warning(
56 "Failed to apply `_map_func` for idx: {}, retry count: {}".format(
57 idx, retry_count
58 )
59 )
60
61
62class DatasetFromList(data.Dataset):

Callers 2

Calls

no outgoing calls

Tested by

no test coverage detected