MCPcopy Create free account
hub / github.com/MotrixLab/AiOS / to_tensor

Function to_tensor

detrsmpl/data/datasets/pipelines/formatting.py:12–32  ·  view source on GitHub ↗

Convert objects of various python types to :obj:`torch.Tensor`. Supported types are: :class:`numpy.ndarray`, :class:`torch.Tensor`, :class:`Sequence`, :class:`int` and :class:`float`.

(data)

Source from the content-addressed store, hash-verified

10
11
12def to_tensor(data):
13 """Convert objects of various python types to :obj:`torch.Tensor`.
14
15 Supported types are: :class:`numpy.ndarray`, :class:`torch.Tensor`,
16 :class:`Sequence`, :class:`int` and :class:`float`.
17 """
18 if isinstance(data, torch.Tensor):
19 return data
20 elif isinstance(data, np.ndarray):
21 return torch.from_numpy(data)
22 elif isinstance(data, Sequence) and not mmcv.is_str(data):
23 return torch.tensor(data)
24 elif isinstance(data, int):
25 return torch.LongTensor([data])
26 elif isinstance(data, float):
27 return torch.FloatTensor([data])
28 else:
29 raise TypeError(
30 f'Type {type(data)} cannot be converted to tensor.'
31 'Supported types are: `numpy.ndarray`, `torch.Tensor`, '
32 '`Sequence`, `int` and `float`')
33
34
35@PIPELINES.register_module()

Callers 3

__call__Method · 0.70
__call__Method · 0.70
__call__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected