hsiangyuzhao / PANet

Prior Attention Network for Multi-Lesion Segmentation in Medical Images
35 stars 4 forks source link

Turn "get_not_nans" to "Ture" does not work. #5

Open Studentpengyu opened 1 year ago

Studentpengyu commented 1 year ago

Dear author, I have Turned "get_not_nans" to " Ture like that:

dice_metric = DiceMetric(include_background=True, reduction='none', get_not_nans=True) dice_et, dice_et_not_nan = dice_metric(y_pred=predictions[:, 1:2, ...], y=ground_truth[:, 1:2, ...])

However, I still got the following error: "dice_et, dice_et_not_nan = dice_metric(y_pred=predictions[:, 1:2, ...], y=ground_truth[:, 1:2, ...]) ValueError: not enough values to unpack (expected 2, got 1)"

How can I fix it? Looking forward to your help! Thanks!

Studentpengyu commented 1 year ago

Dear author, I further revised the code as follows:

dice_metric.reset() dice_metric(y_pred=predictions[:, 1:2, ...], y=ground_truth[:, 1:2, ...]) dice_et, dice_et_not_nan = dice_metric.aggregate()

dice_metric.reset() dice_metric(y_pred=predictions[:, 2:3, ...], y=ground_truth[:, 2:3, ...]) dice_tc, dice_tc_not_nan = dice_metric.aggregate()

dice_metric.reset() dice_metric(y_pred=predictions[:, 3:4, ...], y=ground_truth[:, 3:4, ...]) dice_wt, dice_wt_not_nan = dice_metric.aggregate()

hausdorff_metric.reset() hausdorff_metric(y_pred=predictions[:, 2:3, ...], y=ground_truth[:, 2:3, ...]) hd95_tc, hd95_tc_not_nan = hausdorff_metric.aggregate()

hausdorff_metric.reset() hausdorff_metric(y_pred=predictions[:, 1:2, ...], y=ground_truth[:, 1:2, ...]) hd95_et, hd95_et_not_nan = hausdorff_metric.aggregate()

hahausdorff_metric.reset() hausdorff_metric(y_pred=predictions[:, 3:4, ...], y=ground_truth[:, 3:4, ...]) hd95_wt, hd95_wt_not_nan = hausdorff_metric.aggregate()

hahausdorff_metric.reset() hausdorff_metric(y_pred=predictions[:, 3:4, ...], y=ground_truth[:, 3:4, ...]) hd95_wt, hd95_wt_not_nan = hausdorff_metric.aggregate()

In this way, no errors were raised. However, I got NAN values in the Dice and Hausdorff distance metrics. The predictive results were like that: "Subject ['BraTS20_Training_306'], contain ET tensor(0., device='cuda:0'), predict falsely, Dice=0 Subject ['BraTS20_Training_306'], contain ET tensor(0., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_159'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_369'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_029'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_141'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_062'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_365'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_227'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_232'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_167'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_041'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_136'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_047'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_347'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_031'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_237'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_229'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_161'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_098'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13 Subject ['BraTS20_Training_195'], contain ET tensor(1., device='cuda:0'), predict falsely, HD=373.13"

There must be something wrong here. Hope you can help me! Looking forward to your reply! Thank you!

hsiangyuzhao commented 1 year ago

Hi @Studentpengyu , Sorry for the late reply. I suggest you modify the code like follows:

dice_et = dice_metric(y_pred=predictions[:, 1:2, ...], y=ground_truth[:, 1:2, ...])

This is because MONAI has updated for several versions, and the legacy version that suits our code style is not supported anymore. I suggest you follow the guide of latest MONAI documentary.

GerogeD commented 1 year ago

您好,感谢您能抽空查看我的问题,我在结合您的回答后修改了自己的代码,但是在计算Hd95_wt的值时,我所有的预测结构均为nan,大量的hd95_tc值也为nan,我不知道问题出现在哪里,能麻烦您帮我看一下吗?非常感谢

compute dice and hausdorff distance 95

                dice_et = dice_metric(y_pred=predictions[:, 1:2, ...], y=ground_truth[:, 1:2, ...])
                dice_tc = dice_metric(y_pred=predictions[:, 2:3, ...], y=ground_truth[:, 2:3, ...])
                dice_wt = dice_metric(y_pred=predictions[:, 3:4, ...], y=ground_truth[:, 3:4, ...])

                hd95_et = hausdorff_metric(y_pred=predictions[:, 1:2, ...],
                                           y=ground_truth[:, 1:2, ...])
                hd95_tc = hausdorff_metric(y_pred=predictions[:, 2:3, ...],
                                           y=ground_truth[:, 2:3, ...])
                hd95_wt = hausdorff_metric(y_pred=predictions[:, 3:4, ...],
                                           y=ground_truth[:, 3:4, ...])

                # post-process Dice and HD95 values
                # if subject has no enhancing tumor, empty prediction yields Dice of 1 and HD95 of 0
                # otherwise, false positive yields Dice of 0 and HD95 of 373.13 (worst single case)
                if dice_et.item() == 0:
                    if predictions[:, 1:2, ...].max() == 0 and ground_truth[:, 1:2, ...].max() == 0:
                        dice_et = torch.as_tensor(1)
                        print('Subject {}, contain ET {}, predict correctly, Dice=1'.format(batch_data['name'],
                                                                                            ground_truth[:,
                                                                                            1].max()))
                    else:
                        dice_et = torch.as_tensor(0)
                        print('Subject {}, contain ET {}, predict falsely, Dice=0'.format(batch_data['name'],
                                                                                          ground_truth[:, 1].max()))
                if hd95_wt.item() == 0:
                    if predictions[:, 1:2, ...].max() == 0 and ground_truth[:, 1:2, ...].max() == 0:
                        hd95_et = torch.as_tensor(0)
                        print('Subject {}, contain ET {}, predict correctly, HD=0'.format(batch_data['name'],
                                                                                          ground_truth[:, 1].max()))
                    else:
                        hd95_et = torch.as_tensor(373.13)
                        print('Subject {}, contain ET {}, predict falsely, HD=373.13'.format(batch_data['name'],
                                                                                             ground_truth[:,
                                                                                             1].max()))
                if hd95_wt.item() == np.inf:
                    hd95_et = torch.as_tensor(373.13)
                    print('Subject {}, contain ET {}, predict falsely, HD=373.13'.format(batch_data['name'],
                                                                                         ground_truth[:, 1].max()))

                et_metric = {'et_dice': dice_et.item(), 'et_hd': hd95_et.item()}
                tc_metric = {'tc_dice': dice_tc.item(), 'tc_hd': hd95_tc.item()}
                wt_metric = {'wt_dice': dice_wt.item(), 'wt_hd': hd95_wt.item()}

                avg_dice = (dice_et.item() + dice_tc.item() + dice_wt.item()) / 3
                avg_hd = (hd95_et.item() + hd95_tc.item() + hd95_wt.item()) / 3
                avg_metric = {'avg_dice': avg_dice, 'avg_hd': avg_hd}

                metric = {**et_metric, **tc_metric, **wt_metric, **avg_metric, 'name': batch_data['name']}
                metric_meter.update(metric)
                print('==============  num:%d =============' % step)
                print('dice_et:{}    dice_tc:{}   dice_wt:{}'.format(dice_et, dice_tc, dice_wt))
                print('hd95_et:{}    hd95_tc:{}   hd95_wt:{}\n\n\n'.format(hd95_et, hd95_tc, hd95_wt))
        print('=================  metric_meter   ==========================\n')
        metric_meter.report(print_stats=True)
C-c-candy commented 2 months ago

尊敬的作者,我进一步修改了代码如下:

dice_metric.reset() dice_metric(y_pred=predictions[:, 1:2, ...], y=ground_truth[:, 1:2, ...]) dice_et,dice_et_not_nan = dice_metric.aggregate()

dice_metric.reset() dice_metric(y_pred=predictions[:, 2:3, ...], y=ground_truth[:, 2:3, ...]) dice_tc, dice_tc_not_nan = dice_metric.aggregate()

dice_metric.reset() dice_metric(y_pred=predictions[:, 3:4, ...], y=ground_truth[:, 3:4, ...]) dice_wt, dice_wt_not_nan = dice_metric.aggregate()

hausdorff_metric.reset() hausdorff_metric(y_pred=predictions[:, 2:3, ...], y=ground_truth[:, 2:3, ...]) hd95_tc, hd95_tc_not_nan = hausdorff_metric.aggregate()

hausdorff_metric.reset() hausdorff_metric(y_pred=predictions[:, 1:2, ...], y=ground_truth[:, 1:2, ...]) hd95_et,hd95_et_not_nan = hausdorff_metric.aggregate()

hahausdorff_metric.reset() hausdorff_metric(y_pred=predictions[:, 3:4, ...], y=ground_truth[:, 3:4, ...]) hd95_wt, hd95_wt_not_nan = hausdorff_metric.aggregate()

hahausdorff_metric.reset() hausdorff_metric(y_pred=predictions[:, 3:4, ...], y=ground_truth[:, 3:4, ...]) hd95_wt, hd95_wt_not_nan = hausdorff_metric.aggregate()

这样,就不会出现任何错误。但是,我在 Dice 和 Hausdorff 距离指标中得到了 NAN 值。预测结果是这样的:“受试者 ['BraTS20_Training_306'],包含 ET 张量(0., device='cuda:0'),预测错误,Dice=0 受试者 ['BraTS20_Training_306'],包含 ET 张量(0., device='cuda:0'),预测错误,HD=373.13 受试者 ['BraTS20_Training_159'],包含 ET 张量(1., device='cuda:0'),预测错误,HD=373.13 受试者 ['BraTS20_Training_369'],包含 ET 张量(1., device='cuda:0'),预测错误,HD=373.13 对象 ['BraTS20_Training_029'],包含 ET 张量(1., device='cuda:0'),预测错误,HD=373.13 对象 ['BraTS20_Training_141'],包含 ET 张量(1., device='cuda:0'),预测错误,HD=373.13 对象 ['BraTS20_Training_062'],包含 ET 张量(1., device='cuda:0'),预测错误,HD=373.13 对象 ['BraTS20_Training_365'], 包含 ET 张量(1., device='cuda:0'),预测错误,HD=373.13 主题 ['BraTS20_Training_227'],包含 ET 张量(1., device='cuda:0'),预测错误,HD=373.13 主题 ['BraTS20_Training_232'],包含 ET 张量(1., device='cuda:0'),预测错误,HD=373.13 主题 ['BraTS20_Training_167'],包含 ET 张量(1., device='cuda:0'),预测错误,HD=373.13受试者 ['BraTS20_Training_041'], 包含 ET 张量(1., device='cuda:0'), 预测错误, HD=373.13 受试者 ['BraTS20_Training_136'], 包含 ET 张量(1., device='cuda:0'), 预测错误, HD=373.13 受试者 ['BraTS20_Training_047'], 包含 ET 张量(1., device='cuda:0'), 预测错误, HD=373.13 受试者 ['BraTS20_Training_347'], 包含 ET 张量(1., device='cuda:0'), 错误地预测,HD=373.13 对象 ['BraTS20_Training_031'],包含 ET 张量(1., device='cuda:0'),错误预测,HD=373.13 对象 ['BraTS20_Training_237'],包含 ET 张量(1., device='cuda:0'),错误预测,HD=373.13 对象 ['BraTS20_Training_229'],包含 ET 张量(1., device='cuda:0'),预测错误,HD=373.13 对象 ['BraTS20_Training_161'],包含 ET 张量(1., device='cuda:0'),预测错误,HD=373.13 对象 ['BraTS20_Training_098'],包含 ET 张量(1.,device='cuda:0'),预测错误,HD=373.13 对象 ['BraTS20_Training_195'],包含 ET 张量(1.,device='cuda:0'),预测错误,HD=373.13”

There must be something wrong here. Hope you can help me! Looking forward to your reply! Thank you!

hello!have you fixed this error now?I meet the same things with you