fluid.metrics¶
Accuracy¶

class
paddle.fluid.metrics.
Accuracy
(name=None)[source] Calculate the mean accuracy over multiple batches. https://en.wikipedia.org/wiki/Accuracy_and_precision
 Parameters
name – the metrics name
Examples
import paddle.fluid as fluid #suppose we have batch_size = 128 batch_size=128 accuracy_manager = fluid.metrics.Accuracy() #suppose the accuracy is 0.9 for the 1st batch batch1_acc = 0.9 accuracy_manager.update(value = batch1_acc, weight = batch_size) print("expect accuracy: %.2f, get accuracy: %.2f" % (batch1_acc, accuracy_manager.eval())) #suppose the accuracy is 0.8 for the 2nd batch batch2_acc = 0.8 accuracy_manager.update(value = batch2_acc, weight = batch_size) #the joint acc for batch1 and batch2 is (batch1_acc * batch_size + batch2_acc * batch_size) / batch_size / 2 print("expect accuracy: %.2f, get accuracy: %.2f" % ((batch1_acc * batch_size + batch2_acc * batch_size) / batch_size / 2, accuracy_manager.eval())) #reset the accuracy_manager accuracy_manager.reset() #suppose the accuracy is 0.8 for the 3rd batch batch3_acc = 0.8 accuracy_manager.update(value = batch3_acc, weight = batch_size) print("expect accuracy: %.2f, get accuracy: %.2f" % (batch3_acc, accuracy_manager.eval()))

update
(value, weight) Update minibatch states.
 Parameters
value (floatnumpy.array) – accuracy of one minibatch.
weight (intfloat) – batch size.

eval
() Return the mean accuracy (float or numpy.array) for all accumulated batches.

get_config
() Get the metric and current states. The states are the members who do not has “_” prefix.
 Parameters
None –
 Returns
a dict of metric and states
 Return type
dict

reset
() reset clear the states of metrics. By default, the states are the members who do not has _ prefix, reset set them to inital states. If you violate the implicit name rule, please also custom the reset interface.
Auc¶

class
paddle.fluid.metrics.
Auc
(name, curve='ROC', num_thresholds=4095)[source] The auc metric is for binary classification. Refer to https://en.wikipedia.org/wiki/Receiver_operating_characteristic#Area_under_the_curve Please notice that the auc metric is implemented with python, which may be a little bit slow. If you concern the speed, please use the fluid.layers.auc instead.
The auc function creates four local variables, true_positives, true_negatives, false_positives and false_negatives that are used to compute the AUC. To discretize the AUC curve, a linearly spaced set of thresholds is used to compute pairs of recall and precision values. The area under the ROCcurve is therefore computed using the height of the recall values by the false positive rate, while the area under the PRcurve is the computed using the height of the precision values by the recall.
 Parameters
name – metric name
curve – Specifies the name of the curve to be computed, ‘ROC’ [default] or ‘PR’ for the PrecisionRecallcurve.
“NOTE: only implement the ROC curve type via Python now.”
Examples
import paddle.fluid as fluid import numpy as np # init the auc metric auc_metric = fluid.metrics.Auc("ROC") # suppose that batch_size is 128 batch_num = 100 batch_size = 128 for batch_id in range(batch_num): class0_preds = np.random.random(size = (batch_size, 1)) class1_preds = 1  class0_preds preds = np.concatenate((class0_preds, class1_preds), axis=1) labels = np.random.randint(2, size = (batch_size, 1)) auc_metric.update(preds = preds, labels = labels) # shall be some score closing to 0.5 as the preds are randomly assigned print("auc for iteration %d is %.2f" % (batch_id, auc_metric.eval()))

update
(preds, labels) Update the auc curve with the given predictions and labels
 Parameters
preds – an numpy array in the shape of (batch_size, 2), preds[i][j] denotes the probability
classifying the instance i into the class j. (of) –
labels – an numpy array in the shape of (batch_size, 1), labels[i] is either o or 1, representing
label of the instance i. (the) –

eval
() Return the area (a float score) under auc curve

get_config
() Get the metric and current states. The states are the members who do not has “_” prefix.
 Parameters
None –
 Returns
a dict of metric and states
 Return type
dict

reset
() reset clear the states of metrics. By default, the states are the members who do not has _ prefix, reset set them to inital states. If you violate the implicit name rule, please also custom the reset interface.
ChunkEvaluator¶

class
paddle.fluid.metrics.
ChunkEvaluator
(name=None)[source] Accumulate counter numbers output by chunk_eval from minibatches and compute the precision recall and F1score using the accumulated counter numbers. For some basics of chunking, please refer to Chunking with Support Vector Machines . ChunkEvalEvaluator computes the precision, recall, and F1score of chunk detection, and supports IOB, IOE, IOBES and IO (also known as plain) tagging schemes.
Examples
import paddle.fluid as fluid # init the chuncklevel evaluation manager metric = fluid.metrics.ChunkEvaluator() # suppose the model predict 10 chuncks, while 8 ones are correct and the ground truth has 9 chuncks. num_infer_chunks = 10 num_label_chunks = 9 num_correct_chunks = 8 metric.update(num_infer_chunks, num_label_chunks, num_correct_chunks) numpy_precision, numpy_recall, numpy_f1 = metric.eval() print("precision: %.2f, recall: %.2f, f1: %.2f" % (numpy_precision, numpy_recall, numpy_f1)) # the next batch, predicting 3 prefectly correct chuncks. num_infer_chunks = 3 num_label_chunks = 3 num_correct_chunks = 3 metric.update(num_infer_chunks, num_label_chunks, num_correct_chunks) numpy_precision, numpy_recall, numpy_f1 = metric.eval() print("precision: %.2f, recall: %.2f, f1: %.2f" % (numpy_precision, numpy_recall, numpy_f1))

update
(num_infer_chunks, num_label_chunks, num_correct_chunks) Update the states based on the layers.chunk_eval() ouputs.
 Parameters
num_infer_chunks (intnumpy.array) – The number of chunks in Inference on the given minibatch.
num_label_chunks (intnumpy.array) – The number of chunks in Label on the given minibatch.
num_correct_chunks (intfloatnumpy.array) – The number of chunks both in Inference and Label on the given minibatch.

eval
() Evalute the current metrics based the accumulated states.
 Returns
the metrics via Python.
 Return type
floatlist(float)numpy.array

get_config
() Get the metric and current states. The states are the members who do not has “_” prefix.
 Parameters
None –
 Returns
a dict of metric and states
 Return type
dict

reset
() reset clear the states of metrics. By default, the states are the members who do not has _ prefix, reset set them to inital states. If you violate the implicit name rule, please also custom the reset interface.

CompositeMetric¶

class
paddle.fluid.metrics.
CompositeMetric
(name=None)[source] Composite multiple metrics in one instance. for example, merge F1, accuracy, recall into one Metric.
Examples
import paddle.fluid as fluid import numpy as np preds = [[0.1], [0.7], [0.8], [0.9], [0.2], [0.2], [0.3], [0.5], [0.8], [0.6]] labels = [[0], [1], [1], [1], [1], [0], [0], [0], [0], [0]] preds = np.array(preds) labels = np.array(labels) comp = fluid.metrics.CompositeMetric() precision = fluid.metrics.Precision() recall = fluid.metrics.Recall() comp.add_metric(precision) comp.add_metric(recall) comp.update(preds=preds, labels=labels) numpy_precision, numpy_recall = comp.eval() print("expect precision: %.2f, got %.2f" % ( 3. / 5, numpy_precision ) ) print("expect recall: %.2f, got %.2f" % (3. / 4, numpy_recall ) )

add_metric
(metric) add one metric instance to CompositeMetric.
 Parameters
metric – a instance of MetricBase.

update
(preds, labels) Update every metrics in sequence.
 Parameters
preds (numpy.array) – the predictions of current minibatch
labels (numpy.array) – the labels of current minibatch, if the label is onehot or softlabel, should custom the corresponding update rule.

eval
() Evaluate every metrics in sequence.
 Returns
a list of metrics value in Python.
 Return type
list(floatnumpy.array)

get_config
() Get the metric and current states. The states are the members who do not has “_” prefix.
 Parameters
None –
 Returns
a dict of metric and states
 Return type
dict

reset
() reset clear the states of metrics. By default, the states are the members who do not has _ prefix, reset set them to inital states. If you violate the implicit name rule, please also custom the reset interface.

DetectionMAP¶

class
paddle.fluid.metrics.
DetectionMAP
(input, gt_label, gt_box, gt_difficult=None, class_num=None, background_label=0, overlap_threshold=0.5, evaluate_difficult=True, ap_version='integral')[source] Calculate the detection mean average precision (mAP).
The general steps are as follows:
calculate the true positive and false positive according to the input of detection and labels.
calculate mAP value, support two versions: ‘11 point’ and ‘integral’.
Please get more information from the following articles:
 Parameters
input (Variable) – The detection results, which is a LoDTensor with shape [M, 6]. The layout is [label, confidence, xmin, ymin, xmax, ymax].
gt_label (Variable) – The ground truth label index, which is a LoDTensor with shape [N, 1].
gt_box (Variable) – The ground truth bounding box (bbox), which is a LoDTensor with shape [N, 4]. The layout is [xmin, ymin, xmax, ymax].
gt_difficult (VariableNone) – Whether this ground truth is a difficult bounding bbox, which can be a LoDTensor [N, 1] or not set. If None, it means all the ground truth labels are not difficult bbox.
class_num (int) – The class number.
background_label (int) – The index of background label, the background label will be ignored. If set to 1, then all categories will be considered, 0 by default.
overlap_threshold (float) – The threshold for deciding true/false positive, 0.5 by default.
evaluate_difficult (bool) – Whether to consider difficult ground truth for evaluation, True by default. This argument does not work when gt_difficult is None.
ap_version (string) – The average precision calculation ways, it must be ‘integral’ or ‘11point’. Please check https://sanchom.wordpress.com/tag/averageprecision/ for details.  11point: the 11point interpolated average precision.  integral: the natural integral of the precisionrecall curve.
Examples
import paddle.fluid as fluid import paddle.fluid.layers as layers batch_size = 1 # can be any size image_boxs_num = 10 bounding_bboxes_num = 21 pb = layers.data(name='prior_box', shape=[image_boxs_num, 4], append_batch_size=False, dtype='float32') pbv = layers.data(name='prior_box_var', shape=[image_boxs_num, 4], append_batch_size=False, dtype='float32') loc = layers.data(name='target_box', shape=[batch_size, bounding_bboxes_num, 4], append_batch_size=False, dtype='float32') scores = layers.data(name='scores', shape=[batch_size, bounding_bboxes_num, image_boxs_num], append_batch_size=False, dtype='float32') nmsed_outs = fluid.layers.detection_output(scores=scores, loc=loc, prior_box=pb, prior_box_var=pbv) gt_box = fluid.layers.data(name="gt_box", shape=[batch_size, 4], dtype="float32") gt_label = fluid.layers.data(name="gt_label", shape=[batch_size, 1], dtype="float32") difficult = fluid.layers.data(name="difficult", shape=[batch_size, 1], dtype="float32") exe = fluid.Executor(fluid.CUDAPlace(0)) map_evaluator = fluid.metrics.DetectionMAP(nmsed_outs, gt_label, gt_box, difficult, class_num = 3) cur_map, accum_map = map_evaluator.get_map_var() # see detailed examples at https://github.com/PaddlePaddle/models/blob/43cdafbb97e52e6d93cc5bbdc6e7486f27665fc8/PaddleCV/object_detection

get_map_var
()  Returns: mAP variable of current minibatch and
accumulative mAP variable cross minibatches.

reset
(executor, reset_program=None) Reset metric states at the begin of each pass/user specified batch.
 Parameters
executor (Executor) – a executor for executing the reset_program.
reset_program (ProgramNone) – a single Program for reset process. If None, will create a Program.
EditDistance¶

class
paddle.fluid.metrics.
EditDistance
(name)[source] Edit distance is a way of quantifying how dissimilar two strings (e.g., words) are to each another by counting the minimum number of edit operations (add, remove or replace) required to transform one string into the other. Refer to https://en.wikipedia.org/wiki/Edit_distance
This EditDistance class takes two inputs by using update function: 1. distances: a (batch_size, 1) numpy.array, each element represents the edit distance between two sequences. 2. seq_num: a intfloat value, standing for the number of sequence pairs.
and returns the overall edit distance of multiple sequencepairs.
 Parameters
name – the metrics name
Examples
import paddle.fluid as fluid import numpy as np # suppose that batch_size is 128 batch_size = 128 # init the edit distance manager distance_evaluator = fluid.metrics.EditDistance("EditDistance") # generate the edit distance across 128 sequence pairs, the max distance is 10 here edit_distances_batch0 = np.random.randint(low = 0, high = 10, size = (batch_size, 1)) seq_num_batch0 = batch_size distance_evaluator.update(edit_distances_batch0, seq_num_batch0) avg_distance, wrong_instance_ratio = distance_evaluator.eval() print("the average edit distance for batch0 is %.2f and the wrong instance ratio is %.2f " % (avg_distance, wrong_instance_ratio)) edit_distances_batch1 = np.random.randint(low = 0, high = 10, size = (batch_size, 1)) seq_num_batch1 = batch_size distance_evaluator.update(edit_distances_batch1, seq_num_batch1) avg_distance, wrong_instance_ratio = distance_evaluator.eval() print("the average edit distance for batch0 and batch1 is %.2f and the wrong instance ratio is %.2f " % (avg_distance, wrong_instance_ratio)) distance_evaluator.reset() edit_distances_batch2 = np.random.randint(low = 0, high = 10, size = (batch_size, 1)) seq_num_batch2 = batch_size distance_evaluator.update(edit_distances_batch2, seq_num_batch2) avg_distance, wrong_instance_ratio = distance_evaluator.eval() print("the average edit distance for batch2 is %.2f and the wrong instance ratio is %.2f " % (avg_distance, wrong_instance_ratio))

update
(distances, seq_num) Update the overall edit distance
 Parameters
distances – a (batch_size, 1) numpy.array, each element represents the
distance between two sequences. (edit) –
seq_num – a intfloat value, standing for the number of sequence pairs.

eval
() Return two floats: avg_distance: the average distance for all sequence pairs updated using the update function. avg_instance_error: the ratio of sequence pairs whose edit distance is not zero.

get_config
() Get the metric and current states. The states are the members who do not has “_” prefix.
 Parameters
None –
 Returns
a dict of metric and states
 Return type
dict

reset
() reset clear the states of metrics. By default, the states are the members who do not has _ prefix, reset set them to inital states. If you violate the implicit name rule, please also custom the reset interface.
MetricBase¶

class
paddle.fluid.metrics.
MetricBase
(name)[source] Base Class for all Metrics. MetricBase define a group of interfaces for the model evaluation methods. Metrics accumulate metric states between consecutive minibatches, at every minibatch, use update interface to add current minibatch value to global states. Use eval to compute accumative metric value from last reset() or from scratch on. If you need to custom a new metric, please inherit from MetricBase and custom implementation.
 Parameters
name (str) – The name of metric instance. such as, “accuracy”. It needed if you want to distinct different metrics in a model.

reset
() reset clear the states of metrics. By default, the states are the members who do not has _ prefix, reset set them to inital states. If you violate the implicit name rule, please also custom the reset interface.

get_config
() Get the metric and current states. The states are the members who do not has “_” prefix.
 Parameters
None –
 Returns
a dict of metric and states
 Return type
dict

update
(preds, labels) Updates the metric states at every minibatch. One user can compute the minibatch metric via pure Python, or via a c++ operator.
 Parameters
preds (numpy.array) – the predictions of current minibatch
labels (numpy.array) – the labels of current minibatch, if the label is onehot or softlabel, should custom the corresponding update rule.

eval
() Evalute the current metrics based the accumulated states.
 Returns
the metrics via Python.
 Return type
floatlist(float)numpy.array
Precision¶

class
paddle.fluid.metrics.
Precision
(name=None)[source] Precision (also called positive predictive value) is the fraction of relevant instances among the retrieved instances. https://en.wikipedia.org/wiki/Evaluation_of_binary_classifiers
This class mangages the precision score for binary classification task.
Examples
import paddle.fluid as fluid import numpy as np metric = fluid.metrics.Precision() # generate the preds and labels preds = [[0.1], [0.7], [0.8], [0.9], [0.2], [0.2], [0.3], [0.5], [0.8], [0.6]] labels = [[0], [1], [1], [1], [1], [0], [0], [0], [0], [0]] preds = np.array(preds) labels = np.array(labels) metric.update(preds=preds, labels=labels) numpy_precision = metric.eval() print("expct precision: %.2f and got %.2f" % ( 3.0 / 5.0, numpy_precision))

update
(preds, labels) Updates the metric states at every minibatch. One user can compute the minibatch metric via pure Python, or via a c++ operator.
 Parameters
preds (numpy.array) – the predictions of current minibatch
labels (numpy.array) – the labels of current minibatch, if the label is onehot or softlabel, should custom the corresponding update rule.

eval
() Evalute the current metrics based the accumulated states.
 Returns
the metrics via Python.
 Return type
floatlist(float)numpy.array

get_config
() Get the metric and current states. The states are the members who do not has “_” prefix.
 Parameters
None –
 Returns
a dict of metric and states
 Return type
dict

reset
() reset clear the states of metrics. By default, the states are the members who do not has _ prefix, reset set them to inital states. If you violate the implicit name rule, please also custom the reset interface.

Recall¶

class
paddle.fluid.metrics.
Recall
(name=None)[source] Recall (also known as sensitivity) is the fraction of relevant instances that have been retrieved over the total amount of relevant instances
https://en.wikipedia.org/wiki/Precision_and_recall
This class mangages the recall score for binary classification task.
Examples
import paddle.fluid as fluid import numpy as np metric = fluid.metrics.Recall() # generate the preds and labels preds = [[0.1], [0.7], [0.8], [0.9], [0.2], [0.2], [0.3], [0.5], [0.8], [0.6]] labels = [[0], [1], [1], [1], [1], [0], [0], [0], [0], [0]] preds = np.array(preds) labels = np.array(labels) metric.update(preds=preds, labels=labels) numpy_precision = metric.eval() print("expct precision: %.2f and got %.2f" % ( 3.0 / 4.0, numpy_precision))

update
(preds, labels) Updates the metric states at every minibatch. One user can compute the minibatch metric via pure Python, or via a c++ operator.
 Parameters
preds (numpy.array) – the predictions of current minibatch
labels (numpy.array) – the labels of current minibatch, if the label is onehot or softlabel, should custom the corresponding update rule.

eval
() Evalute the current metrics based the accumulated states.
 Returns
the metrics via Python.
 Return type
floatlist(float)numpy.array

get_config
() Get the metric and current states. The states are the members who do not has “_” prefix.
 Parameters
None –
 Returns
a dict of metric and states
 Return type
dict

reset
() reset clear the states of metrics. By default, the states are the members who do not has _ prefix, reset set them to inital states. If you violate the implicit name rule, please also custom the reset interface.
