|
| 1 | +import os |
| 2 | +import cv2 as cv |
| 3 | +import numpy as np |
| 4 | +from tqdm import tqdm |
| 5 | + |
| 6 | + |
| 7 | +class MiniSupervisely : |
| 8 | + |
| 9 | + ''' |
| 10 | + Refer to https://github.com/PaddlePaddle/PaddleSeg/blob/release/2.7/paddleseg/core/val.py |
| 11 | + for official evaluation implementation. |
| 12 | + ''' |
| 13 | + |
| 14 | + def __init__(self, root) : |
| 15 | + self.root = root |
| 16 | + self.val_path = os.path.join(root, 'val.txt') |
| 17 | + self.image_set = self.load_data(self.val_path) |
| 18 | + self.num_classes = 2 |
| 19 | + self.miou = -1 |
| 20 | + self.class_miou = -1 |
| 21 | + self.acc = -1 |
| 22 | + self.class_acc = -1 |
| 23 | + |
| 24 | + |
| 25 | + @property |
| 26 | + def name(self): |
| 27 | + return self.__class__.__name__ |
| 28 | + |
| 29 | + |
| 30 | + def load_data(self, val_path) : |
| 31 | + """ |
| 32 | + Load validation image set from val.txt file |
| 33 | + Args : |
| 34 | + val_path (str) : path to val.txt file |
| 35 | + Returns : |
| 36 | + image_set (list) : list of image path of input and expected image |
| 37 | + """ |
| 38 | + |
| 39 | + image_set = [] |
| 40 | + with open(val_path, 'r') as f : |
| 41 | + for line in f.readlines() : |
| 42 | + image_set.append(line.strip().split()) |
| 43 | + |
| 44 | + return image_set |
| 45 | + |
| 46 | + |
| 47 | + def eval(self, model) : |
| 48 | + """ |
| 49 | + Evaluate model on validation set |
| 50 | + Args : |
| 51 | + model (object) : PP_HumanSeg model object |
| 52 | + """ |
| 53 | + |
| 54 | + intersect_area_all = np.zeros([1], dtype=np.int64) |
| 55 | + pred_area_all = np.zeros([1], dtype=np.int64) |
| 56 | + label_area_all = np.zeros([1], dtype=np.int64) |
| 57 | + |
| 58 | + pbar = tqdm(self.image_set) |
| 59 | + |
| 60 | + pbar.set_description( |
| 61 | + "Evaluating {} with {} val set".format(model.name, self.name)) |
| 62 | + |
| 63 | + for input_image, expected_image in pbar : |
| 64 | + |
| 65 | + input_image = cv.imread(os.path.join(self.root, input_image)).astype('float32') |
| 66 | + |
| 67 | + expected_image = cv.imread(os.path.join(self.root, expected_image), cv.IMREAD_GRAYSCALE)[np.newaxis, :, :] |
| 68 | + |
| 69 | + output_image = model.infer(input_image) |
| 70 | + |
| 71 | + intersect_area, pred_area, label_area = self.calculate_area( |
| 72 | + output_image.astype('uint32'), |
| 73 | + expected_image.astype('uint32'), |
| 74 | + self.num_classes) |
| 75 | + |
| 76 | + intersect_area_all = intersect_area_all + intersect_area |
| 77 | + pred_area_all = pred_area_all + pred_area |
| 78 | + label_area_all = label_area_all + label_area |
| 79 | + |
| 80 | + self.class_iou, self.miou = self.mean_iou(intersect_area_all, pred_area_all, |
| 81 | + label_area_all) |
| 82 | + self.class_acc, self.acc = self.accuracy(intersect_area_all, pred_area_all) |
| 83 | + |
| 84 | + |
| 85 | + def get_results(self) : |
| 86 | + """ |
| 87 | + Get evaluation results |
| 88 | + Returns : |
| 89 | + miou (float) : mean iou |
| 90 | + class_miou (list) : iou on all classes |
| 91 | + acc (float) : mean accuracy |
| 92 | + class_acc (list) : accuracy on all classes |
| 93 | + """ |
| 94 | + return self.miou, self.class_miou, self.acc, self.class_acc |
| 95 | + |
| 96 | + |
| 97 | + def print_result(self) : |
| 98 | + """ |
| 99 | + Print evaluation results |
| 100 | + """ |
| 101 | + print("Mean IoU : ", self.miou) |
| 102 | + print("Mean Accuracy : ", self.acc) |
| 103 | + print("Class IoU : ", self.class_iou) |
| 104 | + print("Class Accuracy : ", self.class_acc) |
| 105 | + |
| 106 | + |
| 107 | + def calculate_area(self,pred, label, num_classes, ignore_index=255): |
| 108 | + """ |
| 109 | + Calculate intersect, prediction and label area |
| 110 | + Args: |
| 111 | + pred (Tensor): The prediction by model. |
| 112 | + label (Tensor): The ground truth of image. |
| 113 | + num_classes (int): The unique number of target classes. |
| 114 | + ignore_index (int): Specifies a target value that is ignored. Default: 255. |
| 115 | + Returns: |
| 116 | + Tensor: The intersection area of prediction and the ground on all class. |
| 117 | + Tensor: The prediction area on all class. |
| 118 | + Tensor: The ground truth area on all class |
| 119 | + """ |
| 120 | + |
| 121 | + |
| 122 | + if len(pred.shape) == 4: |
| 123 | + pred = np.squeeze(pred, axis=1) |
| 124 | + if len(label.shape) == 4: |
| 125 | + label = np.squeeze(label, axis=1) |
| 126 | + if not pred.shape == label.shape: |
| 127 | + raise ValueError('Shape of `pred` and `label should be equal, ' |
| 128 | + 'but there are {} and {}.'.format(pred.shape, |
| 129 | + label.shape)) |
| 130 | + |
| 131 | + mask = label != ignore_index |
| 132 | + pred_area = [] |
| 133 | + label_area = [] |
| 134 | + intersect_area = [] |
| 135 | + |
| 136 | + #iterate over all classes and calculate their respective areas |
| 137 | + for i in range(num_classes): |
| 138 | + pred_i = np.logical_and(pred == i, mask) |
| 139 | + label_i = label == i |
| 140 | + intersect_i = np.logical_and(pred_i, label_i) |
| 141 | + pred_area.append(np.sum(pred_i.astype('int32'))) |
| 142 | + label_area.append(np.sum(label_i.astype('int32'))) |
| 143 | + intersect_area.append(np.sum(intersect_i.astype('int32'))) |
| 144 | + |
| 145 | + return intersect_area, pred_area, label_area |
| 146 | + |
| 147 | + |
| 148 | + def mean_iou(self,intersect_area, pred_area, label_area): |
| 149 | + """ |
| 150 | + Calculate iou. |
| 151 | + Args: |
| 152 | + intersect_area (Tensor): The intersection area of prediction and ground truth on all classes. |
| 153 | + pred_area (Tensor): The prediction area on all classes. |
| 154 | + label_area (Tensor): The ground truth area on all classes. |
| 155 | + Returns: |
| 156 | + np.ndarray: iou on all classes. |
| 157 | + float: mean iou of all classes. |
| 158 | + """ |
| 159 | + intersect_area = np.array(intersect_area) |
| 160 | + pred_area = np.array(pred_area) |
| 161 | + label_area = np.array(label_area) |
| 162 | + |
| 163 | + union = pred_area + label_area - intersect_area |
| 164 | + |
| 165 | + class_iou = [] |
| 166 | + for i in range(len(intersect_area)): |
| 167 | + if union[i] == 0: |
| 168 | + iou = 0 |
| 169 | + else: |
| 170 | + iou = intersect_area[i] / union[i] |
| 171 | + class_iou.append(iou) |
| 172 | + |
| 173 | + miou = np.mean(class_iou) |
| 174 | + |
| 175 | + return np.array(class_iou), miou |
| 176 | + |
| 177 | + |
| 178 | + def accuracy(self,intersect_area, pred_area): |
| 179 | + """ |
| 180 | + Calculate accuracy |
| 181 | + Args: |
| 182 | + intersect_area (Tensor): The intersection area of prediction and ground truth on all classes.. |
| 183 | + pred_area (Tensor): The prediction area on all classes. |
| 184 | + Returns: |
| 185 | + np.ndarray: accuracy on all classes. |
| 186 | + float: mean accuracy. |
| 187 | + """ |
| 188 | + |
| 189 | + intersect_area = np.array(intersect_area) |
| 190 | + pred_area = np.array(pred_area) |
| 191 | + |
| 192 | + class_acc = [] |
| 193 | + for i in range(len(intersect_area)): |
| 194 | + if pred_area[i] == 0: |
| 195 | + acc = 0 |
| 196 | + else: |
| 197 | + acc = intersect_area[i] / pred_area[i] |
| 198 | + class_acc.append(acc) |
| 199 | + |
| 200 | + macc = np.sum(intersect_area) / np.sum(pred_area) |
| 201 | + |
| 202 | + return np.array(class_acc), macc |
0 commit comments