本文介绍了如何实施ZCA美白? Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施 ZCA美白并找到了一些相关文章,但它们有些令人困惑..有人能为我照耀吗?

Im trying to implement ZCA whitening and found some articles to do it, but they are a bit confusing.. can someone shine a light for me?

感谢任何提示或帮助!

这是我读过的文章:

http://courses.media.mit.edu/2010fall/mas622j/whiten.pdf http://bbabenko.tumblr .com/post/86756017649/learning-low-level-vision-feautres-in-10-lines-of

我尝试了几件事,但其中大多数我不理解,因此被锁在了某个步骤.现在,我以此为基础再次开始:

I tried several things but most of them i didnt understand and i got locked at some step.Right now i have this as base to start again :

dtype = np.float32
data = np.loadtxt("../inputData/train.csv", dtype=dtype, delimiter=',', skiprows=1)
img = ((data[1,1:]).reshape((28,28)).astype('uint8')*255)

推荐答案

以下是用于生成ZCA白化矩阵的python函数:

Here is a python function for generating the ZCA whitening matrix:

def zca_whitening_matrix(X):
    """
    Function to compute ZCA whitening matrix (aka Mahalanobis whitening).
    INPUT:  X: [M x N] matrix.
        Rows: Variables
        Columns: Observations
    OUTPUT: ZCAMatrix: [M x M] matrix
    """
    # Covariance matrix [column-wise variables]: Sigma = (X-mu)' * (X-mu) / N
    sigma = np.cov(X, rowvar=True) # [M x M]
    # Singular Value Decomposition. X = U * np.diag(S) * V
    U,S,V = np.linalg.svd(sigma)
        # U: [M x M] eigenvectors of sigma.
        # S: [M x 1] eigenvalues of sigma.
        # V: [M x M] transpose of U
    # Whitening constant: prevents division by zero
    epsilon = 1e-5
    # ZCA Whitening matrix: U * Lambda * U'
    ZCAMatrix = np.dot(U, np.dot(np.diag(1.0/np.sqrt(S + epsilon)), U.T)) # [M x M]
    return ZCAMatrix

以及用法示例:

X = np.array([[0, 2, 2], [1, 1, 0], [2, 0, 1], [1, 3, 5], [10, 10, 10] ]) # Input: X [5 x 3] matrix
ZCAMatrix = zca_whitening_matrix(X) # get ZCAMatrix
ZCAMatrix # [5 x 5] matrix
xZCAMatrix = np.dot(ZCAMatrix, X) # project X onto the ZCAMatrix
xZCAMatrix # [5 x 3] matrix

希望有帮助!

EdgarAndrésMargffoy Tuay 的答案不正确的原因的详细信息:如前所述在 RM 的, EdgarAndrésMargffoy Tuay 的ZCA白化功能包含一个小而关键的错误:应删除np.diag(S). Numpy以m x 1向量而不是m x m矩阵的形式返回S(这在其他svd实现中很常见,例如Matlab).因此,ZCAMatrix变量变为m x 1向量,而不是应有的m x m矩阵(当输入为m x n时). (此外,只有当X预先居中时,Andfoy答案中的协方差矩阵才有效,即均值为0.)

Details for why Edgar Andrés Margffoy Tuay's answer is not correct: As pointed out in R.M's comment, Edgar Andrés Margffoy Tuay's ZCA whitening function contains a small, but crucial mistake: the np.diag(S) should be removed. Numpy returns S as a m x 1 vector and not a m x m matrix (as is common to other svd implementations, e.g. Matlab). Hence the ZCAMatrix variable becomes a m x 1 vector and not a m x m matrix as it should be (when the input is m x n). (Also, the covariance matrix in Andfoy's answer is only valid if X is pre-centered, i.e mean 0).

ZCA的其他参考:您可以在Python中看到斯坦福UFLDL ZCA Whitening练习的完整答案此处.

Other references for ZCA: You can see the full answer, in Python, to the Stanford UFLDL ZCA Whitening exercise here.

这篇关于如何实施ZCA美白? Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 07:36