Evaluates standard semantic segmentation metrics (http://cocodataset.org/#stuff-eval): * Mean intersection-over-union averaged across classes (mIoU) * Frequency Weighted IoU (fwIoU) * Mean pixel accuracy averaged across classes (mACC) * Pixel Accuracy (pACC)
(self)
| 140 | # return output |
| 141 | |
| 142 | def evaluate(self): |
| 143 | """ |
| 144 | Evaluates standard semantic segmentation metrics (http://cocodataset.org/#stuff-eval): |
| 145 | |
| 146 | * Mean intersection-over-union averaged across classes (mIoU) |
| 147 | * Frequency Weighted IoU (fwIoU) |
| 148 | * Mean pixel accuracy averaged across classes (mACC) |
| 149 | * Pixel Accuracy (pACC) |
| 150 | """ |
| 151 | |
| 152 | if self._distributed: |
| 153 | link.synchronize() |
| 154 | |
| 155 | conf_matrix_list = self.all_gather(self._conf_matrix) |
| 156 | self._predictions = self.all_gather(self._predictions) |
| 157 | self._predictions = list(itertools.chain(*self._predictions)) |
| 158 | if link.get_rank() != 0: |
| 159 | return |
| 160 | |
| 161 | self._conf_matrix = np.zeros_like(self._conf_matrix) |
| 162 | for conf_matrix in conf_matrix_list: |
| 163 | self._conf_matrix += conf_matrix |
| 164 | |
| 165 | if self._output_dir: |
| 166 | os.makedirs(self._output_dir, exist_ok=True) |
| 167 | file_path = os.path.join(self._output_dir, "sem_seg_predictions.json") |
| 168 | with open(file_path, "w") as f: |
| 169 | f.write(json.dumps(self._predictions)) |
| 170 | |
| 171 | acc = np.full(self._num_classes, np.nan, dtype=np.float) |
| 172 | iou = np.full(self._num_classes, np.nan, dtype=np.float) |
| 173 | tp = self._conf_matrix.diagonal()[:-1].astype(np.float) |
| 174 | pos_gt = np.sum(self._conf_matrix[:-1, :-1], axis=0).astype(np.float) |
| 175 | class_weights = pos_gt / np.sum(pos_gt) |
| 176 | pos_pred = np.sum(self._conf_matrix[:-1, :-1], axis=1).astype(np.float) |
| 177 | acc_valid = pos_gt > 0 |
| 178 | acc[acc_valid] = tp[acc_valid] / pos_gt[acc_valid] |
| 179 | iou_valid = (pos_gt + pos_pred) > 0 |
| 180 | union = pos_gt + pos_pred - tp |
| 181 | iou[acc_valid] = tp[acc_valid] / union[acc_valid] |
| 182 | macc = np.sum(acc[acc_valid]) / np.sum(acc_valid) |
| 183 | miou = np.sum(iou[acc_valid]) / np.sum(iou_valid) |
| 184 | fiou = np.sum(iou[acc_valid] * class_weights[acc_valid]) |
| 185 | pacc = np.sum(tp) / np.sum(pos_gt) |
| 186 | |
| 187 | res = {} |
| 188 | res["mIoU"] = 100 * miou |
| 189 | res["fwIoU"] = 100 * fiou |
| 190 | for i, name in enumerate(self._class_names): |
| 191 | res["IoU-{}".format(name)] = 100 * iou[i] |
| 192 | res["mACC"] = 100 * macc |
| 193 | res["pACC"] = 100 * pacc |
| 194 | for i, name in enumerate(self._class_names): |
| 195 | res["ACC-{}".format(name)] = 100 * acc[i] |
| 196 | |
| 197 | if self._output_dir: |
| 198 | file_path = os.path.join(self._output_dir, "sem_seg_evaluation.pth") |
| 199 | with open(file_path, "wb") as f: |
nothing calls this directly
no test coverage detected