-
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
Unify the metrics implementation between low-level and high-level API. #26158
Conversation
Thanks for your contribution! |
✅ This PR's description meets the template requirements! |
12bab82
to
3659013
Compare
python/paddle/metric/metrics.py
Outdated
| || | ||
outputs & labels || | ||
| || tensor data | ||
{Metric.compute} || |
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.
可以调一下缩进对齐一下~
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.
Done
test=develop
3659013
to
6b9e9d7
Compare
test=develop
python/paddle/incubate/hapi/model.py
Outdated
""" | ||
Configures the model before runing. | ||
|
||
Args: | ||
optimizer (Optimizer|None): Optimizer must be set in training | ||
and should be a Optimizer instance. It can be None in eval | ||
and test mode. | ||
loss_function (Loss|callable function|None): Loss function can | ||
loss (Loss|callable function|None): Loss function can | ||
be a `fluid.dygraph.Layer` instance or any callable function |
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.
fluid.dygraph.Layer => paddle.nn.Layer ,下面用到fluid.dygraph.Layer的地方同样都需要修改一下
python/paddle/incubate/hapi/model.py
Outdated
@@ -1362,15 +1362,15 @@ def evaluate( | |||
input = hapi.Input('image', [-1, 1, 28, 28], 'float32') | |||
label = hapi.Input('label', [None, 1], 'int64') | |||
model = hapi.Model(hapi.vision.LeNet(), input, label) | |||
model.prepare(metrics=hapi.metrics.Accuracy()) | |||
model.prepare(metrics=paddle.metric.Accuracy()) | |||
|
|||
result = model.evaluate(val_dataset, batch_size=64) | |||
print(result) | |||
|
|||
# imperative mode | |||
fluid.enable_dygraph() |
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.
fluid.enable_dygraph => paddle.disable_static
python/paddle/metric/metrics.py
Outdated
|
||
Args: | ||
name (str, optional): String name of the metric instance. | ||
Default is `precision`. |
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.
这里name默认值是否应是recall
呢
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.
Done.
python/paddle/metric/metrics.py
Outdated
|
||
Args: | ||
name (str, optional): String name of the metric instance. Default | ||
is `acc`. |
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.
这里name默认值是否应是 auc
呢
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.
Done
name (str, optional): String name of the metric instance. Default | ||
is `acc`. | ||
curve (str): Specifies the mode of the curve to be computed, | ||
'ROC' or 'PR' for the Precision-Recall-curve. Default is 'ROC'. |
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.
Maybe add doc for num_thresholds
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.
Done.
python/paddle/metric/metrics.py
Outdated
.. code-block:: python | ||
def compute(pred, label): | ||
# sort prediction and slice the top-5 scores | ||
pred = fluid.layers.argsort(pred, descending=True)[1][:, :5] |
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.
argsort用paddle.下面的API,去掉fluid
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.
Done. Thx!
python/paddle/metric/metrics.py
Outdated
pred = fluid.layers.argsort(pred, descending=True)[1][:, :5] | ||
# calculate whether the predictions are correct | ||
correct = pred == label | ||
return fluid.layers.cast(correct, dtype='float32') |
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.
去掉fluid
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.
Done. Thx!
python/paddle/metric/metrics.py
Outdated
class Accuracy(Metric): | ||
""" | ||
Encapsulates accuracy metric logic. | ||
def __init__(self, topk=(1, ), name='acc', *args, **kwargs): |
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.
没用的信息吧?
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.
Removed. Thanks!
python/paddle/metric/metrics.py
Outdated
import paddle | ||
|
||
paddle.disable_static() | ||
x = paddle.to_variable(np.array([ |
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.
to_variable => to_tensor
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.
Done
python/paddle/metric/metrics.py
Outdated
[0.1, 0.4, 0.3, 0.2], | ||
[0.1, 0.2, 0.4, 0.3], | ||
[0.1, 0.2, 0.3, 0.4]])) | ||
y = paddle.to_variable(np.array([[0], [1], [2], [3]])) |
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.
同上
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.
Done
python/paddle/metric/metrics.py
Outdated
.. code-block:: python | ||
|
||
import paddle | ||
import paddle.fluid as fluid |
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.
去掉fluid
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.
Done
python/paddle/metric/metrics.py
Outdated
train_dataset = hapi.datasets.MNIST(mode='train') | ||
|
||
model = hapi.Model(hapi.vision.LeNet(classifier_activation=None)) | ||
optim = fluid.optimizer.Adam( |
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.optimizer.Adam
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.
Done
test=develop
… hapi_metrics test=develop
test=develop
test=develop
test=develop
|
||
|
||
|
||
from ..fluid.metrics import Accuracy, Auc, ChunkEvaluator, CompositeMetric, DetectionMAP, EditDistance, \ |
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/fluid/metrics.py 里的API计划怎么处理?
还有ChunkEvaluator, CompositeMetric, DetectionMAP, EditDistance
在重构后的paddle.metric下没有对应的API。
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.
@XiaoguangHu01 晓光麻烦看看__init__.py文件的改动。
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.
相关被使用统计在agroup中。 DetectionMAP、EditDistance建议先废弃,相关套件已在模型里重新实现。Precision/Recall当前没有被任何模型使用到,当前fluid下面的实现还太简单。 会上讨论,本次PR先以统一位置为主,后续版本还需要增强相关功能,包括增加ChunkEvaluator,以及改进功能。
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都添加一下deprecated
decorator吧。
from ..fluid.metrics import Accuracy, Auc, ChunkEvaluator, CompositeMetric, DetectionMAP, EditDistance, \ | ||
Precision, Recall | ||
from .metrics import * | ||
from . import metrics | ||
|
||
from ..fluid.layers.metric_op import accuracy, auc | ||
from ..fluid.layers.nn import chunk_eval, cos_sim, mean_iou |
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.metric下的class形式API,跟小写的调用op的API的关系,是否讨论过啊?
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.
class形式: 提供batch累积功能。一般用于高层API。底层API也可以使用。 参考内部wiki记录。
function(op形式): 保留了fluid下面的api,一般用于底层API。
- Accuracy: 只支持当前batch的统计,需用户做batch累积计算。底层API可以使用。
- auc: 可计算batch和累积batch的auc。 PaddleRec中使用。
python/paddle/metric/metrics.py
Outdated
model.prepare( | ||
optim, | ||
loss=nn.BCELoss(), | ||
metrics=paddle.metric.Recall()) |
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.
这里给一个可以同时计算precison和recall的例子?
通常情况下会同时算这两个指标的。
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.
@jzhang533 Precision和Recall的功能会上讨论后续的版本还需增强,见上面回复,本次先以统一为主。所以后续可以增强下。
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.
我看model.prepare的metrics是可以接受list的,这里改一下是否就可以?(先不考虑运行效率问题)
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.
@jzhang533 Done. Thanks!
"Auc.update", | ||
"Auc.accumulate", | ||
"Auc.name", | ||
"Auc.compute", |
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.
同意这次对于白名单的改动。因为在class Auc等的地方已经有了示例代码。
test=develop
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.
LTGM
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.
lgtm
will have follow up pr to deprecate fluid apis.
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.
LGTM
0fb112c
Fix sample code about Adam, since #26288 is merged. @XiaoguangHu01 @raindrops2sea @jzhang533 @saxon-zh Please help to review again. |
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.
LGTM
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.
LGTM
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.
lgtm
PR types
Function optimization
PR changes
APIs
Describe
add_metric_op
in Metric tocompute
.update()
andaccumulate()
ofAccuracy
.Accuracy
.compute
,update
,reset
..Model
API for Metricspython/paddle/tests