Skip to content

Generalized DICE Metric #7004

Closed
Closed

Description

@vikashg

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

Activity

  1. KumoLiu commented on Sep 19, 2023

    @KumoLiu
    Contributor

    Hi @vikashg, I can't reproduce the error, could you please share more information?

    from monai.metrics import GeneralizedDiceScore
    import torch
    
    val_outputs = torch.zeros(4, 1, 9, 9, 9)
    val_outputs[:, :, 3:4, ...] = 1
    val_labels = torch.zeros(4, 1, 9, 9, 9)
    val_labels[:, :, 3:5, ...] = 1
    dice_metric = GeneralizedDiceScore(include_background=False, reduction="mean_batch")
    dice_metric(y_pred = val_outputs, y = val_labels)
    

    Thanks!

  2. vikashg commented on Sep 19, 2023

    @vikashg
    CollaboratorAuthor
  3. KumoLiu commented on Sep 20, 2023

    @KumoLiu
    Contributor

    Hi @vikashg, I tried with your demo but no error report. May I know your MONAI version? It may caused by your environment.

  4. vikashg commented on Sep 20, 2023

    @vikashg
    CollaboratorAuthor

    hmm weird
    image
    I still get the error. My version is 1.2.0

  5. KumoLiu commented on Sep 21, 2023

    @KumoLiu
    Contributor

    Hi @vikashg, looks like a bug fixed by #6781.
    Could you please try the dev branch or weekly version? Thanks!

  6. vikashg commented on Sep 22, 2023

    @vikashg
    CollaboratorAuthor

    Yes it works on the dev branch. Thanks for the tip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions