简介:PyTorch绘制混淆矩阵代码:深入解析PyTorch混淆矩阵
PyTorch绘制混淆矩阵代码:深入解析PyTorch混淆矩阵
在机器学习和深度学习的应用中,分类任务的评估是一个重要环节。混淆矩阵是评估分类器性能的常用工具之一,它可以清晰地展示模型的错误率和各类别的表现。本文将介绍使用PyTorch构建和绘制混淆矩阵的方法。
首先,让我们简单了解一下混淆矩阵的概念。混淆矩阵,也称为误差矩阵,用于描述分类器在测试集上的性能。它记录了每个真实类别和预测类别的对应关系,以及各种可能的错误。
在Python中,我们可以使用以下代码来计算二分类问题的混淆矩阵:
from sklearn.metrics import confusion_matriximport numpy as npy_true = [0, 1, 0, 1]y_pred = [1, 1, 0, 1]cm = confusion_matrix(y_true, y_pred)print(cm)
对于多分类问题,混淆矩阵的构建方式类似。只需将y_true和y_pred向量替换为相应的多维向量即可。
然而,上述代码只能用于计算混淆矩阵,如果你想要绘制混淆矩阵图,可以使用以下代码:
import matplotlib.pyplot as pltplt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)plt.title('Confusion matrix')plt.colorbar()plt.ylabel('True label')plt.xlabel('Predicted label')tick_marks = np.arange(len(classes)) # classes是你模型中预测的类别数量plt.xticks(tick_marks, classes, rotation=45)plt.yticks(tick_marks, classes)thresh = cm.max() / 2.for i in range(len(classes)):for j in range(len(classes)):plt.text(j, i, cm[i, j],horizontalalignment="center",color="white" if cm[i, j] > thresh else "black")plt.tight_layout()plt.show()
这段代码将创建一个混淆矩阵图像,其中颜色越深表示更高的混淆计数。
现在,我们来看看如何使用PyTorch来绘制混淆矩阵。在PyTorch中,我们可以通过torchvision.utils.make_grid函数来将图像数据拼接成网格,并通过torchvision.transforms模块中的函数来进行图像预处理。下面是一个简单的示例:
```python
import torchvision.transforms as transforms
from torchvision import datasets, transforms, models
from torch.utils.data import DataLoader
from sklearn.metrics import confusion_matrix
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import torchvision.utils as vutils
from torchvision import datasets, transforms, models
from sklearn.preprocessing import MultiLabelBinarizer
import torchvision.transforms as transformsEMA #EMA original EMA normalization code provided by PyTorch team at https://pytorch.org/vision/stable/transforms.html#ema-normalization-transforms-ema-norm and it is modified here for our purpose to calculate EMA for confusion matrix cell values here is the custom transform we are using here: transformsEMA.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) which are ImageNet statistics for ‘RGB’ images order by R,G,B channels order for mean and std values here which are same order as torchvision ImageNet dataset transforms where mean and std values are computed on entire ImageNet dataset in torchvision transform which we use here to normalize input images into this range only! the default ImageNet mean and std values are not divided by number of channels they are [(0.485 - 0.456) / 0.5 + 0.456 , (0.456 - 0.406) / 0.6 + 0.406 , (0.406 - 0.349) / 0.7 + 0.349] order by R,G,B channels respectively here which is different from torchvision ImageNet stats normalization where mean and std values are computed on ImageNet dataset only and not divided by number of channels they are [(0.485 - 0.456) / 0.5 , (0.456 - 0.406) / 0.6 , (