- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 python 通过主成分分析 (PCA) 实现人脸识别。我正在按照本教程中的步骤操作:http://onionesquereality.wordpress.com/2009/02/11/face-recognition-using-eigenfaces-and-distance-classifiers-a-tutorial/
这是我的代码:
import os
from PIL import Image
import numpy as np
import glob
import numpy.linalg as linalg
#Step1: put database images into a 2D array
filenames = glob.glob('C:\\Users\\Karim\\Downloads\\att_faces\\New folder/*.pgm')
filenames.sort()
img = [Image.open(fn).convert('L').resize((90, 90)) for fn in filenames]
images = np.asarray([np.array(im).flatten() for im in img])
#Step 2: find the mean image and the mean-shifted input images
mean_image = images.mean(axis=0)
shifted_images = images - mean_image
#Step 3: Covariance
c = np.cov(shifted_images)
#Step 4: Sorted eigenvalues and eigenvectors
eigenvalues,eigenvectors = linalg.eig(c)
idx = np.argsort(-eigenvalues)
eigenvalues = eigenvalues[idx]
eigenvectors = eigenvectors[:, idx]
#Step 5: Only keep the top 'num_eigenfaces' eigenvectors
num_components = 20
eigenvalues = eigenvalues[0:num_components].copy()
eigenvectors = eigenvectors[:, 0:num_components].copy()
#Step 6: Finding weights
w = eigenvectors.T * np.asmatrix(shifted_images)
#Step 7: Input image
input_image = Image.open('C:\\Users\\Karim\\Downloads\\att_faces\\1.pgm').convert('L').resize((90, 90))
input_image = np.asarray(input_image)
#Step 8: get the normalized image, covariance, eigenvalues and eigenvectors for input image
shifted_in = input_image - mean_image
cov = np.cov(shifted_in)
eigenvalues_in, eigenvectors_in = linalg.eig(cov)
我收到一个错误: Traceback (most recent call last):
File "C:/Users/Karim/Desktop/Bachelor 2/New folder/new3.py", line 47, in <module>
shifted_in = input_image - mean_image
ValueError: operands could not be broadcast together with shapes (90,90) (8100)
我试图删除 .flatten()
来自第 1 步,但这在计算特征值和特征向量时产生了另一个错误: Traceback (most recent call last):
File "C:/Users/Karim/Desktop/Bachelor 2/New folder/new3.py", line 25, in <module>
eigenvalues,eigenvectors = linalg.eig(c)
File "C:\Python27\lib\site-packages\numpy\linalg\linalg.py", line 1016, in eig
_assertRank2(a)
File "C:\Python27\lib\site-packages\numpy\linalg\linalg.py", line 155, in _assertRank2
'two-dimensional' % len(a.shape))
LinAlgError: 4-dimensional array given. Array must be two-dimensional
我还尝试添加 .flatten()
到第 7 步,但在计算输入图像的特征值和特征向量时也会产生另一个错误: Traceback (most recent call last):
File "C:/Users/Karim/Desktop/Bachelor 2/New folder/new3.py", line 49, in <module>
eigenvalues_in, eigenvectors_in = linalg.eig(cov)
File "C:\Python27\lib\site-packages\numpy\linalg\linalg.py", line 1016, in eig
_assertRank2(a)
File "C:\Python27\lib\site-packages\numpy\linalg\linalg.py", line 155, in _assertRank2
'two-dimensional' % len(a.shape))
LinAlgError: 0-dimensional array given. Array must be two-dimensional
谁能帮忙??
最佳答案
我终于看了你提供的教程,作者似乎建议你把图像展平。您现在不妨继续使用扁平化数组,因为它更适合该教程。
我相信修复它的地方是在步骤 7 中,您在该处具有输入图像的协方差。然而,输入图像的协方差矩阵将是一个标量,你无法找到它的特征值和特征向量。您可以将其投影为大小为 (1,1)
的二维矩阵,但是您的特征值将只是协方差,特征向量将为 [[1]]
.
也就是说,例如,
In [563]: input_image = np.random.rand(90,90).flatten()
In [564]: c = np.cov(input_image)
In [565]: c
Out[565]: array(0.08280644230318886)
In [566]: c.shape
Out[566]: ()
In [567]: c.ndim
Out[567]: 0
所以我们将 c
reshape 为 2d:
In [568]: cmat = c.reshape(1,1) # equivalent to cmat = c[...,np.newaxis,np.newaxis]
In [569]: cmat
Out[569]: array([[ 0.08280644]])
In [570]: cmat.shape
Out[570]: (1, 1)
In [571]: cmat.ndim
Out[571]: 2
现在我们可以找到特征值:
In [572]: ceigval, ceigvec = linalg.eig(cmat)
但是对于单元素矩阵,只有一个特征值和一个特征向量,特征值是矩阵的元素,特征向量是长度为1的单位向量/恒等式,所以我不确定这个这真的是您想为人脸识别做的事情。
In [573]: ceigval
Out[573]: array([ 0.08280644])
In [574]: ceigvec
Out[574]: array([[ 1.]])
In [576]: np.isclose(c, ceigval)
Out[576]: True
顺便说一句,这就是为什么我们必须制作 c
2d:
In [577]: linalg.eig(c)
---------------------------------------------------------------------------
LinAlgError: 0-dimensional array given. Array must be two-dimensional
另一方面,您可以获得未展平的 input_image 的协方差,然后您将拥有 N
个特征值和 N
个特征向量:
In [582]: input_image = np.random.rand(90,90)
In [583]: c = np.cov(input_image)
In [584]: c.shape
Out[584]: (90, 90)
In [585]: ceigval, ceigvec = linalg.eig(c)
In [586]: ceigval.shape
Out[586]: (90,)
In [587]: ceigvec.shape
Out[587]: (90, 90)
关于python - 使用 python 的主成分分析 (PCA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16018657/
我听说过两种数据库架构。 大师级 主从 master-master不是更适合现在的web吗,因为它就像Git一样,每个单元都有整套数据,如果一个宕机也无所谓。 主从让我想起了 SVN(我不喜欢它),你
我们当前将 MySQL 配置为支持故障转移:Site1 Site2。当它们被设置为主/主时。在给定时间点,应用程序服务器仅主动写入一个站点。我们想要设置一个新的故障转移站点。然后我们将拥有 Site
我听说过两种数据库架构。 大师-大师 主从 master-master 不是更适合当今的网络吗,因为它就像 Git,每个单元都有整套数据,如果其中一个发生故障,也没关系。 主从让我想起 SVN(我不喜
我正在创建一个标记为类别的表,其中主类别(父列)包含 0,子类别包含父类别的 ID。我听说这叫引用。我的问题:这张表的结构正确吗?或者是否有更好的方法,例如实现遍历树或类似方法? CREATE TAB
我正在阅读一份关于 C++ 与 C 的文档。该文档说与 C 相比,C++ 编写得非常紧凑。一个例子是,C 允许 main() 函数类型为 void。另一方面,C++ 不允许这样做,他给出了标准中的以下
C main函数和Java main函数有什么区别? int main( int argc, const char* argv[] ) 对比 public static void main(Strin
我一直摸不着头脑,但运气不好。设计器有一个包含 3 栏的站点、两个侧边栏和一个主要内容区域。 专为桌面设计,左栏、主要内容、右栏。但是,在较小的设备上,我们希望首先堆叠主要内容。 所以通常情况下,你可
我一直在阅读有关 Jenkins 主/从配置的信息,但我仍然有一些问题: 是不是真的没有像 Jenkins 主站那样安装和启动从站 Jenkins?我假设我会以相同的方式安装一个主 Jenkins 和
据我了解,Viemodel中MVVM背后的概念包括业务逻辑和/或诸如暴露于 View 的数据的主/明细关系之类的事物 因此,正如我发现的那样,有很多ORM生成器,例如模型的telerik a.o以及另
我们有一个群集,其中包含3个主分区,每个主分区有2个副本。主/副本分片的总文档数相同;但是,对于同一查询/文档,我们得到3个不同的分数。当我们将preference = primary添加为查询参数时
我有一个非常大/旧/长时间运行的项目,它使用相对于启动目录的路径访问文件资源(即应用程序仅在从特定目录启动时才工作)。当我需要调试程序时,我可以从 eclipse 启动它并使用“运行配置”->->“工
谁能向我解释一下为什么我在这段代码上遇到段错误?我一直试图弄清楚这一点,但在各种搜索中却一无所获。当我运行代码而不调用 main(argc, argv) 时,它会运行。 Slave 仅将 argv 中
使用 xcode 中的默认项目作为主从应用程序,如果我在折叠委托(delegate)中放置 print 调试语句,当我旋转设备时它似乎永远不会被触发(事实上我永远无法触发它)。 我编辑的代码位于 Ap
是否有任何产品可以使 mysql 主/从故障转移过程更容易?一些可以自动发生的事情,而不是手动修复它。 最佳答案 [...稍后...;) 你所说的“更容易”是什么?MySQL 有很多解决方案: MyS
我有两个 mysql 数据库。我想做主/主复制。 复制以一种方式进行。然而,反过来说却不然。该错误表明它无法与用户“test@IPADDRESS”连接。 如何将用户名更改为 repl?从未进行过测试,
我正在尝试在 MySQL 中运行以下查询: GRANT REPLICATION SLAVE ON *.* TO 'replication'@’10.141.2.%’ IDENTIFIED BY ‘sl
我正在尝试使用 Android 提供的主/详细流程模板创建一个应用程序,并且我正在尝试将多个操作栏菜单项添加到操作栏的主要部分和详细信息部分。这就是我要实现的目标: (来源:softwarecrew.
我正在寻找一个跨平台的 C++ master/worker 库或工作队列库。一般的想法是我的应用程序将创建某种任务或工作对象,将它们传递给工作主机或工作队列,这将依次在单独的线程或进程中执行工作。为了
我似乎看到很多人在他们的 MySQL 模式中任意分配大尺寸的主/外键字段,例如 INT(11) 甚至 WordPress 使用的 BIGINT(20)。 如果我错了,请纠正我,但即使是 INT(4)
如果我有一个可以与多个键相关联的用户,正确的表设置应该是: 一个表有两列,例如: UserName | Key 没有主键且用户可以有多行,或者: 具有匹配标识符的两个表 Table 1 Us
我是一名优秀的程序员,十分优秀!