您可以使用此混淆矩阵 PyTorch 示例来计算误报/漏报等:
import torch
def confusion(prediction, truth):
""" Returns the confusion matrix for the values in the `prediction` and `truth`
tensors, i.e. the amount of positions where the values of `prediction`
and `truth` are
- 1 and 1 (True Positive)
- 1 and 0 (False Positive)
- 0 and 0 (True Negative)
- 0 and 1 (False Negative)
"""
confusion_vector = prediction / truth
# Element-wise division of the 2 tensors returns a new tensor which holds a
# unique value for each case:
# 1 where prediction and truth are 1 (True Positive)
# inf where prediction is 1 and truth is 0 (False Positive)
# nan where prediction and truth are 0 (True Negative)
# 0 where prediction is 0 and truth is 1 (False Negative)
true_positives = torch.sum(confusion_vector == 1).item()
false_positives = torch.sum(confusion_vector == float('inf')).item()
true_negatives = torch.sum(torch.isnan(confusion_vector)).item()
false_negatives = torch.sum(confusion_vector == 0).item()
return true_positives, false_positives, true_negatives, false_negatives
您可以使用 nn.BCEWithLogitsLoss (因此删除 sigmoid)并设置 pos_weight > 1 以增加召回率。或者使用进一步优化它骰子系数 https://towardsdatascience.com/metrics-to-evaluate-your-semantic-segmentation-model-6bcb99639aa2#:%7E:text=3.-,Dice%20Coefficient%20(F1%20Score),of%20pixels%20in%20both%20images.惩罚模型的误报,例如:
def Dice(y_true, y_pred):
"""Returns Dice Similarity Coefficient for ground truth and predicted masks."""
#print(y_true.dtype)
#print(y_pred.dtype)
y_true = np.squeeze(y_true)/255
y_pred = np.squeeze(y_pred)/255
y_true.astype('bool')
y_pred.astype('bool')
intersection = np.logical_and(y_true, y_pred).sum()
return ((2. * intersection.sum()) + 1.) / (y_true.sum() + y_pred.sum() + 1.)
IOU 计算解释
- 计算真阳性 (TP)
- 误报计数 (FP)
- 漏报计数 (FN)
- 交点 = TP
- 并集 = TP + FP + FN
- IOU = 交集/并集
左侧是我们的基本事实,而右侧包含我们的预测。左侧突出显示的单元格记录了我们正在查看哪个类以获取右侧的统计信息。右侧的突出显示以奶油色表示真阳性,以橙色表示假阳性,以黄色表示假阴性(请注意,所有其他都是真阴性 - 它们是作为单个类别进行预测的,不应基于真实情况)。
对于 0 类,仅 4x4 矩阵的顶行应预测为零。这是真实事实的相当简化的版本。实际上,零点可以位于矩阵中的任何位置。在右侧,我们看到 1,0,0,0,这意味着第一个是假阴性,但其他三个是真阳性(也称为 Intersection 的 3)。从那里,我们需要找到错误预测零的其他任何地方,我们注意到这种情况在第二行发生一次,在第四行发生两次,总共三个误报。
为了得到并集,我们将 TP (3)、FP (3) 和 FN (1) 加起来得到 7。因此,该类的 IOU 为 3/7。
如果我们对所有类别都这样做并平均 IOU,我们会得到:
Mean IOU = [(3/7) + (2/6) + (3/4) + (1/6)] / 4 = 0.420
您还需要了解如何提取统计数据mAP(平均精度):
- https://www.youtube.com/watch?v=pM6DJ0ZZee0 https://www.youtube.com/watch?v=pM6DJ0ZZee0
- https://towardsdatascience.com/breaking-down-mean-average- precision-map-ae462f623a52#1a59 https://towardsdatascience.com/breaking-down-mean-average-precision-map-ae462f623a52#1a59
- https://medium.com/@hfdtsinghua/calculate-mean-average- precision-map-for-multi-label-classification-b082679d31be https://medium.com/@hfdtsinghua/calculate-mean-average-precision-map-for-multi-label-classification-b082679d31be
计算协方差矩阵
变量的方差描述了值的分布程度。协方差是一种衡量两个变量之间依赖程度的度量。
正协方差意味着当第二个变量的值也很大时,第一个变量的值也很大。负协方差意味着相反的情况:一个变量的大值与另一个变量的小值相关。
协方差值取决于变量的规模,因此很难对其进行分析。可以使用更容易解释的相关系数。相关系数就是归一化协方差。
A positive covariance means that large values of one variable are associated with big values from the other (left). A negative covariance means that large values of one variable are associated with small values of the other one (right).
The covariance matrix is a matrix that summarises the variances and covariances of a set of vectors and it can tell a lot of things about your variables. The diagonal corresponds to the variance of each vector:
A matrix A and its matrix of covariance. The diagonal corresponds to the variance of each column vector. Let’s check with the formula of the variance:
With n the length of the vector, and x̄ the mean of the vector. For instance, the variance of the first column vector of A is:
这是协方差矩阵的第一个单元格。对角线上的第二个元素对应于 A 中第二个列向量的方差,依此类推。
注:从矩阵A中提取的向量对应于A的列。
The other cells correspond to the covariance between two column vectors from A. For instance, the covariance between the first and the third column is located in the covariance matrix as the column 1 and the row 3 (or the column 3 and the row 1):
协方差矩阵中的位置。列对应于第一个变量,行对应于第二个变量(或相反)。 A 的第一列向量和第三列向量之间的协方差是第 1 列和第 3 行中的元素(或相反 = 相同值)。
Let’s check that the covariance between the first and the third column vector of A is equal to -2.67. The formula of the covariance between two variables Xand Y is:
The variables X and Y are the first and the third column vectors in the last example. Let’s split this formula to be sure that it is crystal clear:
-
The sum symbol (Σ) means that we will iterate on the elements of the vectors. We will start with the first element (i=1) and calculate the first element of X minus the mean of the vector X:
-
Multiply the result with the first element of Y minus the mean of the vector Y:
-
Reiterate the process for each element of the vectors and calculate the sum of all results:
-
除以向量中的元素数量。
EXAMPLE - Let’s start with the matrix A:
We will calculate the covariance between the first and the third column vectors:
and
Which is x̄=3, ȳ=4, and n=3 so we have:
代码示例 -
使用 NumPy,可以使用函数 np.cov 计算协方差矩阵。
值得注意的是,如果您希望 NumPy 使用列作为向量,则必须使用参数 rowvar=False 。另外,bias=True 除以 n 而不是除以 n-1。
我们先创建数组:
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
A = np.array([[1, 3, 5], [5, 4, 1], [3, 8, 6]])
现在我们将使用 NumPy 函数计算协方差:
np.cov(A, rowvar=False, bias=True)
用点积求协方差矩阵
还有另一种方法可以计算 A 的协方差矩阵。您可以将 A 以 0 为中心。从向量的每个元素中减去向量的均值,得到均值等于 0 的向量。将其与其自身的转置相乘,并除以观测值的数量。
让我们从一个实现开始,然后尝试理解与上一个方程的联系:
def calculateCovariance(X):
meanX = np.mean(X, axis = 0)
lenX = X.shape[0]
X = X - meanX
covariance = X.T.dot(X)/lenX
return covariance
print(calculateCovariance(A))
Output:
array([[ 2.66666667, 0.66666667, -2.66666667],
[ 0.66666667, 4.66666667, 2.33333333],
[-2.66666667, 2.33333333, 4.66666667]])
The dot product between two vectors can be expressed:
It is the sum of the products of each element of the vectors:
If we have a matrix A, the dot product between A and its transpose will give you a new matrix:
可视化数据和协方差矩阵
为了更深入地了解协方差矩阵及其用途,我们将创建一个函数来将其与 2D 数据一起可视化。您将能够看到协方差矩阵和数据之间的联系。
正如我们上面所看到的,该函数将计算协方差矩阵。它将创建两个子图 ——一个用于协方差矩阵,一个用于数据。 Seaborn 的 heatmap() 函数用于创建颜色渐变 - 小值将呈现为浅绿色,大值将呈现为深蓝色。我们选择了一种调色板颜色,但您可能更喜欢其他颜色。数据表示为散点图。
def plotDataAndCov(data):
ACov = np.cov(data, rowvar=False, bias=True)
print 'Covariance matrix:\n', ACov
fig, ax = plt.subplots(nrows=1, ncols=2)
fig.set_size_inches(10, 10)
ax0 = plt.subplot(2, 2, 1)
# Choosing the colors
cmap = sns.color_palette("GnBu", 10)
sns.heatmap(ACov, cmap=cmap, vmin=0)
ax1 = plt.subplot(2, 2, 2)
# data can include the colors
if data.shape[1]==3:
c=data[:,2]
else:
c="#0A98BE"
ax1.scatter(data[:,0], data[:,1], c=c, s=40)
# Remove the top and right axes from the data plot
ax1.spines['right'].set_visible(False)
ax1.spines['top'].set_visible(False)
不相关的数据
现在我们有了绘图函数,我们将生成一些随机数据来可视化协方差矩阵可以告诉我们什么。我们将从使用 NumPy 函数 np.random.normal() 从正态分布中提取的一些数据开始。
该函数需要分布的平均值、标准差和观测值数量作为输入。我们将创建两个包含 300 个观测值的随机变量,标准差为 1。第一个变量的平均值为 1,第二个变量的平均值为 2。如果我们从正态分布中随机抽取两组 300 个观测值,则两个向量将为不相关的。
np.random.seed(1234)
a1 = np.random.normal(2, 1, 300)
a2 = np.random.normal(1, 1, 300)
A = np.array([a1, a2]).T
A.shape
注 1:我们用 .T 转置数据,因为原始形状是 (2, 300),并且我们希望观察数为行(因此形状为 (300, 2))。
注 2:我们使用 np.random.seed 函数来提高重现性。下次运行单元时将使用相同的随机数。让我们检查一下数据是什么样子的:
A[:10,:]
array([[ 2.47143516, 1.52704645],
[ 0.80902431, 1.7111124 ],
[ 3.43270697, 0.78245452],
[ 1.6873481 , 3.63779121],
[ 1.27941127, -0.74213763],
[ 2.88716294, 0.90556519],
[ 2.85958841, 2.43118375],
[ 1.3634765 , 1.59275845],
[ 2.01569637, 1.1702969 ],
[-0.24268495, -0.75170595]])
很好,我们有两个列向量;现在,我们可以检查分布是否是正态的:
sns.distplot(A[:,0], color="#53BB04")
sns.distplot(A[:,1], color="#0A98BE")
plt.show()
plt.close()
We can see that the distributions have equivalent standard deviations but different means (1 and 2). So that’s exactly what we have asked for.
现在我们可以用我们的函数绘制数据集及其协方差矩阵:
plotDataAndCov(A)
plt.show()
plt.close()
Covariance matrix:
[[ 0.95171641 -0.0447816 ]
[-0.0447816 0.87959853]]
我们可以在散点图上看到两个维度不相关。请注意,我们有一个维度的平均值为 1(y 轴),另一个维度的平均值为 2(x 轴)。
此外,协方差矩阵显示每个变量的方差非常大(约为 1),而第 1 列和第 2 列的协方差非常小(约为 0)。由于我们确保两个向量是独立的,因此这是一致的。相反的情况不一定成立:协方差为 0 并不能保证独立性 https://stats.stackexchange.com/questions/12842/covariance-and-independence.
相关数据
现在,让我们通过指定一列和另一列来构造相关数据。
np.random.seed(1234)
b1 = np.random.normal(3, 1, 300)
b2 = b1 + np.random.normal(7, 1, 300)/2.
B = np.array([b1, b2]).T
plotDataAndCov(B)
plt.show()
plt.close()
Covariance matrix:
[[ 0.95171641 0.92932561]
[ 0.92932561 1.12683445]]
The correlation between the two dimensions is visible on the scatter plot. We can see that a line could be drawn and used to predict y from x and vice versa. The covariance matrix is not diagonal (there are non-zero cells outside of the diagonal). That means that the covariance between dimensions is non-zero.
从现在起,使用协方差矩阵,您可以进一步研究以下内容:
- 均值归一化
- 标准化或规范化
- 美白
- 零中心
- 去关联
- 重新缩放