How can I save generate image in new separate folder?

I am trying to do data augmentation using GAN. The generated image I got is tensor stored in a list shape of [100, 1, 64, 64] ( I should get 100 images) but how can I save all the generated image into a new seperated folder.


If I understand correctly, this method will save a 3D tensor as an image. But I cannot think of any method to save all images in one batch operation. Although you can save them as a grid, but not separate images.

def save_img(inp, idx, path):
    inp = inp.cpu().numpy().transpose((1, 2, 0))
    mean = np.array([0.485, 0.456, 0.406])  # if inp is normalized
    std = np.array([0.229, 0.224, 0.225])  # if inp is normalized
    inp = std * inp + mean
    inp = np.clip(inp, 0, 1)
    cv2.imwrite(path+str(idx)+'.png', inp)


Thanks for your help.
I just solved the problem by using torchvision.utils

for i in range(100):
torchvision.utils.save_image(image_list[-1][i], “/path to your folder/generated image%d.JPG” %i, normalize = True)

1 Like

Yes, this is much easier way to do this. Although, I thought you might using custom mean and std for normalization not division by max as scaling. For instance, all models in PyTorch torchvision package have been trained by normalizing the images using mean and std obtained from ImageNet dataset.

The reason that I use the snippet is that even though visually the image saved by transforms.utils.save_image and the my snippet are same, but there is differences between images.

For instance, for a specific image, these are min, max and mean values:

img ='../cat.jpg')
print(np.array(img).min(), np.array(img).max(), np.array(img).mean())

# output: 0 237 102.17308268229166

Now, I normalized image using custom mean and std per channel, obviously the range will change:

img = transforms.Normalize(np.array([0.485, 0.456, 0.406]), np.array([0.229, 0.224, 0.225]))(transforms.ToTensor()(img))
print(img.max(), img.min(), img.mean())
# output: tensor(2.2043) tensor(-2.1179) tensor(-0.2132)

Now, I want to unnormalize to obtain original image using utils.save_image:

torchvision.utils.save_image(img, 'test_utils.jpg', normalize=True, )
img_norm = np.array('test_utils.jpg'))
print(img_norm.min(), img_norm.max(), img_norm.mean())

# output: 0 254 112.44019694010417

As you can see there is difference. The reason is that utils.save_image does not know about my mean and std for normalization. So, you have to unnormalize first then pass to this function for saving.

Now same test using my snippet:

img ='../cat.jpg')
img = transforms.Normalize(np.array([0.485, 0.456, 0.406]), np.array([0.229, 0.224, 0.225]))(transforms.ToTensor()(img))
img = img.numpy().transpose((1, 2, 0))
mean = np.array([0.485, 0.456, 0.406])  
std = np.array([0.229, 0.224, 0.225])
img = std * img + mean
img = np.clip(img*255, 0, 255)
print(np.array(img).min(), np.array(img).max(), np.array(img).mean())

# output: 0.0 236.99999450683595 102.17308456857972

As you can see , it is much similar to the original image.

1 Like