Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dice loss - incorrect #19637

Closed
td-jakubl opened this issue Apr 29, 2024 · 3 comments
Closed

Dice loss - incorrect #19637

td-jakubl opened this issue Apr 29, 2024 · 3 comments
Assignees
Labels

Comments

@td-jakubl
Copy link
Contributor

td-jakubl commented Apr 29, 2024

Tensorflow version 2.16.1 (https://www.tensorflow.org/api_docs/python/tf/keras/losses/Dice)

The calculated dice loss is the average of all values in the tensor, which is not always true e.g. for tensors with shape (batch_size, height, width, 1):

y_true = tf.constant([[[[1.0], [1.0]], [[0.0], [0.0]]], [[[1.0], [1.0]], [[0.0], [0.0]]]])
y_pred = tf.constant([[[[0.0], [1.0]], [[0.0], [1.0]]], [[[0.4], [0.0]], [[0.0], [0.9]]]])

dice = tf.keras.losses.Dice()
print(dice(y_true, y_pred))

The expected result should be tf.Tensor([0.5 0.7575755 ], shape=(2,), dtype=float32) instead of tf.Tensor(0.6164384, shape=(), dtype=float32)

Proposed solution:
Adding an axis parameter for which dimensions the loss is to be calculated, analogous to the: tf.keras.losses.BinaryCrossentropy (https://www.tensorflow.org/api_docs/python/tf/keras/losses/BinaryCrossentropy)

@fchollet
Copy link
Member

Sure, we could add axis defaulting to None. Would you be able to open a PR?

@td-jakubl
Copy link
Contributor Author

td-jakubl commented May 6, 2024

Sure, we could add axis defaulting to None. Would you be able to open a PR?

Yes, I am not sure if I created PR correctly, but here it is: #19673

td-jakubl added a commit to td-jakubl/keras that referenced this issue May 6, 2024
fchollet pushed a commit that referenced this issue May 16, 2024
* Add axis parameter to dice loss

#19637

* Add unit test

* Reformat code
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants