免费使用Colab运算资源

白嫖Colab运算资源

一、Colab挂载云盘代码并运行模型

参考使用colab运行深度学习gpu应用(Mask R-CNN)实践

Colab在界面设计上虽然和阿里云天池实验室差不多,但是Google的生态圈实在太大了,在Google云盘中可以用Colab打开代码,大大提高开发者的工作效率。

  • 先在Colab上点击修改 >> 笔记本设置 >> GPU,可以白嫖一台GPU

  • 先将代码上传到Google云盘上(这一步最耗时,在colab使用之前先做,不会占用GPU资源),接着将云盘上的代码挂载到Colab远程虚拟服务器上。需要如下一些命令:

    1)安装必要的包和软件

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

    运行代码,运行中会提示输入验证码,点击程序给出的网址进行验证即可。

    2)挂载Google云盘到Drive中

    !mkdir -p drive
    !google-drive-ocamlfuse -o nonempty drive
    

    3)查看挂载文件

    !ls drive
    

    4)更改当前项目目录为主目录

    import os
    
    os.chdir("/content/drive/faster_rcnn_pytorch_master")
    

附几张图上去

在这里插入图片描述
在这里插入图片描述

常见报错

  • Colab无法执行shell脚本

    shell-init: error retrieving current directory: getcwd: cannot access parent directories: Transport endpoint is not connected
    pwd: error retrieving current directory: getcwd: cannot access parent directories: Transport endpoint is not connected
    

    解决方法参考python - Colab突然无法在目录中导航

    快速解决方法:

    • 前往Colab >> 点击运行时 >> 最后,单击重新启动运行时。(警告:您的局部变量将丢失

    我的解决方法

    • 代码执行程序 >> 将该运行时恢复出厂设置

二、Colab使用detectron2进行模型训练

参考

三、Colab使用MMDetection进行模型训练

1、本地安装mmdetection(windows版)(放弃)

参考

1)ensure you have downloaded gcc and nvcc

参考安装minGW

# Check nvcc version
!nvcc -V
# Check GCC version
!gcc --version

2)install mmdetection

pip install mmdet

3)Create a conda virtual environment and activate it.

conda create -n open-mmlab python=3.7 -y
conda activate open-mmlab

4)Install PyTorch and torchvision following the official instructions, e.g.

# CUDA 10.1
conda install pytorch==1.5.1 torchvision==0.6.1 cudatoolkit=10.1 -c pytorch
#或者
pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html

5)Install mmcv-full, we recommend you to install the pre-build package as below.

pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu101/torch1.4.0/index.html

#或者直接 
pip install mmcv-full
#更换源,下载更快
pip  install --index https://pypi.mirrors.ustc.edu.cn/simple/ mmcv-full

Note:

  • 如果pytorch版本安装错误,mmcv-full会找不到对应版本

    conda uninstall pytorch
    conda uninstall libtorch
    
  • 如果pip连接不上代理,而且下载速度很慢,可以更换源

    pip  install --index https://pypi.mirrors.ustc.edu.cn/simple/ opencv-python==4.5.1.48
    

    参考pip install 提示代理连接失败原因及解决办法

  • ubuntu安装anaconda找不到condarc:

    conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
    conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
    conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
    conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
    conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
    conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
        
    conda config --set show_channel_urls yes
    

放弃治疗…

在这里插入图片描述

在这里插入图片描述

2、ubuntu安装,一次性搞定

参考installation

比较重要的一步:用git clone项目到本地,比直接解压zip好,方便安装requirement中的依赖包,及把mmdetection,mmaction的核心库安装在虚拟环境(open-mmlab)中,也方便显示修改的文档,显示修改的文档,显示修改的文档!!!(重要事情强调3遍)

Clone the MMDetection repository.

git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection

Install build requirements and then install MMDetection.

pip install -r requirements/build.txt
pip install -v -e .  # or "python setup.py develop"

四、常见问题汇总

1、cannot connect to X server

解决措施:关闭 OpenCV 的图像显示函数。

参考在Linux/colab下遇到: cannot connect to X server问题

2、用量限额(Colab免费版支持12h)+ 空闲超时

天下没有免费的午餐,想白嫖就需要有好的姿势(偶尔拿着K80跑一跑项目,或者跑一下小的模型,而且colab保存的重名的权重文件会存放在Google网盘的回收站中,不用担心模型重复训练)

如果想要有好的体验,参考官方说明 选择最适合您的 Colab 方案

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值