confusion_matrix.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import argparse
  3. import os
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. from matplotlib.ticker import MultipleLocator
  7. from mmengine.config import Config, DictAction
  8. from mmengine.registry import init_default_scope
  9. from mmengine.utils import mkdir_or_exist, progressbar
  10. from PIL import Image
  11. from mmseg.registry import DATASETS
  12. init_default_scope('mmseg')
  13. def parse_args():
  14. parser = argparse.ArgumentParser(
  15. description='Generate confusion matrix from segmentation results')
  16. parser.add_argument('config', help='test config file path')
  17. parser.add_argument(
  18. 'prediction_path', help='prediction path where test folder result')
  19. parser.add_argument(
  20. 'save_dir', help='directory where confusion matrix will be saved')
  21. parser.add_argument(
  22. '--show', action='store_true', help='show confusion matrix')
  23. parser.add_argument(
  24. '--color-theme',
  25. default='winter',
  26. help='theme of the matrix color map')
  27. parser.add_argument(
  28. '--title',
  29. default='Normalized Confusion Matrix',
  30. help='title of the matrix color map')
  31. parser.add_argument(
  32. '--cfg-options',
  33. nargs='+',
  34. action=DictAction,
  35. help='override some settings in the used config, the key-value pair '
  36. 'in xxx=yyy format will be merged into config file. If the value to '
  37. 'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
  38. 'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
  39. 'Note that the quotation marks are necessary and that no white space '
  40. 'is allowed.')
  41. args = parser.parse_args()
  42. return args
  43. def calculate_confusion_matrix(dataset, results):
  44. """Calculate the confusion matrix.
  45. Args:
  46. dataset (Dataset): Test or val dataset.
  47. results (list[ndarray]): A list of segmentation results in each image.
  48. """
  49. n = len(dataset.METAINFO['classes'])
  50. confusion_matrix = np.zeros(shape=[n, n])
  51. assert len(dataset) == len(results)
  52. ignore_index = dataset.ignore_index
  53. reduce_zero_label = dataset.reduce_zero_label
  54. prog_bar = progressbar.ProgressBar(len(results))
  55. for idx, per_img_res in enumerate(results):
  56. res_segm = per_img_res
  57. gt_segm = dataset[idx]['data_samples'] \
  58. .gt_sem_seg.data.squeeze().numpy().astype(np.uint8)
  59. gt_segm, res_segm = gt_segm.flatten(), res_segm.flatten()
  60. if reduce_zero_label:
  61. gt_segm = gt_segm - 1
  62. to_ignore = gt_segm == ignore_index
  63. gt_segm, res_segm = gt_segm[~to_ignore], res_segm[~to_ignore]
  64. inds = n * gt_segm + res_segm
  65. mat = np.bincount(inds, minlength=n**2).reshape(n, n)
  66. confusion_matrix += mat
  67. prog_bar.update()
  68. return confusion_matrix
  69. def plot_confusion_matrix(confusion_matrix,
  70. labels,
  71. save_dir=None,
  72. show=True,
  73. title='Normalized Confusion Matrix',
  74. color_theme='OrRd'):
  75. """Draw confusion matrix with matplotlib.
  76. Args:
  77. confusion_matrix (ndarray): The confusion matrix.
  78. labels (list[str]): List of class names.
  79. save_dir (str|optional): If set, save the confusion matrix plot to the
  80. given path. Default: None.
  81. show (bool): Whether to show the plot. Default: True.
  82. title (str): Title of the plot. Default: `Normalized Confusion Matrix`.
  83. color_theme (str): Theme of the matrix color map. Default: `winter`.
  84. """
  85. # normalize the confusion matrix
  86. per_label_sums = confusion_matrix.sum(axis=1)[:, np.newaxis]
  87. confusion_matrix = \
  88. confusion_matrix.astype(np.float32) / per_label_sums * 100
  89. num_classes = len(labels)
  90. fig, ax = plt.subplots(
  91. figsize=(2 * num_classes, 2 * num_classes * 0.8), dpi=300)
  92. cmap = plt.get_cmap(color_theme)
  93. im = ax.imshow(confusion_matrix, cmap=cmap)
  94. colorbar = plt.colorbar(mappable=im, ax=ax)
  95. colorbar.ax.tick_params(labelsize=20) # 设置 colorbar 标签的字体大小
  96. title_font = {'weight': 'bold', 'size': 20}
  97. ax.set_title(title, fontdict=title_font)
  98. label_font = {'size': 40}
  99. plt.ylabel('Ground Truth Label', fontdict=label_font)
  100. plt.xlabel('Prediction Label', fontdict=label_font)
  101. # draw locator
  102. xmajor_locator = MultipleLocator(1)
  103. xminor_locator = MultipleLocator(0.5)
  104. ax.xaxis.set_major_locator(xmajor_locator)
  105. ax.xaxis.set_minor_locator(xminor_locator)
  106. ymajor_locator = MultipleLocator(1)
  107. yminor_locator = MultipleLocator(0.5)
  108. ax.yaxis.set_major_locator(ymajor_locator)
  109. ax.yaxis.set_minor_locator(yminor_locator)
  110. # draw grid
  111. ax.grid(True, which='minor', linestyle='-')
  112. # draw label
  113. ax.set_xticks(np.arange(num_classes))
  114. ax.set_yticks(np.arange(num_classes))
  115. ax.set_xticklabels(labels, fontsize=20)
  116. ax.set_yticklabels(labels, fontsize=20)
  117. ax.tick_params(
  118. axis='x', bottom=False, top=True, labelbottom=False, labeltop=True)
  119. plt.setp(
  120. ax.get_xticklabels(), rotation=45, ha='left', rotation_mode='anchor')
  121. # draw confusion matrix value
  122. for i in range(num_classes):
  123. for j in range(num_classes):
  124. ax.text(
  125. j,
  126. i,
  127. '{}%'.format(
  128. round(confusion_matrix[i, j], 2
  129. ) if not np.isnan(confusion_matrix[i, j]) else -1),
  130. ha='center',
  131. va='center',
  132. color='k',
  133. size=20)
  134. ax.set_ylim(len(confusion_matrix) - 0.5, -0.5) # matplotlib>3.1.1
  135. fig.tight_layout()
  136. if save_dir is not None:
  137. mkdir_or_exist(save_dir)
  138. plt.savefig(
  139. os.path.join(save_dir, 'confusion_matrix.png'), format='png')
  140. if show:
  141. plt.show()
  142. def main():
  143. args = parse_args()
  144. cfg = Config.fromfile(args.config)
  145. if args.cfg_options is not None:
  146. cfg.merge_from_dict(args.cfg_options)
  147. results = []
  148. for img in sorted(os.listdir(args.prediction_path)):
  149. img = os.path.join(args.prediction_path, img)
  150. image = Image.open(img)
  151. image = np.copy(image)
  152. results.append(image)
  153. assert isinstance(results, list)
  154. if isinstance(results[0], np.ndarray):
  155. pass
  156. else:
  157. raise TypeError('invalid type of prediction results')
  158. dataset = DATASETS.build(cfg.test_dataloader.dataset)
  159. confusion_matrix = calculate_confusion_matrix(dataset, results)
  160. plot_confusion_matrix(
  161. confusion_matrix,
  162. dataset.METAINFO['classes'],
  163. save_dir=args.save_dir,
  164. show=args.show,
  165. title=args.title,
  166. color_theme=args.color_theme)
  167. if __name__ == '__main__':
  168. main()