• Google Colab使用教程


    简介
    Google Colaboratory是谷歌开放的云服务平台,提供免费的CPU、GPU和TPU服务器。

    目前深度学习在图像和文本上的应用越来越多,不断有新的模型、新的算法获得更好的效果,然而,一方面对资源的要求越来越高,另一方面很多开源的模型在国内无法使用。例如,前段时间研究的图片风格快速转换中用到的模型训练,在GPU上运行需要4个多小时,在CPU上无法想象。再者,tensorflow-hub开源的很多模型,我在使用某种软件的前提下,依然无法访问。

    解决上述问题的一种方法,就是使用谷歌的Colab平台。他提供的GPU型号是Tesla K80,并且预安装了常用的框架,例如TensorFlow等。

    账号
    Colab 和 Google Drive使用同一账号登录。

    Google Drive: https://drive.google.com/drive
    Colab: https://colab.research.google.com/drive/

    使用流程
    我将谷歌云盘作为Colab的外挂硬盘使用,每次启动需要使用以下步骤:

    在“我的云端硬盘”中创建文件夹“Colab”,用于存放Colab中相关文件。(注意最好不要有空格,以避免不必要的错误)
    点击左上角“新建”,选择Colaboratory。首次使用,需要关联Colab应用。
    创建新应用后,Drive的当前路径下会生成 Unititled*.ipynb,也就是保存当前Colab窗口内容的文件。 每次新建,都需要重新配置环境。
    配置“笔记本设置”。选择“修改”-“笔记本设置”,设置python版本和服务器类型。
    安装必要的包和软件。
    !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}
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    关联Google Drive。
    !mkdir -p drive
    !google-drive-ocamlfuse drive
    1
    2
    点击左侧菜单,可以看到“文件”下方生成了一个“drive”文件夹,和云盘中文件夹保持同步。
    5. 安装需要的工具(可以省略)。

    其他操作
    (1) colab中使用linux命令,前面需要加上"!",例如,创建文件夹:

    !mkdir colab
    1
    (2) 切换工作目录使用chdir函数

    import os
    os.chdir("/content/drive/Colab")
    1
    2
    (3) 设置方便可用的快捷键。

    打开“工具”-“键盘快捷键”进行设置。

    (4) 左侧菜单中间“代码段”中,可用根据需要选择常用代码,例如加载drive中的数据,保存文件到本地系统。

    (5)Colab最多连续使用12小哥,超过时间系统会强制停止,再次使用需重新配置。

    下载TF-hub模型
    因为某种原因,国内无法访问tf-hub,其中提供的很多模型也无法使用。如果直接在Colab中使用,可以按照官网指定代码运行使用。如果想要下载到本地使用,需要在colab中运行以下代码:

    以下载通用句子编码(universal-sentence-encoder model)为例:

    # Create a folder for the TF hub module.
    $ mkdir /tmp/moduleA
    # Download the module, and uncompress it to the destination folder. You might want to do this manually.
    $ curl -L "https://tfhub.dev/google/universal-sentence-encoder/2?tf-hub-format=compressed" | tar -zxvC /tmp/moduleA
    # Test to make sure it works.
    $ python
    > import tensorflow_hub as hub
    > hub.Module("/tmp/moduleA")
    1
    2
    3
    4
    5
    6
    7
    8
    其他模型和示例参考:https://github.com/tensorflow/hub/tree/master/examples”
    ---------------------
    作者:蜡笔大龙猫
    来源:CSDN
    原文:https://blog.csdn.net/pirage/article/details/86741977
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    各大OJ刷题进度和分类
    (HDU)1785 -- You Are All Excellent (你天赋异禀)
    (HDU)1720 -- A+B Coming (A+B来了)
    (HDU)1718 -- Rank (段位)
    (HDU)1708 -- Shopaholic (购物狂)
    (HDU)1678 -- Shopaholic (购物狂)
    (HDU)1673 -- Optimal Parking (停车位)
    (HDU)1587 -- Flowers (花)
    (HDU)1570 -- A C
    (HDU)1563 -- Find your present! (找到你的礼物)
  • 原文地址:https://www.cnblogs.com/jfdwd/p/11227617.html
Copyright © 2020-2023  润新知