-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ Add Cellpose value transform.
- Loading branch information
1 parent
8dd846d
commit ca76c7f
Showing
3 changed files
with
33 additions
and
5 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,2 +1,30 @@ | ||
from cellpose.dynamics import masks_to_flows_gpu_3d | ||
from cellpose.dynamics import masks_to_flows_gpu_3d, masks_to_flows | ||
from cellpose.dynamics import masks_to_flows_gpu as masks_to_flows_gpu_2d | ||
import torch | ||
|
||
|
||
class CellposeFlow: | ||
def __init__(self, ndim: int, device: str | None = None): | ||
self.ndim = ndim | ||
if device is None: | ||
if torch.cuda.is_available(): | ||
device = "cuda" | ||
elif torch.backends.mps.is_available(): | ||
device = "mps" | ||
else: | ||
device = "cpu" | ||
_device = torch.device(device) | ||
if device == "cuda" or device == "mps": | ||
if ndim == 3: | ||
flows_func = lambda x: masks_to_flows_gpu_3d(x, device=_device) | ||
elif ndim == 2: | ||
flows_func = lambda x: masks_to_flows_gpu_2d(x, device=_device) | ||
else: | ||
raise ValueError(f"Unsupported dimension {ndim}") | ||
else: | ||
flows_func = lambda x: masks_to_flows(x, device=_device) | ||
self.flows_func = flows_func | ||
self.device = _device | ||
|
||
def __call__(self, masks): | ||
return self.flows_func(masks) |