Skip to content

Update compute_metric for MetaTensor #780

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

Merged
merged 1 commit into from
Jul 12, 2022
Merged
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
18 changes: 14 additions & 4 deletions modules/compute_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

import nibabel as nib
import numpy as np
import torch
import torch.distributed as dist

from monai.data import create_test_image_3d, partition_dataset
Expand All @@ -57,7 +58,7 @@
KeepLargestConnectedComponentd,
LoadImaged,
ScaleIntensityd,
EnsureTyped,
ToDeviced,
)
from monai.utils import string_list_all_gather

Expand All @@ -77,8 +78,8 @@ def compute(args):
n = nib.Nifti1Image(label, np.eye(4))
nib.save(n, os.path.join(args.dir, f"label{i:d}.nii.gz"))

# initialize the distributed evaluation process, change to NCCL backend if computing on GPU
dist.init_process_group(backend="gloo", init_method="env://")
# initialize the distributed evaluation process, change to gloo backend if computing on CPU
dist.init_process_group(backend="nccl", init_method="env://")

preds = sorted(glob(os.path.join(args.dir, "pred*.nii.gz")))
labels = sorted(glob(os.path.join(args.dir, "label*.nii.gz")))
Expand All @@ -92,13 +93,15 @@ def compute(args):
even_divisible=False,
)[dist.get_rank()]

device = torch.device(f"cuda:{args.local_rank}")
torch.cuda.set_device(device)
# define transforms for predictions and labels
transforms = Compose(
[
LoadImaged(keys=["pred", "label"]),
ToDeviced(keys=["pred", "label"], device=device),
EnsureChannelFirstd(keys=["pred", "label"]),
ScaleIntensityd(keys="pred"),
EnsureTyped(keys=["pred", "label"]),
AsDiscreted(keys="pred", threshold=0.5),
KeepLargestConnectedComponentd(keys="pred", applied_labels=[1]),
]
Expand Down Expand Up @@ -129,6 +132,13 @@ def compute(args):
dist.destroy_process_group()


# usage example(refer to https://github.com/pytorch/pytorch/blob/master/torch/distributed/launch.py):

# python -m torch.distributed.launch --nproc_per_node=NUM_GPUS_PER_NODE
# --nnodes=NUM_NODES --node_rank=INDEX_CURRENT_NODE
# --master_addr="192.168.1.1" --master_port=1234
# compute_metric.py -d DIR_OF_OUTPUT

def main():
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dir", default="./output", type=str, help="root directory of labels and predictions.")
Expand Down