• Google Colab


    Colab 介绍

    • Jupyter Notebook:在 Colab 中,python 代码的执行是基于 .ipynb 文件,也就是 Jupyter Notebook 格式的 python 文件
    • 代码执行程序:代码执行程序就是 Colab 在云端的 “服务器”。笔记本连接到代码执行程序的时长是有限制的,这体现在三个方面:如果关闭浏览器,代码执行程序会在短时间内断开而不是在后台继续执行(这个 “短时间” 大概在几分钟左右,如果只是切换一下 wifi 之类的操作不会产生任何影响);如果空闲状态过长(无互动操作或正在执行的代码块),则会立即断开连接 (可以下载谷歌浏览器扩展工具 Colab Auto Reconnect);如果连接时长到达上限免费用户最长连接 12 小时),也会立刻断开连接。因此运行的代码必须支持 “断点续传” 能力,简单来说就是必须定义 checkpoint 相关功能的函数;假设训练完第 n n n 个 epoch 后掉线,模型能够从第 n + 1 n+1 n+1 个 epoch 继续训练而不必从头开始
    • 实例空间:连接到代码执行程序后,Colab 需要为其分配实例空间 (Instance),可以简单理解为运行笔记本而创建的 “虚拟机”,其中包含了执行 ipynb 文件时的默认配置、环境变量、自带的库等等。笔记本连接到代码执行程序后就可以看到虚拟机的 RAM、磁盘大小以及磁盘中的文件
      在这里插入图片描述实例空间内的文件保存不是永久的,当代码执行程序被断开时,实例空间内的所有资源都会被释放,因此训练过后的模型日志和其他重要的文件需要保存到谷歌云盘,而不是本地的实例空间
    • 会话:当笔记本连接到代码执行程序并分配到实例空间后,就成为了一个会话 (Session)。通过点击 “管理会话” 即可查看当前的所有会话,点击 “终止” 即可断开代码执行程序
      在这里插入图片描述用户所能连接的会话数量是有限的,因此到达上限时再开启新会话需要主动断开之前的会话。免费用户只能开启 1 个会话,Pro 用户则可以开启多个会话。不同的用户可以在一个笔记本上可以进行多个会话,但只能有一个会话执行代码块。如果某个代码块已经开始执行,另一个用户连接到笔记本的会话会显示 “忙碌状态”,需要等待代码块执行完后才能执行其他的代码块。注意:掉线重连、切换网络、刷新页面等操作也会使笔记本进入 “忙碌状态”
      在这里插入图片描述

    Colab 工作流程

    新建笔记本

    • 打开 Google Drive,在云端硬盘中右键创建 Google Colaboratory
      在这里插入图片描述
    • 也可以直接打开 https://colab.research.google.com/,进入 Colab 的页面后点击新建笔记本即可。使用这种方法新建的笔记本时,会在云端硬盘的根目录自动创建一个叫 Colab Notebook 的文件夹,新创建的笔记本就保存在这个文件夹中

    载入笔记本

    • 可以打开云端硬盘中的已经存在的笔记本,还可以从 Github 中导入笔记本。如果关联 Github 账户,就可以选择一个账户中的 Project,如果其中有 .ipynb 文件就可以在 Colab 中打开
      在这里插入图片描述在这里插入图片描述

    设置笔记本的运行时类型

    • 打开笔记本后点击连接按钮即可在 5s 左右的时间内连接到代码执行程序并分配实例空间,此时可以看到消耗的 RAM 和磁盘
    • 笔记本在打开时的默认硬件加速器是 None,运行规格是标准。在深度学习中,我们希望使用 GPU 来训练模型,同时如果购买了 pro,我们希望使用高内存模式。点击代码执行程序,然后点击 “更改运行时类型” 即可。由于免费的用户所能使用的 GPU 运行时有限,因此建议在模型训练结束后调回 None 模式或直接结束会话
      在这里插入图片描述

    执行代码块

    • 在打开笔记本后,我们默认的文件路径是 “/content”,这个路径也是执行笔记本时的路径,同时我们一般把用到的各种文件也保存在这个路径下。在点击 “…” 后即可返回查看根目录 “/”,可以看到根目录中保存的是一些虚拟机的环境变量和预装的库等等。不要随意修改根目录中的内容,以避免运行出错,我们所有的操作都应在 “/content” 中进行
      在这里插入图片描述
    • notebook 文件通过的代码块来执行代码,同时支持通过 “!” 的方式来执行 UNIX 终端命令。Colab 已经预装了大多数常见的深度学习库,比如 pytorchtensorflow 等等,如果有需要额外安装的库可以通过 “!pip3 install ” 命令来安装。下面是一些常见的命令
    # 加载云端硬盘
    from google.colab import drive
    drive.mount('/content/drive')	# 谷歌云盘默认的加载路径是 "/content/drive/MyDrive"
    
    # 查看分配到的 GPU
    gpu_info = !nvidia-smi
    gpu_info = '\n'.join(gpu_info)
    if gpu_info.find('failed') >= 0:
      print('Not connected to a GPU')
    else:
      print(gpu_info)
    
    # 安装 python 包
    !pip3 install <package>
    
    # 使用 Tensorboard
    %reload_ext tensorboard
    %tensorboard --logdir "pathoflogfile"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    加载数据集

    • (1) 将整个数据集从本地上传到实例空间。理论可行但实际不可取。无论是上传压缩包还是文件夹,这种方法都非常的浪费时间,对于较大的数据集不具备可操作性
    • (2) 将整个数据集上传到谷歌云盘,挂载谷歌云盘的之后直接读取云盘内的数据集。理论可行但风险较大。根据谷歌的说明,Colab 读取云盘的 I/O 次数也是有限制的,太琐碎的 I/O 会导致出现 “配额限制”。而且云盘的读取效率也低于直接读取实例空间中的数据的效率
    • (3) 将数据集以压缩包形式上传到谷歌云盘,然后解压到 Colab 实例空间

    运行 Github 项目

    • Colab的基本运行单位是 Jupyter Notebook,如何在一个 notebook 上运行一整个复杂的 Github 项目呢?首先创建多个笔记本来对应多个 py 模块是不行的,因为不同的笔记本会对应不同实例空间,而同一个项目的不同模块应放在同一个实例空间中。为解决这个问题,可以考虑以下几种方法

    • (1) 克隆 git 仓库到实例空间或云盘,通过脚本的方式直接执行项目的主程序
    # 克隆仓库到 /content/my-repo 目录下
    !git clone https://github.com/my-github-username/my-git-repo.git 
    %cd my-git-repo
    !./train.py --logdir /my/log/path --data_root /my/data/root --resume
    
    • 1
    • 2
    • 3
    • 4
    • (2) 克隆 git 仓库到实例空间或云盘,把主程序中的代码用函数封装,然后在 notebook 中调用这些函数
    import sys
    sys.path.append('/content/my-git-repo') # 把 git 仓库的目录添加到系统目录
    from train import my_training_method
    
    my_training_method(arg1, arg2, ...)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • (3) 克隆 git 仓库到实例空间或云盘,把原来的主程序模块直接复制到笔记本中

    Kaggle

    Public API

    Installation & Authentication

    pip install kaggle
    
    • 1
    • Authenticate using an API token: From the site header, click on your user profile picture, then on “My Account” from the dropdown menu. Scroll down to the section of the page labelled API:
      在这里插入图片描述To create a new token, click on the “Create New API Token” button. This will download a fresh authentication token onto your machine. If you are using the Kaggle CLI tool, the tool will look for this token at ~/.kaggle/kaggle.json on Linux, OSX, and other UNIX-based operating systems, and at C:\Users.kaggle\kaggle.json on Windows.

    Interacting with Competitions

    • You cannot accept Competition rules via the API. You must do this by visiting the Kaggle website and accepting the rules there.
    # list the currently active competitions
    kaggle competitions list
    # download files associated with a competition
    kaggle competitions download -c [COMPETITION]
    # make a competition submission
    kaggle competitions submit -c [COMPETITION] -f [FILE] -m [MESSAGE]
    # list all previous submission
    kaggle competitions submissions -c [COMPETITION NAME]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Interacting with Datasets

    # list datasets matching a search term
    kaggle datasets list -s [KEYWORD]
    # download files associated with a dataset
    kaggle datasets download -d [DATASET]
    
    • 1
    • 2
    • 3
    • 4

    Create a New Dataset

    # generate a metadata file
    kaggle datasets init -p /path/to/dataset
    # create the dataset
    kaggle datasets create -p /path/to/dataset
    
    • 1
    • 2
    • 3
    • 4

    Your dataset will be private by default. You can also add a -u flag to make it public when you create it.

    Create a New Dataset Version

    # generate a metadata file
    # Make sure the id field in dataset-metadata.json (or datapackage.json) points to your dataset
    kaggle datasets init -p /path/to/dataset
    # create a new dataset version
    kaggle datasets version -p /path/to/dataset -m "Your message here"
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Interacting with Notebooks

    # list Notebooks matching a search term
    kaggle kernels list -s [KEYWORD]
    # create and run a Notebook on Kaggle
    kaggle kernels push -k [KERNEL] -p /path/to/kernel
    # download code files and metadata associated with a Notebook
    kaggle kernels pull [KERNEL] -p /path/to/download -m
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    Creating and Running a New Notebook

    • Create a local folder containing the code files you want to upload
    # generate a metadata file
    kaggle kernels init -p /path/to/kernel
    
    • 1
    • 2
    • Add your Notebook’s metadata to the generated file, kernel-metadata.json; As you add your title and slug, please be aware that Notebook titles and slugs are linked to each other. A Notebook slug is always the title lowercased with dashes (-) replacing spaces and removing special characters.
    # create and run the Notebook on Kaggle
    kaggle kernels push -p /path/to/kernel
    
    • 1
    • 2

    参考文献

  • 相关阅读:
    DHCP(自动分配ip地址实验案例)
    改进粒子滤波的无人机三维航迹预测方法附Matlab代码
    初识OpenGL (-)纹理过滤(Texture Filtering)
    掌握docker这几招,你也能搞云计算了
    stable diffusion comfyui的api使用教程
    Qt音视频开发01-共享解码线程(耗时一年/性能凶残/至臻完美)
    “第五十天” 机组--数据的表示
    DCMM的定义和基础条件
    8、AQS
    Linux 系统 Vi和Vim编辑器—笔记7
  • 原文地址:https://blog.csdn.net/weixin_42437114/article/details/126398386