简体   繁体   English

如何高效地使用 Google Colab?

[英]How to work with Google Colab efficiently?

I try to train a neural network on Colab using a GPU there.我尝试在那里使用 GPU 在 Colab 上训练神经网络。 I am now wondering if I am on the right pave and if all the steps I am doing are necessary, because the process I am following does not appear very efficient to me.我现在想知道我是否走在正确的道路上,以及我正在做的所有步骤是否都是必要的,因为我所遵循的过程对我来说似乎不是很有效。

# 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)

This commands allow to connect the Notebook in Colab with my Google Drive and stores the data in Colab.此命令允许将 Colab 中的 Notebook 与我的 Google Drive 连接,并将数据存储在 Colab 中。 Because I have a lot of images (more than 180k) the storage of the data in Colab takes very, very long and partially the connection breaks.因为我有很多图像(超过 180k),所以在 Colab 中存储数据需要非常非常长的时间,并且部分连接中断。 I am now wondering if it is necessray to upload all the data from my Google Drive to Colab?我现在想知道是否需要将所有数据从我的 Google Drive 上传到 Colab?

If no, what do I have to do instead to work with the data from Google Drive?如果不是,我该怎么做才能使用 Google Drive 中的数据? If yes, is there a way to do this more efficiently?如果是,有没有办法更有效地做到这一点? Or is there maybe a completely different way I should work with Colab?还是我应该以完全不同的方式与 Colab 合作?

You can access files directly on your Google drive without copying them into Notebook environment.您可以直接访问 Google 驱动器上的文件,而无需将它们复制到笔记本环境中。
Execute this code in one cell:在一个单元格中执行此代码:

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

And try:并尝试:

!ls /content/gdrive

Now you can copy your files from/to /content/gdrive directory and they will appear in your Google Drive.现在您可以将文件从/到/content/gdrive目录复制,它们将出现在您的 Google Drive 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM