簡體   English   中英

如何高效地使用 Google Colab?

[英]How to work with Google Colab efficiently?

我嘗試在那里使用 GPU 在 Colab 上訓練神經網絡。 我現在想知道我是否走在正確的道路上,以及我正在做的所有步驟是否都是必要的,因為我所遵循的過程對我來說似乎不是很有效。

# 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 Drive 中的數據? 如果是,有沒有辦法更有效地做到這一點? 還是我應該以完全不同的方式與 Colab 合作?

您可以直接訪問 Google 驅動器上的文件,而無需將它們復制到筆記本環境中。
在一個單元格中執行此代碼:

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

並嘗試:

!ls /content/gdrive

現在您可以將文件從/到/content/gdrive目錄復制,它們將出現在您的 Google Drive 中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM