Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 'mps' device. #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 3 additions & 8 deletions HD_BET/hd_bet_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def main():
'accurate',
required=False)
parser.add_argument('-device', default='0', type=str, help='used to set on which device the prediction will run. '
'Must be either int or str. Use int for GPU id or '
'\'cpu\' to run on CPU. When using CPU you should '
'consider disabling tta. Default for -device is: 0',
'Must be either int or str. Use int for GPU id, '
'\'cpu\' to run on CPU or \'mps\' for Mac/MPS. When using CPU '
'you should consider disabling tta. Default for -device is: 0',
required=False)
parser.add_argument('-tta', default=1, required=False, type=int, help='whether to use test time data augmentation '
'(mirroring). 1= True, 0=False. Disable this '
Expand Down Expand Up @@ -71,11 +71,6 @@ def main():

assert os.path.abspath(input_file_or_dir) != os.path.abspath(output_file_or_dir), "output must be different from input"

if device == 'cpu':
pass
else:
device = int(device)

if os.path.isdir(input_file_or_dir):
maybe_mkdir_p(output_file_or_dir)
input_files = subfiles(input_file_or_dir, suffix='.nii.gz', join=False)
Expand Down
2 changes: 1 addition & 1 deletion HD_BET/predict_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def predict_case_3D_net(net, patient_data, do_mirroring, num_repeats, BATCH_SIZE
if main_device == 'cpu':
pass
else:
a = a.cuda(main_device)
a = a.to(main_device)

if do_mirroring:
x = 8
Expand Down
11 changes: 7 additions & 4 deletions HD_BET/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def run_hd_bet(mri_fnames, output_fnames, mode="accurate", config_file=os.path.j
:param output_fnames: str or list/tuple of str. If list: must have the same length as output_fnames
:param mode: fast or accurate
:param config_file: config.py
:param device: either int (for device id) or 'cpu'
:param device: either int (for device id) or 'cpu' or 'mps'
:param postprocess: whether to do postprocessing or not. Postprocessing here consists of simply discarding all
but the largest predicted connected component. Default False
:param do_tta: whether to do test time data augmentation by mirroring along all axes. Default: True. If you use
Expand Down Expand Up @@ -62,10 +62,13 @@ def run_hd_bet(mri_fnames, output_fnames, mode="accurate", config_file=os.path.j
cf = cf_module.config()

net, _ = cf.get_network(cf.val_use_train_mode, None)
if device == "cpu":
net = net.cpu()

if device == 'cpu' or device == 'mps':
pass
else:
net.cuda(device)
device = "cuda:" + str(device)

net = net.to(device)

if not isinstance(mri_fnames, (list, tuple)):
mri_fnames = [mri_fnames]
Expand Down