(pred_all, conf_thr, label_all,
class_list, thr=None)
| 186 | |
| 187 | |
| 188 | def select_threshold(pred_all, conf_thr, label_all, |
| 189 | class_list, thr=None): |
| 190 | num_class = class_list[-1] |
| 191 | best_th = 0.0 |
| 192 | best_f = 0 |
| 193 | #best_known = 0 |
| 194 | if thr is not None: |
| 195 | pred_class = pred_all.argmax(axis=1) |
| 196 | ind_unk = np.where(conf_thr > thr)[0] |
| 197 | pred_class[ind_unk] = num_class |
| 198 | return accuracy_score(label_all, pred_class), \ |
| 199 | accuracy_score(label_all, pred_class), \ |
| 200 | accuracy_score(label_all, pred_class) |
| 201 | ran = np.linspace(0.0, 1.0, num=20) |
| 202 | conf_thr = conf_thr / conf_thr.max() |
| 203 | scores = [] |
| 204 | for th in ran: |
| 205 | pred_class = pred_all.argmax(axis=1) |
| 206 | ind_unk = np.where(conf_thr > th)[0] |
| 207 | pred_class[ind_unk] = num_class |
| 208 | score, known, unknown = h_score_compute(label_all, pred_class, |
| 209 | class_list) |
| 210 | scores.append(score) |
| 211 | if score > best_f: |
| 212 | best_th = th |
| 213 | best_f = score |
| 214 | best_known = known |
| 215 | best_unknown = unknown |
| 216 | mean_score = np.array(scores).mean() |
| 217 | print("best known %s best unknown %s " |
| 218 | "best h-score %s"%(best_known, best_unknown, best_f)) |
| 219 | return best_th, best_f, mean_score |
| 220 | |
| 221 | |
| 222 | def h_score_compute(label_all, pred_class, class_list): |
no test coverage detected