MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / apply

Method apply

imperative/python/megengine/data/collator.py:33–64  ·  view source on GitHub ↗
(self, inputs)

Source from the content-addressed store, hash-verified

31 """
32
33 def apply(self, inputs):
34 elem = inputs[0]
35 elem_type = type(elem)
36 if (
37 elem_type.__module__ == "numpy"
38 and elem_type.__name__ != "str_"
39 and elem_type.__name__ != "string_"
40 ):
41 elem = inputs[0]
42 if elem_type.__name__ == "ndarray":
43 # array of string classes and object
44 if np_str_obj_array_pattern.search(elem.dtype.str) is not None:
45 raise TypeError(default_collate_err_msg_format.format(elem.dtype))
46
47 return np.ascontiguousarray(np.stack(inputs))
48 elif elem.shape == (): # scalars
49 return np.array(inputs)
50 elif isinstance(elem, float):
51 return np.array(inputs, dtype=np.float64)
52 elif isinstance(elem, int):
53 return np.array(inputs)
54 elif isinstance(elem, (str, bytes)):
55 return inputs
56 elif isinstance(elem, collections.abc.Mapping):
57 return {key: self.apply([d[key] for d in inputs]) for key in elem}
58 elif isinstance(elem, tuple) and hasattr(elem, "_fields"): # namedtuple
59 return elem_type(*(self.apply(samples) for samples in zip(*inputs)))
60 elif isinstance(elem, collections.abc.Sequence):
61 transposed = zip(*inputs)
62 return [self.apply(samples) for samples in transposed]
63
64 raise TypeError(default_collate_err_msg_format.format(elem_type))

Callers 4

_process_dataMethod · 0.45
_get_next_batchMethod · 0.45
_get_next_batchMethod · 0.45
map_fetcherFunction · 0.45

Calls 3

searchMethod · 0.80
arrayMethod · 0.80
formatMethod · 0.45

Tested by

no test coverage detected