I'm new to learning python. I saw a code on the Internet that saves the generated gan images. But I need these generated images to be saved to a folder in Google Coollaboratory (Colab). How do I do this?
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()
CodePudding user response:
You can use files
from google.colab
library
# 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()
Make sure the plt.show
is called before the files.dowload(..)