PaddleSeg
PaddleSeg copied to clipboard
[General Issue] Adding weights to loss
Hi. I am trying to train a custom dataset - Binary segmentation.
- But since there is a huge class imbalance, I want to add higher weight for the foreground class. How should I add that in my config file? (I think I have to put it under loss)
- Also how do I change my optimizer? Where are the options for it? Like Adam etc.
All of your requirements can be changed in the config file. If you want to train a custom dataset, you need to have a custom config that can build of example config like the following: https://github.com/PaddlePaddle/PaddleSeg/blob/release%2F2.6/configs/upernet/upernet_resnet101_os8_cityscapes_512x1024_40k.yml
change weight:

change the optimizer:

Thanks for the reply.
Is the coef : [ w1, w2, w3, ... ] same as the weights? i.e. in the example you showed for two classes - weights are 1 and 0.4 ?
I actually wanted to know what are the other optimizers that can be used? Do you support only sgd?
https://github.com/PaddlePaddle/PaddleSeg/blob/develop/docs/train/train_tricks.md
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.
Hi, @shiyutang could you help me understand the weight assigning to classes for my semantic segmentation task?
I have 7 classes (1 background, 6 actual categories). Let's hypothetically call them bg and a,b,c,d,e,f. The number of instances for a,b,c 1000, 2500, 500 respectively. The rest d,e,f have around 100 instances.
My class weight calculations for 6 classes are:
{'a': 1.01332, 'b': 1.0, 'c': 1.72774, 'd': 4.08559, 'e': 3.00997, 'f': 3.00997}
Is the following cfg for loss is correct? Or the sum of elements in weights must be 1 (softmaxed)?
loss:
types:
- type: CrossEntropyLoss
weight: [1.013, 1.0, 1.727, 4.085, 3.009, 3.009]
-coef: [1]
What value to write for bg? How does coef change for my case?
Thank you for your time.
hi @bit-scientist
- the weight doesn't need to be summed to 1, your weight is fine.
- the bg should be set as 255, and the num_classes is 6, in this way, you don't have to change your weight coeff.
Thank you, @shiyutang. I don't quite understand setting the bg to 255. Where do I set that?
in your data. change the label of baskground from 0 to 255 and change other label by label = label-1
@shiyutang thank you very much. I will certainly do what you said. One thing, could you share some materials or source of what you just explained, namely setting bg to 255? Where can I find relevant info about this?
and BTW, if I do as you said, do I have to change the number of classes in my config from 7 to 6? I really want to make sure my configs are correct before starting the training as it takes long time to experiment 😮
255 is the ignore_index and you need to change the num_classes to 6. One of source doc:https://github.com/PaddlePaddle/PaddleSeg/blob/develop/configs/README.md
Thank you for the link, I can recall referring to it before. I would like to clarify one aspect of my labels. You said I should set bg to 255 and other classes' id should be lowerred by 1, I totally get that as I have done it before with yolo. However, my custom dataset and labels are not in yolo format. My dataset is in the format of optic_disc_seg. There are images in images folder, png masks in Annotations folder and train,val, labels in txt files. My labels.txt:
bg
a
b
c
d
e
f
I don't think modifying labels.txt will affect something, because they are just names. How do I then accomplish setting bg to 255 and lowering the labels by one? 😲
Thank you for assisting me along the way.
EDIT: I forgot to mention you by name @shiyutang
@bit-scientist
You can maually set the value to 1 by reading each picture and modifing what you want
@Asthestarsfalll I appreciate your help, but I am still not getting it. My masks are in the following format:
Do you mean I should read each mask image and set background color (black 0,0,0) to 255 (white in 8-bit color format) and
a,b,c,d,e,f to 0,1,2,3,4,5, respectively? If true, my masks will become 8 bit color format (gray), right?