如何实施ZCA美白?蟒蛇

9 python correlated pca image-preprocessing

我试图实施ZCA美白并找到一些文章来做,但它们有点令人困惑..有人可以为我照亮吗?

任何提示或帮助表示赞赏!

这是我读过的文章:

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

我尝试了几件事,但大多数我都不明白,我被锁定了一步.现在我以此为基础重新开始:

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

Pas*_*hel 12

这是一个用于生成ZCA白化矩阵的python函数:

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
Run Code Online (Sandbox Code Playgroud)

以及用法示例:

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
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!

详细说明为什么EdgarAndrésMargffoyTuay的答案不正确:正如RM评论所指出的,EdgarAndrésMargffoyTuay的ZCA美白功能包含一个小而重要的错误:np.diag(S)应该删除.Numpy返回S为amx 1向量而不是amxm矩阵(这与其他svd实现(例如Matlab)相同).因此,ZCAMatrix变量变为amx 1向量而不是amxm矩阵,因为它应该是(当输入是mxn时).(另外,Andfoy答案中的协方差矩阵仅在X预先居中时才有效,即均值为0).

对于ZCA其他参考资料:你可以看到完整的答案,在Python,斯坦福UFLDL ZCA美白演习在这里.


Edg*_*uay 10

您的数据是否存储在mxn矩阵中?其中m是数据的维数,n是案例的总数?如果不是这样,您应该调整数据大小.例如,如果您的图像大小为28x28并且您只有一个图像,则应该具有1x784向量.你可以使用这个功能:

import numpy as np

def flatten_matrix(matrix):
    vector = matrix.flatten(1)
    vector = vector.reshape(1, len(vector))
    return vector
Run Code Online (Sandbox Code Playgroud)

然后使用以下方法将ZCA Whitening应用于训练集:

def zca_whitening(inputs):
    sigma = np.dot(inputs, inputs.T)/inputs.shape[1] #Correlation matrix
    U,S,V = np.linalg.svd(sigma) #Singular Value Decomposition
    epsilon = 0.1                #Whitening constant, it prevents division by zero
    ZCAMatrix = np.dot(np.dot(U, np.diag(1.0/np.sqrt(np.diag(S) + epsilon))), U.T)                     #ZCA Whitening matrix
    return np.dot(ZCAMatrix, inputs)   #Data whitening
Run Code Online (Sandbox Code Playgroud)

保存ZCAMatrix矩阵非常重要,如果要在训练神经网络后进行预测,则应将测试用例相乘.

最后,我邀请您参加http://ufldl.stanford.edu/wiki/index.php/UFLDL_Tutorialhttp://ufldl.stanford.edu/tutorial/上的斯坦福大学UFLDL教程.他们对MATLAB有很好的解释和一些编程练习,但几乎所有在MATLAB上找到的函数都在同名的Numpy上.我希望这可以提供一个见解.