colab如何读取google drive(谷歌云盘)的文件

使用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}

2.指定Google Drive云端硬盘的根目录,名为drive

!mkdir -p drive
!google-drive-ocamlfuse drive # 此时colab中出现drive的文件夹,里面就是你的google drive的根目录文件

3.然后更换执行的工作文件夹即可,数据集在这个文件夹中,就可以在notebook里直接使用了

import os
os.chdir("drive/Colab Notebooks") 

猜你喜欢

转载自blog.csdn.net/weixin_43485035/article/details/109905160