Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 978
이성령 Avatar asked Feb 28 '18 14:02

이성령


People also ask

How do I save images from google colab to Google Drive?

save("/content/drive/MyDrive/aa/xyz. jpeg") of PTL library works like magic. Below the overall code snippet. So you can easily read image from google drive and save image to google drive from google colab.

How do I save weights in google Colab?

To save weights manually, use tf. keras. Model. save_weights .


Video Answer


3 Answers

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')
like image 107
Tadej Magajna Avatar answered Oct 22 '22 15:10

Tadej Magajna


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" ) 
like image 21
Nazmus Sakib Avatar answered Oct 22 '22 14:10

Nazmus Sakib


One of the other simple methods to save a file to Google Drive I found here is to use the command cp after you mounted the drive.

Here is the code:

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

Then use this:

!cp -r <CURRENT FILE PATH> <PATH YOU WANT TO SAVE>

Example:

!cp -r './runs/exp0.h5' /content/drive/MyDrive/Exp1/
like image 10
Prof.Plague Avatar answered Oct 22 '22 14:10

Prof.Plague