PaddlePaddle / PaddleSeg

Easy-to-use image segmentation library with awesome pre-trained model zoo, supporting wide-range of practical tasks in Semantic Segmentation, Interactive Segmentation, Panoptic Segmentation, Image Matting, 3D Segmentation, etc.
https://arxiv.org/abs/2101.06175
Apache License 2.0
8.67k stars 1.68k forks source link

how to treat "Maritime Semantic Segmentation Training Dataset" ignore_index #3079

Closed YoungjaeDev closed 8 months ago

YoungjaeDev commented 1 year ago

问题确认 Search before asking

请提出你的问题 Please ask your question

Hello Usingthe Maritime Semantic Segmentation Training Dataset at https://www.vicos.si/resources/mastr1325/ , I trained and even infereced for Custom Dataset. The training/val was divided into 7:3

Below is the reference yml And the labeling is as follows

Obstacles and environment = 0 (value zero) Water = 1 (value one) Sky = 2 (value two) Ignore region / unknown category = 4 (value four)

But what I'm going to focus on in the question is how to set up ignore_index

  1. In the above dataset, ignore_index will be 4, how should I set it in Yml? (loss, train, val .... )

Additionally

  1. And datasets don't have number 3 in labelling, is there no problem with training if I skip this?
  2. Is it still correct to leave num_classes at 5

Thank you

batch_size: 8 
iters: 30000

train_dataset:
  type: Dataset
  dataset_root: data/MaSTr1325_512x384/
  train_path: data/MaSTr1325_512x384/train.txt
  num_classes: 5  # background is also a class
  mode: train
  transforms:
    - type: ResizeStepScaling
      min_scale_factor: 0.5
      max_scale_factor: 2.0
      scale_step_size: 0.25
    # - type: RandomRotation # max_rotation
    - type: RandomPaddingCrop
      crop_size: [512, 512]
    - type: RandomHorizontalFlip
    - type: RandomDistort
      brightness_range: 0.4
      contrast_range: 0.4
      saturation_range: 0.4
    - type: Normalize

val_dataset:
  type: Dataset
  dataset_root: data/MaSTr1325_512x384/
  val_path: data/MaSTr1325_512x384/val.txt
  num_classes: 5
  mode: val
  transforms:
    - type: Normalize

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

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

loss:
  types:
    - type: CrossEntropyLoss
  coef: [1, 1, 1] # total_loss = coef_1 * loss_1 + .... + coef_n * loss_n

model:
  type: PPLiteSeg
  backbone:
    type: STDC2
    pretrained: https://bj.bcebos.com/paddleseg/dygraph/PP_STDCNet2.tar.gz
import os
from PIL import Image

def count_labels_in_masks(mask_dir):
    # Create an empty set to store the unique pixel values
    unique_labels = set()

    # Loop over all the PNG files in the mask directory
    for filename in os.listdir(mask_dir):
        if filename.endswith('.png'):
            # Load the mask image using PIL
            mask = Image.open(os.path.join(mask_dir, filename))

            # Get the unique pixel values in the mask image and add them to the set
            for count, color in mask.getcolors():
                unique_labels.update([color])

    # Print the number of unique pixel values and their values
    print(f"There are {len(unique_labels)} unique labels in the masks:")
    for label in unique_labels:
        print(f"Label value {label} appears")

if __name__ == "__main__":
    count_labels_in_masks('labels/')

---
(print)

Label value 0 appears
Label value 1 appears
Label value 2 appears
Label value 4 appears
Sunting78 commented 1 year ago

you should add ignore_index in train_dataset, val_dataset and type: CrossEntropyLoss. However, it is generally not recommended to do so, which may cause errors during backwards. You can change the ignore_index to 255 in your mask labels. If datasets don't have number 3 in labelling, there is no problem with training, but the results may become worse. Is it still correct to leave num_classes at 5? -- Yes.

YoungjaeDev commented 1 year ago

@Sunting78 If datasets don't have number 3 in labelling, there is no problem with training, but the results may become worse. Following this question, would it be equally bad performance if number 3 changed to 255?

Sunting78 commented 1 year ago

I'm a little confused. You say that datasets don't have number 3 in labelling, so how to change it to 255?

bit-scientist commented 1 year ago

Hello, @Sunting78, I think you can help me with my issue over here. Could you assist me? Thank you.