Google Colab 免费GPU服务器使用教程

 

今天忽然看到一篇推文,里面讲解了如何薅资本主义羊毛,即如何无偿使用Google免费提供的GPU使用权。python

能够无偿使用的方式就是经过Google Colab,全名Colaboratory。咱们能够用它来提升Python技能,也能够用Keras、TensorFlow、PyTorch、OpenCV等等流行的深度学习库来练习开发深度学习的应用。linux

如今咱们介绍如何免费的使用这个很是很是给力的应用!!!git

 一  项目创建与配置

(1)在Google Drive上建立文件夹:这项功能的使用主要是经过Google Drive,首先须要在Google Drive里面建立新的文件夹,由于咱们全部的操做都是经过Google Drive文件的方式进行的,这里咱们建立了一个名为gpu的文件夹,而后打开文件夹;github

(2)建立新的Colaboratory:右键更多选择Colaboratory, 若是更多没有的话,能够点击关联更多应用搜索添加便可!后端

 

 

而且这里能够随意修改文件名服务器

 

 (3)点击修改,设置后端Python版本和免费的GPU使用:而后就能够进行代码编写了~~~网络

 

 

二   受权与挂载

 (4)当完成基本的文件创建和配置后,就须要先运行下面这些代码,来安装必要的库、执app

!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}

 

点击连接地址,获取验证码。less

 提示成功!工具

 (5)受权完成后,就能够挂载Google Drive了

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

 

三   测试阶段

咱们使用安装Keras中的测试样例代码进行效果的测试:

 
 1 # -*- coding: utf-8 -*-
 2 
 3 
 4 '''Trains a simple convnet on the MNIST dataset.
 5 Gets to 99.25% test accuracy after 12 epochs
 6 (there is still a lot of margin for parameter tuning).
 7 16 seconds per epoch on a GRID K520 GPU.
 8 '''
 9 
10 from __future__ import print_function
11 import keras
12 from keras.datasets import mnist
13 from keras.models import Sequential
14 from keras.layers import Dense, Dropout, Flatten
15 from keras.layers import Conv2D, MaxPooling2D
16 from keras import backend as K
17 
18 batch_size = 128
19 num_classes = 10
20 epochs = 12
21 
22 # input image dimensions
23 img_rows, img_cols = 28, 28
24 
25 # the data, shuffled and split between train and test sets
26 (x_train, y_train), (x_test, y_test) = mnist.load_data()
27 
28 if K.image_data_format() == 'channels_first':
29     x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
30     x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
31     input_shape = (1, img_rows, img_cols)
32 else:
33     x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
34     x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
35     input_shape = (img_rows, img_cols, 1)
36 
37 x_train = x_train.astype('float32')
38 x_test = x_test.astype('float32')
39 x_train /= 255
40 x_test /= 255
41 print('x_train shape:', x_train.shape)
42 print(x_train.shape[0], 'train samples')
43 print(x_test.shape[0], 'test samples')
44 
45 # convert class vectors to binary class matrices
46 y_train = keras.utils.to_categorical(y_train, num_classes)
47 y_test = keras.utils.to_categorical(y_test, num_classes)
48 
49 model = Sequential()
50 model.add(Conv2D(32, kernel_size=(3, 3),
51                  activation='relu',
52                  input_shape=input_shape))
53 model.add(Conv2D(64, (3, 3), activation='relu'))
54 model.add(MaxPooling2D(pool_size=(2, 2)))
55 model.add(Dropout(0.25))
56 model.add(Flatten())
57 model.add(Dense(128, activation='relu'))
58 model.add(Dropout(0.5))
59 model.add(Dense(num_classes, activation='softmax'))
60 
61 model.compile(loss=keras.losses.categorical_crossentropy,
62               optimizer=keras.optimizers.Adadelta(),
63               metrics=['accuracy'])
64 
65 model.fit(x_train, y_train,
66           batch_size=batch_size,
67           epochs=epochs,
68           verbose=1,
69           validation_data=(x_test, y_test))
70 score = model.evaluate(x_test, y_test, verbose=0)
71 print('Test loss:', score[0])
72 print('Test accuracy:', score[1])

 

这里使用Google GPU的效率每一个Epoch大概须要11s左右便可完成

 

 

 而咱们使用实验室的工做站

 

 每一个率每一个Epoch大概须要130s+完成

 

 

 四  相关命令

(1)查看是否使用GPU:

1 import tensorflow as tf
2 tf.test.gpu_device_name()

(2)在使用哪一个GPU:

1 from tensorflow.python.client import device_lib
2 device_lib.list_local_devices()

(3)RAM大小:

1 !cat /proc/meminfo

 

固然Google的使用须要自备FQ工具!

重点:

受权:多是google为了防止机器人,因此每次链接都须要验证一下。

挂载:由于咱们链接的gpu服务器与google云盘是两个独立的机器,所以须要经过挂载操做将云盘上的文件夹链接到gpu服务器上。

由于咱们链接的机器安装的是linux系统,咱们可使用linux命令行。(在命令行前面添加!便可

查看当前路径:

 

查看当前目录下的文件:

在这里咱们能够看到drive目录,而这个drive目录保存的是云盘上的全部信息。

进入drive目录:

这里没法使用cd命令行。

运行的时候最好不要关闭页面。

 

 你们能够看到google云盘与Colab的服务器存在时差

云盘上文件即便上传上去了,但极可能早不到文件,这通常是网络延时的缘由。多刷新几回,最好用ls命令看一下

云盘文件上传上去以后,可能会出现名字发生改变,用引号括起来。还多了一窜不知道什么的数字,这个要注意一下。

 https://github.com/astrada/google-drive-ocamlfuse

原文连接:https://medium.com/deep-learning-turkey/google-colab-free-gpu-tutorial-e113627b9f5d

参考文章:https://www.jianshu.com/p/000d2a9d36a0

相关文章
相关标签/搜索