我有一个问题,因为我想在torchmetrics计算一些指标。但是有一个问题:
ValueError: The implied number of classes (from shape of inputs) does not match num_classes.
输出来自UNet,损失函数为BCEWithLogitsLoss(二进制分割)
通道= 1,因为灰度
输入形状:(batch_size, channels, h, w) torch.float32
标签形状:(batch_size, channels, h, w) torch。float32 for BCE
输出形状:(batch_size, channels, h, w): torch.float32
inputs, labels = batch
outputs = model(input)
loss = self.loss_function(outputs, labels)
prec = torchmetrics.Precision(num_classes=1)(outputs, labels.type(torch.int32)
似乎torchmetrics
期望不同的形状。尝试使输出和标签都变平:
prec = torchmetrics.Precision(num_classes=1)(outputs.view(-1), labels.type(torch.int32).view(-1))
我使用torchmetrics库来计算分割任务的F1分数,Precision和Recall;当我遇到上述错误时,我试图获得我的两个个人课程的F1分数,这种解决方案有效,但首先我必须将'multi_class=True
'设置为'num_classes=2
'
torchmetrics_f1_none = torchmetrics.classification.F1Score(average=None, num_classes=2, multiclass=True)
f1_0, f1_1 = torchmetrics_f1_none(thres_out.view(-1), masks.int().view(-1))
print("F1 Score for Background - {}, F1 Score for Foreground - {} n".format(f1_0, f1_1))