File size: 9,136 Bytes
36c95ba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
import torch
import kornia
from kornia.testing import assert_close
class TestMeanIoU:
def test_two_classes_perfect(self, device, dtype):
batch_size = 1
num_classes = 2
actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long)
predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long)
mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
mean_iou_real = torch.tensor([[1.0, 1.0]], device=device, dtype=torch.float32)
assert mean_iou.shape == (batch_size, num_classes)
assert_close(mean_iou, mean_iou_real)
def test_two_classes_perfect_batch2(self, device, dtype):
batch_size = 2
num_classes = 2
actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long).repeat(batch_size, 1)
predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long).repeat(batch_size, 1)
mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
mean_iou_real = torch.tensor([[1.0, 1.0], [1.0, 1.0]], device=device, dtype=torch.float32)
assert mean_iou.shape == (batch_size, num_classes)
assert_close(mean_iou, mean_iou_real)
def test_two_classes(self, device, dtype):
batch_size = 1
num_classes = 2
actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long)
predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 1]], device=device, dtype=torch.long)
mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
mean_iou_real = torch.tensor([[0.75, 0.80]], device=device, dtype=torch.float32)
assert mean_iou.shape == (batch_size, num_classes)
assert_close(mean_iou, mean_iou_real)
def test_four_classes_2d_perfect(self, device, dtype):
batch_size = 1
num_classes = 4
actual = torch.tensor(
[[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
)
predicted = torch.tensor(
[[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
)
mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
mean_iou_real = torch.tensor([[1.0, 1.0, 1.0, 1.0]], device=device, dtype=torch.float32)
assert mean_iou.shape == (batch_size, num_classes)
assert_close(mean_iou, mean_iou_real)
def test_four_classes_one_missing(self, device, dtype):
batch_size = 1
num_classes = 4
actual = torch.tensor(
[[[0, 0, 0, 0], [0, 0, 0, 0], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
)
predicted = torch.tensor(
[[[3, 3, 2, 2], [3, 3, 2, 2], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
)
mean_iou = kornia.metrics.mean_iou(predicted, actual, num_classes)
mean_iou_real = torch.tensor([[0.0, 1.0, 0.5, 0.5]], device=device, dtype=torch.float32)
assert mean_iou.shape == (batch_size, num_classes)
assert_close(mean_iou, mean_iou_real)
class TestConfusionMatrix:
def test_two_classes(self, device, dtype):
num_classes = 2
actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long)
predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 1]], device=device, dtype=torch.long)
conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
conf_mat_real = torch.tensor([[[3, 1], [0, 4]]], device=device, dtype=torch.float32)
assert_close(conf_mat, conf_mat_real)
def test_two_classes_batch2(self, device, dtype):
batch_size = 2
num_classes = 2
actual = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 0]], device=device, dtype=torch.long).repeat(batch_size, 1)
predicted = torch.tensor([[1, 1, 1, 1, 0, 0, 0, 1]], device=device, dtype=torch.long).repeat(batch_size, 1)
conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
conf_mat_real = torch.tensor([[[3, 1], [0, 4]], [[3, 1], [0, 4]]], device=device, dtype=torch.float32)
assert_close(conf_mat, conf_mat_real)
def test_three_classes(self, device, dtype):
num_classes = 3
actual = torch.tensor([[2, 2, 0, 0, 1, 0, 0, 2, 1, 1, 0, 0, 1, 2, 1, 0]], device=device, dtype=torch.long)
predicted = torch.tensor([[2, 1, 0, 0, 0, 0, 0, 1, 0, 2, 2, 1, 0, 0, 2, 2]], device=device, dtype=torch.long)
conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
conf_mat_real = torch.tensor([[[4, 1, 2], [3, 0, 2], [1, 2, 1]]], device=device, dtype=torch.float32)
assert_close(conf_mat, conf_mat_real)
def test_four_classes_one_missing(self, device, dtype):
num_classes = 4
actual = torch.tensor([[3, 3, 1, 1, 2, 1, 1, 3, 2, 2, 1, 1, 2, 3, 2, 1]], device=device, dtype=torch.long)
predicted = torch.tensor([[3, 2, 1, 1, 1, 1, 1, 2, 1, 3, 3, 2, 1, 1, 3, 3]], device=device, dtype=torch.long)
conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
conf_mat_real = torch.tensor(
[[[0, 0, 0, 0], [0, 4, 1, 2], [0, 3, 0, 2], [0, 1, 2, 1]]], device=device, dtype=torch.float32
)
assert_close(conf_mat, conf_mat_real)
def test_three_classes_normalized(self, device, dtype):
num_classes = 3
normalized = True
actual = torch.tensor([[2, 2, 0, 0, 1, 0, 0, 2, 1, 1, 0, 0, 1, 2, 1, 0]], device=device, dtype=torch.long)
predicted = torch.tensor([[2, 1, 0, 0, 0, 0, 0, 1, 0, 2, 2, 1, 0, 0, 2, 2]], device=device, dtype=torch.long)
conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes, normalized)
conf_mat_real = torch.tensor(
[[[0.5000, 0.3333, 0.4000], [0.3750, 0.0000, 0.4000], [0.1250, 0.6667, 0.2000]]],
device=device,
dtype=torch.float32,
)
assert_close(conf_mat, conf_mat_real)
def test_four_classes_2d_perfect(self, device, dtype):
num_classes = 4
actual = torch.tensor(
[[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
)
predicted = torch.tensor(
[[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
)
conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
conf_mat_real = torch.tensor(
[[[4, 0, 0, 0], [0, 4, 0, 0], [0, 0, 4, 0], [0, 0, 0, 4]]], device=device, dtype=torch.float32
)
assert_close(conf_mat, conf_mat_real)
def test_four_classes_2d_one_class_nonperfect(self, device, dtype):
num_classes = 4
actual = torch.tensor(
[[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
)
predicted = torch.tensor(
[[[0, 0, 1, 1], [0, 3, 0, 1], [2, 2, 1, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
)
conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
conf_mat_real = torch.tensor(
[[[3, 0, 0, 1], [1, 3, 0, 0], [0, 0, 4, 0], [0, 1, 0, 3]]], device=device, dtype=torch.float32
)
assert_close(conf_mat, conf_mat_real)
def test_four_classes_2d_one_class_missing(self, device, dtype):
num_classes = 4
actual = torch.tensor(
[[[0, 0, 1, 1], [0, 0, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
)
predicted = torch.tensor(
[[[3, 3, 1, 1], [3, 3, 1, 1], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
)
conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
conf_mat_real = torch.tensor(
[[[0, 0, 0, 4], [0, 4, 0, 0], [0, 0, 4, 0], [0, 0, 0, 4]]], device=device, dtype=torch.float32
)
assert_close(conf_mat, conf_mat_real)
def test_four_classes_2d_one_class_no_predicted(self, device, dtype):
num_classes = 4
actual = torch.tensor(
[[[0, 0, 0, 0], [0, 0, 0, 0], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
)
predicted = torch.tensor(
[[[3, 3, 2, 2], [3, 3, 2, 2], [2, 2, 3, 3], [2, 2, 3, 3]]], device=device, dtype=torch.long
)
conf_mat = kornia.metrics.confusion_matrix(predicted, actual, num_classes)
conf_mat_real = torch.tensor(
[[[0, 0, 4, 4], [0, 0, 0, 0], [0, 0, 4, 0], [0, 0, 0, 4]]], device=device, dtype=torch.float32
)
assert_close(conf_mat, conf_mat_real)
class TestPsnr:
def test_metric(self, device, dtype):
input = torch.ones(1, device=device, dtype=dtype)
expected = torch.tensor(20.0, device=device, dtype=dtype)
actual = kornia.metrics.psnr(input, 1.2 * input, 2.0)
assert_close(actual, expected)
|