open-mmlab / mmsegmentation

OpenMMLab Semantic Segmentation Toolbox and Benchmark.
https://mmsegmentation.readthedocs.io/en/main/
Apache License 2.0
8.25k stars 2.61k forks source link

Training a single-class model #288

Closed jaychen1996-byte closed 3 years ago

jaychen1996-byte commented 3 years ago

Hello, I am new here and I have a question to ask.

I want to train a model with only one category.

What I MODIFIED

  1. I expand my own data set based on the cityscapes data set format.

    Modified configs/_base_/datasets/cityscapes.py to configs/_base_/datasets/cityscapes_custom.py
  2. I used the deeplabv3 model for training and modified configs/_base_/models/deeplabv3_r50-d8.py to configs/_base_/models/deeplabv3_r50-d8_custom_single.py , where the contents of deeplabv3_r50-d8_custom_single.py are as follows:

norm_cfg = dict(type='BN', requires_grad=True)
model = dict(
    type='EncoderDecoder',
    pretrained='open-mmlab://resnet50_v1c',
    backbone=dict(
        type='ResNetV1c',
        depth=50,
        num_stages=4,
        out_indices=(0, 1, 2, 3),
        dilations=(1, 1, 2, 4),
        strides=(1, 2, 1, 1),
        norm_cfg=norm_cfg,
        norm_eval=False,
        style='pytorch',
        contract_dilation=True),
    decode_head=dict(
        type='ASPPHead',
        in_channels=2048,
        in_index=3,
        channels=512,
        dilations=(1, 12, 24, 36),
        dropout_ratio=0.1,
        num_classes=1,
        norm_cfg=norm_cfg,
        align_corners=False,
        loss_decode=dict(
            type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0)),
    auxiliary_head=dict(
        type='FCNHead',
        in_channels=1024,
        in_index=2,
        channels=256,
        num_convs=1,
        concat_input=False,
        dropout_ratio=0.1,
        num_classes=1,
        norm_cfg=norm_cfg,
        align_corners=False,
        loss_decode=dict(type='CrossEntropyLoss', use_sigmoid=False, loss_weight=0.4)))
  1. Modify mmseg/datasets/cityscapes.py to mmseg/datasets/cityscapes_custom_single.py, the changed content is as follows:
CLASSES = ('error')
PALETTE = [[128, 64, 128]]
  1. Add my data format and palette format in mmseg/core/evaluation/class_names.py to adapt to the evaluation script.

  2. Annotate my data. The pixel value of the area that I don’t care about is 255. I only have one category, so the pixel value of my target area is 0.

What IS MY RESULT

decode.loss_seg and aux.loss_seg are always 0.

What IS MY QUESTION

1. Should I modify the corresponding loss function? And how?

2. Should I label the area I don’t care about as a pixel value of 255, and then label other backgrounds as another category, 
so that my category is 2.(I tried, the model can be trained normally, but the effect is not good .)

Looking forward to your reply. Thank you.

selecta81 commented 3 years ago

Please, how did you solve the problem??

Patrickctyyx commented 3 years ago

Since medical image segmentation is also a binary class problem, we can refer to the configurations of UNet.

key point: set num_classes=2 and use_sigmoid=False