Convert a ``numpy.ndarray or Image`` to tensor. See ``ToTensor`` for more details. Args: inputs (numpy.ndarray or Image): Image to be converted to tensor. Returns: Tensor: Converted image.
| 52 | |
| 53 | |
| 54 | class ToTensor(object): |
| 55 | """Convert a ``numpy.ndarray or Image`` to tensor. |
| 56 | |
| 57 | See ``ToTensor`` for more details. |
| 58 | |
| 59 | Args: |
| 60 | inputs (numpy.ndarray or Image): Image to be converted to tensor. |
| 61 | |
| 62 | Returns: |
| 63 | Tensor: Converted image. |
| 64 | """ |
| 65 | def __call__(self, inputs): |
| 66 | if isinstance(inputs, Image.Image): |
| 67 | channels = len(inputs.mode) |
| 68 | inputs = np.array(inputs) |
| 69 | inputs = inputs.reshape(inputs.shape[0], inputs.shape[1], channels) |
| 70 | inputs = torch.from_numpy(inputs.transpose(2, 0, 1)) |
| 71 | else: |
| 72 | inputs = torch.from_numpy(inputs.transpose(2, 0, 1)) |
| 73 | |
| 74 | return inputs.float() |
| 75 | |
| 76 | |
| 77 | class ToLabel(object): |