I am trying to apply ZCA preprocessing step on CIFAR-10. When I am to plot the image and see the output after the preprocessing step but when I am passing it to the dataloader I am getting the error " Cannot handle this data type" and it coming from Image= fromarray(img).
After ZCA the output is of type float36 and the values are between [0,1]. When I change the type to uint8 the dataloader works perfectly but when I’m visualizing the image it’s just black.
If you want to visualize a normalized image, you would habe to resale it back to [0, 255]
.
torchvision.transforms.ToPILImage()
is one way to do it.
I would transform your normalized images to tensors
and return them in your Dataset
.
dataset= dsets.CIFAR10(root='./cifar10/data/', transform=transform, train= True, download=True)
test_set= dsets.CIFAR10(root="./cifar10/data/", transform=transform, train= False)
def ZCA_whitening(X):
X= X.reshape((-1, np.product(X.shape[1:])))
X_centered= X - np.mean(X, axis= 0)
Sigma= np.dot(X_centered.T, X_centered) / X_centered.shape[0]
U, Lambda, _= np.linalg.svd(Sigma)
W= np.dot(U, np.dot(np.diag(1.0/np.sqrt(Lambda + 1e-5)), U.T))
X_ZCA= np.dot(X_centered, W.T)
X_ZCA_rescaled = (X_ZCA - X_ZCA.min()) / (X_ZCA.max() - X_ZCA.min())
return X_ZCA_rescaled
dataset_zca= ZCA_whitening(dataset.train_data)
test_set_zca= ZCA_whitening(test_set.test_data)
dataset_zca= torch.tensor(dataset_zca.reshape(-1,32,32,3))
test_set_zca= torch.tensor(test_set_zca.reshape(-1,32,32,3))
torch.Tensor(dataset.train_data)
torch.Tensor(test_set.test_data)
.
.
.
for idx, (data, target) in enumerate(loader):
Here it’s throwing error: ’ object has no attribute 'array_interface"