Save_image method

so I have a tensor CxHxW = 3x480x640 and would like to save it as a png file to disk. However,

utils.save_image(img, “./img.png”)

yields strange, white-dominated images and I suspect some form of scaling or no executed permutation of the tensor, so that the channel is the last dimension. But if I try to permute the tensor prior to saving it

the expanded size of the tensor (640) must match the existing size (3) at son-singleton dimension 2. Target sizes [3, 480,640] Tensor sizes [480,640,3]

If I do

     img = img.detach().cpu().numpy()
     pic = np.transpose(img, (1, 2, 0))
     imsave("img.png", img)

the image is saved correctly. Do I have to change something in the function call to save_image?

edit: if I put

normalize = True

in the function call, it works, but I dont get why. The image is uint8 with 3 channels, and if I set normalize to true, it ought to scale the values in the within the interval [0,1]. Also this official example baffles me. In line 7 and 8, are normalize = true and range = (0,1) not the same?

1 Like

I think the problem is that you need to clamp the values in the image

try this:

def save_im(tensor, title):
    image = tensor.cpu().clone()
    x = image.clamp(0, 255)
    x = x.view(x.size(0), 3, 480, 640)
    save_image(x, "image_{}.png".format(title))

make sure you unsqueeze the matrix to have an extra dimension or change line 3 in above function

the image, as is, is already within the range of 0 to 255

According to the documentation normalize=True must shift the values of the image between (0, 1) but this is contradicting with the notebook example as you mentioned, i think this is because by default the range is calculated by function and normalize acts only within this range, if you give a range explicitly it will normalize it within the given range.