Apply `func(tensor)` to any tensors in the list or dict. Args: func: function that takes a tensor and outputs a tensor arr: list of tensor, dict of tensor Returns: list or dict containing the output tensors
(
func: T.Callable[[torch.Tensor], torch.Tensor],
arr: T.Union[torch.Tensor, T.List[torch.Tensor], T.Dict[T.Any, torch.Tensor]],
)
| 8 | |
| 9 | |
| 10 | def tensorfun( |
| 11 | func: T.Callable[[torch.Tensor], torch.Tensor], |
| 12 | arr: T.Union[torch.Tensor, T.List[torch.Tensor], T.Dict[T.Any, torch.Tensor]], |
| 13 | ) -> T.Union[torch.Tensor, T.List[torch.Tensor], T.Dict[T.Any, torch.Tensor], None]: |
| 14 | """ |
| 15 | Apply `func(tensor)` to any tensors in the list or dict. |
| 16 | |
| 17 | Args: |
| 18 | func: |
| 19 | function that takes a tensor and outputs a tensor |
| 20 | arr: |
| 21 | list of tensor, dict of tensor |
| 22 | |
| 23 | Returns: |
| 24 | list or dict containing the output tensors |
| 25 | """ |
| 26 | |
| 27 | if arr is None: |
| 28 | return None |
| 29 | |
| 30 | if isinstance(arr, torch.Tensor): |
| 31 | return func(arr) |
| 32 | |
| 33 | if isinstance(arr, (list, tuple)): |
| 34 | return [tensorfun(func, a) for a in arr] |
| 35 | |
| 36 | if isinstance(arr, dict): |
| 37 | out_dict = dict() |
| 38 | for key in arr: |
| 39 | out_dict[key] = tensorfun(func, arr[key]) |
| 40 | return out_dict |
| 41 | |
| 42 | raise RuntimeError(f'Not supported type: {type(arr)}, {arr}') |
| 43 | |
| 44 | |
| 45 | def random_crop( |
nothing calls this directly
no outgoing calls
no test coverage detected