Search code examples
deep-learningpytorchgenerative-adversarial-network

how to save the generated images from this code separated


I have run StarGAN Code from github, this code generate all the generated images in one picture.

How I can save all the generated images separated to single folder? I do not want to save all the images in one picture.

this is how it generate the output (sample image enter image description here

I want to save the generated images from the trained model, not as samples with all the images in one picture but just a file that contains all the generated images.

This is the part of the code i want to change it

# Translate fixed images for debugging.
if (i+1) % self.sample_step == 0:
    with torch.no_grad():
        x_fake_list = [x_fixed]
        for c_fixed in c_fixed_list:
            x_fake_list.append(self.G(x_fixed, c_fixed))
        x_concat = torch.cat(x_fake_list, dim=3)
        sample_path = os.path.join(self.sample_dir, '{}-images.jpg'.format(i+1))
        save_image(self.denorm(x_concat.data.cpu()), sample_path, nrow=1, padding=0)
        print('Saved real and fake images into {}...'.format(sample_path))

Solution

  • The generator self.G is called on each element of c_fixed_list to generate images. All results are concatenated, then saved using torchvision.utils.save_image.

    I don't see what's holding you from saving the images inside the loop. Something that would resemble:

    for j, c_fixed in enumerate(c_fixed_list):
        x_fake = self.G(x_fixed, c_fixed)
        for k in range(len(x_fake)):
            sample_path = os.path.join(self.sample_dir, f'{i+1}-{k}-feat{j}-image.jpg')
            save_image(self.denorm(x_fake.data[k].cpu()), sample_path, nrow=1, padding=0)