keras-cv icon indicating copy to clipboard operation
keras-cv copied to clipboard

Add Dice Loss

Open innat opened this issue 2 years ago • 11 comments

Close https://github.com/keras-team/keras-cv/issues/296

image

Ps. Tested on actual training, works fine so far for 2D and 3D cases; now needs some polish.

innat avatar Apr 26 '22 22:04 innat

Thanks. Will take a look. (Do u still want to keep it as a draft or make it a formal PR?)

qlzh727 avatar Apr 27 '22 22:04 qlzh727

@qlzh727 This PR can be reviewed now. Though I will add some more test cases in the dice_test.py, feel free to provide some initial comments. Thanks.

innat avatar Apr 28 '22 15:04 innat

I can see axis=[1,2] in this and some other classes in this repository. Don't know if this has been discussed somewhere else before, but I would like to suggest that we assumed all axes contained positional information, except for the batch and channels axes. This way, every single method/procedure would handle multi-dimentional cases (2D, 3D, etc) transparently.

This can be achieved by reshaping the n-d signal into a (batch, positions, channels) one, and reducing over positions:

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)

lucasdavid avatar May 25 '22 12:05 lucasdavid

Thanks for your patience @innat, wanted to merge a Loss first before this. Ill revisit this now that we have done don

LukeWood avatar Aug 04 '22 01:08 LukeWood

This is the oldest open PR. What is its status?

bhack avatar Sep 27 '22 22:09 bhack

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.

LukeWood avatar Sep 27 '22 22:09 LukeWood

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 avatar Sep 27 '22 22:09 LukeWood

@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. )

innat avatar Sep 28 '22 04:09 innat

Can I take over this one and #449?

DavidLandup0 avatar Oct 10 '22 16:10 DavidLandup0

Awesome, thanks! I'll be working on these in the upcoming days then. What's left is the documentation, test cases and implementing the suggestions from the comments, right?

DavidLandup0 avatar Oct 11 '22 18:10 DavidLandup0

Awesome, thanks! I'll be working on these in the upcoming days then. What's left is the documentation, test cases and implementing the suggestions from the comments, right?

Sort of.

innat avatar Oct 12 '22 05:10 innat

@innat Could you share the training script you mentioned if you still have it?

DavidLandup0 avatar Oct 28 '22 17:10 DavidLandup0

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.)

innat avatar Oct 28 '22 17:10 innat

Oh, I meant this:

Ps. Tested on actual training, works fine so far for 2D and 3D cases; now needs some polish.

Sorry to bug you while you're on leave - enjoy your time off! :)

DavidLandup0 avatar Oct 28 '22 17:10 DavidLandup0

Of course! I'll get an example up and running as soon as I finish the test cases for the other PR

DavidLandup0 avatar Nov 11 '22 17:11 DavidLandup0

@tanzhenyu added an example run in the new PR #968 based on your training script for DeepLabV3

DavidLandup0 avatar Nov 22 '22 00:11 DavidLandup0