How can I save generate image in new separate folder?

nalin_thoummala: <p>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.</p>

Nikronic: <p>Hi,</p>
<p>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.</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>Bests</p>

nalin_thoummala: <p>Thanks for your help.<br>
I just solved the problem by using torchvision.utils</p>
<blockquote></blockquote>
<p>for i in range(100):<br>
torchvision.utils.save_image(image_list[-1][i], “/path to your folder/generated image%d.JPG” %i, normalize = True)</p>

Nikronic: <p>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 <code>torchvision</code> package have been trained by normalizing the images using mean and std obtained from ImageNet dataset.</p>
<p>The reason that I use the snippet is that even though visually the image saved by <code>transforms.utils.save_image</code> and the my snippet are same, but there is differences between images.</p>
<p>For instance, for a specific image, these are min, max and mean values:</p>
<pre><code class="lang-auto">img = Image.open('../cat.jpg')
print(np.array(img).min(), np.array(img).max(), np.array(img).mean())

# output: 0 237 102.17308268229166
</code></pre>
<p>Now, I normalized image using custom mean and std per channel, obviously the range will change:</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>Now, I want to unnormalize to obtain original image using <code>utils.save_image</code>:</p>
<pre><code class="lang-auto">torchvision.utils.save_image(img, 'test_utils.jpg', normalize=True, )
img_norm = np.array(Image.open('test_utils.jpg'))
print(img_norm.min(), img_norm.max(), img_norm.mean())

# output: 0 254 112.44019694010417
</code></pre>
<p>As you can see there is difference. The reason is that <code>utils.save_image</code> does not know about my mean and std for normalization. So, you have to unnormalize first then pass to this function for saving.</p>
<p>Now same test using my snippet:</p>
<pre><code class="lang-auto">img = Image.open('../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
</code></pre>
<p>As you can see , it is much similar to the original image.</p>