我正在训练1000张28×28大小的图像。但在训练之前,我参考如何实现ZCA白化?Python对我的数据进行了ZCA白化处理。
由于我有1000张28×28大小的数据图像,经过扁平化处理后变为1000×784。但根据下面的代码,X是否是我的1000×784的图像数据集?
如果是这样的话,那么ZCAMatrix的大小应该是1000×1000。在这种情况下,用于预测的图像大小为28×28,或者可以说,尺寸为1×784。因此,将ZCAMatrix乘以图像是没有意义的。
所以我认为,X是图像数据集的转置。我的理解正确吗?如果我正确的话,那么ZCAMatrix的大小应该是784×784。
现在我应该如何计算ZCA白化后的图像,我应该使用np.dot(ZCAMatrix, transpose_of_image_to_be_predict)
还是np.dot(image_to_be_predict, ZCAMatrix)
?非常欢迎建议。
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] matrixZCAMatrix = zca_whitening_matrix(X) # get ZCAMatrixZCAMatrix # [5 x 5] matrixxZCAMatrix = np.dot(ZCAMatrix, X) # project X onto the ZCAMatrixxZCAMatrix # [5 x 3] matrix
回答:
我参考了可在这里找到的Keras代码。
很明显,在我的情况下,协方差矩阵将生成一个784×784的矩阵,并对其进行奇异值分解。它会生成三个矩阵,用于计算主成分,然后使用这些主成分来找到ZCA白化后的数据。
现在我的问题是
我应该如何计算ZCA白化后的图像,是使用np.dot(ZCAMatrix, transpose_of_image_to_be_predict)还是np.dot(image_to_be_predict, ZCAMatrix)?非常欢迎建议。
对此,我参考了这里的资料。
在这里,我需要使用np.dot(image_to_be_predict, ZCAMatrix)
来计算ZCA白化后的图像。