(sample, error_str="Unsupported callback return type. Got: `{}`.")
| 119 | |
| 120 | |
| 121 | def sample_to_numpy(sample, error_str="Unsupported callback return type. Got: `{}`."): |
| 122 | import_numpy() |
| 123 | assert_cpu_sample_data_type(sample, error_str) |
| 124 | if isinstance(sample, np.ndarray): |
| 125 | return sample |
| 126 | if types._is_mxnet_array(sample): |
| 127 | if sample.context.device_type != "cpu": |
| 128 | raise TypeError( |
| 129 | "Unsupported callback return type. " |
| 130 | "GPU tensors are not supported. Got an MXNet GPU tensor." |
| 131 | ) |
| 132 | return sample.asnumpy() |
| 133 | if types._is_torch_tensor(sample): |
| 134 | if sample.device.type != "cpu": |
| 135 | raise TypeError( |
| 136 | "Unsupported callback return type. " |
| 137 | "GPU tensors are not supported. Got a PyTorch GPU tensor." |
| 138 | ) |
| 139 | return sample.numpy() |
| 140 | elif isinstance(sample, tensors.TensorCPU): |
| 141 | return np.array(sample) |
| 142 | raise TypeError(error_str.format(type(sample))) |
| 143 | |
| 144 | |
| 145 | def batch_to_numpy( |
no test coverage detected