今天突然看到一篇推文, 里面讲解了如何薅资本主义羊毛, 即如何免费使用 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 中的测试样例代码进行效果的测试:
# -*- coding: utf-8 -*-
'''Trains a simple convnet on the MNIST dataset.
Gets to 99.25% test accuracy after 12 epochs
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
batch_size = 128
num_classes = 10
epochs = 12
# input image dimensions
img_rows, img_cols = 28, 28
# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
这里使用 Google GPU 的效率每个 Epoch 大概需要 11s 左右即可完成
而我们使用实验室的工作站
每个率每个 Epoch 大概需要 130s + 完成
四 相关命令
(1) 查看是否使用 GPU:
import tensorflow as tf
tf.test.gpu_device_name()
(2) 在使用哪个 GPU:
from tensorflow.python.client import device_lib
device_lib.list_local_devices()
(3)RAM 大小:
1 !cat /proc/meminfo
当然 Google 的使用需要自备 FQ 工具!
来源: https://www.cnblogs.com/infaraway/p/8372881.html