Skip to content
Merged
Changes from 1 commit
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
30 changes: 21 additions & 9 deletions monai/apps/nnunet/nnunetv2_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,17 +529,26 @@ def train_single_model(self, config: Any, fold: int, gpu_id: tuple | list | int
run_cmd(cmd, shell=True)

def train_single_model_command(self, config, fold, gpu_id, kwargs):
if isinstance(gpu_id, (tuple, list)):
device_setting = ""
num_gpus = 1
if isinstance(gpu_id, str):
device_setting = f"CUDA_VISIBLE_DEVICES={gpu_id}"
num_gpus = 1
elif isinstance(gpu_id, (tuple, list)):
if len(gpu_id) > 1:
gpu_ids_str = ""
for _i in range(len(gpu_id)):
gpu_ids_str += f"{gpu_id[_i]},"
device_setting = f"CUDA_VISIBLE_DEVICES={gpu_ids_str[:-1]}"
else:
gpu_ids_str = ",".join(str(x) for x in gpu_id)
device_setting = f"CUDA_VISIBLE_DEVICES={gpu_ids_str}"
num_gpus = len(gpu_id)
elif len(gpu_id) == 1:
device_setting = f"CUDA_VISIBLE_DEVICES={gpu_id[0]}"
num_gpus = 1
Comment thread
ytl0623 marked this conversation as resolved.
else:
device_setting = f"CUDA_VISIBLE_DEVICES={gpu_id}"
num_gpus = 1 if isinstance(gpu_id, int) or len(gpu_id) == 1 else len(gpu_id)
num_gpus = 1
env_cuda = os.environ.get("CUDA_VISIBLE_DEVICES")
if env_cuda is not None and device_setting == "CUDA_VISIBLE_DEVICES=0":
logger.info(f"Using existing environment variable CUDA_VISIBLE_DEVICES='{env_cuda}'")
device_setting = ""

cmd = (
f"{device_setting} nnUNetv2_train "
Expand Down Expand Up @@ -779,7 +788,7 @@ def predict(
part_id: int = 0,
num_processes_preprocessing: int = -1,
num_processes_segmentation_export: int = -1,
gpu_id: int = 0,
gpu_id: int | str = 0,
) -> None:
"""
Use this to run inference with nnU-Net. This function is used when you want to manually specify a folder containing
Expand Down Expand Up @@ -815,7 +824,10 @@ def predict(
More is not always better. Beware of out-of-RAM issues.
gpu_id: which GPU to use for prediction.
"""
os.environ["CUDA_VISIBLE_DEVICES"] = f"{gpu_id}"
if "CUDA_VISIBLE_DEVICES" in os.environ and gpu_id == 0:
logger.info(f"Predict: Using existing CUDA_VISIBLE_DEVICES={os.environ['CUDA_VISIBLE_DEVICES']}")
else:
os.environ["CUDA_VISIBLE_DEVICES"] = f"{gpu_id}"
Comment thread
ytl0623 marked this conversation as resolved.
Outdated

from nnunetv2.inference.predict_from_raw_data import nnUNetPredictor

Expand Down
Loading