Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion synapse_net/inference/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .mitochondria import segment_mitochondria
from .ribbon_synapse import segment_ribbon_synapse_structures
from .vesicles import segment_vesicles
from .cristae import segment_cristae
from .util import get_device
from ..file_utils import get_cache_dir

Expand All @@ -25,6 +26,7 @@ def _get_model_registry():
"compartments": "527983720f9eb215c45c4f4493851fd6551810361eda7b79f185a0d304274ee1",
"mitochondria": "24625018a5968b36f39fa9d73b121a32e8f66d0f2c0540d3df2e1e39b3d58186",
"mitochondria2": "553decafaff4838fff6cc8347f22c8db3dee5bcbeffc34ffaec152f8449af673",
"cristae": "f96c90484f4ea92ac0515a06e389cc117580f02c2aacdc44b5828820cf38c3c3",
"ribbon": "7c947f0ddfabe51a41d9d05c0a6ca7d6b238f43df2af8fffed5552d09bb075a9",
"vesicles_2d": "eb0b74f7000a0e6a25b626078e76a9452019f2d1ea6cf2033073656f4f055df1",
"vesicles_3d": "b329ec1f57f305099c984fbb3d7f6ae4b0ff51ec2fa0fa586df52dad6b84cf29",
Expand All @@ -35,6 +37,7 @@ def _get_model_registry():
"compartments": "https://owncloud.gwdg.de/index.php/s/DnFDeTmDDmZrDDX/download",
"mitochondria": "https://owncloud.gwdg.de/index.php/s/1T542uvzfuruahD/download",
"mitochondria2": "https://owncloud.gwdg.de/index.php/s/GZghrXagc54FFXd/download",
"cristae": "https://owncloud.gwdg.de/index.php/s/Df7OUOyQ1Kc2eEO/download",
"ribbon": "https://owncloud.gwdg.de/index.php/s/S3b5l0liPP1XPYA/download",
"vesicles_2d": "https://owncloud.gwdg.de/index.php/s/d72QIvdX6LsgXip/download",
"vesicles_3d": "https://owncloud.gwdg.de/index.php/s/A425mkAOSqePDhx/download",
Expand Down Expand Up @@ -214,14 +217,16 @@ def run_segmentation(
"""
if model_type.startswith("vesicles"):
segmentation = segment_vesicles(image, model=model, tiling=tiling, scale=scale, verbose=verbose, **kwargs)
elif model_type == "mitochondria":
elif model_type == "mitochondria" or model_type == "mitochondria2":
segmentation = segment_mitochondria(image, model=model, tiling=tiling, scale=scale, verbose=verbose, **kwargs)
elif model_type == "active_zone":
segmentation = segment_active_zone(image, model=model, tiling=tiling, scale=scale, verbose=verbose, **kwargs)
elif model_type == "compartments":
segmentation = segment_compartments(image, model=model, tiling=tiling, scale=scale, verbose=verbose, **kwargs)
elif model_type == "ribbon":
segmentation = _segment_ribbon_AZ(image, model=model, tiling=tiling, scale=scale, verbose=verbose, **kwargs)
elif model_type == "cristae":
segmentation = segment_cristae(image, model=model, tiling=tiling, scale=scale, verbose=verbose, **kwargs)
else:
raise ValueError(f"Unknown model type: {model_type}")
return segmentation
8 changes: 6 additions & 2 deletions synapse_net/inference/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import time
import warnings
from glob import glob
from typing import Dict, Optional, Tuple, Union
from typing import Dict, List, Optional, Tuple, Union

# # Suppress annoying import warnings.
# with warnings.catch_warnings():
Expand Down Expand Up @@ -85,6 +85,7 @@ def get_prediction(
model: Optional[torch.nn.Module] = None,
verbose: bool = True,
with_channels: bool = False,
channels_to_normalize: Optional[List[int]] = [0],
Copy link
Contributor

@constantinpape constantinpape Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional implies that this value can be None. What happens if it is None?
I think in this case all channels should be normalized (independently).
I would also set this as the default, and for the cristae model pass channels_to_normalize=[0].

mask: Optional[np.ndarray] = None,
) -> np.ndarray:
"""Run prediction on a given volume.
Expand All @@ -99,6 +100,7 @@ def get_prediction(
tiling: The tiling configuration for the prediction.
verbose: Whether to print timing information.
with_channels: Whether to predict with channels.
channels_to_normalize: List of channels to normalize. Defaults to 0.
mask: Optional binary mask. If given, the prediction will only be run in
the foreground region of the mask.

Expand All @@ -120,8 +122,10 @@ def get_prediction(
# We standardize the data for the whole volume beforehand.
# If we have channels then the standardization is done independently per channel.
if with_channels:
input_volume = input_volume.astype(np.float32, copy=False)
# TODO Check that this is the correct axis.
input_volume = torch_em.transform.raw.standardize(input_volume, axis=(1, 2, 3))
for ch in channels_to_normalize:
input_volume[ch] = torch_em.transform.raw.normalize(input_volume[ch])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave this at normalize.

else:
input_volume = torch_em.transform.raw.standardize(input_volume)

Expand Down
3 changes: 3 additions & 0 deletions synapse_net/tools/segmentation_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ def on_predict(self):
if model_type == "ribbon": # Currently only the ribbon model needs the extra seg.
extra_seg = self._get_layer_selector_data(self.extra_seg_selector_name)
kwargs = {"extra_segmentation": extra_seg}
elif model_type == "cristae": # Cristae model expects 2 3D volumes
image = np.stack([image, self._get_layer_selector_data(self.extra_seg_selector_name)], axis=0)
kwargs = {}
else:
kwargs = {}
segmentation = run_segmentation(
Expand Down