使用colab訓練深度學習模型的時候,需要讀入本地採集好的數據集。這時候可以將數據集先上傳到google drive雲端硬盤,再在colab的notebook讀取google drive的數據集(文本、圖片、數據表等)。colab類似一臺linux服務器,要使用google drive就需要把drive掛在到主機上。
這時候需要以下三個步驟:
1.首先需要讓colab獲得google drive的授權,在google colab裏執行如下代碼:
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
2.指定Google Drive雲端硬盤的根目錄,名爲drive
!mkdir -p drive
!google-drive-ocamlfuse drive # 此時colab中出現drive的文件夾,裏面就是你的google drive的根目錄文件
- 1
3.然後更換執行的工作文件夾即可,數據集在這個文件夾中,就可以在notebook裏直接使用了
import os
os.chdir("drive/Colab Notebooks")
- 1