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

Add dice loss #10717

Merged
merged 4 commits into from
May 22, 2018
Merged

Add dice loss #10717

merged 4 commits into from
May 22, 2018

Conversation

wanghaoshuang
Copy link
Contributor

No description provided.

Copy link
Contributor

@qingqing01 qingqing01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition, we should put this op in layers/nn.py or https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/fluid/nets.py ?

"""
**Dice loss Layer**
Dice loss for comparing the similarity of two batch of data,
usually be used for binary image segmentation i.e. labels are binary.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be -> is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.Thx.

**Dice loss Layer**
Dice loss for comparing the similarity of two batch of data,
usually be used for binary image segmentation i.e. labels are binary.
The value between 0 to 1, 1 means totally match.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value between 0 to 1

是什么值呢? 语法也不对。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该是loss的值介于[0, 1]之间

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loss值为0,才代表完全match, 公式化表达是 dice-loss = 1 - 2 * intersection_area / total_area = (total_area - intersection_area) - intersection_area = (union_area - intersection_area) / total_area

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

补充增加第三个等式的分母 dice-loss = 1 - 2 * intersection_area / total_area =( (total_area - intersection_area) - intersection_area) / total_area = (union_area - intersection_area) / total_area

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thx.

The value between 0 to 1, 1 means totally match.

Args:
input (Variable): The predictions with shape [batch_size, ... , num_classes].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The shape [batch_size, ... , num_classes] is not clear. Is the rank large than 2 (>=2D)? The first dimension is batch size, the last dimension is class number?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thx.


Args:
input (Variable): The predictions with shape [batch_size, ... , num_classes].
label (Variable): The groud truth with shape [batch_szie, ... , 1].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rank of label is the same with input?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thx.

Args:
input (Variable): The predictions with shape [batch_size, ... , num_classes].
label (Variable): The groud truth with shape [batch_szie, ... , 1].
num_classes (integer): The number of target classes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the num_classes can be got from input.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thx.

input (Variable): The predictions with shape [batch_size, ... , num_classes].
label (Variable): The groud truth with shape [batch_szie, ... , 1].
num_classes (integer): The number of target classes.
reduce_dim (list<int>): The dimensions to be reduced. It should be set to input.shape[1:].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be set to input.shape[1:].

If so, the reduce_dim can be set Inside this function? there is no need to be an argument?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thx.

Default: 0.00001

Returns:
dice_loss (Variable): The dice loss.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Give the shape for the returned value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thx.

input, dim=reduce_dim) + reduce_sum(
label, dim=reduce_dim)
dice_score = (inse * 2 + epsilon) / (dice_denominator + epsilon)
return reduce_mean(dice_score)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the returned loss should be 1 - dice_score from pytorch/pytorch#1249 .

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may not be necessary to add epsilon to the numerator

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Thx.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may not be necessary to add epsilon to the numerator

Should be necessary since prediction [0,0,0,0] and target [0,0,0,0] expect a perfect score.

@wanghaoshuang wanghaoshuang merged commit 67f5eaf into PaddlePaddle:develop May 22, 2018
Computer Vision: OCR automation moved this from In progress to Done May 22, 2018
@wanghaoshuang wanghaoshuang deleted the dice_loss branch May 20, 2022 03:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

6 participants