我正在训练 MONAI
model =SegResNet( out_channels=2)
来完成分割任务。我有两个类,前景和背景。真实分割/标签是 1 通道图像。
我使用 MONAI DiceLoss(softmax=True, include_background=False, to_onehot_y=True)
,它似乎有效,它减少了,而且预测看起来不错。 (include_background
是假的,因为背景比前景大得多。)但是我似乎不知道如何使用DiceMetric(include_background=False, reduction="mean", get_not_nans=False)
。
要么给出错误,要么给出 1 或 0 或大于 1 的数字。 我阅读了我能找到的所有教程,并尝试复制那些但没有成功......
我这样使用损失:
vloss = loss_fn(voutputs, vlabels)
在教程中,“AsDiscreted”是一个常见的步骤。我不想使用 MONAI 变换,所以这就是我尝试过的:
voutputs_bin=voutputs
voutputs_bin=torch.nn.functional.softmax(voutputs_bin,dim=1)
#voutputs_bin=torch.argmax(voutputs_bin,dim=1)
voutputs_bin = torch.nn.functional.one_hot(voutputs_bin.to(torch.int64), num_classes=-1)
#voutputs_bin=voutputs_bin[:,1:,:,:]
#voutputs_bin=torch.nn.functional.threshold(voutputs_bin,0.5,1)
vlabels_bin=torch.nn.functional.one_hot(vlabels.to(torch.int64), num_classes=-1)
metric(y_pred=voutputs_bin, y=vlabels_bin)
我包含了注释行,因为这是我尝试过的以及它的不同组合。 验证循环之后我会这样做:
vmetric=metric.aggregate().item()
print(f' dice metric: {vmetric}')
metric.reset()
您能告诉我并解释一下,我应该如何正确使用DiceMetric吗?
from monai.losses import DiceLoss
def dice_metric(predicted, target):
dice_value = DiceLoss(to_onehot_y=True, sigmoid=True,squared_pred=True)
value = 1 - dice_value(predicted, target).item()
return value
如何使用;
metric = dice_metric(outputs, label)
希望能成功!