Vision inference

Run trained segmentation models on new images and restore the masks to the original image space.

source

save_series_pred

def save_series_pred(
    series_obj, save_dir, val:str='1234'
):

Saves series prediction with updated DICOM UIDs.


source

load_system_resources

def load_system_resources(
    models_path, learner_fn, variables_fn
):

Load necessary resources like learner and variables.


source

load_model_resources

def load_model_resources(
    model_path, device:str='cpu'
):

Load a safe model artifact and its embedded standard inference config.

This is the Safetensors-only counterpart to load_system_resources.


source

predict_from_config

def predict_from_config(
    model, inference_config, input
):

Apply serialized item transforms, execute one input, and decode its output contract.

The input is cast to float32 first, as images are during training.


source

inference_from_config

def inference_from_config(
    model, inference_config, fn:(<class 'str'>, <class 'pathlib.Path'>)='',
    save_path:(<class 'str'>, <class 'pathlib.Path'>)=None, org_img:NoneType=None, input_img:NoneType=None,
    org_size:NoneType=None, label_interpolation:str='shape'
):

Run model-only binary/multiclass segmentation and restore original image space.

The mask is restored with shape-based interpolation; pass label_interpolation=‘nearest’ to copy the nearest voxel instead.


source

inference

def inference(
    learn_inf, apply_reorder, target_spacing, fn:(<class 'str'>, <class 'pathlib.Path'>)='',
    save_path:(<class 'str'>, <class 'pathlib.Path'>)=None, org_img:NoneType=None, input_img:NoneType=None,
    org_size:NoneType=None, label_interpolation:str='shape'
):

Segment an image with an exported learner and restore the mask to the original image space.

Only segmentation learners are supported: the decoded prediction is treated as a mask.

Args: learn_inf: Exported segmentation learner, e.g. from load_learner. apply_reorder: Whether to reorder the image to RAS+, as in training. target_spacing: Voxel spacing used in training, or None to keep the original spacing. fn: Image path, or a list of paths stacked as channels. Not needed when org_img, input_img and org_size are given. save_path: Directory to save the mask in as ‘pred_’. If None, the mask is returned instead. org_img: Original image, as returned by med_img_reader(..., only_tensor=False). input_img: Preprocessed image from the same call. It is cast to float32 before prediction. org_size: Original image shape from the same call. label_interpolation: ‘shape’ (default) restores the mask with shape-based interpolation; ‘nearest’ copies the nearest voxel instead.

Returns: The saved file path if save_path is given, otherwise the mask as a ScalarImage with the original shape, orientation and affine.


source

compute_binary_tumor_volume

def compute_binary_tumor_volume(
    mask_data:Image
):

Compute the volume of the tumor in milliliters (ml).

Post-processing


source

refine_binary_pred_mask

def refine_binary_pred_mask(
    pred_mask, remove_size:(<class 'int'>, <class 'float'>), percentage:float=0.2, verbose:bool=False
)->Tensor:

Removes small objects from the predicted binary mask.

Args: pred_mask: The (already binary) predicted mask to clean up. remove_size: Absolute reference object size in voxels (required, > 0). Objects smaller than remove_size * percentage are removed. percentage: Fraction of remove_size used as the cutoff. Defaults to 0.2. verbose: If True, print the number of components. Defaults to False.

Returns: The processed mask with small objects removed.


source

keep_largest

def keep_largest(
    pred_mask:Tensor
)->Tensor:

Keep only the largest connected component in a binary mask.

Args: pred_mask: Binary prediction mask tensor.

Returns: Binary mask with only the largest connected component.