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)
| 10 | |
| 11 | |
| 12 | def 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() |