-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add dice loss #10717
Conversation
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.
In addition, we should put this op in layers/nn.py or https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/fluid/nets.py ?
python/paddle/fluid/layers/nn.py
Outdated
""" | ||
**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. |
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.
be -> is
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.
Fixed.Thx.
python/paddle/fluid/layers/nn.py
Outdated
**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. |
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.
The value between 0 to 1
是什么值呢? 语法也不对。
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.
应该是loss的值介于[0, 1]之间
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.
loss值为0,才代表完全match, 公式化表达是 dice-loss = 1 - 2 * intersection_area / total_area = (total_area - intersection_area) - intersection_area = (union_area - intersection_area) / total_area
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.
补充增加第三个等式的分母 dice-loss = 1 - 2 * intersection_area / total_area =( (total_area - intersection_area) - intersection_area) / total_area = (union_area - intersection_area) / total_area
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.
Fixed. Thx.
python/paddle/fluid/layers/nn.py
Outdated
The value between 0 to 1, 1 means totally match. | ||
|
||
Args: | ||
input (Variable): The predictions with shape [batch_size, ... , num_classes]. |
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.
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?
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.
Fixed. Thx.
python/paddle/fluid/layers/nn.py
Outdated
|
||
Args: | ||
input (Variable): The predictions with shape [batch_size, ... , num_classes]. | ||
label (Variable): The groud truth with shape [batch_szie, ... , 1]. |
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.
The rank of label
is the same with input
?
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.
Fixed. Thx.
python/paddle/fluid/layers/nn.py
Outdated
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. |
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.
It seems the num_classes
can be got from input
.
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.
Fixed. Thx.
python/paddle/fluid/layers/nn.py
Outdated
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:]. |
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.
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?
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.
Fixed. Thx.
python/paddle/fluid/layers/nn.py
Outdated
Default: 0.00001 | ||
|
||
Returns: | ||
dice_loss (Variable): The dice loss. |
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.
Give the shape for the returned value.
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.
Fixed. Thx.
python/paddle/fluid/layers/nn.py
Outdated
input, dim=reduce_dim) + reduce_sum( | ||
label, dim=reduce_dim) | ||
dice_score = (inse * 2 + epsilon) / (dice_denominator + epsilon) | ||
return reduce_mean(dice_score) |
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.
It seems the returned loss should be 1 - dice_score from pytorch/pytorch#1249 .
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.
It may not be necessary to add epsilon to the numerator
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.
Fixed. Thx.
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.
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.
No description provided.