-
Notifications
You must be signed in to change notification settings - Fork 330
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
Add Dice Loss #371
Add Dice Loss #371
Conversation
Thanks. Will take a look. (Do u still want to keep it as a draft or make it a formal PR?) |
@qlzh727 This PR can be reviewed now. Though I will add some more test cases in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, and sorry for the late reply.
keras_cv/losses/dice.py
Outdated
0.5549314 | ||
|
||
>>> # Calling with 'sample_weight'. | ||
>>> dice(y_true, y_pred, sample_weight=tf.constant([[0.5, 0.5]])).numpy() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should provide a sample_weights that is matching to the y_true/pred without boardcast, eg with shape [5] which match to the batch size.
per_sample: If `True`, the loss will be calculated for each sample in | ||
batch and then averaged. Otherwise the loss will be calculated for | ||
the whole batch. Default to `False`. | ||
epsilon: Small float added to dice score to avoid dividing by zero. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this one is probably not needed. We can just use keras.backend.epsilon() directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is the smoothing parameter and sometimes users may want to set their own. WDYT? But it can be removed of course. As a default value, I use backend.epsilon
though.
I can see This can be achieved by reshaping the n-d signal into a def some_metric(y_true, y_pred, axis=None):
if axis is None:
y_true = squeeze(y_true, dims=3) # 2d, 3d, 4d, ..., nd -> 3d
y_pred = squeeze(y_pred, dims=3) # 2d, 3d, 4d, ..., nd -> 3d
axis = 1
# calculate metric result
# ...
return tf.reduce_mean(result, axis=axis)
def squeeze(y, dims: int = 3):
shape = tf.shape(y)
if dims not in (2, 3):
raise ValueError(f'Illegal value for parameter dims=`{dims}`. Can only squeeze '
'positional signal, resulting in a tensor with rank 2 or 3.')
new_shape = [shape[0], -1]
if dims == 3: # keep channels.
new_shape += [shape[-1]]
return tf.reshape(y, new_shape) |
axis: An optional sequence of `int` specifying the axis to perform reduce | ||
ops for raw dice score. For 2D model, it should be [1,2] or [2,3] | ||
for the `channels_last` or `channels_first` format respectively. And for | ||
3D mdoel, it should be [1,2,3] or [2,3,4] for the `channels_last` or |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be "model" on line 146
Thanks for your patience @innat, wanted to merge a Loss first before this. Ill revisit this now that we have done don |
This is the oldest open PR. What is its status? |
To be honest it simply hasn’t been prioritized due to it not being a part of one of our flagship workflows (OD w/ RetinaNet, classification training in a reusable template with SOTA augmentation, and segmentation map prediction with model TBD) one thing to double check is that the loss reduction matches the construct set forth in the rest of the losses (see smoothL1, focal as examples). we can revisit this, but at the moment we are focusing effort on delivering those flagship workflows. |
That explanation said @innat, feel free to rebase and re request a review. It’s almost ready so happy to prioritize this. |
@LukeWood I may not able to work on it due to tight schedule. If it's prioritized, someone may need to take it from here. ( Same goes to Jaccard PR. ) |
Can I take over this one and #449? |
Awesome, thanks! I'll be working on these in the upcoming days then. |
Sort of. |
@innat Could you share the training script you mentioned if you still have it? |
I am not sure which one you're referring to. Could you please more precise? ( I'm in leave, so if I've anything that is related, I will share in upcoming days.) |
Oh, I meant this:
Sorry to bug you while you're on leave - enjoy your time off! :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
A general comment -- is it possible to start with a simple one, say binary dice, test it with our existing models? This would help a LOT in speed-up the review process.
Of course! I'll get an example up and running as soon as I finish the test cases for the other PR |
@tanzhenyu added an example run in the new PR #968 based on your training script for DeepLabV3 |
Close #296
Ps. Tested on actual training, works fine so far for 2D and 3D cases; now needs some polish.