Google Colab 免費GPU服務器使用教程


 

今天突然看到一篇推文,里面講解了如何薅資本主義羊毛,即如何免費使用Google免費提供的GPU使用權。

可以免費使用的方式就是通過Google Colab,全名Colaboratory。我們可以用它來提高Python技能,也可以用Keras、TensorFlow、PyTorch、OpenCV等等流行的深度學習庫來練習開發深度學習的應用。

現在我們介紹如何免費的使用這個非常非常給力的應用!!!

 一  項目建立與配置

(1)在Google Drive上創建文件夾:這項功能的使用主要是通過Google Drive,首先需要在Google Drive里面創建新的文件夾,因為我們所有的操作都是通過Google Drive文件的方式進行的,這里我們創建了一個名為gpu的文件夾,然后打開文件夾;

(2)創建新的Colaboratory:右鍵更多選擇Colaboratory, 如果更多沒有的話,可以點擊關聯更多應用搜索添加即可!

 

 

並且這里可以隨意修改文件名

 

 (3)點擊修改,設置后端Python版本和免費的GPU使用:然后就可以進行代碼編寫了~~~

 

 

二   授權與掛載

 (4)當完成基本的文件建立和配置后,就需要先運行下面這些代碼,來安裝必要的庫、執

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

 

點擊鏈接地址,獲取驗證碼。

 提示成功!

 (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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM