-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
295c10d
commit 2843465
Showing
8 changed files
with
168 additions
and
10 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
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
24 changes: 24 additions & 0 deletions
24
tests/unit/mazepa_layer_processing/segmentation/test_watershed.py
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import pytest | ||
import torch | ||
|
||
from zetta_utils.mazepa_layer_processing.segmentation import watershed | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"fragments_in_xy", | ||
[False, True], | ||
) | ||
def test_ws_dummy_data_lsd(fragments_in_xy): | ||
affs = torch.zeros(3, 8, 8, 8) | ||
ret = watershed.watershed_from_affinities(affs, method="lsd", fragments_in_xy=fragments_in_xy) | ||
assert ret.shape == (1, 8, 8, 8) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"size_threshold", | ||
[0, 200], | ||
) | ||
def test_ws_dummy_data_abiss(size_threshold): | ||
affs = torch.zeros(3, 8, 8, 8) | ||
ret = watershed.watershed_from_affinities(affs, method="abiss", size_threshold=size_threshold) | ||
assert ret.shape == (1, 8, 8, 8) |
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 +1,2 @@ | ||
from . import masks | ||
from . import masks, watershed | ||
from zetta_utils.internal import segmentation |
114 changes: 114 additions & 0 deletions
114
zetta_utils/mazepa_layer_processing/segmentation/watershed.py
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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Literal | ||
|
||
import abiss | ||
import einops | ||
import numpy as np | ||
import torch | ||
from lsd.post.fragments import watershed_from_affinities as _watershed_lsd | ||
|
||
from zetta_utils import builder | ||
|
||
|
||
def _run_watershed_abiss( | ||
affs: torch.Tensor, | ||
aff_threshold_low: float = 0.01, | ||
aff_threshold_high: float = 0.99, | ||
size_threshold: int = 0, | ||
# agglomeration_threshold: float = 0.0, | ||
) -> torch.Tensor: | ||
""" | ||
Args: | ||
affs: | ||
Affinity tensor in float32 with values [0.0, 1.0]. | ||
aff_threshold_low, aff_threshold_high: | ||
Low and high watershed thresholds. | ||
size_threshold: | ||
If greater than 0, perform single-linkage merging as a subsequent | ||
step. | ||
agglomeration_threshold: | ||
If greater than 0.0, perform agglomeration as a subsequent | ||
step with this threshold. | ||
""" | ||
affs = torch.nn.functional.pad(affs, (1, 1, 1, 1, 1, 1)) # abiss requires 1px padding | ||
affs = einops.rearrange(affs, "C X Y Z -> X Y Z C") # channel last | ||
ret = abiss.watershed( | ||
affs=affs.numpy(), | ||
aff_threshold_low=aff_threshold_low, | ||
aff_threshold_high=aff_threshold_high, | ||
size_threshold=size_threshold, | ||
# agglomeration_threshold=agglomeration_threshold, | ||
) | ||
ret = ret[1:-1, 1:-1, 1:-1] | ||
ret = np.expand_dims(ret, axis=0) | ||
return ret | ||
|
||
|
||
def _run_watershed_lsd( | ||
affs: torch.Tensor, | ||
fragments_in_xy: bool = False, | ||
min_seed_distance: int = 10, | ||
affs_in_xyz: bool = True, | ||
) -> torch.Tensor: | ||
""" | ||
Args: | ||
affs: | ||
Affinity tensor in either float32 or uint8. | ||
fragments_in_xy: | ||
Produce supervoxels in xy. | ||
min_seed_distance: | ||
Controls distance between seeds in voxels. | ||
""" | ||
""" | ||
TODO: | ||
- add supervoxel filtering based on average aff value | ||
- add option to also perform agglomeration | ||
""" | ||
affs_np = einops.rearrange(affs, "C X Y Z -> C Z Y X").numpy() | ||
if affs_in_xyz: | ||
# aff needs to be zyx | ||
affs_np = np.flip(affs_np, 0) | ||
if affs_np.dtype == np.uint8: | ||
max_affinity_value = 255.0 | ||
affs_np = affs_np.astype(np.float32) | ||
else: | ||
max_affinity_value = 1.0 | ||
|
||
ret, _ = _watershed_lsd( | ||
affs=affs_np, | ||
max_affinity_value=max_affinity_value, | ||
fragments_in_xy=fragments_in_xy, | ||
min_seed_distance=min_seed_distance, | ||
) | ||
ret = einops.rearrange(ret, "Z Y X -> X Y Z") | ||
ret = np.expand_dims(ret, axis=0) | ||
return ret | ||
|
||
|
||
@builder.register("watershed_from_affinities") | ||
def watershed_from_affinities( | ||
affs: torch.Tensor, # in CXYZ | ||
method: Literal["abiss", "lsd"], | ||
**kwargs, | ||
) -> torch.Tensor: | ||
""" | ||
Produce supervoxels by running watershed on aff data. Optionally perform | ||
agglomeration and output segmentation. | ||
""" | ||
if method == "lsd": | ||
seg = _run_watershed_lsd(affs, **kwargs) | ||
elif method == "abiss": | ||
seg = _run_watershed_abiss(affs, **kwargs) | ||
""" | ||
TODO: write a wrapper for multi-chunk watershed that performs: | ||
- relabel supervoxels based on chunkid & chunk size | ||
- add supervoxel filtering based on mask | ||
- store a list of supervoxels within a chunk to a database | ||
""" | ||
return seg |