def get_nms_preds(b_clas, b_bb, idx, anchors): a_ic = actn_to_bb(b_bb[idx], anchors, grid_sizes) clas_pr, clas_ids = b_clas[idx].max(1) clas_pr = clas_pr.sigmoid() conf_scores = b_clas[idx].sigmoid().t().data out1, out2, cc = [], [], [] for cl in range(0, len(conf_scores)-1): c_mask = conf_scores[cl] > 0.1 if c_mask.sum() == 0: continue scores = conf_scores[cl][c_mask] l_mask = c_mask.unsqueeze(1).expand_as(a_ic) boxes = a_ic[l_mask].view(-1, 4) ids, count = nms(boxes.data, scores, 0.1, 50) ids = ids[:count] out1.append(scores[ids]) out2.append(boxes.data[ids]) cc.append([cl]*count) if cc == []: cc = [[0]] cc = T(np.concatenate(cc)) if out1 == []: out1 = [torch.Tensor()] out1 = torch.cat(out1) if out2 == []: out2 = [torch.Tensor()] out2 = torch.cat(out2) bbox, clas, prs, thresh = out2, cc, out1, 0. return predictions(to_np(bbox), to_np(clas), to_np(prs) if prs is not None else None, thresh)
def predictions(bbox, clas=None, prs=None, thresh=0.3): #bb = [bb_hw(o) for o in bbox.reshape(-1,4)] bb = bbox if prs is None: prs = [None]*len(bb) if clas is None: clas = [None]*len(bb) predictions = [] for i, (b, c, pr) in enumerate(zip(bb, clas, prs)): if((b[2]>0) and (pr is None or pr > thresh)): cat_str = 'pool' #'bg' if c[i]==len(id2cat) else id2cat[c[i]] score = pr*100 bb_np = to_np(b).astype('float64') predictions.append(pred2dict(bb_np, score, cat_str)) return predictions
def torch_gt(ax, ima, bbox, clas, prs=None, thresh=0.4): return show_ground_truth(ax, ima, to_np((bbox*224).long()),to_np(clas), to_np(prs) if prs is not None else None, thresh)
model.load_state_dict(torch.load(dir)) if torch.cuda.is_available(): model.cuda() model.eval() test_sub = np.zeros((len(label_df['id']), 3), dtype=np.float) batch = len(label_df['id']) // args.batch_size for i, (test_data, event_labels) in enumerate(test_loader): test_text, test_mask = to_var(test_data[0]), to_var(test_data[1]) test_text = test_text.long() test_mask = test_mask.float() test_outputs, domain_outputs = model(test_text, test_mask) if i != batch: test_sub[i * args.batch_size:(i + 1) * args.batch_size, :] = to_np(test_outputs) else: test_sub[i * args.batch_size:len(test_df['id']), :] = to_np(test_outputs) score_t = 0 for i in range(len(label_df)): score_t += np.dot(np.log(test_sub[i, :]), label_df.loc[i, TARGET].T) / len(label_df) score = 1 / (1 - score_t) print(score) test_pred = np.argmax(test_sub, axis=1) test_true = [get_label(item[TARGET]) for index, item in label_df.iterrows()] test_accuracy = metrics.accuracy_score(test_true, test_pred) test_f1 = metrics.f1_score(test_true, test_pred, average='macro')