如何保存从此代码分离生成的图像

how to save the generated images from this code separated

我有 运行 StarGAN Code 来自 github,此代码在一张图片中生成所有生成的图像。

如何将所有生成的图像分开保存到一个文件夹中?我不想将所有图像保存在一张图片中。

这是它生成输出的方式(示例图像

我想保存训练模型生成的图像,而不是将所有图像保存为一张照片中的样本,而只是一个包含所有生成图像的文件。

这是我要更改的代码部分

# 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))

c_fixed_list 的每个元素上调用生成器 self.G 以生成图像。连接所有结果,然后使用 torchvision.utils.save_image.

保存

我不明白是什么阻碍了您在循环中保存图像。类似于:

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)