synapse_net.inference
This submodule implements SynapseNet's segmentation functionality.
def
compute_scale_from_voxel_size(voxel_size: Dict[str, float], model_type: str) -> List[float]:
360def compute_scale_from_voxel_size( 361 voxel_size: Dict[str, float], 362 model_type: str 363) -> List[float]: 364 """Compute the appropriate scale factor for inference with a given pretrained model. 365 366 Args: 367 voxel_size: The voxel size of the data for inference. 368 model_type: The name of the pretrained model. 369 370 Returns: 371 The scale factor, as a list in zyx order, or in yx order for 2D data or a 2D model. 372 """ 373 training_voxel_size = get_model_training_resolution(model_type) 374 axes = "zyx" if len(voxel_size) == 3 and len(training_voxel_size) == 3 else "yx" 375 return [voxel_size[ax] / training_voxel_size[ax] for ax in axes]
Compute the appropriate scale factor for inference with a given pretrained model.
Arguments:
- voxel_size: The voxel size of the data for inference.
- model_type: The name of the pretrained model.
Returns:
The scale factor, as a list in zyx order, or in yx order for 2D data or a 2D model.
def
get_model( model_type: str, device: Union[torch.device, str, NoneType] = None) -> torch.nn.modules.module.Module:
313def get_model(model_type: str, device: Optional[Union[str, torch.device]] = None) -> torch.nn.Module: 314 """Get a pretrained model. 315 316 Args: 317 model_type: The name of the pretrained model, see `get_available_models`. 318 device: The device to use. 319 320 Returns: 321 The model. 322 """ 323 if device is None: 324 device = get_device(device) 325 model_path = get_model_path(model_type) 326 model = _restore_legacy_attributes(torch.load(model_path, weights_only=False)) 327 model.to(device) 328 return model
Get a pretrained model.
Arguments:
- model_type: The name of the pretrained model, see
get_available_models. - device: The device to use.
Returns:
The model.
def
get_segmentation_function(model_type: str) -> Callable:
383def get_segmentation_function(model_type: str) -> Callable: 384 """Get the segmentation function associated with a model type. 385 386 Args: 387 model_type: The name of the pretrained model. 388 389 Returns: 390 The segmentation function used for the model type. 391 392 Raises: 393 ValueError: If the model type is unknown. 394 """ 395 if model_type in _MODELS: 396 return _MODELS[model_type].segmentation 397 # A name that is not in the model registry is resolved by the structure in the name. 398 if model_type.startswith("vesicles"): 399 return segment_vesicles 400 if "cristae" in model_type: 401 return segment_cristae 402 raise ValueError(f"Unknown model type: {model_type}")
Get the segmentation function associated with a model type.
Arguments:
- model_type: The name of the pretrained model.
Returns:
The segmentation function used for the model type.
Raises:
- ValueError: If the model type is unknown.
def
run_segmentation( image: numpy.ndarray, model: torch.nn.modules.module.Module, model_type: str, tiling: Optional[Dict[str, Dict[str, int]]] = None, scale: Optional[List[float]] = None, verbose: bool = False, **kwargs) -> Union[numpy.ndarray, Dict[str, numpy.ndarray]]:
405def run_segmentation( 406 image: np.ndarray, 407 model: torch.nn.Module, 408 model_type: str, 409 tiling: Optional[Dict[str, Dict[str, int]]] = None, 410 scale: Optional[List[float]] = None, 411 verbose: bool = False, 412 **kwargs, 413) -> np.ndarray | Dict[str, np.ndarray]: 414 """Run synaptic structure segmentation. 415 416 Args: 417 image: The input image or image volume. 418 model: The segmentation model. 419 model_type: The model type. This will determine which segmentation post-processing is used. 420 tiling: The tiling settings for inference. By default, the tiling is derived from the patch shape 421 that was used for training the model. 422 scale: A scale factor for resizing the input before applying the model. 423 The output will be scaled back to the initial size. 424 verbose: Whether to print detailed information about the prediction and segmentation. 425 kwargs: Optional parameters for the segmentation function. 426 427 Returns: 428 The segmentation. For models that return multiple segmentations, this function returns a dictionary. 429 """ 430 if tiling is None: 431 tiling = get_default_tiling(patch_shape=get_model_training_patch_shape(model_type)) 432 segmentation_function = get_segmentation_function(model_type) 433 if segmentation_function is segment_cristae: 434 training_resolution = get_model_training_resolution(model_type) 435 voxel_size = np.mean(list(training_resolution.values())) 436 segmentation = segmentation_function( 437 image, model=model, tiling=tiling, scale=scale, verbose=verbose, voxel_size=voxel_size, **kwargs 438 ) 439 else: 440 segmentation = segmentation_function( 441 image, model=model, tiling=tiling, scale=scale, verbose=verbose, **kwargs 442 ) 443 return segmentation
Run synaptic structure segmentation.
Arguments:
- image: The input image or image volume.
- model: The segmentation model.
- model_type: The model type. This will determine which segmentation post-processing is used.
- tiling: The tiling settings for inference. By default, the tiling is derived from the patch shape that was used for training the model.
- scale: A scale factor for resizing the input before applying the model. The output will be scaled back to the initial size.
- verbose: Whether to print detailed information about the prediction and segmentation.
- kwargs: Optional parameters for the segmentation function.
Returns:
The segmentation. For models that return multiple segmentations, this function returns a dictionary.