参考:
- 如何免费云端运行Python深度学习框架?
- Google Colab Free GPU Tutorial (PS:需要爱国上网)
上星期折腾了两个晚上,算是知道了Colaboratory与Google Drive进行绑定的过程。记录一下,以备不时之需。
Colaboratory跑不需要额外文件的notebook倒是挺方便的,就是当需要额外文件的时候显得特别麻烦(主要麻烦还是体现在外网上上传文件的网速很不稳定)。感觉Colaboratory也是Google为自家Chromebook打造的。毕竟Chromebook上面只有个Chrome浏览器其他啥也没有。但还是要感谢谷歌提供的免费使用GPU的机会。
1.将所需文件上传至Google Drive

2.新建或上传ipnb文件,并用Colaboratory打开

3.在notebook中运行下方代码进行授权绑定
运行下方代码(傻瓜式粘贴运行即可):
!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}
运行完,过一会儿会要求两次点进链接登陆google账号并完成相关授权。

4. 指定工作目录
在指定之前先用!ls
命令查看一下云端自动分配的默认文件目录,云端默认的文件根目录是datalab

运行下方代码,指定文件根目录:
# 指定Google Drive云端硬盘的根目录,名为drive
!mkdir -p drive
!google-drive-ocamlfuse drive
指定完之后,再用!ls
命令查看绑定的文件根目录,根目录变为drive。

5. 指定当前工作文件夹
# 指定当前的工作文件夹
import os
# 此处为google drive中的文件路径,drive为之前指定的工作根目录,要加上
os.chdir("drive/Colab Notebooks/dog_project")
再次用!ls
查看当前的文件目录

需要注意的是,Colaboratory是完全基于云端运行的,每次登陆操作,后台分配的机子都是随机的,所以如果notebook运行需要额外的文件,那么在运行之前都要将文件先上传至Google Drive,然后对Colaboratory指定所需的工作目录。
以下是每次绑定都需要运行的代码,将其添加在notebook前面运行。
# 授权绑定Google Drive
!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}
# 指定Google Drive云端硬盘的根目录,名为drive
!mkdir -p drive
!google-drive-ocamlfuse drive
# 指定当前的工作目录
import os
# 此处为google drive中的文件路径,drive为之前指定的工作根目录,要加上
os.chdir("drive/.../...")
# 查看文件目录,是否包含所需的文件
!ls
网友评论
#!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
!wget https://launchpad.net/~alessandro-strada/+archive/ubuntu/google-drive-ocamlfuse-beta/+build/15331130/+files/google-drive-ocamlfuse_0.7.0-0ubuntu1_amd64.deb
!dpkg -i google-drive-ocamlfuse_0.7.0-0ubuntu1_amd64.deb
!apt-get install -f
!apt-get -y install -qq 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}