mmsegmentation使用记录
mmseg官方代码
https://github.com/open-mmlab/mmsegmentation
一、环境搭建
环境搭建可参考如下链接:
https://github.com/open-mmlab/mmsegmentation/blob/master/docs/get_started.md#installation
1.conda install pytorch=1.6.0 torchvision cudatoolkit=10.1 -c pytorch
2.pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu101/torch1.5.0/index.html
3.cd mmsegmentation
python setup.py develop
查看是否成功安装:
from mmseg.apis import inference_segmentor, init_segmentor
import mmcv
config_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)
# test a video and show the results
video = mmcv.VideoReader('video.mp4')
for frame in video:
result = inference_segmentor(model, frame)
model.show_result(frame, result, wait_time=1)
二、mmseg使用
1.下载公开数据集:
这里使用cityscapes公开数据集,分为精细标注与粗略标注两部分,这里只利用精细标注的5000张图像,下载地址:https://aistudio.baidu.com/aistudio/datasetdetail/48855
下载之后把数据分别存放在gtFine与leftImg8bit两个文件夹下就可以了
2.处理数据:
推荐使用官方处理代码:https://github.com/mcordts/cityscapesScripts
其中主要修改两个文件:./helpers/labels.py和./preparation/createTrainIdLabelImgs.py
其中labels.py修改你想训练的类别,将不想训练的trainID设为255,ignoreInEval设为True
想训练的trainID为0-类别数,ignoreInEval设为False。然后将createTrainIdLabelImgs.py中的路径设置为你数据集的路径:
# Where to look for Cityscapes
# if 'CITYSCAPES_DATASET' in os.environ:
# cityscapesPath = os.environ['CITYSCAPES_DATASET']
# else:
# cityscapesPath = os.path.join(os.path.dirname(os.path.realpath(__file__)),'..','..')
cityscapesPath = 'E:\cityscapesScripts-master\data1'
# how to search for all ground truth
searchFine &#