image-processing - 分类指标不能处理多标签指标和多类目标的混合
问题描述
我正在尝试计算两个图像的精度和召回率。一个图像作为模型的输出(形状 [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
解决方案
推荐阅读
- pandas - 将索引列复制为新列
- javascript - Agora 不是为原生移动应用程序录制和保存视频,但在网络上可以正常工作
- javascript - 尝试导入错误:“消息”未从“firebase/app”导出
- jsxgraph - JSXGraph 如何添加悬停效果?
- c++ - 在销毁派生类之前清理基类的资源
- laravel - 在 Laravel 中按日期删除重复的数据库记录
- java - 我将如何在特定时间执行事件
- easy-batch - 如何使用 Easy-Batch 作业将一条记录转换为多条记录
- java - 如何通过单击按钮打开另一个布局中包含的片段
- google-sheets - 如何将天气数据导入谷歌表格以获取特定日期的坐标