inference.swinunetr.runner
SwinUNETR Inference Runner
Provides functionalities related to running the SwinUNETR inference on the input data.
1""" 2### SwinUNETR Inference Runner 3 4Provides functionalities related to running the SwinUNETR inference on the input data. 5""" 6 7 8import subprocess 9from pathlib import Path 10import torch 11from typing import Any, List 12 13 14def run_infer_swinunetr( 15 input_path: Path, 16 output_folder: Path, 17 challenge: Any, 18 folds: List[int] = [0, 1, 2, 3, 4] 19) -> List[Path]: 20 """Run SwinUNETR inference on the provided input data. 21 22 This function executes the SwinUNETR inference script for each specified fold, 23 using the appropriate pretrained model weights, and collects the paths to the 24 resulting probability npz files. 25 26 Args: 27 input_path (Path): Path to the folder containing the 4 input files. 28 output_folder (Path): Path to the folder to store the npz files. 29 challenge (Any): The challenge identifier or related parameter. 30 folds (List[int], optional): List of fold indices to process. Defaults to [0, 1, 2, 3, 4]. 31 32 Returns: 33 List[Path]: List of paths to the npz files generated by inference. 34 """ 35 pretrained_name = 'best_model.pt' 36 npz_folder_list: List[Path] = [] 37 38 for fold in folds: 39 # Determine the epoch based on fold 40 if fold in [3, 4]: 41 e = 1000 42 else: 43 e = 650 44 45 # Define the path to the pretrained model 46 pretrained_path = Path(f"/tmp/swinunetr_e{e}_f{fold}_b1p4/") 47 48 # Load arguments from the pretrained model 49 args = torch.load(pretrained_path / pretrained_name)['args'] 50 51 # Define the output directory for this fold 52 output_dir = output_folder / f"swinunetr3d_f{fold}" 53 npz_folder_list.append(output_dir) 54 55 # Construct the command to run the SwinUNETR inference 56 cmd = 'python swinunetr/inference.py' 57 cmd = ' '.join((cmd, f"--datadir='{str(input_path)}'")) 58 cmd = ' '.join((cmd, f"--exp_path='{str(output_dir)}'")) 59 cmd = ' '.join((cmd, f'--roi_x={args.roi_x}')) 60 cmd = ' '.join((cmd, f'--roi_y={args.roi_y}')) 61 cmd = ' '.join((cmd, f'--roi_z={args.roi_z}')) 62 cmd = ' '.join((cmd, f'--in_channels={args.in_channels}')) 63 cmd = ' '.join((cmd, f'--out_channels={args.out_channels}')) 64 cmd = ' '.join((cmd, '--spatial_dims=3')) 65 cmd = ' '.join((cmd, '--use_checkpoint')) 66 cmd = ' '.join((cmd, '--feature_size=48')) 67 cmd = ' '.join((cmd, '--infer_overlap=0.625')) 68 cmd = ' '.join((cmd, '--cacherate=1.0')) 69 cmd = ' '.join((cmd, '--workers=0')) 70 cmd = ' '.join((cmd, f'--pretrained_model_name={pretrained_name}')) 71 cmd = ' '.join((cmd, f'--pretrained_dir={str(pretrained_path)}')) 72 cmd = ' '.join((cmd, '--pred_label')) 73 74 print(cmd) 75 subprocess.run(cmd, shell=True) # Executes the command in the shell 76 77 # Generate list of npz paths based on output folders 78 npz_path_list = [f / f"{input_path.name}-t1n.npz" for f in npz_folder_list] 79 return npz_path_list
def
run_infer_swinunetr( input_path: pathlib.Path, output_folder: pathlib.Path, challenge: Any, folds: List[int] = [0, 1, 2, 3, 4]) -> List[pathlib.Path]:
15def run_infer_swinunetr( 16 input_path: Path, 17 output_folder: Path, 18 challenge: Any, 19 folds: List[int] = [0, 1, 2, 3, 4] 20) -> List[Path]: 21 """Run SwinUNETR inference on the provided input data. 22 23 This function executes the SwinUNETR inference script for each specified fold, 24 using the appropriate pretrained model weights, and collects the paths to the 25 resulting probability npz files. 26 27 Args: 28 input_path (Path): Path to the folder containing the 4 input files. 29 output_folder (Path): Path to the folder to store the npz files. 30 challenge (Any): The challenge identifier or related parameter. 31 folds (List[int], optional): List of fold indices to process. Defaults to [0, 1, 2, 3, 4]. 32 33 Returns: 34 List[Path]: List of paths to the npz files generated by inference. 35 """ 36 pretrained_name = 'best_model.pt' 37 npz_folder_list: List[Path] = [] 38 39 for fold in folds: 40 # Determine the epoch based on fold 41 if fold in [3, 4]: 42 e = 1000 43 else: 44 e = 650 45 46 # Define the path to the pretrained model 47 pretrained_path = Path(f"/tmp/swinunetr_e{e}_f{fold}_b1p4/") 48 49 # Load arguments from the pretrained model 50 args = torch.load(pretrained_path / pretrained_name)['args'] 51 52 # Define the output directory for this fold 53 output_dir = output_folder / f"swinunetr3d_f{fold}" 54 npz_folder_list.append(output_dir) 55 56 # Construct the command to run the SwinUNETR inference 57 cmd = 'python swinunetr/inference.py' 58 cmd = ' '.join((cmd, f"--datadir='{str(input_path)}'")) 59 cmd = ' '.join((cmd, f"--exp_path='{str(output_dir)}'")) 60 cmd = ' '.join((cmd, f'--roi_x={args.roi_x}')) 61 cmd = ' '.join((cmd, f'--roi_y={args.roi_y}')) 62 cmd = ' '.join((cmd, f'--roi_z={args.roi_z}')) 63 cmd = ' '.join((cmd, f'--in_channels={args.in_channels}')) 64 cmd = ' '.join((cmd, f'--out_channels={args.out_channels}')) 65 cmd = ' '.join((cmd, '--spatial_dims=3')) 66 cmd = ' '.join((cmd, '--use_checkpoint')) 67 cmd = ' '.join((cmd, '--feature_size=48')) 68 cmd = ' '.join((cmd, '--infer_overlap=0.625')) 69 cmd = ' '.join((cmd, '--cacherate=1.0')) 70 cmd = ' '.join((cmd, '--workers=0')) 71 cmd = ' '.join((cmd, f'--pretrained_model_name={pretrained_name}')) 72 cmd = ' '.join((cmd, f'--pretrained_dir={str(pretrained_path)}')) 73 cmd = ' '.join((cmd, '--pred_label')) 74 75 print(cmd) 76 subprocess.run(cmd, shell=True) # Executes the command in the shell 77 78 # Generate list of npz paths based on output folders 79 npz_path_list = [f / f"{input_path.name}-t1n.npz" for f in npz_folder_list] 80 return npz_path_list
Run SwinUNETR inference on the provided input data.
This function executes the SwinUNETR inference script for each specified fold, using the appropriate pretrained model weights, and collects the paths to the resulting probability npz files.
Args: input_path (Path): Path to the folder containing the 4 input files. output_folder (Path): Path to the folder to store the npz files. challenge (Any): The challenge identifier or related parameter. folds (List[int], optional): List of fold indices to process. Defaults to [0, 1, 2, 3, 4].
Returns: List[Path]: List of paths to the npz files generated by inference.