Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Apr 3, 2023
1 parent 80b8356 commit 8ab65cb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
15 changes: 6 additions & 9 deletions monai/apps/auto3dseg/auto_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from time import sleep
from typing import Any, cast

import numpy as np
import torch

from monai.apps.auto3dseg.bundle_gen import BundleGen
Expand All @@ -30,8 +29,7 @@
from monai.apps.utils import get_logger
from monai.auto3dseg.utils import algo_to_pickle
from monai.bundle import ConfigParser
from monai.utils.enums import AlgoEnsembleKeys
from monai.utils.module import look_up_option, optional_import
from monai.utils.module import optional_import

logger = get_logger(module_name=__name__)

Expand Down Expand Up @@ -284,7 +282,7 @@ def __init__(
self.set_analyze_params()

self.ensemble_method_name = 'AlgoEnsembleBestByFold'
self.set_num_fold(num_fold=num_fold)
self.set_num_fold(num_fold=num_fold)
self.kwargs = kwargs
self.gpu_customization = False
self.gpu_customization_specs: dict[str, Any] = {}
Expand Down Expand Up @@ -472,8 +470,8 @@ def set_training_params(self, params: dict[str, Any] | None = None) -> None:
"""
self.train_params = deepcopy(params) if params is not None else {}

def set_device_info(self, cuda_visible_devices: list[int] | str=os.environ.get('CUDA_VISIBLE_DEVICES', None),
num_nodes: int=os.environ.get('NUM_NODES', 1),
def set_device_info(self, cuda_visible_devices: list[int] | str=os.environ.get('CUDA_VISIBLE_DEVICES', None),
num_nodes: int=os.environ.get('NUM_NODES', 1),
mn_start_method: str=os.environ.get('MN_START_METHOD', 'bcprun')):
""" Set the device related info
Args:
Expand Down Expand Up @@ -729,12 +727,11 @@ def run(self):

# step 4: model ensemble and write the prediction to disks.
if self.ensemble:
ensemble_runner = EnsembleRunner(data_src_cfg_name=self.data_src_cfg_name,
ensemble_runner = EnsembleRunner(data_src_cfg_name=self.data_src_cfg_name,
work_dir=self.work_dir, num_fold=self.num_fold,
ensemble_method_name=self.ensemble_method_name,
ensemble_method_name=self.ensemble_method_name,
mgpu=self.device_setting['n_devices']>1,
**self.kwargs, # for set_image_save_transform
**self.pred_params) # for inference
ensemble_runner.run()
logger.info("Auto3Dseg pipeline is complete successfully.")

8 changes: 4 additions & 4 deletions monai/apps/auto3dseg/bundle_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def _create_cmd(self, train_params: dict={}):
if self.device_setting['NUM_NODES'] > 1:
# if using multinode
if self.device_setting.get['MN_START_METHOD'] == 'bcprun':
cmd = "python "
cmd = "python "
else:
raise NotImplementedError(f"{self.device_setting['MN_START_METHOD']} is not supported yet. \
Try modify BundleAlgo._create_cmd for your cluster.")
Expand Down Expand Up @@ -211,14 +211,14 @@ def train(self, train_params: dict={}, device_setting: dict={}):
algo_config.yaml file, which is pre-filled by the fill_template_config function in the same instance.
Args:
train_params: training parameters
device_settings: device related settings, should follow the device_setting in auto_runner.set_device_info.
train_params: training parameters
device_settings: device related settings, should follow the device_setting in auto_runner.set_device_info.
'CUDA_VISIBLE_DEVICES' should be a string e.g. '0,1,2,3'
"""
# device_setting set default value and sanity check, in case device_setting not from autorunner
self.device_setting.update(device_setting)
self.device_setting['n_devices'] = len(self.device_setting['CUDA_VISIBLE_DEVICES'].split(','))

cmd = self._create_cmd(train_params)
return self._run_cmd(cmd)

Expand Down
30 changes: 15 additions & 15 deletions monai/apps/auto3dseg/ensemble_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,18 +339,18 @@ def get_ensemble(self):
class EnsembleRunner:
def __init__(self, data_src_cfg_name: str='./work_dir/input.yaml',
work_dir: str='./work_dir',
num_fold: int=5,
ensemble_method_name: str='AlgoEnsembleBestByFold',
mgpu: bool=True,
num_fold: int=5,
ensemble_method_name: str='AlgoEnsembleBestByFold',
mgpu: bool=True,
**kwargs):
self.data_src_cfg_name = data_src_cfg_name
self.work_dir = work_dir
self.set_num_fold(num_fold=num_fold)
self.ensemble_method_name = ensemble_method_name
self.mgpu = mgpu
self.save_image = self.set_image_save_transform(kwargs)
self.set_ensemble_method(ensemble_method_name)
self.pred_params = kwargs
self.save_image = self.set_image_save_transform(kwargs)
self.set_ensemble_method(ensemble_method_name)
self.pred_params = kwargs
history = import_bundle_algo_history(self.work_dir, only_trained=True)
if len(history) == 0:
raise ValueError(
Expand Down Expand Up @@ -383,8 +383,8 @@ def set_ensemble_method(self, ensemble_method_name: str = "AlgoEnsembleBestByFol
elif self.ensemble_method_name == "AlgoEnsembleBestByFold":
self.ensemble_method = AlgoEnsembleBestByFold(n_fold=self.num_fold)
else:
raise NotImplementedError(f"Ensemble method {self.ensemble_method_name} is not implemented.")
raise NotImplementedError(f"Ensemble method {self.ensemble_method_name} is not implemented.")

def set_image_save_transform(self, kwargs):
"""
Set the ensemble output transform.
Expand Down Expand Up @@ -442,7 +442,7 @@ def ensemble(self):
self.ensembler.infer_files = infer_files

preds = self.ensembler(pred_param=self.pred_params)

if len(preds) > 0:
logger.info("Auto3Dseg picked the following networks to ensemble:")
for algo in ensembler.get_algo_ensemble():
Expand All @@ -461,8 +461,8 @@ def run(self, device_setting: dict={}):
algo_config.yaml file, which is pre-filled by the fill_template_config function in the same instance.
Args:
train_params: training parameters
device_settings: device related settings, should follow the device_setting in auto_runner.set_device_info.
train_params: training parameters
device_settings: device related settings, should follow the device_setting in auto_runner.set_device_info.
'CUDA_VISIBLE_DEVICES' should be a string e.g. '0,1,2,3'
"""
# device_setting set default value and sanity check, in case device_setting not from autorunner
Expand All @@ -484,13 +484,13 @@ def _create_cmd(self):

if self.pred_params and isinstance(self.pred_params, Mapping):
for k, v in self.pred_params.items():
base_cmd += f" --{k}={v}"
base_cmd += f" --{k}={v}"
# define env for subprocess
ps_environ = os.environ.copy()
ps_environ["CUDA_VISIBLE_DEVICES"] = self.device_setting['CUDA_VISIBLE_DEVICES']
if self.device_setting['NUM_NODES'] > 1:
if self.device_setting['NUM_NODES'] > 1:
# if multinode

if self.device_setting['MN_START_METHOD'] == 'bcprun':
logger.info(f"Ensembling on {self.device_setting['NUM_NODES']} nodes!")
cmd = 'python -m ' + base_cmd
Expand All @@ -506,4 +506,4 @@ def _create_cmd(self):
else:
# if single GPU
logger.info('Ensembling using single GPU!')
self.ensemble()
self.ensemble()

0 comments on commit 8ab65cb

Please sign in to comment.