I am doing image segmentation using fully convolutional neural networks (link to the paper): https://people.eecs.berkeley.edu/~jonlong/long_shelhamer_fcn.pdf
This can be considered as pixel classification (in the end each pixel is getting a label)
I am using the tf.nn.sparse_softmax_cross_entropy_with_logits loss function.
loss = tf.reduce_mean((tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
labels=tf.squeeze(annotation, squeeze_dims=[3]),
name="entropy")))
Everything is going well. However, I saw that one class occurs in the vast majority of pixels (95%+), call this class 0. Lets say that we have another three classes, 1, 2 and 3.
What would be the easiest way to put weights to the classes? Essentially, I would like to have very low weight for class 0 (like 0.1) compared to the other three classes who should have normal weight 1.
I know that this function exists: https://www.tensorflow.org/api_docs/python/tf/losses/sparse_softmax_cross_entropy
It just looks to me that it does something totally different and I do not understand how the weights should have the same rank as labels. I mean, in my case, weights should be something like Tensor([0.1, 1, 1, 1]) so shape (4,) and rank 1, while labels have shape (batch_size, width, height) and so rank 3. Am I missing something?
The equivalent on PyTorch would be
torch.nn.CrossEntropyLoss(weight=None, size_average=True, ignore_index=-100)
where weight is a torch tensor [0.1, 1, 1, 1]
Thanks!