From 941bbf7cb3829e390bc8e1eec3cd76d056b329ac Mon Sep 17 00:00:00 2001 From: Guanghua Yu <742925032@qq.com> Date: Wed, 15 Jun 2022 20:47:14 +0800 Subject: [PATCH] Improve the usability of voc metric (#6197) --- ppdet/metrics/metrics.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/ppdet/metrics/metrics.py b/ppdet/metrics/metrics.py index 4ec5e5d39..270071b49 100644 --- a/ppdet/metrics/metrics.py +++ b/ppdet/metrics/metrics.py @@ -248,11 +248,13 @@ class VOCMetric(Metric): self.detection_map.reset() def update(self, inputs, outputs): - bbox_np = outputs['bbox'].numpy() + bbox_np = outputs['bbox'].numpy() if isinstance( + outputs['bbox'], paddle.Tensor) else outputs['bbox'] bboxes = bbox_np[:, 2:] scores = bbox_np[:, 1] labels = bbox_np[:, 0] - bbox_lengths = outputs['bbox_num'].numpy() + bbox_lengths = outputs['bbox_num'].numpy() if isinstance( + outputs['bbox_num'], paddle.Tensor) else outputs['bbox_num'] if bboxes.shape == (1, 1) or bboxes is None: return @@ -261,18 +263,26 @@ class VOCMetric(Metric): difficults = inputs['difficult'] if not self.evaluate_difficult \ else None - scale_factor = inputs['scale_factor'].numpy( - ) if 'scale_factor' in inputs else np.ones( - (gt_boxes.shape[0], 2)).astype('float32') + if 'scale_factor' in inputs: + scale_factor = inputs['scale_factor'].numpy() if isinstance( + inputs['scale_factor'], + paddle.Tensor) else inputs['scale_factor'] + else: + scale_factor = np.ones((gt_boxes.shape[0], 2)).astype('float32') bbox_idx = 0 for i in range(len(gt_boxes)): - gt_box = gt_boxes[i].numpy() + gt_box = gt_boxes[i].numpy() if isinstance( + gt_boxes[i], paddle.Tensor) else gt_boxes[i] h, w = scale_factor[i] gt_box = gt_box / np.array([w, h, w, h]) - gt_label = gt_labels[i].numpy() - difficult = None if difficults is None \ - else difficults[i].numpy() + gt_label = gt_labels[i].numpy() if isinstance( + gt_labels[i], paddle.Tensor) else gt_labels[i] + if difficults is not None: + difficult = difficults[i].numpy() if isinstance( + difficults[i], paddle.Tensor) else difficults[i] + else: + difficult = None bbox_num = bbox_lengths[i] bbox = bboxes[bbox_idx:bbox_idx + bbox_num] score = scores[bbox_idx:bbox_idx + bbox_num] -- GitLab