【问题标题】:How to work with Google Colab efficiently?如何高效地使用 Google Colab?
【发布时间】:2020-05-26 23:58:22
【问题描述】:

我尝试在 Colab 上使用 GPU 训练神经网络。我现在想知道我是否走在正确的道路上,以及我正在做的所有步骤是否都是必要的,因为我所遵循的过程对我来说似乎不是很有效。

# Install the PyDrive wrapper & import libraries.
# This only needs to be done once per notebook.
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client.
# This only needs to be done once per notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

import os

# choose a local (colab) directory to store the data.
local_root_path = os.path.expanduser("~/data")
try:
  os.makedirs(local_root_path)
except: pass

def ListFolder(google_drive_id, destination):
  file_list = drive.ListFile({'q': "'%s' in parents and trashed=false" % google_drive_id}).GetList()
  counter = 0
  for f in file_list:
    # If it is a directory then, create the dicrectory and upload the file inside it
    if f['mimeType']=='application/vnd.google-apps.folder': 
      folder_path = os.path.join(destination, f['title'])
      os.makedirs(folder_path)
      print('creating directory {}'.format(folder_path))
      ListFolder(f['id'], folder_path)
    else:
      fname = os.path.join(destination, f['title'])
      f_ = drive.CreateFile({'id': f['id']})
      f_.GetContentFile(fname)
      counter += 1
  print('{} files were uploaded in {}'.format(counter, destination))

ListFolder("1s1Ks_Gf_cW-F-RwXFjBu96svbmqiXB0o", local_root_path)

此命令允许将 Colab 中的 Notebook 与我的 Google Drive 连接,并将数据存储在 Colab 中。因为我有很多图像(超过 180k),所以在 Colab 中存储数据需要非常非常长的时间,并且部分连接中断。我现在想知道是否需要将所有数据从我的 Google Drive 上传到 Colab?

如果不是,我应该怎么做才能使用 Google 云端硬盘中的数据? 如果是,有没有办法更有效地做到这一点? 或者我是否应该以完全不同的方式与 Colab 合作?

【问题讨论】:

    标签: python google-colaboratory


    【解决方案1】:

    您可以直接访问 Google 驱动器上的文件,而无需将它们复制到笔记本环境中。
    在一个单元格中执行此代码:

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

    然后尝试:

    !ls /content/gdrive
    

    现在您可以将文件从/复制到 /content/gdrive 目录,它们将出现在您的 Google 云端硬盘中。

    【讨论】:

    • 从那里创建一个进程,将一个小的图像缓冲区加载到实例的内存中,这样对于训练 NN 的主进程来说,它看起来好像所有图像都很容易在实例的内存中可用。
    • 谢谢!这意味着,我不必运行我之前运行的所有代码?
    • 是的,您只需要安装 gdrive 并在 /content/gdrive 目录下为您的数据集加载器提供图像的绝对路径
    猜你喜欢
    • 2020-04-14
    • 2021-12-14
    • 2020-08-24
    • 2019-04-11
    • 2019-04-25
    • 2020-05-17
    • 1970-01-01
    • 2018-07-03
    • 2021-12-15
    相关资源
    最近更新 更多