Evaluate semantic segmentation metrics.
| 67 | |
| 68 | |
| 69 | class SemSegEvaluator(DatasetEvaluator): |
| 70 | """ |
| 71 | Evaluate semantic segmentation metrics. |
| 72 | """ |
| 73 | |
| 74 | def __init__( |
| 75 | self, |
| 76 | dataset_name, |
| 77 | config, |
| 78 | distributed=True, |
| 79 | output_dir=None, |
| 80 | ): |
| 81 | """ |
| 82 | Args: |
| 83 | dataset_name (str): name of the dataset to be evaluated. |
| 84 | distributed (bool): if True, will collect results from all ranks for evaluation. |
| 85 | Otherwise, will evaluate the results in the current process. |
| 86 | output_dir (str): an output directory to dump results. |
| 87 | num_classes, ignore_label: deprecated argument |
| 88 | """ |
| 89 | self._logger = logging.getLogger(__name__) |
| 90 | |
| 91 | self._dataset_name = dataset_name |
| 92 | self._distributed = distributed |
| 93 | self._output_dir = output_dir |
| 94 | |
| 95 | self._cpu_device = torch.device("cpu") |
| 96 | |
| 97 | self._class_names = config.dataset.kwargs.cfg.label_list[1:] |
| 98 | self._num_classes = len(self._class_names) |
| 99 | assert self._num_classes == config.dataset.kwargs.cfg.num_classes, f"{self._num_classes} != {config.dataset.kwargs.cfg.num_classes}" |
| 100 | self._contiguous_id_to_dataset_id = {i: k for i, k in enumerate(self._class_names)} # Dict that maps contiguous training ids to COCO category ids |
| 101 | self._ignore_label = config.dataset.kwargs.cfg.ignore_value |
| 102 | |
| 103 | def reset(self): |
| 104 | self._conf_matrix = np.zeros((self._num_classes + 1, self._num_classes + 1), dtype=np.int64) |
| 105 | self._predictions = [] |
| 106 | |
| 107 | def process(self, inputs, outputs): |
| 108 | """ |
| 109 | Args: |
| 110 | inputs: the inputs to a model. |
| 111 | It is a list of dicts. Each dict corresponds to an image and |
| 112 | contains keys like "height", "width", "file_name". |
| 113 | outputs: the outputs of a model. It is either list of semantic segmentation predictions |
| 114 | (Tensor [H, W]) or list of dicts with key "sem_seg" that contains semantic |
| 115 | segmentation prediction in the same format. |
| 116 | """ |
| 117 | # for input, output in zip(inputs, outputs): |
| 118 | input, output = inputs, outputs[0] |
| 119 | |
| 120 | output = output["sem_seg"].argmax(dim=0).to(self._cpu_device) |
| 121 | pred = np.array(output, dtype=np.int) |
| 122 | |
| 123 | gt = input["gt"] |
| 124 | gt[gt == self._ignore_label] = self._num_classes |
| 125 | |
| 126 | self._conf_matrix += np.bincount( |
no outgoing calls