Download scripts/utils.py from monai-test/lung_nodule_ct_detection: direct link, hf CLI and curl.
- Browser
- Download file 705 Bytes
-
https://huggingface.co/monai-test/lung_nodule_ct_detection/resolve/main/scripts/utils.py
- Command line
-
hf download hf://monai-test/lung_nodule_ct_detection/scripts/utils.py
-
curl -L -o utils.py https://huggingface.co/monai-test/lung_nodule_ct_detection/resolve/main/scripts/utils.py
705 Bytes
| from typing import Dict, List, Union | |
| import numpy as np | |
| import torch | |
| def detach_to_numpy(data: Union[List, Dict, torch.Tensor]) -> Union[List, Dict, torch.Tensor]: | |
| """ | |
| Recursively detach elements in data | |
| """ | |
| if isinstance(data, torch.Tensor): | |
| return data.cpu().detach().numpy() # pytype: disable=attribute-error | |
| elif isinstance(data, np.ndarray): | |
| return data | |
| elif isinstance(data, list): | |
| return [detach_to_numpy(d) for d in data] | |
| elif isinstance(data, dict): | |
| for k in data.keys(): | |
| data[k] = detach_to_numpy(data[k]) | |
| return data | |
| else: | |
| raise ValueError("data should be tensor, numpy array, dict, or list.") | |