|
| 1 | +import os |
| 2 | +from typing import Optional, Union |
| 3 | + |
| 4 | +import pooch |
| 5 | +import torch |
| 6 | +from .file_utils import get_cache_dir |
| 7 | + |
| 8 | + |
| 9 | +def _get_default_device(): |
| 10 | + # Check that we're in CI and use the CPU if we are. |
| 11 | + # Otherwise the tests may run out of memory on MAC if MPS is used. |
| 12 | + if os.getenv("GITHUB_ACTIONS") == "true": |
| 13 | + return "cpu" |
| 14 | + # Use cuda enabled gpu if it's available. |
| 15 | + if torch.cuda.is_available(): |
| 16 | + device = "cuda" |
| 17 | + # As second priority use mps. |
| 18 | + # See https://pytorch.org/docs/stable/notes/mps.html for details |
| 19 | + elif torch.backends.mps.is_available() and torch.backends.mps.is_built(): |
| 20 | + device = "mps" |
| 21 | + # Use the CPU as fallback. |
| 22 | + else: |
| 23 | + device = "cpu" |
| 24 | + return device |
| 25 | + |
| 26 | + |
| 27 | +def get_device(device: Optional[Union[str, torch.device]] = None) -> Union[str, torch.device]: |
| 28 | + """Get the torch device. |
| 29 | +
|
| 30 | + If no device is passed the default device for your system is used. |
| 31 | + Else it will be checked if the device you have passed is supported. |
| 32 | +
|
| 33 | + Args: |
| 34 | + device: The input device. |
| 35 | +
|
| 36 | + Returns: |
| 37 | + The device. |
| 38 | + """ |
| 39 | + if device is None or device == "auto": |
| 40 | + device = _get_default_device() |
| 41 | + else: |
| 42 | + device_type = device if isinstance(device, str) else device.type |
| 43 | + if device_type.lower() == "cuda": |
| 44 | + if not torch.cuda.is_available(): |
| 45 | + raise RuntimeError("PyTorch CUDA backend is not available.") |
| 46 | + elif device_type.lower() == "mps": |
| 47 | + if not (torch.backends.mps.is_available() and torch.backends.mps.is_built()): |
| 48 | + raise RuntimeError("PyTorch MPS backend is not available or is not built correctly.") |
| 49 | + elif device_type.lower() == "cpu": |
| 50 | + pass # cpu is always available |
| 51 | + else: |
| 52 | + raise RuntimeError(f"Unsupported device: {device}. Please choose from 'cpu', 'cuda', or 'mps'.") |
| 53 | + return device |
| 54 | + |
| 55 | + |
| 56 | +def get_model_registry() -> None: |
| 57 | + """Get the model registry for downloading pre-trained CochleaNet models. |
| 58 | + """ |
| 59 | + registry = { |
| 60 | + "SGN": "3058690b49015d6210a8e8414eb341c34189fee660b8fac438f1fdc41bdfff98", |
| 61 | + "IHC": "89afbcca08ed302aa6dfbaba5bf2530fc13339c05a604b6f2551d97cf5f12774", |
| 62 | + "Synapses": "2a42712b056f082b4794f15cf41b15678aab0bec1acc922ff9f0dc76abe6747e", |
| 63 | + # TODO |
| 64 | + # "SGN-lowres": "", |
| 65 | + # "IHC-lowres": "", |
| 66 | + } |
| 67 | + urls = { |
| 68 | + "SGN": "https://owncloud.gwdg.de/index.php/s/NZ2vv7hxX1imITG/download", |
| 69 | + "IHC": "https://owncloud.gwdg.de/index.php/s/GBBJkPQFraz1ZzU/download", |
| 70 | + "Synapses": "https://owncloud.gwdg.de/index.php/s/A9W5NmOeBxiyZgY/download", |
| 71 | + # TODO |
| 72 | + # "SGN-lowres": "", |
| 73 | + # "IHC-lowres": "", |
| 74 | + } |
| 75 | + cache_dir = get_cache_dir() |
| 76 | + models = pooch.create( |
| 77 | + path=os.path.join(cache_dir, "models"), |
| 78 | + base_url="", |
| 79 | + registry=registry, |
| 80 | + urls=urls, |
| 81 | + ) |
| 82 | + return models |
| 83 | + |
| 84 | + |
| 85 | +def get_model_path(model_type: str) -> str: |
| 86 | + """Get the local path to a pretrained model. |
| 87 | +
|
| 88 | + Args: |
| 89 | + The model type. |
| 90 | +
|
| 91 | + Returns: |
| 92 | + The local path to the model. |
| 93 | + """ |
| 94 | + model_registry = get_model_registry() |
| 95 | + model_path = model_registry.fetch(model_type) |
| 96 | + return model_path |
| 97 | + |
| 98 | + |
| 99 | +def get_model(model_type: str, device: Optional[Union[str, torch.device]] = None) -> torch.nn.Module: |
| 100 | + """Get the model for a specific segmentation type. |
| 101 | +
|
| 102 | + Args: |
| 103 | + model_type: The model for one of the following segmentation or detection tasks: |
| 104 | + 'SGN', 'IHC', 'Synapses', 'SGN-lowres', 'IHC-lowres'. |
| 105 | + device: The device to use. |
| 106 | +
|
| 107 | + Returns: |
| 108 | + The model. |
| 109 | + """ |
| 110 | + if device is None: |
| 111 | + device = get_device(device) |
| 112 | + model_path = get_model_path(model_type) |
| 113 | + model = torch.load(model_path, weights_only=False) |
| 114 | + model.to(device) |
| 115 | + return model |
0 commit comments