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

Function to_tensor

mogen/datasets/pipelines/formatting.py:11–31  ·  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

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

Callers 1

__call__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected