2

I am doing a multiclass image segmentation using keras and TensorFlow. My trained network is giving me good predictions but fails to separate the touching objects, that's why I want to use softmax_cross_entropy loss function which has a "weights" parameter.

How can I calculate those weights knowing that my y_true shape is (batch-size,128,128,3)?

tf.losses.softmax_cross_entropy(y_true,y_pred,weights=my_weights)
double-beep
  • 5,031
  • 17
  • 33
  • 41
Jano
  • 21
  • 1

2 Answers2

0

According to the documentation of tf.losses.softmax_cross_entropy, you can use that parameter to weight the samples from each batch differently and it should be a tensor of the shape batch_size.

weights acts as a coefficient for the loss. If a scalar is provided, then the loss is simply scaled by the given value. If weights is a tensor of shape [batch_size], then the loss weights apply to each corresponding sample.

However, this answer shows how to assign different weights for each class (maybe you have an imbalanced dataset?). I guess this is more what you might want to achieve.

mrzo
  • 2,002
  • 1
  • 14
  • 26
  • the distribution of classes in my labels are not the same so i assume my dataset is imbalanced. based on the answer in the link you posted they use `ratio` to calculate the `class_weight`, how can i find this ration when i have 800 training image labels ? – Jano Apr 18 '19 at 10:02
0

You can use sklearn to compute the class weight for imbalanced dataset. And use those calculated parameters to assign weights in tf.losses.softmax_cross_entropy. This will help to tackle your issue.

Krunal V
  • 1,255
  • 10
  • 23