Skip to content

Commit 4f03f00

Browse files
authored
added dynamic erosion voxel size based in input data voxel size (#164)
* added dynamic erosion voxel size based in input data voxel size * increased min_size to 2000 pixels and increased erosion size of mitochondira from 6nm to 10nm * added comments for mitochondria erosion logic in cristae segment function * use average voxel size instead of just one value
1 parent 97acb6f commit 4f03f00

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

synapse_net/inference/cristae.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def _erode_instances(mito_data, erode_voxels, verbose):
1818
"""Erodes instances globally and returns a memory-efficient boolean mask."""
1919
if verbose:
2020
t_erode = time.time()
21-
print("Eroding mitochondria instances globally...")
21+
print(f"Eroding mitochondria instances globally by {erode_voxels} voxels ...")
2222

2323
footprint = ball(erode_voxels)
2424
props = regionprops(mito_data)
@@ -95,10 +95,11 @@ def threshold_block(block):
9595

9696
def segment_cristae(
9797
input_volume: np.ndarray,
98+
voxel_size: float,
9899
model_path: Optional[str] = None,
99100
model: Optional[torch.nn.Module] = None,
100101
tiling: Optional[Dict[str, Dict[str, int]]] = None,
101-
min_size: int = 500,
102+
min_size: int = 2000,
102103
verbose: bool = True,
103104
distance_based_segmentation: bool = False,
104105
return_predictions: bool = False,
@@ -110,6 +111,7 @@ def segment_cristae(
110111
111112
Args:
112113
input_volume: The input volume to segment. Expects 2 3D volumes: raw and mitochondria
114+
voxel_size: The voxel size of the model's training data.
113115
model_path: The path to the model checkpoint if `model` is not provided.
114116
model: Pre-loaded model. Either `model_path` or `model` is required.
115117
tiling: The tiling configuration for the prediction.
@@ -143,6 +145,10 @@ def segment_cristae(
143145
mito_seg = scaler.scale_input(mitochondria, is_segmentation=True)
144146
input_volume = np.stack([volume, mito_seg], axis=0)
145147

148+
# target 10nm erosion for mitochondria
149+
# voxel_size is the model's training voxel size, which is the space we erode in
150+
erode_voxels = max(1, round(10.0 / voxel_size))
151+
146152
# Run prediction and segmentation.
147153
if mask is not None:
148154
mask = scaler.scale_input(mask, is_segmentation=True)
@@ -151,7 +157,8 @@ def segment_cristae(
151157
tiling=tiling, with_channels=with_channels, channels_to_standardize=channels_to_standardize, verbose=verbose
152158
)
153159
foreground, boundaries = pred[:2]
154-
seg = _run_segmentation(foreground, verbose=verbose, min_size=min_size, mito_seg=mito_seg)
160+
seg = _run_segmentation(foreground, verbose=verbose, min_size=min_size, mito_seg=mito_seg,
161+
erode_voxels=erode_voxels)
155162
seg = scaler.rescale_output(seg, is_segmentation=True)
156163

157164
if return_predictions:

synapse_net/inference/inference.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,10 @@ def run_segmentation(
261261
elif model_type == "ribbon":
262262
segmentation = _segment_ribbon_AZ(image, model=model, tiling=tiling, scale=scale, verbose=verbose, **kwargs)
263263
elif model_type == "cristae" or model_type == "cristae2" or model_type == "cristae3":
264-
segmentation = segment_cristae(image, model=model, tiling=tiling, scale=scale, verbose=verbose, **kwargs)
264+
training_resolution = get_model_training_resolution(model_type)
265+
voxel_size = np.mean(list(training_resolution.values()))
266+
segmentation = segment_cristae(image, model=model, tiling=tiling, scale=scale, verbose=verbose,
267+
voxel_size=voxel_size, **kwargs)
265268
else:
266269
raise ValueError(f"Unknown model type: {model_type}")
267270
return segmentation

0 commit comments

Comments
 (0)