(step, dataset_test, name, n_share, G, Cs,
open_class = None, open=False, entropy=False, thr=None)
| 88 | |
| 89 | |
| 90 | def test(step, dataset_test, name, n_share, G, Cs, |
| 91 | open_class = None, open=False, entropy=False, thr=None): |
| 92 | G.eval() |
| 93 | for c in Cs: |
| 94 | c.eval() |
| 95 | ## Known Score Calculation. |
| 96 | correct = 0 |
| 97 | correct_close = 0 |
| 98 | size = 0 |
| 99 | per_class_num = np.zeros((n_share + 1)) |
| 100 | per_class_correct = np.zeros((n_share + 1)).astype(np.float32) |
| 101 | class_list = [i for i in range(n_share)] |
| 102 | for batch_idx, data in enumerate(dataset_test): |
| 103 | with torch.no_grad(): |
| 104 | img_t, label_t = data[0].cuda(), data[1].cuda() |
| 105 | feat = G(img_t) |
| 106 | out_t = Cs[0](feat) |
| 107 | if batch_idx == 0: |
| 108 | open_class = int(out_t.size(1)) |
| 109 | class_list.append(open_class) |
| 110 | pred = out_t.data.max(1)[1] |
| 111 | correct_close += pred.eq(label_t.data).cpu().sum() |
| 112 | out_t = F.softmax(out_t) |
| 113 | entr = -torch.sum(out_t * torch.log(out_t), 1).data.cpu().numpy() |
| 114 | if entropy: |
| 115 | pred_unk = -torch.sum(out_t * torch.log(out_t), 1) |
| 116 | ind_unk = np.where(entr > thr)[0] |
| 117 | else: |
| 118 | out_open = Cs[1](feat) |
| 119 | out_open = F.softmax(out_open.view(out_t.size(0), 2, -1),1) |
| 120 | tmp_range = torch.range(0, out_t.size(0)-1).long().cuda() |
| 121 | pred_unk = out_open[tmp_range, 0, pred] |
| 122 | ind_unk = np.where(pred_unk.data.cpu().numpy() > 0.5)[0] |
| 123 | pred[ind_unk] = open_class |
| 124 | correct += pred.eq(label_t.data).cpu().sum() |
| 125 | pred = pred.cpu().numpy() |
| 126 | k = label_t.data.size()[0] |
| 127 | for i, t in enumerate(class_list): |
| 128 | t_ind = np.where(label_t.data.cpu().numpy() == t) |
| 129 | correct_ind = np.where(pred[t_ind[0]] == t) |
| 130 | per_class_correct[i] += float(len(correct_ind[0])) |
| 131 | per_class_num[i] += float(len(t_ind[0])) |
| 132 | size += k |
| 133 | if open: |
| 134 | label_t = label_t.data.cpu().numpy() |
| 135 | if batch_idx == 0: |
| 136 | label_all = label_t |
| 137 | pred_open = pred_unk.data.cpu().numpy() |
| 138 | pred_all = out_t.data.cpu().numpy() |
| 139 | pred_ent = entr |
| 140 | else: |
| 141 | pred_open = np.r_[pred_open, pred_unk.data.cpu().numpy()] |
| 142 | pred_ent = np.r_[pred_ent, entr] |
| 143 | pred_all = np.r_[pred_all, out_t.data.cpu().numpy()] |
| 144 | label_all = np.r_[label_all, label_t] |
| 145 | if open: |
| 146 | Y_test = label_binarize(label_all, classes=[i for i in class_list]) |
| 147 | roc = roc_auc_score(Y_test[:, -1], pred_open) |
no test coverage detected