首页 > 解决方案 > 分类指标不能处理多标签指标和多类目标的混合

问题描述

我正在尝试计算两个图像的精度和召回率。一个图像作为模型的输出(形状 [3,786,1024])和另一个基本事实(形状 [1, 786, 1024])。但我不断收到错误消息。我是 python 新手,我能够理解我做错了什么。我尝试了一些方法,例如将输入和输出设置为相同的形状,但现在出现错误:

raise ValueError("Classification metrics can't handle a mix of {0} " ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets 如果有人能帮我解决这个问题。我将不胜感激。

我的代码在这里:

def precision(outputs, labels):
op = outputs#.cpu()
la = labels#.cpu()
_, preds = torch.max(op, dim=1)
return torch.tensor(precision_score(la,preds, average='micro'))

def recall(outputs, labels):
op = outputs#.cpu()
la = labels#.cpu()
_, preds = torch.max(op, dim=1)
return torch.tensor(recall_score(la,preds, average='weighted', zero_division = 1))
def f1(outputs, labels):
op = outputs#.cpu()
la = labels#.cpu()
_, preds = torch.max(op, dim=1)
return torch.tensor(f1_score(la,preds, average='weighted'))


img50 = Image.open('0050_999.png')
numpydata_in = np.array(img50).astype('uint8')
#print("test shape",numpydata_in.shape)
numpydata_in= torch.from_numpy(numpydata_in)
print("final input shape", numpydata_in.shape)

imgT50 = Image.open('0050.jpg')
numpydata_out = np.array(imgT50).astype('uint8')
numpydata_out = transforms.ToTensor()(numpydata_out)
#print(numpydata_out.shape)
numpydata_out = torch.argmax(numpydata_out, dim=0)
#print(numpydata_out.shape)
numpydata_out = torch.squeeze(numpydata_out)
print("final shape target",numpydata_out.shape)

prec = precision(numpydata_in, numpydata_out)
print("precision is",prec)
rec = recall(numpydata_in, numpydata_out)
print("recall is",rec)
f1 = f1(numpydata_in, numpydata_out)
print("f1 score is",f1)

最终输入形状 torch.Size([768, 1024]) 最终形状目标 torch.Size([768, 1024])

raise ValueError("Classification metrics can't handle a mix of {0} " ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets

标签: image-processingpytorchconv-neural-networkmetrics

解决方案


推荐阅读