如何从 Colaboratory 下载大文件(例如模型的权重)?

问题描述 投票:0回答:5

我尝试从谷歌合作实验室下载小文件。它们很容易下载,但每当我尝试下载大尺寸的文件时,它会显示错误?大文件有什么办法下载?

python-3.x tensorflow gpu google-colaboratory
5个回答
19
投票

如果您创建了一个大的 zip 文件,例如 my_archive.zip,那么您可以按如下方式下载它:

  1. 从 Google colab 笔记本安装 Google 驱动器。你会 被要求输入验证码。
from google.colab import drive
drive.mount('/content/gdrive',force_remount=True)
  1. 将 zip 文件复制到任何 Google 云端硬盘文件夹(例如下载文件夹)。您还可以将文件复制到“我的云端硬盘”的根文件夹中。
!cp my_archive.zip '/content/gdrive/My Drive/downloads/'
!ls -lt '/content/gdrive/My Drive/downloads/' 

最后,您可以将 zip 文件从 Google 驱动器下载到本地计算机。


11
投票

这就是我处理这个问题的方法:

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

auth.authenticate_user()

然后点击链接,授权Google Drive并将代码粘贴到笔记本中。

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

然后:

save_file_to_drive(destination_name, path_to_file)

这会将所有文件保存到您的 Google 云端硬盘,您可以在其中下载或同步它们或其他任何内容。


1
投票

我尝试了很多不同的解决方案。唯一有效且快速的方法是压缩文件/文件夹,然后直接下载:

 !zip -r model.zip model.pkl

下载:


0
投票

Google colab 不允许您使用

files.download()
下载大文件。但您可以使用以下方法之一来访问它:

  1. 最简单的一种是使用 github 提交并推送文件,然后将其克隆到本地计算机。
  2. 您可以将 google-drive 安装到您的 colab 实例并在那里写入文件。

0
投票

如果您想从互联网直接下载一个大的 zip 文件到您的谷歌驱动器,这个解决方案适合我

!pip install gdown

import os
import gdown
import zipfile

# Define function to download file using gdown
def download_file_from_google_drive(id, destination):
    url = f'https://drive.google.com/uc?id={id}'
    gdown.download(url, destination, quiet=False)

# Define root directory for the dataset
destination_path = '/content/drive/MyDrive/Colab Notebooks/destination_path'
if not os.path.exists(destination_path):
    os.makedirs(destination_path)

# Define file ID for the dataset
file_id = 'theFileId'

# Define destination path for the downloaded file
destination_path = os.path.join(destination_path, 'filename.zip')

# Download the file
download_file_from_google_drive(file_id, destination_path)
© www.soinside.com 2019 - 2024. All rights reserved.