I’m working with Google Colab and need to save my generated images and trained model weights to Google Drive instead of the default temporary storage. Right now when I check my current working directory, it shows /content
which is just temporary storage.
import os
current_path = os.getcwd()
print(current_path)
# Output: /content
Is there a way to mount Google Drive or change the save location so my files don’t get lost when the runtime disconnects? I want to make sure my work is permanently saved to external storage like Google Drive or access local drives if possible.
here’s another trick - use symbolic links after mounting your drive. once you run drive.mount('/content/drive')
, create a symlink with !ln -s /content/drive/MyDrive/colab_files /content/files
. now you don’t have to remember that long path every time. just save to /content/files/
and it goes straight to your drive automatically.
You can save files and model weights to Google Drive from Colab using the drive module. Import it with from google.colab import drive
, then mount using drive.mount('/content/drive')
. Be sure to authenticate your Google account during this process. Once mounted, your Drive files will be accessible under /content/drive/MyDrive/
. It’s wise to create a dedicated folder in your Drive for your Colab projects. When saving your model weights, use the complete path like /content/drive/MyDrive/my_project/model.pth
to ensure that you don’t lose any files when the session disconnects. Keep in mind that saving to Drive might take more time compared to local storage.
I’d set up automatic syncing to prevent data loss. Mount your Drive normally, then create a backup function that saves your work periodically. You can also update your existing save commands - just change the file paths to point to your mounted Drive location. For example, with torch.save()
or model.save()
, change the path parameter to /content/drive/MyDrive/your_folder/
. Watch out for upload speed though - larger model files can take several minutes to sync. I save checkpoints at regular intervals during training instead of waiting until the end, especially for longer sessions where connection drops are more likely.