CNN推理期间如何应用ZCA Whitening?

时间:2019-02-02 15:54:41

标签: python deep-learning image-preprocessing

我想实现ZCA增白小CNN。的输入是要成为72x72x3(HWC)BGR图像。

到目前为止我我从白化函数Pascal Timshels answer on ZCA whitening是:

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-6
  # 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

有关图像的预处理,我执行以下操作:

data = data / 255.0 # data.shape = (batch_size, H * W * C)
data_norm = data - data.mean(axis=0)
ZCA_M = zca_whitening_matrix(data_norm)
xZCA = np.dot(ZCA_M, data_norm)
xZCA = (xZCA - xZCA.min()) / (xZCA.max() - xZCA.min())

现在这个代码工作在训练批次。据我了解,人们可以为整个训练集计算zca白化矩阵,然后对整个数据进行白化。

由于该矩阵取决于批量大小,一个人如何应用稍后推理此矩阵(对于正好一个图片)?我只是平均吗?还是我误会了一些基本的东西...

感谢答案!

0 个答案:

没有答案