colab配置mmdetection环境_by2021.6

本文基于colab环境下,配置mmdetection环境,配置下来也踩了不少坑,希望能对有需要的朋友一点点帮助。

安装库最常见的bug大都是由于版本不兼容导致的(软件和硬件不兼容、不同库之间有冲突),所以在安装前要先确认平台的基本信息:平台系统、cuda版本、GCC版本。

#输入
!sudo lsb_release -a # 查看系统版本
!nvcc -V #或者!nvidia-smi # 查看cuda版本
!gcc --version # 查看GCC版本
——————————————————手动分割线————————————————
#输出
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 18.04.5 LTS
Release:	18.04
Codename:	bionic
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Wed_Jul_22_19:09:09_PDT_2020
Cuda compilation tools, release 11.0, V11.0.221
Build cuda_11.0_bu.TC445_37.28845127_0
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

可知:系统是Ubuntu18.04,cuda11.0,GCC7.5.0。

colab目前是默认安装了torch1.8.1+cu101,torchvision 0.9.1+cu101

# 输入
import torch, torchvision
print(torch.__version__, torch.cuda.is_available())
# 输出
1.8.1+cu101 True

我这里选择了GPU,如果使用的是CPU,上面的torch.cuda.is_available()结果会输出Flase,表示没有GPU加速。

但是,因为系统“硬件”cuda是11.0版本的,而torch是cu101,如果不修改直接导入mmdet会报错。看下图:

个人觉得就是版本不一致的问题,所以要重新安装pytorch到cu111版本。👇

# 把Google云盘挂载到colab上
from google.colab import drive
drive.mount('/content/gdrive') #这一步会让你点击链接获取验证码
#目录"/content/gdrive/MyDrive/"就指向你的谷歌云盘了
%cd /content/gdrive/MyDrive/ # 进入你的云盘,并作为工作目录

#卸载原有的pytorch
!pip uninstall torch torchvision -y

#线上安装新的pytorch
!pip install -U torch==1.8.1+cu111 torchvision==0.9.1+cu111 -f https://download.pytorch.org/whl/torch_stable.html

#本地安装pytorch-from-google-cloud
#因为我嫌弃有时候线上安装每次要重新下载,所以我把pytorch的安装包放到我的云盘了
#每次直接冲云盘读取安装包,会节省60s左右吧,好像也无所谓哈~
#!pip install /content/gdrive/MyDrive/installer/pytorch/torch-1.8.1+cu111-cp37-cp37m-linux_x86_64.whl
#!pip install /content/gdrive/MyDrive/installer/pytorch/torchvision-0.9.1+cu111-cp37-cp37m-linux_x86_64.whl


#安装mmdetection
!rm -rf mmdetection #如果有mmdetection文件夹就删掉,下面新建
!git clone https://github.com/open-mmlab/mmdetection.git
%cd mmdetection

!pip install mmcv-full
!pip install -e .
# install Pillow 7.0.0 back in order to avoid bug in colab
!pip install Pillow==7.0.0
#静候安装完成,预计6min(360s)

如果在安装完成后看到以下提示,让你restart runtime。不要直接刷新页面或者点击下图中的"RESTART RUNTIME",因为你点了之后刚刚安装的库就都没有了......

WARNING: The following packages were previously imported in this runtime:
You must restart the runtime in order to use newly installed versions.

目前的解决办法,在下面新建一个单元格,执行以下两行代码,会重启服务器,之前安装的库都还在。

import os
os.kill(os.getpid(), 9)

检查下torch1.8.1+cu111、mmdet和mmcv是否安装完成:

# Check Pytorch installation
import torch, torchvision
print(torch.__version__, torch.cuda.is_available())

# Check MMDetection installation
import mmdet
print(mmdet.__version__)

# Check mmcv installation
from mmcv.ops import get_compiling_cuda_version, get_compiler_version
print(get_compiling_cuda_version())
print(get_compiler_version())

#输出:
1.8.1+cu111 True
2.13.0
11.0
GCC 7.5

最后,导入mmdet和mmcv看看可以用不,执行以下两行如果不报错就基本OK了。

from mmdet.apis import init_detector, inference_detector, show_result_pyplot
import mmcv

赶紧拿个官方代码跑一下吧~

<本文完结>研一新生刚开始摸索,很多理解不到位,请大家多多指教~蟹蟹

  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值