Semantic Segmentation metric - making confusion matrix
I want to get meanIoU and pixel Accuracy, so I customize get metric function from Deeplabv3+ github Evaluator class: https://github.com/jfzhang95/pytorch-deeplab-xception.
Currently I am having difficulty writing the confusion matrix of the two images.
Here is my first code block:
for i, row in df.iterrows():    # dataframe have filenames of ground-truth and predicted image
    print(&quot; --- Iterrows start --- &quot;)
    print(&quot;Target Image: \n{}\n{}\n&quot;.format(row['Yt'], row['Yp']))
    # gt image : aaa.png | target(predicted image) : segmented_aaa.jpg

    # Read gt segmap imagefile
    gt_image = Image.open(row['Yt']).convert('L')
    gt_image = np.array(gt_image)
    gt_image = cv2.resize(gt_image, (513, 513))

    # Read target(predicted) segmap imagefile
    target_image = Image.open(row['Yp']).convert('L')
    target_image = np.array(target_image)
    target_image = cv2.resize(target_image, (513, 513))

    print(gt_image.shape)        # (513, 513)
    print(target_image.shape)    # (513, 513)
    print(&quot;gt_image:&quot;, &quot;\n&quot;, gt_image)
    print(&quot;target_image:&quot;, &quot;\n&quot;, target_image)
    print(&quot;gt_image type:&quot;, &quot;\n&quot;, type(gt_image))
    print(&quot;target_image type:&quot;, &quot;\n&quot;, type(target_image))
    print(&quot;gt_image unique:&quot;, &quot;\n&quot;, np.unique(gt_image))
    print(&quot;target_image unique:&quot;, &quot;\n&quot;, np.unique(target_image))
    print(gt_image.shape, target_image.shape)

    # mIoU = get_iou(target_image, gt_image, num_classes)
    # print(mIoU)

    evaluator.add_batch(gt_image, target_image)
    print(&quot; --- Iterrows end --- &quot;)

Result of first code block in console log:
(513, 513)
(513, 513)
gt_image: 
 [[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]
target_image: 
 [[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]]
gt_image type: 
 &lt;class 'numpy.ndarray'&gt;
target_image type: 
 &lt;class 'numpy.ndarray'&gt;
gt_image unique: 
 [  0   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  59  60  61  62  63  64  65  66  67  68  69  70  71  72
  73  74  75  77  78  79  80  81  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 197 198 199 200
 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
 219 220]
target_image unique: 
 [  0   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 160]

and Evaluator(second) code block is:
    def _generate_matrix(self, gt_image, pre_image):
        print(np.unique(gt_image))
        mask = (gt_image &gt;= 0) &amp; (gt_image &lt; self.num_class)
        print(&quot;\nMASK INFO&quot;)
        print(mask, mask.shape)
        print(np.unique(mask))
        print()

        print(&quot;\nGT_IMAGE INFO&quot;)
        print(gt_image[mask])
        print(np.unique(gt_image[mask]))
        print(gt_image[mask].astype('int'))
        print(set(gt_image[mask].astype('int').tolist()))
        print(sorted(set(self.num_class * gt_image[mask].astype('int'))))
        print(pre_image[mask], set(pre_image[mask].tolist()))
        print()

        label = self.num_class * gt_image[mask].astype('int') + pre_image[mask]
        # label[10] = 449
        print(&quot;\nLABEL INFO&quot;)
        print(label, label.shape, len(label.tolist()))
        print(set(label.tolist()))
        print(len(set(label.tolist())))
        print()

        count = np.bincount(label, minlength=self.num_class**2)
        print(&quot;\nCOUNT INFO&quot;)
        print(count.shape)
        print()
        
        # !!!!!!!!!! ERROR POINT !!!!!!!!!!
        confusion_matrix = count.reshape(self.num_class, self.num_class)
        return confusion_matrix

    def add_batch(self, gt_image, pre_image):
        if gt_image.shape != pre_image.shape:
            print(&quot;GT_Image's shape is different with PRE_IMAGE's shape!&quot;)
            exit(0)
        # assert gt_image.shape == pre_image.shape
        self.confusion_matrix += self._generate_matrix(gt_image, pre_image)

Result of second code block in console log:
(513, 513) (513, 513)
[  0   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  59  60  61  62  63  64  65  66  67  68  69  70  71  72
  73  74  75  77  78  79  80  81  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 197 198 199 200
 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
 219 220]

MASK INFO
[[ True  True  True ...  True  True  True]
 [ True  True  True ...  True  True  True]
 [ True  True  True ...  True  True  True]
 ...
 [ True  True  True ...  True  True  True]
 [ True  True  True ...  True  True  True]
 [ True  True  True ...  True  True  True]] (513, 513)


GT_IMAGE INFO
[0 0 0 ... 0 0 0]
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
[0, 21, 42, 63, 84, 105, 126, 147, 168, 189, 210, 231, 252, 273, 294, 315, 336, 357, 378, 399, 420]
[0 0 0 ... 0 0 0] {0, 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, 142, 145, 146}


LABEL INFO
[0 0 0 ... 0 0 0] (175389,) 175389
{0, 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, 142, 145, 146, 147, 148, 153, 157, 162, 168, 173, 189, 210, 218, 231, 240, 252, 253, 254, 273, 277, 294, 295, 315, 317, 336, 337, 357, 361, 364, 377, 378, 384, 395, 399, 407, 410, 420, 421, 424, 425, 432, 496}
183


COUNT INFO
(497,)

cannot reshape array of size 497 into shape (21,21)

In my opinion, if there is a value like 480 that is greater than 21*21(=441), it will cause an error.
21 is the number of classes in the Pascal VOC dataset.
The ground-truth and predicted image used as input is this image:
 [ground-truth image]
 [predicted image]

Important is this: Save the inference image in PNG format.
In my case, metric's code block was fine, but it failed because I saved it with jpeg format. I saved images in png format and my code worked fine.
