Repository navigation
Generalized DICE Metric #7004
Copy link
Copy link
Closed
Labels
bugSomething isn't workingSomething isn't working
Description
Describe the bug
When I try to compute GeneralizedDiceMetric. I get the following error. I must point out that if I replace the 'GeneralizedDiceMetricwithDiceMetric` it works fine.
File "train_4.py", line 120, in main
dice_metric(y_pred = val_outputs.to(dtype=torch.int32), y = val_labels.to(dtype=torch.int32))
File "/opt/conda/lib/python3.8/site-packages/monai/metrics/metric.py", line 344, in __call__
ret = super().__call__(y_pred=y_pred, y=y, **kwargs)
File "/opt/conda/lib/python3.8/site-packages/monai/metrics/metric.py", line 77, in __call__
return self._compute_tensor(y_pred.detach(), y_, **kwargs)
File "/opt/conda/lib/python3.8/site-packages/monai/metrics/generalized_dice.py", line 77, in _compute_tensor
return compute_generalized_dice(
File "/opt/conda/lib/python3.8/site-packages/monai/metrics/generalized_dice.py", line 173, in compute_generalized_dice
(y_pred_o == 0)[denom_zeros],
File "/opt/conda/lib/python3.8/site-packages/monai/data/meta_tensor.py", line 297, in __torch_function__
ret = MetaTensor.update_meta(ret, func, args, kwargs)
File "/opt/conda/lib/python3.8/site-packages/monai/data/meta_tensor.py", line 237, in update_meta
ret_meta = decollate_batch(args[0], detach=False)[batch_idx]
File "/opt/conda/lib/python3.8/site-packages/monai/data/meta_tensor.py", line 276, in __torch_function__
ret = super().__torch_function__(func, types, args, kwargs)
File "/opt/conda/lib/python3.8/site-packages/torch/_tensor.py", line 1295, in __torch_function__
ret = func(*args, **kwargs)
TypeError: only integer tensors of a single element can be converted to an index
To Reproduce
I am sharing a relevant code snippet
post_trans = Compose([Activations(sigmoid=True), AsDiscrete(threshold=0.5)])
dice_metric = GeneralizedDiceScore(include_background=False, reduction="mean_batch")
for val_data in val_loader:
val_image = val_data["image"].to(device)
val_labels = val_data["mask"].to(device)
_val_outputs = model(val_image)
val_outputs = post_trans(_val_outputs)
dice_metric(y_pred = val_outputs, y = val_labels)
The shape of val_outputs is [4, 1, 256, 256, 24] and is a binary tensor.
Expected behavior
I expect the behavior to be the same DiceMetric
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working
Activity
KumoLiu commentedon Sep 19, 2023
Hi @vikashg, I can't reproduce the error, could you please share more information?
Thanks!
vikashg commentedon Sep 19, 2023
Hi @KumoLiu,
Thanks for looking into it Here is a gist for the training
https://gist.github.com/vikashg/0f2131aec1b18eef20073a8ec59df894
Here is the sample image
https://drive.google.com/drive/folders/1l0kicu68z6oYw-fCbgYbVD5Z4Pz5Rkoj?usp=sharing
KumoLiu commentedon Sep 20, 2023
Hi @vikashg, I tried with your demo but no error report. May I know your MONAI version? It may caused by your environment.
vikashg commentedon Sep 20, 2023
hmm weird

I still get the error. My version is 1.2.0
KumoLiu commentedon Sep 21, 2023
Hi @vikashg, looks like a bug fixed by #6781.
Could you please try the dev branch or weekly version? Thanks!
vikashg commentedon Sep 22, 2023
Yes it works on the dev branch. Thanks for the tip.