From 51f731098b873aa8f6aaffbf8ef2f68dbbc74aa1 Mon Sep 17 00:00:00 2001 From: Constantin Pape Date: Tue, 30 Jun 2026 15:28:01 +0200 Subject: [PATCH] Use new inference function and expose batch size as inference parameter --- synapse_net/inference/actin.py | 5 ++++- synapse_net/inference/active_zone.py | 8 +++++++- synapse_net/inference/compartments.py | 7 ++++++- synapse_net/inference/cristae.py | 8 ++++++-- synapse_net/inference/mitochondria.py | 5 ++++- synapse_net/inference/ribbon_synapse.py | 6 +++++- synapse_net/inference/scalable_segmentation.py | 8 +++++++- synapse_net/inference/util.py | 12 ++++++++++-- synapse_net/inference/vesicles.py | 8 +++++++- synapse_net/tools/cli.py | 8 +++++++- 10 files changed, 63 insertions(+), 12 deletions(-) diff --git a/synapse_net/inference/actin.py b/synapse_net/inference/actin.py index 9c956577..8a67bbd1 100644 --- a/synapse_net/inference/actin.py +++ b/synapse_net/inference/actin.py @@ -25,6 +25,7 @@ def segment_actin( mask: Optional[np.ndarray] = None, preprocess: Optional[Callable] = None, dtype: torch.dtype = torch.float32, + batch_size: int = 1, ) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]: """Segment actin in an input volume. @@ -43,6 +44,8 @@ def segment_actin( used during training. By default, torch_em standardization is used. dtype: The dtype for running the model. Pass `torch.bfloat16` to predict with bf16 autocast, which is much faster on GPUs with bf16 support. By default, the model runs in float32. + batch_size: The number of blocks to stack into a single forward pass during prediction. + Larger values can increase GPU throughput at the cost of higher memory usage. Returns: The segmentation mask as a numpy array, or a tuple containing the segmentation mask @@ -60,7 +63,7 @@ def segment_actin( pred = get_prediction( input_volume, model=model, model_path=model_path, - tiling=tiling, verbose=verbose, preprocess=preprocess, dtype=dtype, + tiling=tiling, verbose=verbose, preprocess=preprocess, dtype=dtype, batch_size=batch_size, ) foreground, boundaries = pred[:2] diff --git a/synapse_net/inference/active_zone.py b/synapse_net/inference/active_zone.py index 39590086..bc2c6955 100644 --- a/synapse_net/inference/active_zone.py +++ b/synapse_net/inference/active_zone.py @@ -78,6 +78,7 @@ def segment_active_zone( mask: Optional[np.ndarray] = None, compartment: Optional[np.ndarray] = None, foreground_threshold: float = 0.5, + batch_size: int = 1, ) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]: """Segment active zones in an input volume. @@ -92,6 +93,8 @@ def segment_active_zone( compartment: Pass a compartment segmentation, to intersect the boundaries of the compartments with the active zone prediction. foreground_threshold: The threshold for binarizing the foreground prediction. + batch_size: The number of blocks to stack into a single forward pass during prediction. + Larger values can increase GPU throughput at the cost of higher memory usage. Returns: The foreground mask as a numpy array. @@ -105,7 +108,10 @@ def segment_active_zone( # Rescale the mask if it was given and run prediction. if mask is not None: mask = scaler.scale_input(mask, is_segmentation=True) - pred = get_prediction(input_volume, model_path=model_path, model=model, tiling=tiling, mask=mask, verbose=verbose) + pred = get_prediction( + input_volume, model_path=model_path, model=model, tiling=tiling, mask=mask, verbose=verbose, + batch_size=batch_size, + ) # Run segmentation and rescale the result if necessary. foreground = pred[0] diff --git a/synapse_net/inference/compartments.py b/synapse_net/inference/compartments.py index 639f052a..4b614a97 100644 --- a/synapse_net/inference/compartments.py +++ b/synapse_net/inference/compartments.py @@ -157,6 +157,7 @@ def segment_compartments( n_slices_exclude: int = 0, boundary_threshold: float = 0.4, min_z_extent: int = 10, + batch_size: int = 1, **kwargs, ) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]: """Segment synaptic compartments in an input volume. @@ -172,6 +173,8 @@ def segment_compartments( n_slices_exclude: The number of slices to exclude at the start and end of a 3D volume. boundary_threshold: Threshold used to derive compartment seeds from the boundary prediction. min_z_extent: The minimum number of slices a 3D compartment must span. + batch_size: The number of blocks to stack into a single forward pass during prediction. + Larger values can increase GPU throughput at the cost of higher memory usage. Returns: The segmentation mask as a numpy array, or a tuple containing the segmentation mask @@ -186,7 +189,9 @@ def segment_compartments( # Run prediction. Support models with a single or multiple channels, # assuming that the first channel is the boundary prediction. - pred = get_prediction(input_volume, tiling=tiling, model_path=model_path, model=model, verbose=verbose) + pred = get_prediction( + input_volume, tiling=tiling, model_path=model_path, model=model, verbose=verbose, batch_size=batch_size, + ) # Remove channel axis if necessary. if pred.ndim != input_volume.ndim: diff --git a/synapse_net/inference/cristae.py b/synapse_net/inference/cristae.py index 364409af..cef49acf 100644 --- a/synapse_net/inference/cristae.py +++ b/synapse_net/inference/cristae.py @@ -108,6 +108,7 @@ def segment_cristae( mask: Optional[np.ndarray] = None, foreground_threshold: float = 0.5, erosion_distance_nm: float = 0.0, + batch_size: int = 1, **kwargs ) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]: """Segment cristae in an input volume. @@ -128,6 +129,8 @@ def segment_cristae( erosion_distance_nm: The distance in nanometers used to erode mitochondria instances before restricting the cristae prediction. Set to 0 to disable erosion and rely on the min_size filter to remove small cristae detached on the mitochondria membranes. + batch_size: The number of blocks to stack into a single forward pass during prediction. + Larger values can increase GPU throughput at the cost of higher memory usage. Returns: The segmentation mask as a numpy array, or a tuple containing the segmentation mask @@ -155,7 +158,7 @@ def segment_cristae( erode_voxels = max(0, round(erosion_distance_nm / voxel_size)) # Use the mitochondria segmentation as the prediction mask so that - # predict_with_halo skips tiles with no mito voxels entirely. + # predict_with_halo_pipelined skips tiles with no mito voxels entirely. # Replace with the optional external mask if one is provided. if mask is None: mask = mito_seg > 0 @@ -166,7 +169,8 @@ def segment_cristae( # Run prediction and segmentation. pred = get_prediction( input_volume, model_path=model_path, model=model, mask=mask, - tiling=tiling, with_channels=with_channels, channels_to_standardize=channels_to_standardize, verbose=verbose + tiling=tiling, with_channels=with_channels, channels_to_standardize=channels_to_standardize, verbose=verbose, + batch_size=batch_size, ) foreground, boundaries = pred[:2] seg = _run_segmentation( diff --git a/synapse_net/inference/mitochondria.py b/synapse_net/inference/mitochondria.py index 792caaea..e35050bf 100644 --- a/synapse_net/inference/mitochondria.py +++ b/synapse_net/inference/mitochondria.py @@ -68,6 +68,7 @@ def segment_mitochondria( boundary_threshold: float = 0.25, area_threshold: int = 5000, preprocess: Callable = None, + batch_size: int = 1, ) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]: """Segment mitochondria in an input volume. @@ -88,6 +89,8 @@ def segment_mitochondria( boundary_threshold: The boundary threshold distance calculation. area_threshold: The maximum area (in pixels) of holes to be removed or filled in the segmentation. This parameter is passed to `skimage.morphology.remove_small_holes`. + batch_size: The number of blocks to stack into a single forward pass during prediction. + Larger values can increase GPU throughput at the cost of higher memory usage. Returns: The segmentation mask as a numpy array, or a tuple containing the segmentation mask @@ -104,7 +107,7 @@ def segment_mitochondria( mask = scaler.scale_input(mask, is_segmentation=True) pred = get_prediction( input_volume, model_path=model_path, model=model, tiling=tiling, mask=mask, verbose=verbose, - preprocess=preprocess + preprocess=preprocess, batch_size=batch_size, ) # Run segmentation and rescale the result if necessary. diff --git a/synapse_net/inference/ribbon_synapse.py b/synapse_net/inference/ribbon_synapse.py index 0d26d636..547c1683 100644 --- a/synapse_net/inference/ribbon_synapse.py +++ b/synapse_net/inference/ribbon_synapse.py @@ -16,6 +16,7 @@ def segment_ribbon_synapse_structures( threshold: Optional[Union[float, Dict[str, float]]] = None, scale: Optional[Sequence[float]] = None, mask: Optional[np.ndarray] = None, + batch_size: int = 1, ) -> np.ndarray: """Segment ribbon synapse structures. @@ -30,6 +31,8 @@ def segment_ribbon_synapse_structures( threshold: The threshold for binarizing predictions. scale: The scale factor to use for rescaling the input volume before prediction. mask: An optional mask that is used to restrict the segmentation. + batch_size: The number of blocks to stack into a single forward pass during prediction. + Larger values can increase GPU throughput at the cost of higher memory usage. Returns: The segmentation mask as a numpy array, or a tuple containing the segmentation mask @@ -44,7 +47,8 @@ def segment_ribbon_synapse_structures( if mask is not None: mask = scaler.scale_input(mask, is_segmentation=True) predictions = get_prediction( - input_volume, model_path=model_path, model=model, tiling=tiling, mask=mask, verbose=verbose + input_volume, model_path=model_path, model=model, tiling=tiling, mask=mask, verbose=verbose, + batch_size=batch_size, ) assert len(structure_names) == predictions.shape[0] diff --git a/synapse_net/inference/scalable_segmentation.py b/synapse_net/inference/scalable_segmentation.py index 2a0bdc9c..908912f3 100644 --- a/synapse_net/inference/scalable_segmentation.py +++ b/synapse_net/inference/scalable_segmentation.py @@ -80,6 +80,7 @@ def scalable_segmentation( verbose: bool = True, mask: Optional[ArrayLike] = None, devices: Optional[List[str]] = None, + batch_size: int = 1, ) -> None: """Run segmentation based on a prediction with foreground and boundary channel. @@ -103,6 +104,8 @@ def scalable_segmentation( verbose: Whether to print timing information. devices: The devices for running prediction. If not given will use the GPU if available, otherwise the CPU. + batch_size: The number of blocks to stack into a single forward pass during prediction. + Larger values can increase GPU throughput at the cost of higher memory usage. """ if mask is not None: raise NotImplementedError @@ -136,5 +139,8 @@ def scalable_segmentation( seeds = f.create_dataset("seeds", shape=input_.shape, dtype="uint64", chunks=chunks) # Run prediction and segmentation. - get_prediction(input_, prediction=prediction, tiling=tiling, model=model, verbose=verbose, devices=devices) + get_prediction( + input_, prediction=prediction, tiling=tiling, model=model, verbose=verbose, devices=devices, + batch_size=batch_size, + ) _run_segmentation(prediction, output, seeds, chunks, seed_threshold, min_size, verbose, original_shape) diff --git a/synapse_net/inference/util.py b/synapse_net/inference/util.py index 7d45bd0a..4baab2f2 100644 --- a/synapse_net/inference/util.py +++ b/synapse_net/inference/util.py @@ -25,7 +25,7 @@ from skimage.transform import rescale, resize from torch_em.model.unet import Decoder from torch_em.model.unetr import UNETRBase -from torch_em.util.prediction import predict_with_halo +from torch_em.util.prediction import predict_with_halo_pipelined from tqdm import tqdm @@ -138,6 +138,7 @@ def get_prediction( devices: Optional[List[str]] = None, preprocess: Optional[callable] = None, dtype: torch.dtype = torch.float32, + batch_size: int = 1, ) -> ArrayLike: """Run prediction on a given volume. @@ -164,6 +165,8 @@ def get_prediction( standardization is used. dtype: The dtype for running the model. Pass `torch.bfloat16` to predict with bf16 autocast, which is much faster on GPUs with bf16 support. By default, the model runs in float32. + batch_size: The number of blocks to stack into a single forward pass. + Larger values can increase GPU throughput at the cost of higher memory usage. Returns: The predicted volume. @@ -218,6 +221,7 @@ def get_prediction( prediction = get_prediction_torch_em( input_volume, updated_tiling, model_path, model, verbose, with_channels, mask=mask, prediction=prediction, devices=devices, preprocess=preprocess, dtype=dtype, + batch_size=batch_size, ) return prediction @@ -259,6 +263,7 @@ def get_prediction_torch_em( devices: Optional[List[str]] = None, preprocess: Optional[callable] = None, dtype: torch.dtype = torch.float32, + batch_size: int = 1, ) -> np.ndarray: """Run prediction using torch-em on a given volume. @@ -280,6 +285,8 @@ def get_prediction_torch_em( Applied to the whole volume if the input is np.ndarray, otherwise applied per block. dtype: The dtype for running the model. Pass `torch.bfloat16` to predict with bf16 autocast, which is much faster on GPUs with bf16 support. By default, the model runs in float32. + batch_size: The number of blocks to stack into a single forward pass. + Larger values can increase GPU throughput at the cost of higher memory usage. Returns: The predicted volume. @@ -326,11 +333,12 @@ def predict_with_autocast(net, inp): prediction_function = None if dtype == torch.float32 else predict_with_autocast - prediction = predict_with_halo( + prediction = predict_with_halo_pipelined( input_volume, model, gpu_ids=devices, block_shape=block_shape, halo=halo, preprocess=preprocess, with_channels=with_channels, mask=mask, output=prediction, prediction_function=prediction_function, + batch_size=batch_size, ) if verbose: print("Prediction time in", time.time() - t0, "s") diff --git a/synapse_net/inference/vesicles.py b/synapse_net/inference/vesicles.py index 133cbc8d..144e5231 100644 --- a/synapse_net/inference/vesicles.py +++ b/synapse_net/inference/vesicles.py @@ -174,6 +174,7 @@ def segment_vesicles( exclude_boundary: bool = False, exclude_boundary_vesicles: bool = False, mask: Optional[np.ndarray] = None, + batch_size: int = 1, ) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]: """Segment vesicles in an input volume or image. @@ -198,6 +199,8 @@ def segment_vesicles( out less vesicles at the boundary and is better suited for volumes with small context in z. If `exclude_boundary` is also set to True, then this option will have no effect. mask: An optional mask that is used to restrict the segmentation. + batch_size: The number of blocks to stack into a single forward pass during prediction. + Larger values can increase GPU throughput at the cost of higher memory usage. Returns: The segmentation mask as a numpy array, or a tuple containing the segmentation mask @@ -220,7 +223,10 @@ def segment_vesicles( # Rescale the mask if it was given and run prediction. if mask is not None: mask = scaler.scale_input(mask, is_segmentation=True) - pred = get_prediction(input_volume, tiling=tiling, model_path=model_path, model=model, verbose=verbose, mask=mask) + pred = get_prediction( + input_volume, tiling=tiling, model_path=model_path, model=model, verbose=verbose, mask=mask, + batch_size=batch_size, + ) foreground, boundaries = pred[:2] # Deal with 2D segmentation case. diff --git a/synapse_net/tools/cli.py b/synapse_net/tools/cli.py index e9b6e7e3..7411b6f6 100644 --- a/synapse_net/tools/cli.py +++ b/synapse_net/tools/cli.py @@ -172,6 +172,11 @@ def segmentation_cli(): "--halo", type=int, nargs=3, help="The halo for prediction, in ZYX order. Increase the halo to minimize boundary artifacts." ) + parser.add_argument( + "--batch_size", type=int, default=1, + help="The number of blocks to stack into a single forward pass during prediction. " + "Larger values can increase GPU throughput at the cost of higher memory usage." + ) parser.add_argument( "--data_ext", default=".mrc", help="The extension of the tomogram data. By default .mrc." ) @@ -244,13 +249,14 @@ def segmentation_cli(): f"vesicles, mitochondria, or active zones, not for {args.model}." ) segmentation_function = partial( - scalable_segmentation, model=model, tiling=tiling, verbose=args.verbose + scalable_segmentation, model=model, tiling=tiling, verbose=args.verbose, batch_size=args.batch_size ) allocate_output = True else: segmentation_function = partial( run_segmentation, model=model, model_type=args.model, verbose=args.verbose, tiling=tiling, + batch_size=args.batch_size, ) allocate_output = False