(
x: Union[torch.Tensor, np.ndarray, int, float, list, tuple],
accept_none: bool=True)
| 247 | |
| 248 | |
| 249 | def convert_any_to_numpy( |
| 250 | x: Union[torch.Tensor, np.ndarray, int, float, list, tuple], |
| 251 | accept_none: bool=True) -> np.ndarray: |
| 252 | if x is None and accept_none: return None |
| 253 | if x is None and not accept_none: raise ValueError('Trying to convert an empty value.') |
| 254 | if isinstance(x, np.ndarray): return x |
| 255 | elif isinstance(x, int) or isinstance(x, float): return np.array([x, ]) |
| 256 | elif isinstance(x, torch.Tensor): |
| 257 | if x.numel() == 0 and accept_none: return None |
| 258 | if x.numel() == 0 and not accept_none: raise ValueError('Trying to convert an empty value.') |
| 259 | if x.numel() >= 1: return x.detach().cpu().numpy() |
| 260 | elif isinstance(x, list) or isinstance(x, tuple): |
| 261 | return np.array(x) |
| 262 | else: |
| 263 | raise TypeError(f'input value {x}({type(x)}) can not be converted as numpy type.') |
| 264 | |
| 265 | |
| 266 | def convert_any_to_torch_tensor( |
no outgoing calls
no test coverage detected