why does pytorch's utils.save_image() change the color of my image

1.8k views Asked by At

I am saving two images with pytorch's utils.save_image() function here. one is the real image and the other, the perturbed image (just a patch). However, the latter image lost its real appearance when saved with save_image().

# save source image
utils.save_image(data.data, "./%s/%d_%d_org.png" % ("log", batch_idx, labels),
                            normalize=True)
    
# save adv image
utils.save_image(noised_data.data, "./%s/%d_%d_adv.png" % ("log", batch_idx, target_class), normalize=True)

This is the saved clean imageclean image

This is the saved perturbed image enter image description here

1

There are 1 answers

0
Craving_gold On BEST ANSWER

So, I managed to solve this by adding make_grid() to the save method and clipping the image to [0,1] without normalizing.

utils.save_image(utils.make_grid(torch.clip(noised_data.data, 0, 1)), "./%s/%d_%d_adv.png" % ("log", batch_idx, target_class))