当前位置: 首页 > 工具软件 > PaddleSeg > 使用案例 >

用paddleseg跑segformer自己的数据集

董飞
2023-12-01

框架是paddlepaddle;
单卡跑;
segformer-b5;
数据集是 voc 格式;

代码:https://github.com/PaddlePaddle/PaddleSeg/blob/release/2.4/docs/whole_process_cn.md
环境的安装:https://blog.csdn.net/Scenery0519/article/details/128028857

1. 单卡训练的命令

在这里插入代码片export CUDA_VISIBLE_DEVICES=0 # 设置1张可用的卡

**windows下请执行以下命令**
**set CUDA_VISIBLE_DEVICES=0**
python train.py \
       --config configs/segformer/segformer_b5_voc_03_10k.yml \
       --do_eval \
       --use_vdl \
       --save_interval 500 \
       --save_dir output

config 配置文件是自己新建的。

2. segformer_b5_voc_03_10k.yml

PaddleSeg/configs/segformer 中新建一个配置文件。
我是直接复制了segformer_b5_cityscapes_1024x1024_160k.yml这个文件。
新建的文件我命名为 : segformer_b5_voc_03_10k.yml

_base_: '../_base_/huangche15_voc.yml'	# 1.改这里

batch_size: 1
iters: 10000	# 2.改这里

model:
  type: SegFormer_B5
  num_classes: 16		# 3.改这里
  pretrained: https://bj.bcebos.com/paddleseg/dygraph/mix_vision_transformer_b5.tar.gz

optimizer:
  _inherited_: False
  type: AdamW
  beta1: 0.9
  beta2: 0.999
  weight_decay: 0.01

lr_scheduler:
  type: PolynomialDecay
  learning_rate: 0.00006
  power: 1

loss:
  types:
    - type: CrossEntropyLoss
  coef: [1]

test_config:
    is_slide: True`在这里插入代码片`
    crop_size: [1152, 648]		# 4. 改这里
    stride: [768, 512]		# 5.改这里

  1. _base_: …/base/ 目录下自己数据集格式的配置文件
  2. iters: 迭代次数
  3. num_classes:标签数,如果有_background_ 背景类,需要将类别数+1
  4. crop_size: 图片裁剪大小
  5. stride: 需要比crop_size尺寸要小

3. huangche15_voc.yml

路径:PaddleSeg/configs/_base_
参考文件:pascal_voc12.yml
新建文件:huangche15_voc.yml

也就是 segformer_b5_voc_03_10k.yml 文件中的 _base_: ‘…/_base_/huangche15_voc.yml’

batch_size: 4
iters: 40000

train_dataset:
  type: Huangche15VOC		# 1.改这里
  dataset_root: /data/huangche_15		# 2.改这里
  transforms:
    - type: ResizeStepScaling
      min_scale_factor: 0.5
      max_scale_factor: 2.0
      scale_step_size: 0.25
    - type: RandomPaddingCrop
      crop_size: [1152, 648]		# 3.改这里
    - type: RandomHorizontalFlip
    - type: RandomDistort
      brightness_range: 0.4
      contrast_range: 0.4
      saturation_range: 0.4
    - type: Normalize
  mode: train

val_dataset:
  type: Huangche15VOC		# 4.改这里
  dataset_root: data/huangche_15		# 5.改这里
  transforms:
    - type: Padding
      target_size: [3840, 2160]		# 6.改这里
    - type: Normalize
  mode: val


optimizer:
  type: sgd
  momentum: 0.9
  weight_decay: 4.0e-5

lr_scheduler:
  type: PolynomialDecay
  learning_rate: 0.01
  end_lr: 0
  power: 0.9

loss:
  types:
    - type: CrossEntropyLoss
  coef: [1]

  1. type: 定义自己数据集的类
  2. dataset_root: 自己数据集的路径。(如果是voc格式的数据集,记得路径下是 VOC2012 文件夹,VOC2012 文件夹下才是数据集)
    例如上面的路径是 data/huangche_15,那么目录结构该是:
    data/huangche_15
      >VOC2012
        >ImageSets
        >JPEGImages
        >SegmentationClass
  3. crop_size: 裁剪的图片大小,和segformer_b5_voc_03_10k.yml文件中保持一致。
  4. target_size:应是原图像大小

4. huangche15voc.py

路径:PaddleSeg/paddleseg/datasets
参考文件: voc.py
新建文件:huangche15voc.py

# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

from paddleseg.datasets import Dataset
from paddleseg.utils.download import download_file_and_uncompress
from paddleseg.utils import seg_env
from paddleseg.cvlibs import manager
from paddleseg.transforms import Compose

URL = "http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar"


@manager.DATASETS.add_component
class Huangche15VOC(Dataset):		# 1.改这里
    """
    PascalVOC2012 dataset `http://host.robots.ox.ac.uk/pascal/VOC/`.
    If you want to augment the dataset, please run the voc_augment.py in tools.

    Args:
        transforms (list): Transforms for image.
        dataset_root (str): The dataset directory. Default: None
        mode (str, optional): Which part of dataset to use. it is one of ('train', 'trainval', 'trainaug', 'val').
            If you want to set mode to 'trainaug', please make sure the dataset have been augmented. Default: 'train'.
        edge (bool, optional): Whether to compute edge while training. Default: False
    """
    NUM_CLASSES = 16		# 2.改这里

    def __init__(self, transforms, dataset_root=None, mode='train', edge=False):
        self.dataset_root = dataset_root
        self.transforms = Compose(transforms)
        mode = mode.lower()
        self.mode = mode
        self.file_list = list()
        self.num_classes = self.NUM_CLASSES
        self.ignore_index = 255
        self.edge = edge

        if mode not in ['train', 'trainval', 'trainaug', 'val']:
            raise ValueError(
                "`mode` should be one of ('train', 'trainval', 'trainaug', 'val') in PascalVOC dataset, but got {}."
                .format(mode))

        if self.transforms is None:
            raise ValueError("`transforms` is necessary, but it is None.")

        if self.dataset_root is None:
            self.dataset_root = download_file_and_uncompress(
                url=URL,
                savepath=seg_env.DATA_HOME,
                extrapath=seg_env.DATA_HOME,
                extraname='VOCdevkit')
        elif not os.path.exists(self.dataset_root):
            self.dataset_root = os.path.normpath(self.dataset_root)
            savepath, extraname = self.dataset_root.rsplit(
                sep=os.path.sep, maxsplit=1)
            self.dataset_root = download_file_and_uncompress(
                url=URL,
                savepath=savepath,
                extrapath=savepath,
                extraname=extraname)

        image_set_dir = os.path.join(self.dataset_root, 'VOC2012', 'ImageSets',
                                     'Segmentation')
        if mode == 'train':
            file_path = os.path.join(image_set_dir, 'train.txt')
        elif mode == 'val':
            file_path = os.path.join(image_set_dir, 'val.txt')
        elif mode == 'trainval':
            file_path = os.path.join(image_set_dir, 'trainval.txt')
        elif mode == 'trainaug':
            file_path = os.path.join(image_set_dir, 'train.txt')
            file_path_aug = os.path.join(image_set_dir, 'aug.txt')

            if not os.path.exists(file_path_aug):
                raise RuntimeError(
                    "When `mode` is 'trainaug', Pascal Voc dataset should be augmented, "
                    "Please make sure voc_augment.py has been properly run when using this mode."
                )

        img_dir = os.path.join(self.dataset_root, 'VOC2012', 'JPEGImages')
        label_dir = os.path.join(self.dataset_root, 'VOC2012',
                                 'SegmentationClass')
        label_dir_aug = os.path.join(self.dataset_root, 'VOC2012',
                                     'SegmentationClassAug')

        with open(file_path, 'r') as f:
            for line in f:
                line = line.strip()
                image_path = os.path.join(img_dir, ''.join([line, '.jpg']))
                label_path = os.path.join(label_dir, ''.join([line, '.png']))
                self.file_list.append([image_path, label_path])
        if mode == 'trainaug':
            with open(file_path_aug, 'r') as f:
                for line in f:
                    line = line.strip()
                    image_path = os.path.join(img_dir, ''.join([line, '.jpg']))
                    label_path = os.path.join(label_dir_aug,
                                              ''.join([line, '.png']))
                    self.file_list.append([image_path, label_path])

这个文件需要改两个地方

  1. 定义类名。与 huangche15_voc.yml 文件中的 “type: Huangche15VOC”保持一致。
  2. NUM_CLASSES 标签类别数量,与 segformer_b5_voc_03_10k.yml 文件中的标签数量保持一致。

更改同级目录下的 _init_ 文件

from .dataset import Dataset
from .cityscapes import Cityscapes
from .voc import PascalVOC
from .huangche15voc import Huangche15VOC	# 1. 更改这里
from .ade import ADE20K
from .optic_disc_seg import OpticDiscSeg
from .pascal_context import PascalContext
from .mini_deep_globe_road_extraction import MiniDeepGlobeRoadExtraction
from .eg1800 import EG1800
from .supervisely import SUPERVISELY
from .cocostuff import CocoStuff
from .stare import STARE
from .drive import DRIVE
from .hrf import HRF
from .chase_db1 import CHASEDB1
from .pp_humanseg14k import PPHumanSeg14K
from .pssl import PSSLDataset

导入新建的自己的数据集的类

训练

在这里插入代码片export CUDA_VISIBLE_DEVICES=0 # 设置1张可用的卡

**windows下请执行以下命令**
**set CUDA_VISIBLE_DEVICES=0**
python train.py \
       --config configs/segformer/segformer_b5_voc_03_10k.yml \
       --do_eval \
       --use_vdl \
       --save_interval 500 \
       --save_dir output
 类似资料: