参考(官方文档):开始使用:安装并运行 MMSeg — MMSegment 1.1.1 文档 (mmsegmentation.readthedocs.io)
前提是大家先有了深度学习一些基础的环境配置,比如anaconda或miniconda,cuda,pytorch等
首先就是得安装mmengine和mmcv分别使用以下三个命令:
pip install -U openmim
mim install mmengine
mim install "mmcv>=2.0.0"
不过对于第三条,也可以自定义进行安装,如果有需求也可以安装其他版本的mmcv,这里就以2.0.0为例(注意版本适配,不然后续会出现版本不兼容的报错)
点击进入mmcv版本选择:download.openmmlab.com/mmcv/dist/cu111/torch1.8.0/index.html
找到适合自己电脑和python版本的对应文件进行下载
再按住ctrl+R使用命令提示符窗口,进入虚拟环境,并切换到mmcv下载到的文件夹(进入文件夹后使用pip install mmcv按住tab键进行补全)
至此mmengine和mmcv就已经安装好了
这里开始安装mmsegmentation:
方法一:推荐直接下载源码,连接在这里
GitHub - open-mmlab/mmsegmentation: OpenMMLab Semantic Segmentation Toolbox and Benchmark.
方法二:进行克隆仓库(如果失败多试几次,大概率是网络问题导致)
git clone -b main https://github.com/open-mmlab/mmsegmentation.git
cd mmsegmentation
pip install -v -e .
接下来就是验证是否安装完成的环节:
在刚刚的命令提示符窗口输入以下指令来下载权重文件和网络代码(最后有个点):
mim download mmsegmentation --config pspnet_r50-d8_4xb2-40k_cityscapes-512x1024 --dest .
注意这里网上有一些错误的验证命令,就会导致一些报错,错误命令主要是因为文件名不正确造成的。报错大概长这样:
意思就是说希望的配置文件名字在以下这一堆红字里面,不一样的话就会报错
代码执行完之后会在执行命令的目录下生成两个文件 :pspnet_r50-d8_4xb2-40k_cityscapes-512x1024.py 和
pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth
就说明下载好了已经
最后就是进行验证推理,使用命令:
python demo/image_demo.py demo/demo.png configs/pspnet/pspnet_r50-d8_4xb2-40k_cityscapes-512x1024.py pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth --device cuda:0 --out-file result.jpg
或者直接在pycharn中使用代码
from mmseg.apis import inference_model, init_model, show_result_pyplot
import mmcvconfig_file = 'pspnet_r50-d8_4xb2-40k_cityscapes-512x1024.py'
checkpoint_file = 'pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth'# build the model from a config file and a checkpoint file
model = init_model(config_file, checkpoint_file, device='cuda:0')# test a single image and show the results
img = 'demo/demo.png' # or img = mmcv.imread(img), which will only load it once
result = inference_model(model, img)
# visualize the results in a new window
show_result_pyplot(model, img, result, show=True)
# or save the visualization results to image files
# you can change the opacity of the painted segmentation map in (0, 1].
show_result_pyplot(model, img, result, show=True, out_file='result.jpg', opacity=0.5)
# test a video and show the results
video = mmcv.VideoReader('video.mp4')
for frame in video:
result = inference_segmentor(model, frame)
show_result_pyplot(model, result, wait_time=1)
最后运行出这个效果就说明安装成功了!