计算机视觉的统计帮助[关闭]

2024-02-19

我正在计算机视觉领域做我的毕业项目,我只修了一门讨论非常基本概念的统计学课程,现在我在相当高级的主题上面临更多困难,所以我需要帮助(书籍、教程、课程) ,..等)掌握和回顾统计学的基本思想和概念,然后深入研究细节(统计细节)用于计算机视觉。


您可以使用此混淆矩阵 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 计算解释

  1. 计算真阳性 (TP)
  2. 误报计数 (FP)
  3. 漏报计数 (FN)
  4. 交点 = TP
  5. 并集 = TP + FP + FN
  6. 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(平均精度):

  1. https://www.youtube.com/watch?v=pM6DJ0ZZee0 https://www.youtube.com/watch?v=pM6DJ0ZZee0
  2. https://towardsdatascience.com/breaking-down-mean-average- precision-map-ae462f623a52#1a59 https://towardsdatascience.com/breaking-down-mean-average-precision-map-ae462f623a52#1a59
  3. 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: enter image description here

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: enter image description here

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: enter image description here

这是协方差矩阵的第一个单元格。对角线上的第二个元素对应于 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): enter image description here

协方差矩阵中的位置。列对应于第一个变量,行对应于第二个变量(或相反)。 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: enter image description here

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: enter image description here

  1. 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: enter image description here

  2. Multiply the result with the first element of Y minus the mean of the vector Y: enter image description here

  3. Reiterate the process for each element of the vectors and calculate the sum of all results: enter image description here

  4. 除以向量中的元素数量。

EXAMPLE - Let’s start with the matrix A: enter image description here

We will calculate the covariance between the first and the third column vectors: enter image description here and enter image description here

Which is x̄=3, ȳ=4, and n=3 so we have: enter image description here

代码示例 -

使用 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: enter image description here

It is the sum of the products of each element of the vectors: enter image description here

If we have a matrix A, the dot product between A and its transpose will give you a new matrix: enter image description here

可视化数据和协方差矩阵

为了更深入地了解协方差矩阵及其用途,我们将创建一个函数来将其与 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()

enter image description here 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]]

enter image description here 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.

从现在起,使用协方差矩阵,您可以进一步研究以下内容:

  1. 均值归一化
  2. 标准化或规范化
  3. 美白
  4. 零中心
  5. 去关联
  6. 重新缩放
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

计算机视觉的统计帮助[关闭] 的相关文章

  • 使用畸变从图像平面计算相机矢量

    我正在尝试使用相机模型来重建可以使用某些相机及其 外部 内部 参数拍摄的图像 这一点我没有任何问题 现在我想添加扭曲 正如它们中所描述的那样OpenCV https docs opencv org 4 x dc dbb tutorial p
  • 计算流数据的直方图 - 在线直方图计算

    我正在寻找一种算法来生成大量流数据的直方图 最大值和最小值事先未知 但标准差和平均值在特定范围内 我很欣赏你的想法 Cheers 我刚刚找到了一个解决方案 秒 从流式并行决策树算法构建在线直方图 论文的 2 2 该算法由 Hive 项目中的
  • 好的 PHP 开源分析/统计软件吗? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在构建的网址缩短服务需要向用户显示一些基本的点击统计信息 点击次数 转化次数 引用域和国家 地区
  • opencv createsamples没有错误,但是没有找到样本

    我在用着this http coding robin de 2013 07 22 train your own opencv haar classifier html教程 我正在根据我的正面图像创建大量样本 我正在使用 Windows 这是
  • PostgreSQL 对 string\varchar 的各种清理

    我必须通过以下方式清理一些 varchar 删除特殊字符 例如 来自封闭列表 我已经成功地通过大量使用replace regexp replace来做到这一点 但我正在寻找类似于SQL Server中的东西 删除以下数字但不删除相邻的数字含
  • 解释 scipy.stats.entropy 值

    我正在尝试使用scipy stats 熵来估计库尔巴克 莱布勒 KL 两个分布之间的散度 更具体地说 我想使用 KL 作为衡量标准来确定两个分布的一致性 但是 我无法解释 KL 值 例如 t1 numpy random normal 2 5
  • 当两个模式共享“when”子句时,模式匹配不完整

    A 共同的惊喜 https stackoverflow com q 18691622 2314532对于 F 初学者来说 以下事实是不完全匹配 let x y 5 10 match something with when x lt y gt
  • 从点云检测平面集

    我有一组点云 我想测试3D房间中是否有角落 所以我想讨论一下我的方法 以及在速度方面是否有更好的方法 因为我想在手机上测试它 我将尝试使用霍夫变换来检测线 然后我将尝试查看是否有三条线相交 并且它们也形成了两个相交的平面 如果点云数据来自深
  • 计算互相关函数?

    In R 我在用ccf or acf计算成对互相关函数 以便我可以找出哪个移位给我带来最大值 从它的外观来看 R给我一个标准化的值序列 Python 的 scipy 中是否有类似的东西 或者我应该使用fft模块 目前 我正在这样做 xcor
  • 创建新的保护子句

    在 Elixir 中 我将如何为函数创建新的保护子句 显然 我已经看到你不能只调用 a 中的任何函数when声明 但如果能够做这样的事情那就太好了 defmodule Player do def play card player do de
  • 如何加速 svm.predict?

    我正在编写一个滑动窗口来提取特征并将其输入到 CvSVM 的预测函数中 然而 我偶然发现 svm predict 函数相对较慢 基本上 窗口以固定的步幅长度在图像比例上滑动穿过图像 遍历图像加上提取每个图像特征的速度 窗口大约需要 1000
  • 与heroku配合使用的统计引擎

    我有一个 Heroku Rails 应用程序 需要处理一些重要的数字 并且我需要使用像 R 这样的统计库 更糟糕的是 MatLab 我正在寻找以下任何问题的答案 是否有不需要二进制文件的功能齐全的统计包 GEM 是否可以将 R 二进制文件作
  • Fast R-CNN 中 ROI 层的用途是什么?

    In this https leonardoaraujosantos gitbooks io artificial inteligence content object localization and detection html关于目标
  • 匹配没有周围字符列表的单词列表

    我有这个正则表达式 one common word or another 除非这两个单词相邻 否则它匹配得很好 One one s more word word common word or another word more anothe
  • 计算数据集列的百分位数

    最亲爱的 R 专家 为您快速介绍一下 我正在做一项作业 在这个练习中 我被要求从数据中获取基本统计数据infert数据集 它是内置的 特别是其中的一列 infert age 对于不熟悉数据集的人 gt table ages Which is
  • Scala 模式匹配与 Option[Any] 的混淆

    我有以下 Scala 代码 import scala actors Actor object Alice extends Actor this start def act loop react case Hello gt sender Hi
  • Python绕相机轴旋转图像

    假设我有一个图像 是在对某些原始图像应用单应性变换 H 后获得的 未显示原始图像 将单应性 H 应用于原始图像的结果是该图像 我想围绕合适的轴 可能是相机所在的位置 如果有的话 将此图像旋转 30 度以获得此图像 如果我不知道相机参数 如何
  • 来自 data.frame 每一列的随机样本

    我想从 a 的每一行中抽取随机样本data frame独立于其他行 这是一个例子 此代码为每行选择相同的列 但我需要为每行独立选择列 library plyr set seed 12345 df1 lt mdply data frame m
  • 在 opencv 中一次性将旋转和平移结合起来

    我有一段用于旋转和平移图像的代码 Point2f pt 0 in rows double angle atan trans c trans b 180 M PI Mat r getRotationMatrix2D pt angle 1 0
  • 解释 survreg 中的威布尔参数

    我正在尝试使用从 R 中的 survreg 估计的参数生成逆威布尔分布 我的意思是 对于给定的概率 这将是在 MS Excel 中实现的小型模拟模型中的随机数 返回使用我的参数预计出现故障 的时间 我理解逆威布尔分布的一般形式是 X b l

随机推荐

  • 为什么这段代码不起作用?刽子手

    我正在创建一个刽子手游戏 一切正常 我已经准备好了代码 可用于使游戏失败并给猜测值 1 尽管当将它添加到 else 语句中时 它会重复等于单词的长度 并且它也会给出一个猜测 即使它是正确的 我没看出代码有什么问题 我相信这是我的代码在猜测错
  • 假脱机多个文件

    我有一份报告 需要将 n 个供应商导出到 csv 文件 我有一种感觉 我需要多次运行它 所以我想尽可能地自动化它 我为一个供应商编写了所需的 sql plus 我想知道如何编写脚本来为每个供应商运行 我将供应商列表存储在数据库的表中 但知道
  • Rails - 当模型验证失败时,URL 更改会令人困惑

    我有一个用户资源在哪里 name是模型上的必需属性 如果我尝试创建一个新用户without一个名称 则验证失败并且错误消息按预期显示在表单顶部 but页面的 URL 更改为 users new to users 直到今晚我第一次开始玩水豚时
  • Microsoft Azure 备份在保留期缩短后不会减少可用恢复点或目标使用情况

    在保留期缩短后 Microsoft Azure 备份不会减少可用恢复点或目标使用情况 我将保留期设置为 30 天 备份量约为 6 8TB 一周前 我将保留期更改为 7 天 花了几天时间总恢复点才降至 7 个 使用率仍在上升 我今天 星期一
  • Django 每个应用程序的不同设置

    如何为每个应用程序使用不同的设置 例如 http www mysite com app1 http www mysite com app1使用 app1 文件夹的 settings py local settings py http www
  • git 合并不同的存储库?

    我所有的项目都使用 SVN 有时项目 B 是项目 A 的副本 当项目 A 有一般性变更时 我可以使用svn merge A在目录 B 中 它将合并这些更改 现在 如果我想使用 git 我不喜欢将所有项目放在同一个存储库中 因为这样我就必须克
  • WPF:找不到 Microsoft_Windows_Themes

    我在 WPF 应用程序中收到此错误 找不到类型 Microsoft Windows Themes ScrollChrome 验证您没有缺少程序集引用并且所有引用的程序集均已构建 任何想法 确保将此引用添加到控件 页面 资源字典 其他内容的最
  • 读取外部网站提交的 Angular 中的 POST 表单

    我正在开发一个网站 后端使用 Java 前端使用 Angular 有一种情况 一些外部网站可能会使用POST形式向我的网站发送数据 例如 General请求网址 https myangularwebsite 请求方式 POST 请求标头内容
  • 我可以在四元数中切换 X Y Z 吗?

    我有一个 Y 轴向上的坐标系 我需要将其转换为 Z 向上的坐标系 我将旋转存储在四元数中 所以我的问题是 如果我有一个四元数 X Y Z 我可以用 Z 切换 Y 并得到 Z 实际上是 UP 的结果吗 只是交换四元数中的两个轴 不 这不起作用
  • Azure Bicep - 有条件地创建一个秘密

    我正在使用 Bicep 创建一个 KeyVault 并且我想在保管库中创建一个秘密 但前提是还没有给定名称的秘密 检查 KeyVault 是否存在不起作用 因此我现在正在检查特定标签是否存在 创建 Vault 时 我在资源组中写入一个标签
  • 如何查看.RData 文件中的数据?

    我必须加载 isfar RData 文件才能在其他计算中使用它 此处描述并不重要 我想简单地看看 isfar RData 文件中的数据如何 例如它携带什么数字 列 行 首先我加载我的文件 isfar lt load C Users isfa
  • 打印 NSMutableURLRequest 内容

    我想问是否有人尝试过打印 NSMutableURLRequest request 的值 这是我的场景 我已经形成了我的 XML 并尝试使用 Firefox Poster 插件发送它 我成功地处理了有效和无效的内容 所以是时候进入 iOS 了
  • 全面的初学者 virtualenv 教程? [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我最近听到有关 virtualenv 的传闻 我很感兴趣 但我所听到的只是一些赞扬 并不清楚它是什么或如何使用它 我正在寻找 理想情况下
  • java中如何调用一个线程在特定时间运行?

    我想让线程在特定的确切时间执行 例如 2012 07 11 13 12 24 和 2012 07 11 15 23 45 我检查了ScheduledExecutorService 但它只支持在第一次运行后的特定时间段后执行 而且我没有任何固
  • UITableView 在动画过程中崩溃,已找到解决方案,但没有找到根本原因,想知道为什么?

    在我的iphone项目中 我总是将UITableView作为IBOutlet插入到视图控制器中 大多数时候它运行良好 但是当popToRootViewControllerAnimated调用动画时会发生随机崩溃 通过僵尸跟踪 发现崩溃是由于
  • 如何构建 JSON 对象?

    目前我通过执行以下操作构建 JSON 对象 users User all users each do user userlist lt lt id gt user id fname gt user fname lname gt user l
  • 使用 C# 和 WIN32 滚动记事本

    我正在尝试使用 C 应用程序滚动记事本窗口 相关的代码块如下 移动 调整窗口大小的调用有效 所以我知道句柄是有效的 请你看看我错过了什么 运行时什么也没有发生 Flags public enum SetWindowPosFlags uint
  • 如何在启动新窗口时关闭当前窗口(在代码中)

    SignInWindow signIn new SignInWindow signIn ShowDialog 上面的代码位于我的 MainWindow 类中 当显示新窗口时 我希望关闭当前窗口 最好的方法是什么 我的应用程序是 C WPF
  • 使用 twitter bootstrap 创建工具提示/弹出窗口后的回调函数?

    我想在使用 twitter bootstrap 创建工具提示或弹出窗口后对其进行操作 据我所知 没有构建方法可以做到这一点 selector popover placement bottom 例如 假设我想将创建的工具提示从计算的位置向上移
  • 计算机视觉的统计帮助[关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在计算机视觉领域做我的毕业项目 我只修了一门讨论非常基本概念的统计学课程 现在我在相当高级的主题上面