Semantic Segmentation metric - 制作混淆矩阵

Semantic Segmentation metric - making confusion matrix

我想得到meanIoU和pixel Accuracy,所以我自定义了Deeplabv3+的get metric函数 github Evaluator class: https://github.com/jfzhang95/pytorch-deeplab-xception.

目前我写两张图片的混淆矩阵有困难

这是我的第一个代码块:

for i, row in df.iterrows():    # dataframe have filenames of ground-truth and predicted image
    print(" --- Iterrows start --- ")
    print("Target Image: \n{}\n{}\n".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("gt_image:", "\n", gt_image)
    print("target_image:", "\n", target_image)
    print("gt_image type:", "\n", type(gt_image))
    print("target_image type:", "\n", type(target_image))
    print("gt_image unique:", "\n", np.unique(gt_image))
    print("target_image unique:", "\n", 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(" --- Iterrows end --- ")

控制台日志中第一个代码块的结果:

(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: 
 <class 'numpy.ndarray'>
target_image type: 
 <class 'numpy.ndarray'>
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]

和 Evaluator(第二个)代码块是:

    def _generate_matrix(self, gt_image, pre_image):
        print(np.unique(gt_image))
        mask = (gt_image >= 0) & (gt_image < self.num_class)
        print("\nMASK INFO")
        print(mask, mask.shape)
        print(np.unique(mask))
        print()

        print("\nGT_IMAGE INFO")
        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("\nLABEL INFO")
        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("\nCOUNT INFO")
        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("GT_Image's shape is different with PRE_IMAGE's shape!")
            exit(0)
        # assert gt_image.shape == pre_image.shape
        self.confusion_matrix += self._generate_matrix(gt_image, pre_image)

控制台日志中第二个代码块的结果:

(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)

在我看来,如果有像480这样的值大于21*21(=441),就会报错

21 是 Pascal VOC 数据集中 classes 的数量。

用作输入的真实图像和预测图像是这张图像: [地面真实图像]

[预测图像]

重要的是:以 PNG 格式保存推理图像。

在我的例子中,metric 的代码块很好,但它失败了,因为我用 jpeg 格式保存了它。我以 png 格式保存图像,我的代码运行良好。