From b1263f9ceae4dffec299529cfd24da7d6bc51cc4 Mon Sep 17 00:00:00 2001 From: Constantin Pape Date: Wed, 19 Nov 2025 10:40:20 +0100 Subject: [PATCH 1/3] Update SGN model --- development/check_uploaded_models.py | 31 +++++++++++++++++++--------- development/export_models.py | 4 ++-- flamingo_tools/model_utils.py | 4 ++-- flamingo_tools/segmentation/cli.py | 14 +++++++++++-- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/development/check_uploaded_models.py b/development/check_uploaded_models.py index eb18262..a7e79a1 100644 --- a/development/check_uploaded_models.py +++ b/development/check_uploaded_models.py @@ -18,17 +18,23 @@ } -def check_segmentation_model(model_name, checkpoint_path=None): +def check_segmentation_model(model_name, checkpoint_path=None, input_path=None, check_prediction=False, **kwargs): output_folder = f"result_{model_name}" os.makedirs(output_folder, exist_ok=True) - input_path = os.path.join(output_folder, f"{model_name}.tif") - if not os.path.exists(input_path): - data_path = _sample_registry().fetch(data_dict[model_name]) - copyfile(data_path, input_path) + if input_path is None: + input_path = os.path.join(output_folder, f"{model_name}.tif") + if not os.path.exists(input_path): + data_path = _sample_registry().fetch(data_dict[model_name]) + copyfile(data_path, input_path) output_path = os.path.join(output_folder, "segmentation.zarr") if not os.path.exists(output_path): - cmd = ["flamingo_tools.run_segmentation", "-i", input_path, "-o", output_folder, "-m", model_name] + cmd = [ + "flamingo_tools.run_segmentation", "-i", input_path, "-o", output_folder, "-m", model_name, + "--disable_masking", "--min_size", "5", + ] + for name, val in kwargs.items(): + cmd.extend([f"--{name}", str(val)]) if checkpoint_path is not None: cmd.extend(["-c", checkpoint_path]) subprocess.run(cmd) @@ -38,6 +44,9 @@ def check_segmentation_model(model_name, checkpoint_path=None): image = imageio.imread(input_path) v = napari.Viewer() v.add_image(image) + if check_prediction: + prediction = zarr.open(os.path.join(output_folder, "predictions.zarr"))["prediction"][:] + v.add_image(prediction) v.add_labels(segmentation, name=f"{model_name}-segmentation") napari.run() @@ -77,11 +86,13 @@ def main(): # - Prediction works well on the GPU. # check_segmentation_model("IHC") - # TODO: Update model. # SGN segmentation (lowres): - # - Prediction does not work well on the CPU. - # - Prediction does not work well on the GPU. - check_segmentation_model("SGN-lowres", checkpoint_path="SGN-lowres.pt") + # - Prediction works well on the CPU. + # - Prediction works well on the GPU. + check_segmentation_model( + "SGN-lowres", + # boundary_distance_threshold=0.5, center_distance_threshold=None, + ) # IHC segmentation (lowres): # - Prediction works well on the CPU. diff --git a/development/export_models.py b/development/export_models.py index 69a62ec..f4b833d 100644 --- a/development/export_models.py +++ b/development/export_models.py @@ -21,8 +21,8 @@ def export_synapses(): def export_sgn_lowres(): - path = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/trained_models/SGN/cochlea_distance_unet_sgn-low-res-v4" # noqa - model = load_model(path, device="cpu") + path = "/mnt/vast-nhr/projects/nim00007/data/moser/cochlea-lightsheet/trained_models/SGN/cochlea_distance_unet_sgn-low-res-v5" # noqa + model = load_model(path, device="cpu", name="latest") torch.save(model, "SGN-lowres.pt") diff --git a/flamingo_tools/model_utils.py b/flamingo_tools/model_utils.py index c8676fe..64b0f67 100644 --- a/flamingo_tools/model_utils.py +++ b/flamingo_tools/model_utils.py @@ -64,14 +64,14 @@ def get_model_registry() -> None: "SGN": "3058690b49015d6210a8e8414eb341c34189fee660b8fac438f1fdc41bdfff98", "IHC": "752dab7995b076ec4b8526b0539d1b33ade5de9251aaf6863d9bd8cc9cd036b6", "Synapses": "2a42712b056f082b4794f15cf41b15678aab0bec1acc922ff9f0dc76abe6747e", - "SGN-lowres": "6accba4b4c65158fccf25623dcd0fb3b14203305d033a0d443a307114ec5dd8c", + "SGN-lowres": "2c773792f0ef6022c7d052c452071cf7bf45dfce6b498b408ad6cd1cc3a30d35", "IHC-lowres": "537f1d4afc5a582771b87adeccadfa5635e1defd13636702363992188ef5bdbd", } urls = { "SGN": "https://owncloud.gwdg.de/index.php/s/NZ2vv7hxX1imITG/download", "IHC": "https://owncloud.gwdg.de/index.php/s/wB7d2MjV5LRTP06/download", "Synapses": "https://owncloud.gwdg.de/index.php/s/A9W5NmOeBxiyZgY/download", - "SGN-lowres": "https://owncloud.gwdg.de/index.php/s/8hwZjBVzkuYhHLm/download", + "SGN-lowres": "https://owncloud.gwdg.de/index.php/s/OS7985CKaTTBT5g/download", "IHC-lowres": "https://owncloud.gwdg.de/index.php/s/EhnV4brhpvFbSsy/download", } cache_dir = get_cache_dir() diff --git a/flamingo_tools/segmentation/cli.py b/flamingo_tools/segmentation/cli.py index 3dd445b..b971ebc 100644 --- a/flamingo_tools/segmentation/cli.py +++ b/flamingo_tools/segmentation/cli.py @@ -19,6 +19,8 @@ def _parse_kwargs(extra_kwargs, **default_kwargs): def _convert_argval(value): # The values for the parsed arguments need to be in the expected input structure as provided. # i.e. integers and floats should be in their original types. + if value is None or value == "None": + return None try: return int(value) except ValueError: @@ -47,13 +49,21 @@ def _convert_argval(value): def _parse_segmentation_kwargs(extra_kwargs, model_type): - if model_type.startswith("SGN"): + if model_type == "SGN": default_kwargs = { "center_distance_threshold": 0.4, "boundary_distance_threshold": 0.5, "fg_threshold": 0.5, "distance_smoothing": 0.0, - "seg_class": "sgn_low" if model_type == "SGN-lowres" else "sgn", + "seg_class": "sgn", + } + elif model_type == "SGN-lowres": + default_kwargs = { + "center_distance_threshold": None, + "boundary_distance_threshold": 0.5, + "fg_threshold": 0.5, + "distance_smoothing": 0.0, + "seg_class": "sgn_low", } else: assert model_type.startswith("IHC") From 8bf1f74aa74a4e1c4033a9093144f101ddb93fff Mon Sep 17 00:00:00 2001 From: Constantin Pape Date: Wed, 19 Nov 2025 11:02:18 +0100 Subject: [PATCH 2/3] Bump version --- flamingo_tools/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flamingo_tools/version.py b/flamingo_tools/version.py index 0aeeb7e..eed0c7d 100644 --- a/flamingo_tools/version.py +++ b/flamingo_tools/version.py @@ -1,4 +1,4 @@ """@private """ -__version__ = "0.0.1" +__version__ = "0.1.0" From e4e65570eea3b93359d9305f8cc03d6c764a2bc2 Mon Sep 17 00:00:00 2001 From: Constantin Pape Date: Wed, 19 Nov 2025 11:19:07 +0100 Subject: [PATCH 3/3] Update documentation --- doc/documentation.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/doc/documentation.md b/doc/documentation.md index fc4cef0..4d6be1f 100644 --- a/doc/documentation.md +++ b/doc/documentation.md @@ -87,12 +87,19 @@ Use `--file_ext .raw` if the data is stored in raw files. The the output data fo - If you specify `.n5` the data will be exported to the [bdv.n5 format](https://github.com/bigdataviewer/bigdataviewer-core/blob/master/BDV%20N5%20format.md). It can be opened with BigDataViewer via `Plugins->BigDataViewer->Open XML/HDF5` or with [BigStitcher](https://imagej.net/plugins/bigstitcher/) as described [here](https://imagej.net/plugins/bigstitcher/open-existing). - If you specify `.ome.zarr` the data will be exported to the [ome.zarr format](https://ngff.openmicroscopy.org/). -`flamingo_tools.run_segmentation`: To segment cells in volumetric light microscopy data. +`flamingo_tools.run_segmentation`: To segment cells in volumetric light microscopy data. You can use this command as follows: +```bash +flamingo_tools.run_segmentation -i /path/to/data.tif -o /path/to/output_folder -m SGN +``` +Here, `-m` determines which model is used. See also [available models](#available-models). +To use a custom trained model you can use the argument `--checkpoint_path` (`-c`). -`flamingo_tools.run_detection`: To detect synapses in volumetric light microscopy data. +`flamingo_tools.run_detection`: To detect synapses in volumetric light microscopy data. The command is used similarly to `flamingo_tools.run_segmentation`. For more information on any of the command run `flamingo_tools. -h` (e.g. `flamingo_tools.run_segmentation -h`) in your terminal. +We will add additional command line functionality soon. + ### Python Library CochleaNet's functionality is implemented in the `flamingo_tools` python library. It implements: