-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[Accuracy diff No.127] Fix accuracy diff for paddle.nn.functional.sigmoid_focal_loss API #73430
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
这看上去直接将 C 实现改为 python 实现了😶🌫️😶🌫️,这不太符合 paddle 库的贡献标准,破坏了动静图的区分,改动太大
paddleapitest 只是个粗略的测试项目,一切以 paddle 实现为准~
请同学参考参考贡献文档🫡:https://www.paddlepaddle.org.cn/documentation/docs/zh/dev_guides/index_cn.html
Uh oh!
There was an error while loading. Please reload this page.
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.
那就按 paddle 的实现修改 SigmoidFocalLossRule 中 torch 的转换规则?
(paddle 的实现确认没问题的话就可能还需要改一下 paddle 的文档里对 label 的描述, 应该必须是 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.
参考 API 文档:https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/api/paddle/nn/functional/sigmoid_focal_loss_cn.html#sigmoid-focal-loss
其中 label 为 Tensor 类型,其值可以取 [0, 1] 中的任意值,不存在 “label 取值为 0.0 或 1.0” 的说法~测试代码:
但精度问题可能是
SigmoidFocalLossRule
写错了,也可能是 paddle 内核代码有问题,后者可以具体看内核代码是如何处理的:paddle/phi/kernels/gpu/sigmoid_cross_entropy_with_logits_kernel.cu
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.
文档中的公式是
−label ∗ [alpha ∗ (1−sigmoid(logit))**gamma] * log(sigmoid(logit)) − (1−label ) ∗ [(1−alpha) ∗ sigmoid(logit)**gamma] log(1−sigmoid(logit))
我理解的现在 paddle 的实现是这样:
loss = -label * sigmoid(logit) - (1 - label) * log (1 - sigmoid(logit)) // _C_ops.sigmoid_cross_entropy_with_logits
// 这一步可以看作上面公式里除去 alpha 和 gamma 的项
pred = sigmoid(logit)
p_t = pred * label + (1 - pred) * (1 - label)
alpha_t = alpha * label + (1 - alpha) * (1 - label)
loss = alpha_t * loss = [alpha * label + (1 - alpha) * (1 - label)] * [-label * sigmoid(logit) - (1 - label) * log (1 - sigmoid(logit))]
// 从这里开始就可以看出如果 label 不是 0 或 1 那计算的结果就不是文档中公式计算的结果,后面的 gamma_t 也一样。
我的意思是这个 API 虽然可以输入 0-1 之间的label值,也可以得到一个计算结果,但这个计算结果并不是文档中公式的结果,只有当这个 API 输入 0或1 的 label 值时,结果才和文档公式的结果一致。(对应单测在修改之前也是只有 label 是 0或1 的case)
如果要把现在改的 paddle API 组合实现替换成相应的 _C_ops 也行。
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.
我明白你的意思了,代码中的表达确实与文档不一致,文档的含义是:
$\text{loss} = -\text{Labels} \cdot \alpha \cdot (1 - \sigma(\text{Logit}))^{\gamma} \log(\sigma(\text{Logit})) - (1 - \text{Labels}) \cdot (1 - \alpha) \cdot \sigma(\text{Logit})^{\gamma} \log(1 - \sigma(\text{Logit}))$
而代码调制的结果是:
$\text{loss} = \alpha_t \cdot (1 - p_t)^{\gamma} \cdot \left[ -\text{Labels} \cdot \log(\text{pred}) - (1 - \text{Labels}) \cdot \log(1 - \text{pred}) \right]$
$\text{loss} = \left[ \text{Labels} \cdot \alpha + (1 - \text{Labels}) \cdot (1 - \alpha) \right] \cdot \left( 1 - \text{Labels} \cdot \text{pred} - (1 - \text{Labels}) \cdot (1 - \text{pred}) \right)^{\gamma} \cdot \left[ -\text{Labels} \cdot \log(\text{pred}) - (1 - \text{Labels}) \cdot \log(1 - \text{pred}) \right]$
这看上去确实与设计有所误差,但目前认为这是合理的表达,参考论文原文3.2节:https://arxiv.org/pdf/1708.02002
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.
是的,原文里第三节也规定了 y \in {+/- 1},要不就修改一下 PaddleAPITest/tester/api_config/config_analyzer.py 里 get_numpy_tensor 给这个 API 的输入,限定为 0.0 或 1.0?
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.
可以的,需要限制输入,论文和 paddle api 文档里都说了是 0/1 的标签,看了一下 torchvision: sigmoid_focal_loss 的实现也是同样的假设和类似的计算过程