How to download large files (like weights of a model) from Colaboratory?

痞子三分冷 提交于 2019-12-05 23:22:19

This is how I handle this issue:

from google.colab import auth
from googleapiclient.http import MediaFileUpload
from googleapiclient.discovery import build

auth.authenticate_user()

Then click on the link, authorize Google Drive and paste the code in the notebook.

drive_service = build('drive', 'v3')

def save_file_to_drive(name, path):
    file_metadata = {
      'name': name,
      'mimeType': 'application/octet-stream'
     }

     media = MediaFileUpload(path, 
                    mimetype='application/octet-stream',
                    resumable=True)

     created = drive_service.files().create(body=file_metadata,
                                   media_body=media,
                                   fields='id').execute()

     print('File ID: {}'.format(created.get('id')))

     return created

Then:

save_file_to_drive(destination_name, path_to_file)

This will save whatever files to your Google Drive, where you can download or sync them or whatever.

Google colab doesn't allow you to download large files using files.download(). But you can use one of the following methods to access it:

  1. The easiest one is to use github to commit and push your files and then clone it to your local machine.
  2. You can mount google-drive to your colab instance and write the files there.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!