Ejemplo n.º 1
0
 def test_cm(self):
     metric = ConfusionMatrix()
     cm = metric.result()
     self.assertTrue((cm == 0).all().item())
     metric.update(self.y, self.out)
     cm = metric.result()
     self.assertTrue((cm >= 0).all().item())
     metric.reset()
     cm = metric.result()
     self.assertTrue((cm == 0).all().item())
Ejemplo n.º 2
0
    def test_standalone_cm_error_handling(self):
        uut = ConfusionMatrix(num_classes=10)

        # Initial confusion matrix should be a 10x10 matrix filled with zeros
        self.assertTrue(
            torch.equal(torch.zeros((10, 10), dtype=torch.long), uut.result()))

        truth = torch.as_tensor([0, 5, 2, 1, 0])
        predicted = torch.as_tensor([2, 3, -1, 5, 0])

        with self.assertRaises(ValueError):
            uut.update(truth, predicted)

        truth = torch.as_tensor([0, 5, 2, 1, -5])
        predicted = torch.as_tensor([2, 3, 1, 5, 0])

        with self.assertRaises(ValueError):
            uut.update(truth, predicted)
Ejemplo n.º 3
0
    def test_standalone_cm_fixed_size(self):
        uut = ConfusionMatrix(num_classes=10)

        # Initial confusion matrix should be a 10x10 matrix filled with zeros
        self.assertTrue(
            torch.equal(torch.zeros((10, 10), dtype=torch.long), uut.result()))

        truth = torch.as_tensor([0, 5, 2, 1, 0])
        predicted = torch.as_tensor([2, 3, 2, 5, 0])
        uut.update(truth, predicted)

        expected = torch.zeros((10, 10), dtype=torch.long)
        expected[0][2] += 1
        expected[5][3] += 1
        expected[2][2] += 1
        expected[1][5] += 1
        expected[0][0] += 1

        self.assertTrue(torch.equal(expected, uut.result()))

        # After-reset matrix should be a 10x10 matrix filled with zeros
        uut.reset()
        self.assertTrue(
            torch.equal(torch.zeros((10, 10), dtype=torch.long), uut.result()))

        # Should throw exception when len(truth) != len(predicted)
        with self.assertRaises(ValueError):
            truth = torch.as_tensor([0, 0, 1, 0])
            predicted = torch.as_tensor([1, 1, 1])
            uut.update(truth, predicted)

        # Check that matrix didn't change after error
        self.assertTrue(
            torch.equal(torch.zeros((10, 10), dtype=torch.long), uut.result()))

        # Test logits / one-hot support
        uut.reset()

        # Test one-hot (truth)
        truth = torch.as_tensor([[0, 1], [1, 0], [1, 0], [1, 0]])
        predicted = torch.as_tensor([1, 1, 1, 1])
        uut.update(truth, predicted)

        expected = torch.zeros((10, 10), dtype=torch.long)
        expected[1][1] += 1
        expected[0][1] += 1
        expected[0][1] += 1
        expected[0][1] += 1

        self.assertTrue(torch.equal(expected, uut.result()))

        # Test logits (predictions)
        truth = torch.as_tensor([1, 1, 0, 0])
        predicted = torch.as_tensor([[0.73, 0.1], [0.22, 0.33], [0.99, 0.01],
                                     [0.12, 0.11]])
        uut.update(truth, predicted)

        expected[1][0] += 1
        expected[1][1] += 1
        expected[0][0] += 1
        expected[0][0] += 1

        self.assertTrue(torch.equal(expected, uut.result()))

        # Test one-hot (truth) + logits (predictions)
        truth = torch.as_tensor([[1, 0], [1, 0], [0, 1], [0, 1]])
        predicted = torch.as_tensor([[0.73, 0.1], [0.22, 0.33], [0.99, 0.01],
                                     [0.12, 0.11]])
        uut.update(truth, predicted)

        expected[0][0] += 1
        expected[0][1] += 1
        expected[1][0] += 1
        expected[1][0] += 1

        self.assertTrue(torch.equal(expected, uut.result()))