为什么PCA图像与原始图像完全不相似?

我正在尝试在没有使用任何库的情况下实现PCA来进行图像降维。我尝试了O’Reilly计算机视觉书中的代码,并将其应用于一个样本的lenna图片上:

    from PIL import Image    from numpy import *    def pca(X):        num_data, dim = X.shape        mean_X = X.mean(axis=0)        X = X - mean_X        if dim > num_data:            # PCA compact trick            M = np.dot(X, X.T) # covariance matrix            e, U = np.linalg.eigh(M) # calculate eigenvalues an deigenvectors            tmp = np.dot(X.T, U).T            V = tmp[::-1] # reverse since the last eigenvectors are the ones we want            S = np.sqrt(e)[::-1] #reverse since the last eigenvalues are in increasing order            for i in range(V.shape[1]):                V[:,i] /= S        else:            # normal PCA, SVD method            U,S,V = np.linalg.svd(X)            V = V[:num_data] # only makes sense to return the first num_data        return V, S, mean_Ximg=color.rgb2gray(io.imread('D:\lenna.png'))x,y,z=pca(img)plt.imshow(x)

原始图像PCA图像

但是PCA图像的显示结果与原始图像完全不相似。据我所知,PCA确实会降低图像的维度,但它仍然应该在某种程度上保留原始图像的轮廓,只是细节较少。代码哪里出了问题?


回答:

严格来说,你的代码本身并没有问题,但如果你明白你真正想要做的是什么,你展示的内容并不是正确的!

针对你的问题,我会这样写代码:

def pca(X, number_of_pcs):    num_data, dim = X.shape    mean_X = X.mean(axis=0)    X = X - mean_X    if dim > num_data:        # PCA compact trick        M = np.dot(X, X.T) # covariance matrix        e, U = np.linalg.eigh(M) # calculate eigenvalues an deigenvectors        tmp = np.dot(X.T, U).T        V = tmp[::-1] # reverse since the last eigenvectors are the ones we want        S = np.sqrt(e)[::-1] #reverse since the last eigenvalues are in increasing order        for i in range(V.shape[1]):            V[:,i] /= S        return V, S, mean_X    else:        # normal PCA, SVD method        U, S, V = np.linalg.svd(X, full_matrices=False)        # reconstruct the image using U, S and V        # otherwise you're just outputting the eigenvectors of X*X^T        V = V.T        S = np.diag(S)        X_hat = np.dot(U[:, :number_of_pcs], np.dot(S[:number_of_pcs, :number_of_pcs], V[:,:number_of_pcs].T))              return X_hat, S, mean_X

这里的变化在于我们希望使用给定的特征向量数量(由number_of_pcs决定)来重建图像。

需要记住的是,在np.linalg.svd中,U的列是X.X^T的特征向量。

这样做,我们可以得到以下结果(这里显示了使用1和10个主成分的结果):


X_hat, S, mean_X = pca(img, 1)plt.imshow(X_hat)

输入图像描述


X_hat, S, mean_X = pca(img, 10)plt.imshow(X_hat)

输入图像描述

附注:请注意,由于matplotlib.pyplot的原因,图片并未以灰度显示,但这是一个非常次要的问题。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注