From 42894424b1036a3305c4e661e063a4e287ab2d04 Mon Sep 17 00:00:00 2001 From: qingqing01 Date: Wed, 11 Nov 2020 05:35:56 +0000 Subject: [PATCH 1/3] Support squeezed label as input in paddle.metric.Accuracy --- python/paddle/metric/metrics.py | 1 + python/paddle/tests/test_metrics.py | 25 ++++++++++++++++++++++--- python/paddle/vision/datasets/cifar.py | 4 +++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/python/paddle/metric/metrics.py b/python/paddle/metric/metrics.py index fed659562cbb0..510b99c03008d 100644 --- a/python/paddle/metric/metrics.py +++ b/python/paddle/metric/metrics.py @@ -244,6 +244,7 @@ def compute(self, pred, label, *args): Tensor: Correct mask, a tensor with shape [batch_size, topk]. """ pred = paddle.argsort(pred, descending=True)[:, :self.maxk] + label = paddle.reshape(label, (-1, 1)) correct = pred == label return paddle.cast(correct, dtype='float32') diff --git a/python/paddle/tests/test_metrics.py b/python/paddle/tests/test_metrics.py index f05cdf9c6da10..96bbd880da70d 100644 --- a/python/paddle/tests/test_metrics.py +++ b/python/paddle/tests/test_metrics.py @@ -28,6 +28,7 @@ def accuracy(pred, label, topk=(1, )): maxk = max(topk) pred = np.argsort(pred)[:, ::-1][:, :maxk] + label = label.reshape(-1, 1) correct = (pred == np.repeat(label, maxk, 1)) batch_size = label.shape[0] @@ -47,13 +48,18 @@ def convert_to_one_hot(y, C): class TestAccuracy(unittest.TestCase): - def test_acc(self): + def test_acc(self, squeeze_y=False): paddle.disable_static() x = paddle.to_tensor( np.array([[0.1, 0.2, 0.3, 0.4], [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_tensor(np.array([[0], [1], [2], [3]])) + + y = np.array([[0], [1], [2], [3]]) + if squeeze_y: + y = y.squeeze() + + y = paddle.to_tensor(y) m = paddle.metric.Accuracy(name='my_acc') @@ -61,7 +67,8 @@ def test_acc(self): self.assertEqual(m.name(), ['my_acc']) correct = m.compute(x, y) - # check results + # check shape and results + self.assertEqual(correct.shape, [4, 1]) self.assertEqual(m.update(correct), 0.75) self.assertEqual(m.accumulate(), 0.75) @@ -80,6 +87,9 @@ def test_acc(self): self.assertEqual(m.count[0], 0.0) paddle.enable_static() + def test_1d_label(self): + self.test_acc(True) + class TestAccuracyDynamic(unittest.TestCase): def setUp(self): @@ -87,12 +97,15 @@ def setUp(self): self.class_num = 5 self.sample_num = 1000 self.name = None + self.squeeze_label = False def random_pred_label(self): label = np.random.randint(0, self.class_num, (self.sample_num, 1)).astype('int64') pred = np.random.randint(0, self.class_num, (self.sample_num, 1)).astype('int32') + if self.squeeze_label: + label = label.squeeze() pred_one_hot = convert_to_one_hot(pred, self.class_num) pred_one_hot = pred_one_hot.astype('float32') @@ -123,9 +136,14 @@ def setUp(self): self.class_num = 10 self.sample_num = 1000 self.name = "accuracy" + self.squeeze_label = True class TestAccuracyStatic(TestAccuracyDynamic): + def setUp(self): + super().setUp() + self.squeeze_label = True + def test_main(self): main_prog = fluid.Program() startup_prog = fluid.Program() @@ -164,6 +182,7 @@ def setUp(self): self.class_num = 10 self.sample_num = 100 self.name = "accuracy" + self.squeeze_label = False class TestPrecision(unittest.TestCase): diff --git a/python/paddle/vision/datasets/cifar.py b/python/paddle/vision/datasets/cifar.py index c531f3d0e4e3d..8c1bedddc4cde 100644 --- a/python/paddle/vision/datasets/cifar.py +++ b/python/paddle/vision/datasets/cifar.py @@ -53,7 +53,9 @@ class Cifar10(Dataset): :attr:`data_file` is not set. Default True Returns: - Dataset: instance of cifar-10 dataset + Dataset: instance of cifar-10 dataset. If transform is None, the shape + of each data iterm is [3, 32, 32], and default dtype is float32. + The dtype of label is int64. Examples: From c83949f155e4a7ba86268da57415be2496181ff0 Mon Sep 17 00:00:00 2001 From: qingqing01 Date: Wed, 11 Nov 2020 07:49:39 +0000 Subject: [PATCH 2/3] Revert cifar doc --- python/paddle/vision/datasets/cifar.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/python/paddle/vision/datasets/cifar.py b/python/paddle/vision/datasets/cifar.py index 2cde8a125d422..599993e3ee84d 100644 --- a/python/paddle/vision/datasets/cifar.py +++ b/python/paddle/vision/datasets/cifar.py @@ -58,9 +58,7 @@ class Cifar10(Dataset): default backend is 'pil'. Default: None. Returns: - Dataset: instance of cifar-10 dataset. If transform is None, the shape - of each data iterm is [3, 32, 32], and default dtype is float32. - The dtype of label is int64. + Dataset: instance of cifar-10 dataset. Examples: From d2db0994518de5e0e26e8a1068ce48ee72c2449c Mon Sep 17 00:00:00 2001 From: qingqing01 Date: Thu, 12 Nov 2020 02:32:50 +0000 Subject: [PATCH 3/3] Revert cifar and fix UT --- python/paddle/tests/test_metrics.py | 5 ++++- python/paddle/vision/datasets/cifar.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/python/paddle/tests/test_metrics.py b/python/paddle/tests/test_metrics.py index 96bbd880da70d..b1f53168e62ce 100644 --- a/python/paddle/tests/test_metrics.py +++ b/python/paddle/tests/test_metrics.py @@ -141,7 +141,10 @@ def setUp(self): class TestAccuracyStatic(TestAccuracyDynamic): def setUp(self): - super().setUp() + self.topk = (1, ) + self.class_num = 5 + self.sample_num = 1000 + self.name = None self.squeeze_label = True def test_main(self): diff --git a/python/paddle/vision/datasets/cifar.py b/python/paddle/vision/datasets/cifar.py index 599993e3ee84d..7a766828d84d0 100644 --- a/python/paddle/vision/datasets/cifar.py +++ b/python/paddle/vision/datasets/cifar.py @@ -58,7 +58,7 @@ class Cifar10(Dataset): default backend is 'pil'. Default: None. Returns: - Dataset: instance of cifar-10 dataset. + Dataset: instance of cifar-10 dataset Examples: