安装配置MMSegmentation环境
为了验证 MMSegmentation 和所需的环境是否安装正确,我们可以运行示例 python 代码来初始化分段器并推断演示图像:
from mmseg.apis import inference_segmentor, init_segmentor
import mmcvconfig_file = 'configs/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes.py'
checkpoint_file = 'checkpoints/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth'# build the model from a config file and a checkpoint file
model = init_segmentor(config_file, checkpoint_file, device='cuda:0')# test a single image and show the results
img = 'test.jpg' # or img = mmcv.imread(img), which will only load it once
result = inference_segmentor(model, img)
# visualize the results in a new window
model.show_result(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].
model.show_result(img, result, out_file='result.jpg', opacity=0.5)
安装MMCV官方
mmsegmention环境安装
Ubuntu20运行SegNeXt代码提取道路水体(一)——从mmsegmentation安装到测试代码环境配置全过程摸索
怎么把图片保存出来?
在SegNeXt/mmseg/models/segmentors/base.py里的show_result函数里加了out_file = 'demo/result_demo.png'
但是会报错
cv2.error: OpenCV(4.5.5) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
出现的问题
后面下载了ade数据集,刚开始运行
python tools/train.py configs/pspnet/pspnet_r50-d8_512x512_80k_ade20k.py
还能跑起来,但是不知道为什么就报错AttributeError: 'ConfigDict' object has no attribute 'log_level'
解决:AttributeError: ‘ConfigDict‘ object has no attribute ‘log_level‘
训练与测试
训练:
python tools/train.py configs/pspnet/pspnet_r50-d8_512x512_80k_ade20k.py
测试:
python tools/test.py work_dirs/pspnet_r50-d8_512x512_80k_ade20k/pspnet_r50-d8_512x512_80k_ade20k.py work_dirs/pspnet_r50-d8_512x512_80k_ade20k/iter_80000.pth --eval mIoU
想要把预测的图片生成出来:
python tools/test.py work_dirs/pspnet_r50-d8_512x512_80k_ade20k/pspnet_r50-d8_512x512_80k_ade20k.py work_dirs/pspnet_r50-d8_512x512_80k_ade20k/iter_80000.pth --show-dir work_results/ --eval mIoU
使用SegNeXt进行训练及预测
下载ImageNet预训练模型:
用相同的方法进行训练 看看ade数据效果如何
把这个预训练放在这个路径下:pretrained/mscan_b.pth
修改数据的位置,放到这个路径下:/root/ADEChallengeData2016/images/training
出现错误:PermissionError: ADE20KDataset: [Errno 13] Permission denied: ‘/root/ADEChallengeData2016/images/training’
修改之后,
训练
python tools/train.py local_configs/segnext/base/segnext.base.512x512.ade.160k.py
预测
python tools/test.py work_dirs/segnext.base.512x512.ade.160k/segnext.base.512x512.ade.160k.py work_dirs/segnext.base.512x512.ade.160k/latest.pth --show-dir work_results_segnext/ --eval mIoU
结果:
代码详解
代码详解
参考:
Ubuntu20运行SegNeXt代码提取道路水体(一)——从mmsegmentation安装到测试代码环境配置全过程摸索
Ubuntu20运行SegNeXt代码提取道路水体(二)——SegNeXt源代码安装到测试环境配置全过程摸索
MMSegmentation V0.27.0训练与推理自己的数据集(二)