特征白化生成相同的图像
Feature Whitening generates same image
我正在查看此处显示的特征白化示例:How to implement ZCA Whitening? Python
我已经实现了下面的代码,但是每当我 运行 它时,我在所有 4 张图片中得到相同的图像,我不确定为什么:
读取数据的代码(工作正常)
import cv2
import matplotlib.pyplot as plt
from urllib.request import urlopen, Request
new_size = 50
urls = ['https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg',
'https://dogtime.com/assets/uploads/2018/10/puppies-cover.jpg',
'https://www.readersdigest.ca/wp-content/uploads/2013/03/6-facts-to-know-before-owning-a-puppy.jpg',
'https://images.newscientist.com/wp-content/uploads/2021/06/03141753/03-june_puppies.jpg']
puppies = []
for url in urls:
r = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
response = urlopen(r).read()
arr = np.asarray(bytearray(response), dtype=np.uint8)
puppies.append(cv2.imdecode(arr, 0)) # 'Load it as it is'
#item_pics = [cv2.imread( './pictures/picture_'+ str(x) + '.jpeg', 0) for x in range(1,5)]
puppies_scaled = [cv2.resize(x, [new_size,new_size], interpolation = cv2.INTER_AREA) for x in puppies]
puppies_vectors = [x.reshape(new_size * new_size) for x in puppies_scaled]
美白代码
#borrowed heavily from
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-25
# 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
美白代码
combined_data = np.array(puppies_vectors).T
my_zca_transformer_image_stuff = zca_whitening_matrix(combined_data)
xZCAMatrix = np.dot(my_zca_transformer_image_stuff, combined_data)
reshaped_pics = [xZCAMatrix[:,i].reshape(new_size,new_size) for i in range(4)]
结果(所有图片都一样)
对zero-mean数据进行白化处理。这意味着,矩阵是根据 zero-mean 数据计算的,并且它们也应该应用于 zero-mean 数据。如果不这样做,您基本上会在所有图像中看到转换后的平均向量 W*mean_image
。如果它在幅度上占主导地位,由于四舍五入的影响,所有图片看起来都一样。但是,如果您计算图像之间的差异,它们就不是。
在代码中,您可以通过
看到所有内容
combined_data = np.array(puppies_vectors).T
my_zca_transformer_image_stuff = zca_whitening_matrix(combined_data)
mean_image = np.mean(combined_data,axis=1)
without_mean = (combined_data-mean_image[:,None])
xZCAMatrix = np.dot(my_zca_transformer_image_stuff, without_mean)
reshaped_pics = [xZCAMatrix[:,i].reshape(new_size,new_size) for i in range(4)]
for i in reshaped_pics:
plt.imshow(i, cmap='gray')
plt.show() # Should look good now
## What you essential saw is this transformed mean image
i = np.dot(my_zca_transformer_image_stuff, mean_image).reshape(new_size,new_size)
plt.imshow(i, cmap='gray')
plt.show()
我正在查看此处显示的特征白化示例:How to implement ZCA Whitening? Python
我已经实现了下面的代码,但是每当我 运行 它时,我在所有 4 张图片中得到相同的图像,我不确定为什么:
读取数据的代码(工作正常)
import cv2
import matplotlib.pyplot as plt
from urllib.request import urlopen, Request
new_size = 50
urls = ['https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/dog-puppy-on-garden-royalty-free-image-1586966191.jpg',
'https://dogtime.com/assets/uploads/2018/10/puppies-cover.jpg',
'https://www.readersdigest.ca/wp-content/uploads/2013/03/6-facts-to-know-before-owning-a-puppy.jpg',
'https://images.newscientist.com/wp-content/uploads/2021/06/03141753/03-june_puppies.jpg']
puppies = []
for url in urls:
r = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
response = urlopen(r).read()
arr = np.asarray(bytearray(response), dtype=np.uint8)
puppies.append(cv2.imdecode(arr, 0)) # 'Load it as it is'
#item_pics = [cv2.imread( './pictures/picture_'+ str(x) + '.jpeg', 0) for x in range(1,5)]
puppies_scaled = [cv2.resize(x, [new_size,new_size], interpolation = cv2.INTER_AREA) for x in puppies]
puppies_vectors = [x.reshape(new_size * new_size) for x in puppies_scaled]
美白代码
#borrowed heavily from
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-25
# 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
美白代码
combined_data = np.array(puppies_vectors).T
my_zca_transformer_image_stuff = zca_whitening_matrix(combined_data)
xZCAMatrix = np.dot(my_zca_transformer_image_stuff, combined_data)
reshaped_pics = [xZCAMatrix[:,i].reshape(new_size,new_size) for i in range(4)]
结果(所有图片都一样)
对zero-mean数据进行白化处理。这意味着,矩阵是根据 zero-mean 数据计算的,并且它们也应该应用于 zero-mean 数据。如果不这样做,您基本上会在所有图像中看到转换后的平均向量 W*mean_image
。如果它在幅度上占主导地位,由于四舍五入的影响,所有图片看起来都一样。但是,如果您计算图像之间的差异,它们就不是。
在代码中,您可以通过
看到所有内容combined_data = np.array(puppies_vectors).T
my_zca_transformer_image_stuff = zca_whitening_matrix(combined_data)
mean_image = np.mean(combined_data,axis=1)
without_mean = (combined_data-mean_image[:,None])
xZCAMatrix = np.dot(my_zca_transformer_image_stuff, without_mean)
reshaped_pics = [xZCAMatrix[:,i].reshape(new_size,new_size) for i in range(4)]
for i in reshaped_pics:
plt.imshow(i, cmap='gray')
plt.show() # Should look good now
## What you essential saw is this transformed mean image
i = np.dot(my_zca_transformer_image_stuff, mean_image).reshape(new_size,new_size)
plt.imshow(i, cmap='gray')
plt.show()