How do I derive weights for CrossEntropy Loss on my custom dataset?

I have five classes + 1 background for my semantic segmentation. My mask is like the one below:
It has a ton of background and classes are rarely seen on both image.
download (2)

I’ve seen codes using CrossEntropyLoss with designated weights but usually they are using available datasets such as CityScapes so they have the weights on them.

How do I derive CrossEntropyLoss weights for my dataset?

This should work:
(Average number of pixels per image) / (Average number of pixels of a class per image)

Haven’t worked a lot on segmentation, so take the suggestion lightly

are weights in the losses BCE and CE designated PER minibatch? or is it computed on the whole dataset and assigned before the training?

Usually, it is computed per dataset.

You can do per batch as well, but that would result in a very noisy training process, since the class priorities for the network might change per batch.