一张一张保存GAN生成的图片

Save GAN generated images one by one

我已经从 Fashion Mnist 数据集中生成了一些图像,但是,我无法想出将每张图像保存为单个文件的函数或方法。我只找到了一种将它们分组保存的方法。谁能帮我一张一张保存图片?

这是我目前拥有的:

def generate_and_save_images(model, 
     epoch,test_input):
     predictions = model(test_input, training=False)

     fig = plt.figure(figsize=(4,4))

    for i in range(predictions.shape[0]):
        plt.subplot(4, 4, i+1)
        plt.imshow(predictions[i, :, :, 0] * 127.5 + 
        127.5, cmap='gray')
        plt.axis('off')

    plt.savefig('image_at_epoch_{:04d}.png'.format(epoch))

    plt.show()

尝试使用plt.imsave分别保存每张图片:

def generate_and_save_images(model, epoch, test_input):
  predictions = model(test_input, training=False)
  fig = plt.figure(figsize=(4, 4))

  for i in range(predictions.shape[0]):
      plt.subplot(4, 4, i+1)
      plt.imshow(predictions[i, :, :, 0] * 127.5 + 127.5, cmap='gray')
      plt.imsave('image_at_epoch_{:04d}-{}.png'.format(epoch, i), predictions[i, :, :, 0] * 127.5 + 127.5, cmap='gray')
      plt.axis('off')

  plt.savefig('image_at_epoch_{:04d}.png'.format(epoch))
  plt.show()