【发布时间】: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