在此代码上,我从mnist为zca输入一个图像(数字)。 但我不确定我是应该输入整个数组还是仅输入图像。 我做错了吗?
它似乎可以改变图像,但我不知道每次应该是1张图像。
另外,我需要在ZCA之前应用标准化吗?
import numpy as np
import matplotlib.pyplot as plt
from AuxMethods import flatten_matrix
def zca_whitening(inputs):
# Correlation matrix
sigma = np.dot(inputs, inputs.T) / inputs.shape[1]
# Singular Value Decomposition
U, S, V = np.linalg.svd(sigma)
# Whitening constant, it prevents division by zero
epsilon = 0.1
# ZCA Whitening matrix
ZCAMatrix = np.dot(np.dot(U, np.diag(1.0 / np.sqrt(np.diag(S) + epsilon))), U.T)
# Data whitening
result = np.dot(ZCAMatrix, inputs)
print "sigma :",sigma
#print "U",U
# result = result.T
return result
def apply_zca_whitening(data, show_image=False, width=28, height=28):
aux_data = data.copy()
print "Processing images with ZCA...Please wait!"
for x in xrange(0, len(data[:, 0])):
#whitened = zca_whitening(flatten_matrix(((data[x, :].T).reshape((width, height)).T).astype(np.float32)))
whitened = zca_whitening(flatten_matrix(((data[x, :].T).reshape((width, height)).T)))
image_aux = np.copy(whitened)
#image_aux = image_aux.astype(np.float32)
aux_data[x, :] = image_aux.tolist()
if show_image is True:
for x in xrange(0, len(aux_data[:, 0])):
img = aux_data[x, :]
image = np.reshape(img, (width, height))
imgplot = plt.imshow(image, cmap=plt.gray())
plt.show()
print "ZCA processing : Done!"
return aux_data