Sample weights behaviour on custom losses

Hello,

I was reading the keras documentation on how to create custom losses on keras and noticed this statement

Note that sample weighting is automatically supported for any such loss.

I have used sample weights before for segmentation problems like the one in the tensorflow segmentation guide. Nevertheless, I’ve always used them with losses like binary_crossentropy. Given that my experience is limited to losses that are already implemented in the tf API, I have the following questions:

  1. How are sample weights applied to custom loss functions?
  2. Binary crossentropy can be computed on every pixel of a 2D mask. Nevertheless, similarity metrics like dice score return one value per image. Taking into account the previous statement: is is possible to use 2D sample weights with loss functions that are based on similarity metrics (e.g. Dice Score)?

Thanks for the attention.

Hi @sangohe ,

Here are my thoughts in response to your queries. You can also share your thoughts if I miss something.

1. How are sample weights applied to custom loss functions?

These links can give you more information with a detailed code explanation.

  1. [Custom Loss Function in Keras with Sample Weights] (python - Custom Loss Function in Keras with Sample Weights - Stack Overflow).
  2. [Custom weighted loss function in Keras for weighing each element] (python - Custom weighted loss function in Keras for weighing each element - Stack Overflow)
  3. [Custom loss function with weights in Keras] (python - Custom loss function with weights in Keras - Stack Overflow).

On top of that, if your custom loss function is based on the dice score, you can multiply the dice score by the sample weights before computing the mean. This will give more weight to samples that are more important for the task at hand.

With loss functions based on similarity metrics like dice score, it is possible to use 2D sample weights, where each pixel in the mask is given a weight value, and the sample weights are then used in the calculation of the dice score loss. These weights can be used to up or down-weight specific regions of the mask during the loss calculation, and the sample weights can be used to adjust each pixel’s contribution to the final loss value, allowing you to account for the relative importance of different regions of the image during the optimization process.

Please let me know if this helps you.

Thanks.