what.models.detection.frcnn.meter.confusion_meter

 1from . import meter
 2import numpy as np
 3
 4
 5class ConfusionMeter(meter.Meter):
 6    """Maintains a confusion matrix for a given calssification problem.
 7
 8    The ConfusionMeter constructs a confusion matrix for a multi-class
 9    classification problems. It does not support multi-label, multi-class problems:
10    for such problems, please use MultiLabelConfusionMeter.
11
12    Args:
13        k (int): number of classes in the classification problem
14        normalized (boolean): Determines whether or not the confusion matrix
15            is normalized or not
16
17    """
18
19    def __init__(self, k, normalized=False):
20        super(ConfusionMeter, self).__init__()
21        self.conf = np.ndarray((k, k), dtype=np.int32)
22        self.normalized = normalized
23        self.k = k
24        self.reset()
25
26    def reset(self):
27        self.conf.fill(0)
28
29    def add(self, predicted, target):
30        """Computes the confusion matrix of K x K size where K is no of classes
31
32        Args:
33            predicted (tensor): Can be an N x K tensor of predicted scores obtained from
34                the model for N examples and K classes or an N-tensor of
35                integer values between 0 and K-1.
36            target (tensor): Can be a N-tensor of integer values assumed to be integer
37                values between 0 and K-1 or N x K tensor, where targets are
38                assumed to be provided as one-hot vectors
39
40        """
41        predicted = predicted.cpu().numpy()
42        target = target.cpu().numpy()
43
44        assert predicted.shape[0] == target.shape[0], \
45            'number of targets and predicted outputs do not match'
46
47        if np.ndim(predicted) != 1:
48            assert predicted.shape[1] == self.k, \
49                'number of predictions does not match size of confusion matrix'
50            predicted = np.argmax(predicted, 1)
51        else:
52            assert (predicted.max() < self.k) and (predicted.min() >= 0), \
53                'predicted values are not between 1 and k'
54
55        onehot_target = np.ndim(target) != 1
56        if onehot_target:
57            assert target.shape[1] == self.k, \
58                'Onehot target does not match size of confusion matrix'
59            assert (target >= 0).all() and (target <= 1).all(), \
60                'in one-hot encoding, target values should be 0 or 1'
61            assert (target.sum(1) == 1).all(), \
62                'multi-label setting is not supported'
63            target = np.argmax(target, 1)
64        else:
65            assert (predicted.max() < self.k) and (predicted.min() >= 0), \
66                'predicted values are not between 0 and k-1'
67
68        # hack for bincounting 2 arrays together
69        x = predicted + self.k * target
70        bincount_2d = np.bincount(x.astype(np.int32),
71                                  minlength=self.k ** 2)
72        assert bincount_2d.size == self.k ** 2
73        conf = bincount_2d.reshape((self.k, self.k))
74
75        self.conf += conf
76
77    def value(self):
78        """
79        Returns:
80            Confustion matrix of K rows and K columns, where rows corresponds
81            to ground-truth targets and columns corresponds to predicted
82            targets.
83        """
84        if self.normalized:
85            conf = self.conf.astype(np.float32)
86            return conf / conf.sum(1).clip(min=1e-12)[:, None]
87        else:
88            return self.conf
class ConfusionMeter(what.models.detection.frcnn.meter.meter.Meter):
 6class ConfusionMeter(meter.Meter):
 7    """Maintains a confusion matrix for a given calssification problem.
 8
 9    The ConfusionMeter constructs a confusion matrix for a multi-class
10    classification problems. It does not support multi-label, multi-class problems:
11    for such problems, please use MultiLabelConfusionMeter.
12
13    Args:
14        k (int): number of classes in the classification problem
15        normalized (boolean): Determines whether or not the confusion matrix
16            is normalized or not
17
18    """
19
20    def __init__(self, k, normalized=False):
21        super(ConfusionMeter, self).__init__()
22        self.conf = np.ndarray((k, k), dtype=np.int32)
23        self.normalized = normalized
24        self.k = k
25        self.reset()
26
27    def reset(self):
28        self.conf.fill(0)
29
30    def add(self, predicted, target):
31        """Computes the confusion matrix of K x K size where K is no of classes
32
33        Args:
34            predicted (tensor): Can be an N x K tensor of predicted scores obtained from
35                the model for N examples and K classes or an N-tensor of
36                integer values between 0 and K-1.
37            target (tensor): Can be a N-tensor of integer values assumed to be integer
38                values between 0 and K-1 or N x K tensor, where targets are
39                assumed to be provided as one-hot vectors
40
41        """
42        predicted = predicted.cpu().numpy()
43        target = target.cpu().numpy()
44
45        assert predicted.shape[0] == target.shape[0], \
46            'number of targets and predicted outputs do not match'
47
48        if np.ndim(predicted) != 1:
49            assert predicted.shape[1] == self.k, \
50                'number of predictions does not match size of confusion matrix'
51            predicted = np.argmax(predicted, 1)
52        else:
53            assert (predicted.max() < self.k) and (predicted.min() >= 0), \
54                'predicted values are not between 1 and k'
55
56        onehot_target = np.ndim(target) != 1
57        if onehot_target:
58            assert target.shape[1] == self.k, \
59                'Onehot target does not match size of confusion matrix'
60            assert (target >= 0).all() and (target <= 1).all(), \
61                'in one-hot encoding, target values should be 0 or 1'
62            assert (target.sum(1) == 1).all(), \
63                'multi-label setting is not supported'
64            target = np.argmax(target, 1)
65        else:
66            assert (predicted.max() < self.k) and (predicted.min() >= 0), \
67                'predicted values are not between 0 and k-1'
68
69        # hack for bincounting 2 arrays together
70        x = predicted + self.k * target
71        bincount_2d = np.bincount(x.astype(np.int32),
72                                  minlength=self.k ** 2)
73        assert bincount_2d.size == self.k ** 2
74        conf = bincount_2d.reshape((self.k, self.k))
75
76        self.conf += conf
77
78    def value(self):
79        """
80        Returns:
81            Confustion matrix of K rows and K columns, where rows corresponds
82            to ground-truth targets and columns corresponds to predicted
83            targets.
84        """
85        if self.normalized:
86            conf = self.conf.astype(np.float32)
87            return conf / conf.sum(1).clip(min=1e-12)[:, None]
88        else:
89            return self.conf

Maintains a confusion matrix for a given calssification problem.

The ConfusionMeter constructs a confusion matrix for a multi-class classification problems. It does not support multi-label, multi-class problems: for such problems, please use MultiLabelConfusionMeter.

Args: k (int): number of classes in the classification problem normalized (boolean): Determines whether or not the confusion matrix is normalized or not

ConfusionMeter(k, normalized=False)
20    def __init__(self, k, normalized=False):
21        super(ConfusionMeter, self).__init__()
22        self.conf = np.ndarray((k, k), dtype=np.int32)
23        self.normalized = normalized
24        self.k = k
25        self.reset()
def reset(self):
27    def reset(self):
28        self.conf.fill(0)

Resets the meter to default settings.

def add(self, predicted, target):
30    def add(self, predicted, target):
31        """Computes the confusion matrix of K x K size where K is no of classes
32
33        Args:
34            predicted (tensor): Can be an N x K tensor of predicted scores obtained from
35                the model for N examples and K classes or an N-tensor of
36                integer values between 0 and K-1.
37            target (tensor): Can be a N-tensor of integer values assumed to be integer
38                values between 0 and K-1 or N x K tensor, where targets are
39                assumed to be provided as one-hot vectors
40
41        """
42        predicted = predicted.cpu().numpy()
43        target = target.cpu().numpy()
44
45        assert predicted.shape[0] == target.shape[0], \
46            'number of targets and predicted outputs do not match'
47
48        if np.ndim(predicted) != 1:
49            assert predicted.shape[1] == self.k, \
50                'number of predictions does not match size of confusion matrix'
51            predicted = np.argmax(predicted, 1)
52        else:
53            assert (predicted.max() < self.k) and (predicted.min() >= 0), \
54                'predicted values are not between 1 and k'
55
56        onehot_target = np.ndim(target) != 1
57        if onehot_target:
58            assert target.shape[1] == self.k, \
59                'Onehot target does not match size of confusion matrix'
60            assert (target >= 0).all() and (target <= 1).all(), \
61                'in one-hot encoding, target values should be 0 or 1'
62            assert (target.sum(1) == 1).all(), \
63                'multi-label setting is not supported'
64            target = np.argmax(target, 1)
65        else:
66            assert (predicted.max() < self.k) and (predicted.min() >= 0), \
67                'predicted values are not between 0 and k-1'
68
69        # hack for bincounting 2 arrays together
70        x = predicted + self.k * target
71        bincount_2d = np.bincount(x.astype(np.int32),
72                                  minlength=self.k ** 2)
73        assert bincount_2d.size == self.k ** 2
74        conf = bincount_2d.reshape((self.k, self.k))
75
76        self.conf += conf

Computes the confusion matrix of K x K size where K is no of classes

Args: predicted (tensor): Can be an N x K tensor of predicted scores obtained from the model for N examples and K classes or an N-tensor of integer values between 0 and K-1. target (tensor): Can be a N-tensor of integer values assumed to be integer values between 0 and K-1 or N x K tensor, where targets are assumed to be provided as one-hot vectors

def value(self):
78    def value(self):
79        """
80        Returns:
81            Confustion matrix of K rows and K columns, where rows corresponds
82            to ground-truth targets and columns corresponds to predicted
83            targets.
84        """
85        if self.normalized:
86            conf = self.conf.astype(np.float32)
87            return conf / conf.sum(1).clip(min=1e-12)[:, None]
88        else:
89            return self.conf

Returns: Confustion matrix of K rows and K columns, where rows corresponds to ground-truth targets and columns corresponds to predicted targets.