保存 GAN 生成的图像

Save GAN generated images

我刚开始学习 python。在网上看到一个代码,将生成的gan图片保存起来。但是我需要将这些生成的图像保存到 Google Coollaboratory (Colab) 中的一个文件夹中。我该怎么做?

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

您可以使用 google.colab 库中的 files


# Import files from google colab
from google.colab import files

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('folder_name/image_at_epoch_{:04d}.png'.format(epoch))

    # Saves the file
    files.download('folder_name/image_at_epoch_{:04d}.png'.format(epoch))

    plt.show()

确保 plt.showfiles.dowload(..)

之前被调用