-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Description
I found that save_image will not give normal image if the type of input is torch.unit8.
Data source
"cifar10.1_v4_data.npy" in https://github.com/modestyachts/CIFAR-10.1/tree/master/datasets
Load data code
from torchvision.utils import save_image
import numpy as np
import torch
data_new = np.load('./data/cifar10.1_v4_data.npy') # the range of data_new is 0-255
data_T = np.transpose(data_new, [0,3,1,2]) # transpose for save_image
data0 = torch.from_numpy(data_T[0]) # convert to tensor. The type of data0 is torch.unit8
Save image code
save_image(data0, "./images/Cifar10_v4_100.png", nrow=1, normalize=False)
Issues
There are two issues: 1) abnormal saved image and 2) data0 changes after executing save_image commend (very strange!)
Solution & New code
If I convert type of data0 to torch.float, two issues will be addressed. Thus, the new code is
save_image(data0.to(torch.float)/255, "./images/Cifar10_v4_100.png", nrow=1, normalize=True)
I think it is a bug inside the code of save_image. We probably need to fix it.