-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
21 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
from typing import Union | ||
|
||
import torch | ||
|
||
from bioimageio.core.model_adapters._pytorch_model_adapter import PytorchModelAdapter | ||
from bioimageio.spec.model import v0_4, v0_5 | ||
from bioimageio.spec.utils import download | ||
|
||
try: | ||
import torch | ||
except ImportError: | ||
torch = None | ||
|
||
|
||
# additional convenience for pytorch state dict, eventually we want this in python-bioimageio too | ||
# and for each weight format | ||
def load_torch_model( # pyright: ignore[reportUnknownParameterType] | ||
node: Union[v0_4.PytorchStateDictWeightsDescr, v0_5.PytorchStateDictWeightsDescr], | ||
): | ||
assert torch is not None | ||
model = ( # pyright: ignore[reportUnknownVariableType] | ||
PytorchModelAdapter.get_network(node) | ||
) | ||
state = torch.load( # pyright: ignore[reportUnknownVariableType] | ||
download(node.source).path, map_location="cpu" | ||
) | ||
state = torch.load(download(node.source).path, map_location="cpu") | ||
model.load_state_dict(state) # FIXME: check incompatible keys? | ||
return model.eval() # pyright: ignore[reportUnknownVariableType] |