EdmondFrank's 时光足迹

この先は暗い夜道だけかもしれない それでも信じて進むんだ。星がその道を少しでも照らしてくれるのを。
或许前路永夜,即便如此我也要前进,因为星光即使微弱也会我为照亮前途。
——《四月は君の嘘》

如何免费使用谷歌GPU训练神经网络



完全云端运行:免费使用谷歌GPU训练神经网络

背景

对,你没有听错,高大上的GPU,现在不花钱也能用上了。这是Google的一项免费云端机器学习服务,全名Colaboratory。

Colaboratory 是一个 Google 研究项目,旨在帮助传播机器学习培训和研究成果。它是一个 Jupyter 笔记本环境,不需要进行任何设置就可以使用,并且完全在云端运行。Colaboratory 笔记本存储在 Google 云端硬盘中,并且可以共享,就如同您使用 Google 文档或表格一样。Colaboratory 可免费使用,而且最重要的还提供免费的英伟达Tesla K80 GPU。还有这等好事?事不宜迟,本文马上介绍如何使用 Google CoLaboratory 训练神经网络。

准备工作

在Google Drive上创建文件夹

Colab用的数据都存储在Google Drive云端硬盘上,所以,我们需要先指定要在Google Drive上用的文件夹。

比如说,可以在Google Drive上创建一个“app”文件夹,或者其他什么名字,也可以选择Colab笔记本默认的文件夹。

新建Colab笔记本

在刚刚创建的app文件夹里点击右键,选择“更多”,然后从菜单里选择“Colaboratory”,这样就新建出了一个Colab笔记本。

若是更多选项中没有“Colaboratory”选项,可以点击“关联更多应用”选项,然后在打开的页面中,搜索“Colaboratory”,然后再点关联应用,再次点击右键就可以在“更多”选项中看到“Colaboratory”选项了。

设置免费GPU

新建Colaboratory成功后,在笔记本里点Edit>Notebook settings(编辑>笔记本设置),或者Runtime>Change runtime type(运行时>改变运行时类型),然后在Hardware accelerator(硬件加速器)一栏选择GPU。

然后,Google Colab就可以用了。

关联Google Drive

为了能让Colaboratory使用到你的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的SDK来访问你的Google Drive,而这个验证码就相当于access_key了)

授权完成后,就可以挂载Google Drive了:

!mkdir -p drive
!google-drive-ocamlfuse drive

测试GPU

这时,我们在本地电脑上创建一个.py文件来测试一下,挂载是否成功以及GPU是否在工作吧。

echo "import tensorflow as tf\nprint(tf.test.gpu_device_name())" > test.py

然后将test.py上传到我们开始时创建的app的文件夹里。

然后在Colaboratory笔记本中运行一下代码:

!python3 drive/app/test.py

不出意外的话,就会输出类似以下的结果:

/usr/local/lib/python3.6/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
2018-02-18 12:37:05.172726: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:898] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-02-18 12:37:05.172988: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1208] Found device 0 with properties: 
name: Tesla K80 major: 3 minor: 7 memoryClockRate(GHz): 0.8235
pciBusID: 0000:00:04.0
totalMemory: 11.17GiB freeMemory: 503.62MiB
2018-02-18 12:37:05.173016: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1308] Adding visible gpu devices: 0
2018-02-18 12:37:05.457665: I tensorflow/core/common_runtime/gpu/gpu_device.cc:989] Creating TensorFlow device (/device:GPU:0 with 243 MB memory) -> physical GPU (device: 0, name: Tesla K80, pci bus id: 0000:00:04.0, compute capability: 3.7)
/device:GPU:0

到这里的话,那么恭喜你,你的GPU环境基本可以用了,只要把你的项目文件夹上传到你的app文件夹下,搭建好深度学习的库环境,就可以通过类似上面的操作进行神经网络训练了。

Tips

如何安装库?

安装Keras:

!pip install -q keras
import keras

安装PyTorch:

!pip install -q http://download.pytorch.org/whl/cu75/torch-0.2.0.post3-cp27-cp27mu-manylinux1_x86_64.whl torchvision
import torch

安装OpenCV:

!apt-get -qq install -y libsm6 libxext6 && pip install -q -U opencv-python
import cv2

安装XGBoost:

!pip install -q xgboost==0.4a30
import xgboost

安装GraphViz:

!apt-get -qq install -y graphviz && pip install -q pydot
import pydot

安装7zip Reader:

!apt-get -qq install -y libarchive-dev && pip install -q -U libarchive
import libarchive

安装其他库:

用!pip install或者!apt-get install命令。