When I use Google Colaboratory, how to save image, weights in my Google Drive?

老子叫甜甜 提交于 2019-12-03 07:24:45

问题


I use Google Colaboratory then I want to save output images in my Google Drive or SSD, HHD but its directory is "/content"

import os     
print(os.getcwd())
# "/content"

so is it possible to change path (HDD, SSD, googledrive)?


回答1:


To save the weight you can run the following after training.

saver = tf.train.Saver()
save_path = saver.save(session, "data/dm.ckpt")
print('done saving at',save_path)

Check the location where the ckpt files were saved.

import os
print( os.getcwd() )
print( os.listdir('data') )

Finally download the file!

from google.colab import files
files.download( "data/dm.ckpt.meta" ) 



回答2:


You need to mount google drive to your Colab session.

from google.colab import drive
drive.mount('/content/gdrive')

Then you can simply write to google drive as you would to a local file system like so:

with open('/content/gdrive/My Drive/file.txt', 'w') as f:
  f.write('content')



回答3:


Take a look at the example on interfacing with external files. The general workflow is to output the file to the cloud environment, then download it.

Let's output the plot from the "Hello, Colaboratory" example to a file. I made a copy of the notebook to my Google Drive and ran the following commands:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(20)
y = [x_i + np.random.randn(1) for x_i in x]
a, b = np.polyfit(x, y, 1)
f = plt.figure()
_ = plt.plot(x, y, 'o', np.arange(20), a*np.arange(20)+b, '-')

f.savefig( "test.png")

If we list the files in the Google Collaboratory environment, we will see test.png among them:

import os
print( os.getcwd() )
print( os.listdir() )
# /content
# ['datalab', '.local', '.config', '.forever', '.cache', '.rnd', 'test.png', '.ipython']

All that's left to do is download it to my local machine using the example I linked at the beginning on this answer:

from google.colab import files
files.download( "test.png" )    

Finally, if you really need the files on Google Drive instead of your local machine, you can use the Google Drive API to move the files accordingly.

P.S. If you don't like writing files to /content, you can always create a subdirectory and os.chdir() into it, but keep in mind that this subdirectory is still local to your cloud environment and requires you to download files as above.



来源:https://stackoverflow.com/questions/49031798/when-i-use-google-colaboratory-how-to-save-image-weights-in-my-google-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!