MMSegmentation trains its own semantic segmentation dataset

The whole process, training semantic segmentation data set

Data annotation

# 安装
pip install labelme
# 启动labelme
labelme

Insert image description here
Then press ctrl +N to enable polygon annotation. After naming the class as person,
Insert image description here
it will be saved to a json file in the same directory:
Insert image description here

json to mask

Download the conversion code in the labelme code: Run the instructions
Insert image description here
in the following form stored in labels
Insert image description here

python labelme2voc.py ./img output labels.txt

Generated as follows
Insert image description here

Run source code MMSegmentation

A my_data.py file is generated in mmseg/datasets. This file stores category information and seg color.
You need to add an extra backbone.

# Copyright (c) OpenMMLab. All rights reserved.
from mmseg.registry import DATASETS
from .basesegdataset import BaseSegDataset


@DATASETS.register_module()
class mydata(BaseSegDataset):
    """Cityscapes dataset.

    The ``img_suffix`` is fixed to '_leftImg8bit.png' and ``seg_map_suffix`` is
    fixed to '_gtFine_labelTrainIds.png' for Cityscapes dataset.
    """
    METAINFO = dict(
        classes=('backbone','person'),
        palette=[[128, 64, 128], [244, 35, 232]])

    def __init__(self,
                 img_suffix='.jpg',
                 seg_map_suffix='.png',
                 reduce_zero_label=True,
                 **kwargs) -> None:
        super().__init__(
            img_suffix=img_suffix,
            seg_map_suffix=seg_map_suffix,
            reduce_zero_label=reduce_zero_label,
            **kwargs)

Add to the mmseg/utils/class_names.py file: No error will be reported without adding backbone. It is not added here. It is best to add it. In addition, the seg color must be consistent with the above file.

def mydata_classes():
    """shengteng class names for external use."""
    return [
        'person'
    ]

def mydata_palette():
    return [[244, 35, 232]]

Added in mmseg/datasets/ init.py ,

from .my_data import mydata

Insert image description here

Create a new my_data.py file under the configs/ base /datasets file:
This is a file for reading data, including data address, type, and loading methods.

# dataset settings
dataset_type = 'mydata' #改
data_root = 'data/my_dataset'  #改
crop_size = (512, 512)
train_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(type='LoadAnnotations', reduce_zero_label=True),
    dict(
        type='RandomResize',
        scale=(2048, 512),
        ratio_range=(0.5, 2.0),
        keep_ratio=True),
    dict(type='RandomCrop', crop_size=crop_size, cat_max_ratio=0.75),
    dict(type='RandomFlip', prob=0.5),
    dict(type='PhotoMetricDistortion'),
    dict(type='PackSegInputs')
]
test_pipeline = [
    dict(type='LoadImageFromFile'),
    dict(type='Resize', scale=(2048, 512), keep_ratio=True),
    # add loading annotation after ``Resize`` because ground truth
    # does not need to do resize data transform
    dict(type='LoadAnnotations', reduce_zero_label=True),
    dict(type='PackSegInputs')
]
img_ratios = [0.5, 0.75, 1.0, 1.25, 1.5, 1.75]
tta_pipeline = [
    dict(type='LoadImageFromFile', backend_args=None),
    dict(
        type='TestTimeAug',
        transforms=[
            [
                dict(type='Resize', scale_factor=r, keep_ratio=True)
                for r in img_ratios
            ],
            [
                dict(type='RandomFlip', prob=0., direction='horizontal'),
                dict(type='RandomFlip', prob=1., direction='horizontal')
            ], [dict(type='LoadAnnotations')], [dict(type='PackSegInputs')]
        ])
]
train_dataloader = dict(
    batch_size=4,
    num_workers=4,
    persistent_workers=True,
    sampler=dict(type='InfiniteSampler', shuffle=True),
    dataset=dict(
        type=dataset_type,
        data_root=data_root,
        data_prefix=dict(
            img_path='images/training', seg_map_path='annotations/training'),  #改
        pipeline=train_pipeline))
val_dataloader = dict(
    batch_size=1,
    num_workers=4,
    persistent_workers=True,
    sampler=dict(type='DefaultSampler', shuffle=False),
    dataset=dict(
        type=dataset_type,
        data_root=data_root,
        data_prefix=dict(
            img_path='images/validation', #改
            seg_map_path='annotations/validation'), #改
        pipeline=test_pipeline))
test_dataloader = val_dataloader

val_evaluator = dict(type='IoUMetric', iou_metrics=['mIoU'])
test_evaluator = val_evaluator

Model selection run section

What I chose is configs/deeplabv3/deeplabv3_r50-d8_4xb2-40k_cityscapes-512x1024.py, mainly to modify the inherited data part
Insert image description here

Run
Every time you modify the configuration file, it is best to run it againpython setup.py install

python setup.py install
python ./tools/train.py ./configs/deeplabv3/deeplabv3_r50-d8_4xb2-40k_cityscapes-512x1024.py

![在这里插入图片描述](https://img-blog.csdnimg.cn/2ec531af24a94c6b982f55bffe7024bf.png)

Guess you like

Origin blog.csdn.net/qq_44224801/article/details/132587205