zoukankan      html  css  js  c++  java
  • 如何免费使用Google的GPU跑深度学习代码

    作者:凌逆战

    博客地址:https:////www.cnblogs.com/LXP-Never/p/11614053.html


      从事深度学习的研究者都知道,深度学习代码需要设计海量的数据,需要很大很大很大(重要的事情说三遍)的计算量,以至于CPU算不过来,需要通过GPU帮忙,但这必不意味着CPU的性能没GPU强,CPU是那种综合性的,GPU是专门用来做图像渲染的,这我们大家都知道,做图像矩阵的计算GPU更加在行,应该我们一般把深度学习程序让GPU来计算,事实也证明GPU的计算速度比CPU块,但是(但是前面的话都是废话)我们穷,买不起呀,一块1080Ti现在也要3500左右,2080Ti要9000左右,具体价格还要看显存大小,因此本文给大家带来了福利——Google免费的GPU Colaboratory。

    Google Colab简介

      Google Colaboratory是谷歌开放的一款研究工具,主要用于机器学习的开发研究,这款工具现在可以免费使用,但是不是永久免费暂时还不确定,Google Colab最大的好处是给广大开发AI者提供免费的GPU使用!GPU型号是Tesla K80,你可以在上面轻松地跑例如:Keras、Tensorflow、Pytorch等框架。

      Colabortory是一个jupyter notebook环境,它支持python2和python3,还包括TPU和GPU加速,该软件与Google云盘硬盘集成,用户可以轻松共享项目或将其他共享项目复制到自己的帐户中。

    Colaboratory使用步骤

    1、登录谷歌云盘

    https://drive.google.com/drive/my-drive(没有账号的可以注册一个)

    (1)、右键新建文件夹,作为我们的项目文件夹。

    2、创建Colab文件

    右键在更多里面选择google Colaboratry(如果没有Colaboratory需要在关联更多应用里面关联Colaboratory)

    3、开始使用

    这时候会直接跳转到Colaboratory界面,这个界面很像Jupyter Notebook,Jupyter的命令在Colaboratory一样适用,值得一提的是,Colab不仅可以运行Python代码,只要在命令前面加一个"  !",这条命令就变成了linux命令,比如我们可以" ! ls"查看文件夹文件,还可以!pip安装库。以及运行py程序!python2 temp.py

    可以写一段代码进行测试

    更改工作目录,在Colab中cd命令是无效的,切换工作目录使用chdir函数

    复制代码
    !pwd # 用 pwd 命令显示工作路径
    # /content
    !ls # 查看的是 content 文件夹下有哪些文件
    # sample_data
    !ls "drive/My Drive"
    # TensorFlow (这就是我们之前创建的那个文件夹)
    
    # 更改工作目录
    import os
    os.chdir("/content/drive/My Drive/TensorFlow")
    os.getcwd()
    # '/content/drive/My Drive/TensorFlow'
    复制代码

    重新启动Colab命令:!kill -9 -1

    (3)、选择配置环境

      我们大家肯定会疑虑,上述方法跑的那段程序是不是用GPU跑的呢?不是,想要用GPU跑程序我们还需要配置环境,

      点击工具栏“修改”,选择笔记本设置

    在运行时类型我们可以选择Python 2或Python 3,硬件加速器我们可以选择GPU或者TPU(后面会讲到),或者None什么都不用。

    加载数据

    从本地加载数据

    从本地上传数据

    files.upload 会返回已上传文件的字典。 此字典的键为文件名,值为已上传的数据。

    复制代码
    from google.colab import files
    
    uploaded = files.upload()
    for fn in uploaded.keys():
      print('用户上传的文件 "{name}" 有 {length} bytes'.format(
          name=fn, length=len(uploaded[fn])))
    复制代码

    我们运行该段程序之后,就会让我们选择本地文件,点击上传后,该文件就能被读取了

    将文件下载到本地

    from google.colab import files
    
    files.download('./example.txt')        # 下载文件

    从谷歌云盘加载数据

    使用授权代码在运行时装载 Google 云端硬盘

    from google.colab import drive
    drive.mount('/content/gdrive')

    在Colab中运行上述代码,会出现一段链接,点击链接,复制链接中的密钥,输入到Colab中就可以成功把Colab与谷歌云盘相连接,连接后进行路径切换,就可以直接读取谷歌云盘数据了。

    向Google Colab添加表单

    为了不每次都在代码中更改超参数,您可以简单地将表单添加到Google Colab。

    点击之后就会出现左右两个框,我们在左框中输入

    复制代码
    # @title 字符串
    
    text = 'value' #@param {type:"string"}
    dropdown = '1st option' #@param ["1st option", "2nd option", "3rd option"]
    text_and_dropdown = 'value' #@param ["选项1", "选项2", "选项3"] {allow-input: true}
    
    print(text)
    print(dropdown)
    print(text_and_dropdown)
    复制代码

    双击右边栏可以隐藏代码

    Colab中的GPU

    首先我们要让Colab连上GPU,导航栏-->编辑-->笔记本设置-->选择GPU

    接下来我们来确认可以使用Tensorflow连接到GPU

    复制代码
    import tensorflow as tf
    
    device_name = tf.test.gpu_device_name()
    if device_name != '/device:GPU:0':
      raise SystemError('没有发现GPU device')
    print('Found GPU at: {}'.format(device_name))
    # Found GPU at: /device:GPU:0
    复制代码

    我们可以在Colab上运行以下代码测试GPU和CPU的速度

    import tensorflow as tf
    import timeit
    
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    
    with tf.device('/cpu:0'):
      random_image_cpu = tf.random_normal((100, 100, 100, 3))
      net_cpu = tf.layers.conv2d(random_image_cpu, 32, 7)
      net_cpu = tf.reduce_sum(net_cpu)
    
    with tf.device('/device:GPU:0'):
      random_image_gpu = tf.random_normal((100, 100, 100, 3))
      net_gpu = tf.layers.conv2d(random_image_gpu, 32, 7)
      net_gpu = tf.reduce_sum(net_gpu)
    
    sess = tf.Session(config=config)
    
    # 确保TF可以检测到GPU
    try:
      sess.run(tf.global_variables_initializer())
    except tf.errors.InvalidArgumentError:
      print(
          '
    
    此错误很可能表示此笔记本未配置为使用GPU。 '
          '通过命令面板(CMD/CTRL-SHIFT-P)或编辑菜单在笔记本设置中更改此设置.
    
    ')
      raise
    
    def cpu():
      sess.run(net_cpu)
      
    def gpu():
      sess.run(net_gpu)
      
    # 运行一次进行测试
    cpu()
    gpu()
    
    # 多次运行op
    print('将100*100*100*3通过滤波器卷积到32*7*7*3(批处理x高度x宽度x通道)大小的图像'
        '计算10次运训时间的总和')
    print('CPU (s):')
    cpu_time = timeit.timeit('cpu()', number=10, setup="from __main__ import cpu")
    print(cpu_time)
    print('GPU (s):')
    gpu_time = timeit.timeit('gpu()', number=10, setup="from __main__ import gpu")
    print(gpu_time)
    print('GPU加速超过CPU: {}倍'.format(int(cpu_time/gpu_time)))
    
    sess.close()
    # CPU (s):
    # 3.593296914000007
    # GPU (s):
    # 0.1831514239999592
    # GPU加速超过CPU: 19倍
    View Code

    Colab中的TPU

    首先我们要让Colab连上GPU,导航栏-->编辑-->笔记本设置-->选择TPU

    接下来我们来确认可以使用Tensorflow连接到TPU

    复制代码
    import os
    import pprint
    import tensorflow as tf
    
    if 'COLAB_TPU_ADDR' not in os.environ:
      print('您没有连接到TPU,请完成上述操作')
    else:
      tpu_address = 'grpc://' + os.environ['COLAB_TPU_ADDR']
      print ('TPU address is', tpu_address)     
      # TPU address is grpc://10.97.206.146:8470
    
      with tf.Session(tpu_address) as session:
        devices = session.list_devices()
        
      print('TPU devices:')
      pprint.pprint(devices)
    复制代码

    使用TPU进行简单运算

    复制代码
    import numpy as np
    
    def add_op(x, y):
      return x + y
      
    x = tf.placeholder(tf.float32, [10,])
    y = tf.placeholder(tf.float32, [10,])
    tpu_ops = tf.contrib.tpu.rewrite(add_op, [x, y])
      
    session = tf.Session(tpu_address)
    try:
      print('Initializing...')
      session.run(tf.contrib.tpu.initialize_system())
      print('Running ops')
      print(session.run(tpu_ops, {x: np.arange(10), y: np.arange(10)}))
      # [array([ 0.,  2.,  4.,  6.,  8., 10., 12., 14., 16., 18.], dtype=float32)]
    finally:
      # 目前,tpu会话必须与关闭会话分开关闭。
      session.run(tf.contrib.tpu.shutdown_system())
      session.close()
    复制代码

    在Colab中运行Tensorboard

    想要在Google Colab中运行Tensorboard,请运行以下代码

    复制代码
    !wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
    !unzip ngrok-stable-linux-amd64.zip
    
    # 添加TensorBoard的路径
    import os
    log_dir = 'tb_logs'
    if not os.path.exists(log_dir):
      os.makedirs(log_dir)
    
    # 开启ngrok service,绑定port 6006(tensorboard)
    get_ipython().system_raw('tensorboard --logdir {} --host 0.0.0.0 --port 6006 &'.format(log_dir))
    get_ipython().system_raw('./ngrok http 6006 &')
    
    # 产生网站,点击网站访问tensorboard
    !curl -s http://localhost:4040/api/tunnels | python3 -c 
        "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"
    复制代码

    您可以使用创建的ngrok.io URL 跟踪Tensorboard日志。您将在输出末尾找到URL。请注意,您的Tensorboard日志将保存到tb_logs目录。当然,您可以更改目录名称。

    之后,我们可以看到Tensorboard发挥作用!运行以下代码后,您可以通过ngrok URL跟踪Tensorboard日志。

    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
    from keras.callbacks import TensorBoard
    
    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'])
    
    
    tbCallBack = TensorBoard(log_dir=LOG_DIR, 
                             histogram_freq=1,
                             write_graph=True,
                             write_grads=True,
                             batch_size=batch_size,
                             write_images=True)
    
    model.fit(x_train, y_train,
              batch_size=batch_size,
              epochs=epochs,
              verbose=1,
              validation_data=(x_test, y_test),
              callbacks=[tbCallBack])
    score = model.evaluate(x_test, y_test, verbose=0)
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])
    View Code

    参考

    Colaboratory官方文档

    一位外国小哥写的博客,总结的不错

    作者:凌逆战
    欢迎任何形式的转载,但请务必注明出处。
    限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。
    本文章不做任何商业用途,仅作为自学所用,文章后面会有参考链接,我可能会复制原作者的话,如果介意,我会修改或者删除。

    出处:https://www.cnblogs.com/LXP-Never/p/11614053.html

  • 相关阅读:
    Java-对象数组排序
    aoj 0118 Property Distribution
    poj 3009 Curling 2.0
    poj 1979 Red and Black
    AtCoder Regular Contest E
    AtCoder Beginner Contest 102
    AtCoder Beginner Contest 104
    SoundHound Inc. Programming Contest 2018
    poj 3233 Matrix Power Series
    poj 3734 Blocks
  • 原文地址:https://www.cnblogs.com/mq0036/p/12690623.html
Copyright © 2011-2022 走看看