Todo el proceso, entrenamiento del conjunto de datos de segmentación semántica.
Anotación de datos
# 安装
pip install labelme
# 启动labelme
labelme
Luego presione Ctrl + N para habilitar la anotación de polígonos. Después de nombrar la clase como persona,
se guardará en un archivo json en el mismo directorio:
json para enmascarar
Descargue el código de conversión en el código labelme: Ejecute las instrucciones
en el siguiente formulario almacenado en etiquetas
python labelme2voc.py ./img output labels.txt
Generado de la siguiente manera
Ejecute el código fuente MMSegmentation
Se genera un archivo my_data.py en mmseg/datasets. Este archivo almacena información de categoría y color de segmento.
Es necesario agregar una red troncal adicional.
# 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)
Agregar al archivo mmseg/utils/class_names.py: no se informará ningún error sin agregar la red troncal. No se agrega aquí. Es mejor agregarlo. Además, el color del segmento debe ser consistente con el archivo anterior.
def mydata_classes():
"""shengteng class names for external use."""
return [
'person'
]
def mydata_palette():
return [[244, 35, 232]]
Agregado en mmseg/datasets/ init.py ,
from .my_data import mydata
Cree un nuevo archivo my_data.py en el archivo configs/ base /datasets:
este es un archivo para leer datos, incluida la dirección, el tipo y los métodos de carga de los datos.
# 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
Sección de ejecución de selección de modelo
Lo que elegí es configs/deeplabv3/deeplabv3_r50-d8_4xb2-40k_cityscapes-512x1024.py, principalmente para modificar la parte de datos heredados.
Ejecutar
Cada vez que modifique el archivo de configuración, es mejor ejecutarlo nuevamente.python 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)