Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion synapse_net/inference/actin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -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]

Expand Down
8 changes: 7 additions & 1 deletion synapse_net/inference/active_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
Expand All @@ -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]
Expand Down
7 changes: 6 additions & 1 deletion synapse_net/inference/compartments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions synapse_net/inference/cristae.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion synapse_net/inference/mitochondria.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion synapse_net/inference/ribbon_synapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -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]

Expand Down
8 changes: 7 additions & 1 deletion synapse_net/inference/scalable_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand Down Expand Up @@ -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)
12 changes: 10 additions & 2 deletions synapse_net/inference/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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.

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand All @@ -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.
Expand Down Expand Up @@ -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")
Expand Down
8 changes: 7 additions & 1 deletion synapse_net/inference/vesicles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -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.
Expand Down
8 changes: 7 additions & 1 deletion synapse_net/tools/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
)
Expand Down Expand Up @@ -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

Expand Down
Loading