def eval(dataloader, faster_rcnn, test_num=opt.test_num): pred_bboxes, pred_labels, pred_scores, pred_masks = list(), list(), list( ), list() gt_bboxes, gt_labels, gt_difficults, gt_masks = list(), list(), list( ), list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_, gt_mask_) in tqdm(enumerate(dataloader)): pred_bboxes_, pred_labels_, pred_scores_, pred_mask_ = faster_rcnn.predict( imgs, visualize=True) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_masks += list(gt_mask_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ pred_masks.append(pred_mask_) if ii == test_num: break result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) return result
def eval(dataloader, faster_rcnn, vis, test_num=10000): pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(enumerate(dataloader)): # plot groud truth bboxes sizes = [sizes[0][0].item(), sizes[1][0].item()] pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict( imgs, [sizes]) img = imgs.cuda().float() ori_img_ = inverse_normalize(at.tonumpy(img[0])) pred_img = visdom_bbox(ori_img_, at.tonumpy(pred_bboxes_[0]), at.tonumpy(pred_labels_[0]).reshape(-1), at.tonumpy(pred_scores_[0])) vis.img('test_pred_img', pred_img) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) return result
def eval(dataloader, faster_rcnn, test_num=10000): """ 验证 """ #空的list pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(enumerate(dataloader)): sizes = [sizes[0][0], sizes[1][0]] #预测目标框、类别、分数存入list pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict(imgs, [sizes]) #将真实的目标框、类别、difficults存入list gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) #将预测的目标框、类别、分数存入list pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break #返回dictz字典,两个key值:AP、mAP result = eval_detection_voc( pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) return result
def eval(dataloader, faster_rcnn, test_num=2000): # pred_bboxes, pred_labels, pred_scores, pred_depth, pred_y_rot = list(), list(), list(), list(), list() pred_bboxes, pred_labels, pred_scores = list(), list(), list() # gt_bboxes, gt_labels, gt_difficults, gt_depths, gt_y_rots = list(), list(), list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() # for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_, gt_depths_, gt_y_rots_) in tqdm(enumerate(dataloader)): for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(enumerate(dataloader)): sizes = [sizes[0][0].item(), sizes[1][0].item()] pred_bboxes_, pred_labels_, pred_scores_, pred_depth_, pred_y_rot_ = faster_rcnn.predict( imgs, [sizes]) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) # gt_depths += list(gt_depths_.numpy()) # gt_y_rots_ += list(gt_y_rots_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ # pred_depth += pred_depth_ # pred_y_rot += pred_y_rot_ if ii == test_num: break result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) # result = 0 # print("train.py:Func:eval:result: ", result) # Here need to repalce the evaluation into a new one return result
def eval(dataloader, faster_rcnn, test_num=10000): # 预测框的位置,预测框的类别和分数 pred_bboxes, pred_labels, pred_scores = list(), list(), list() # 真实框的位置,类别,是否为明显目标 gt_bboxes, gt_labels, gt_difficults = list(), list(), list() # 一个for循环,从 enumerate(dataloader)里面依次读取数据, # 读取的内容是: imgs图片,sizes尺寸,gt_boxes真实框的位置 # gt_labels真实框的类别以及gt_difficults for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(enumerate(dataloader)): sizes = [sizes[0][0].item(), sizes[1][0].item()] # 用faster_rcnn.predict(imgs,[sizes]) 得出预测的pred_boxes_, # pred_labels_,pred_scores_预测框位置,预测框标记以及预测框 # 的分数等等 pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict(imgs, [sizes]) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break # 将pred_bbox,pred_label,pred_score ,gt_bbox,gt_label,gt_difficult # 预测和真实的值全部依次添加到开始定义好的列表里面去,如果迭代次数等于测 # 试test_num,那么就跳出循环!调用 eval_detection_voc函数,接收上述的 # 六个列表参数,完成预测水平的评估!得到预测的结果 result = eval_detection_voc( pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) return result
def eval(dataloader, faster_rcnn, test_num=10000, flagadvtrain=False, adversary=None): pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(enumerate(dataloader)): sizes = [sizes[0][0].item(), sizes[1][0].item()] # if flagadvtrain: # scale = imgs[0].shape[2] / sizes[1] # imgs, gt_bboxes_, gt_labels_ = imgs.cuda().float(), gt_bboxes_.cuda(), gt_labels_.cuda() # imgs = adversary(imgs, gt_bboxes_, gt_labels_, scale) # gt_bboxes_, gt_labels_ = gt_bboxes_.cpu(), gt_labels_.cpu() pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict(imgs, [sizes]) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break result = eval_detection_voc( pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) return result
def eval(dataloader, faster_rcnn, test_num=100): pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_, id_) in enumerate(dataloader): if len(gt_bboxes_) == 0: continue sizes = [sizes[0][0].item(), sizes[1][0].item()] pred_bboxes_, pred_labels_, pred_scores_, _feature = faster_rcnn.predict( imgs, [sizes]) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break # print len(pred_bboxes) # 这个评价函数是返回ap 和map值 其中传入的pred_bboxes格式为3维的数组的list格式, # 也就是说每个list都是一个3维数组(有batch的考量) # 其他的同理 result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) return result
def rfcn_md_eval(dataloader, r_fcn: RFCN, test_num=10000): """ eval model on the special dataset :param dataloader: test dataset :param r_fcn: model :param test_num: :return: """ pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() ii = -1 for (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(dataloader): ii += 1 sizes = [sizes[0][0].item(), sizes[1][0].item()] imgs = imgs.cuda() pred_bboxes_, pred_labels_, pred_scores_ = r_fcn.predict(imgs, [sizes]) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) return result
def eval(dataloader, faster_rcnn, test_num=10000): pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() tk = tqdm(dataloader, total=int(len(dataloader))) for (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tk: sizes = [sizes[0][0].item(), sizes[1][0].item()] pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict( imgs, [sizes]) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True, iou_thresh=0.2) tk.set_postfix(score=result['map']) return result
def eval(dataloader, faster_rcnn, test_num=10000): pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(enumerate(dataloader)): '''print("shape: ", imgs.shape)''' sizes = [sizes[0][0].item(), sizes[1][0].item()] pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict(imgs, [sizes]) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break result = eval_detection_voc( pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) i=0 for ap in result['ap']: print(('AP for {} = {:.4f}'.format(VOC_BBOX_LABEL_NAMES[i], ap))) i+=1 return result
def eval(dataloader, faster_rcnn, test_num=10000): pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() gt_image_ids = list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_, image_id) in tqdm(enumerate(dataloader)): sizes = [sizes[0][0].item(), sizes[1][0].item()] pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict( imgs, [sizes]) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ gt_image_ids.append(image_id) if ii == test_num: break #if ii == 10: break save_result_json(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_image_ids) result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) return result
def eval(dataloader, faster_rcnn, test_num=10000, prob_thre=0.7): pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(enumerate(dataloader)): # imgs here are reshaped images # sizes here are the original shape of images sizes = [sizes[0][0], sizes[1][0]] pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict( imgs, [sizes], prob_thre=prob_thre) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) return result
def eval_model(dataloader, faster_rcnn, test_num=10000): pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(enumerate(dataloader)): sizes = [sizes[0][0].item(), sizes[1][0].item()] #print('Eval start time - {}'.format(datetime.datetime.now().time())) pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict( imgs, [sizes]) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) return result
def eval(dataloader, faster_rcnn, category=None, file=None, test_num=10000): pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(enumerate(dataloader)): sizes = [sizes[0][0].item(), sizes[1][0].item()] pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict(imgs, [sizes]) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break result = eval_detection_voc( pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) i = 0 if file is not None: file.write(str(category) + "," + str(result['map']) + ',') for ap in result['ap']: print(('AP for {} = {:.4f}'.format(VOC_BBOX_LABEL_NAMES[i], ap))) if file is not None: file.write(str(ap) + ",") i += 1 if file is not None: file.write("\n") return result
def eval(self, dataloader, faster_rcnn, test_num=10000): pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() total_losses = list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, scale, gt_difficults_) \ in tqdm.tqdm(enumerate(dataloader)): img = imgs.cuda().float() bbox = gt_bboxes_.cuda() label = gt_labels_.cuda() sizes = [sizes[0][0].item(), sizes[1][0].item()] pred_bboxes_, pred_labels_, pred_scores_ = \ faster_rcnn.predict(imgs, [sizes]) losses = self.trainer.forward(img, bbox, label, float(scale)) total_losses.append(losses.total_loss.item()) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break result = eval_detection_voc( pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) total_loss = sum(total_losses) / len(total_losses) return total_loss, result
def eval(dataloader, faster_rcnn, test_num=10000): pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(enumerate(dataloader)): sizes = [sizes[0][0], sizes[1][0]] pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict( imgs, [sizes]) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break result = eval_detection_voc( pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True, ) return result
def every_map(pred_bboxes, pred_labels, pred_scores, gt_bboxes1, gt_labels1, gt_difficults1=None, iou_thresh=0.5): map_result = np.zeros((len(pred_labels))) pred_bboxes = iter(pred_bboxes) pred_labels = iter(pred_labels) pred_scores = iter(pred_scores) gt_bboxes1 = iter(gt_bboxes1) gt_labels1 = iter(gt_labels1) if gt_difficults1 is None: gt_difficults1 = itertools.repeat(None) else: gt_difficults1 = iter(gt_difficults1) n_pos = defaultdict(int) score = defaultdict(list) match = defaultdict(list) # print map_result.shape i = 0 for pred_bbox, pred_label, pred_score, gt_bbox, gt_label, gt_difficult in \ six.moves.zip( pred_bboxes, pred_labels, pred_scores, gt_bboxes1, gt_labels1, gt_difficults1): pred_bboxes_, pred_labels_, pred_scores_ = list(), list(), list() gt_bboxes_, gt_labels_, gt_difficults_ = list(), list(), list() bbox1 = np.expand_dims(gt_bbox, axis=0) label1 = np.expand_dims(pred_label, axis=0) labels1 = np.expand_dims(gt_label, axis=0) bounding1 = np.expand_dims(pred_bbox, axis=0) confidence1 = np.expand_dims(pred_score, axis=0) difficults1 = np.expand_dims(gt_difficult, axis=0) gt_bboxes_ += list(bbox1) gt_labels_ += list(labels1) gt_difficults_ += list(difficults1) pred_bboxes_ += list(bounding1) pred_labels_ += list(label1) pred_scores_ += list(confidence1) # print pred_bboxes_[0].shape result = eval_detection_voc(pred_bboxes_, pred_labels_, pred_scores_, gt_bboxes_, gt_labels_, gt_difficults_, use_07_metric=True) map_result[i] = result['map'] i += 1 return map_result
def eval(dataloader, faster_rcnn, test_num, thresh=0.01): pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in enumerate(dataloader): sizes = [sizes[0][0].item(), sizes[1][0].item()] pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict( imgs, [sizes]) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break #print("gt_bboxes: ", len(gt_bboxes), gt_bboxes) #print("gt_labels: ", len(gt_labels), gt_labels) #print("gt_difficults: ", len(gt_difficults), gt_difficults) #print("pred_bboxes: ", len(pred_bboxes), pred_bboxes) #print("pred_labels: ", len(pred_labels), pred_labels) #print("pred_scores: ", len(pred_scores), pred_scores) #eval = Metric(visualize=False, visualization_root=None) #filtered_score = [] #filtered_pred = [] #filtered_target = [] #for i in range(len(pred_bboxes)): # if pred_scores[i] > thresh: # filtered_score.append(pred_scores[i]) # filtered_pred.append(pred_bboxes[i]) # filtered_target.append(gt_bboxes[i]) #pred_list = pred_bboxes[i] #target_list = gt_bboxes[i] #pred_list = [p for p in pred_list if p[4] >= thresh] #filtered_p = [p[:4] for p in pred_list] #filtered_target = [p for p in target_list] # image = None # eval.eval_add_result(filtered_target, filtered_pred, image=image, image_name=i) #precision, recall = evl.get_result() #F1 = 2 * (precision * recall) / max((precision + recall), 1e-5) #F2 = 5 * (precision * recall) / max((4 * precision + recall), 1e-5) #print("detect time: ", time.time() - st) #print("Prec: ", precision, "Rec: ", recall, "F1: ", F1, "F2: ", F2) result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) #print("detect time: ", time.time() - st) print("precision and recall: ", result) return result
def eval(seq_loader, faster_rcnn, signal_type, scale=1., test_num=10000, stop=False): carrada = download('Carrada') pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() # print('*** Evaluation ***') for n_seq, sequence_data in tqdm(enumerate(seq_loader)): seq_name, seq = sequence_data # Overfit an image # seq = [seq[155]] # large seq = [seq[115]] # medium # seq = [seq[28]] # small path_to_frames = os.path.join(carrada, seq_name[0]) frame_set = TestCarradaDataset(opt, seq, 'box', signal_type, path_to_frames) frame_loader = data_.DataLoader(frame_set, batch_size=1, shuffle=False, num_workers=opt.num_workers) for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(enumerate(frame_loader)): sizes = [sizes[0][0].item(), sizes[1][0].item()] imgs = normalize(imgs) pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict( imgs, signal_type, [sizes]) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ # if ii == test_num: break result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, iou_thresh=0.5, use_07_metric=True) ious = bbox_iou(gt_bboxes[0], pred_bboxes[0]) try: best_iou = ious.max() except ValueError: best_iou = 0 if stop: import ipdb ipdb.set_trace() # print('Best IoU in validation: {}'.format(ious.max())) return result, best_iou
def diversity_map(pred_bboxes, pred_labels, pred_scores, gt_bboxes1, gt_labels1, gt_difficult1): pred_bboxes_, pred_labels_, pred_scores_ = list(), list(), list() gt_bboxes_, gt_labels_, gt_difficults_ = list(), list(), list() result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes1, gt_labels1, use_07_metric=True) return result['map']
def test(model, yolo_loss, dataloader, opt): model.eval() pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels = list(), list() for batch_i, (imgs, targets) in tqdm(enumerate(dataloader), total=len(dataloader)): imgs = imgs.cuda() targets = targets.numpy() with torch.no_grad(): output = model(imgs) all_output = [] for yolo, out in zip(yolo_loss, output): all_output.append(yolo(out)) output = torch.cat(all_output, 1) output = non_max_suppression(output, opt.num_classes, conf_thres=opt.conf_thres, nms_thres=opt.nms_thres) # Compute average precision for each sample for sample_i in range(targets.shape[0]): # Get labels for sample where width is not zero (dummies) annotations = targets[sample_i, targets[sample_i, :, 3] != 0] # Extract detections detections = output[sample_i] if detections is None: # If there are no detections but there are annotations mask as zero AP detection_bboxes = np.zeros((0, 4)) detection_labels = np.zeros(0) detection_scores = np.zeros(0) else: detections = detections.cpu().numpy() detection_bboxes = detections[:, 0:4] detection_scores = detections[:, 5] detection_labels = detections[:, 6] # Extract target boxes as (x1, y1, x2, y2) target_boxes = np.zeros((len(annotations), 4)) target_boxes[:, 0] = (annotations[:, 1] - annotations[:, 3] / 2) target_boxes[:, 1] = (annotations[:, 2] - annotations[:, 4] / 2) target_boxes[:, 2] = (annotations[:, 1] + annotations[:, 3] / 2) target_boxes[:, 3] = (annotations[:, 2] + annotations[:, 4] / 2) target_boxes *= opt.img_size target_labels = annotations[:, 0] pred_bboxes.append(detection_bboxes) pred_labels.append(detection_labels.astype(np.int)) pred_scores.append(detection_scores) gt_bboxes.append(target_boxes) gt_labels.append(target_labels.astype(np.int)) result = eval_detection_voc( pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, use_07_metric=True) print("Mean Average Precision:" + str(result)) return result["map"]
def eval(dataloader, faster_rcnn, test_num=10000): pred_bboxes, pred_labels, pred_scores = list(), list(), list()#预测结果List包含,框,类别,分数。 gt_bboxes, gt_labels, gt_difficults = list(), list(), list()#验证的gt框,类别,difficult疑问。 for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(enumerate(dataloader)): sizes = [sizes[0][0].item(), sizes[1][0].item()] #尺寸的计算方式,疑问? pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict(imgs, [sizes]) #输入图片以及图片的尺寸进入模型得到预测结果 gt_bboxes += list(gt_bboxes_.numpy()) #真实框,存放N张图片的bboxes的list,[N,(X,4)] gt_labels += list(gt_labels_.numpy()) #真实类别,存放N张图片的labels的list,[N,(X,)?] gt_difficults += list(gt_difficults_.numpy()) #真实difficult,存放N张图片的bboxes的难易程度,[N,(X,)] pred_bboxes += pred_bboxes_ #预测框,存放N张图片的预测bboxes的list,[N,(X,4)] pred_labels += pred_labels_ #预测类别,存放N张图片的预测labels的list,[N,(X,)] pred_scores += pred_scores_ #预测分数,存放N张图片的预测label的分数的list,[N,(X,)] if ii == test_num: break #限制数量,预测test_num张图片 result = eval_detection_voc( #用voc评估方法评估 pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) return result
def eval(dataloader, faster_rcnn, test_num=981): """ 验证 """ #空的list print('evaling') pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(enumerate(dataloader)): sizes = [int(sizes[0][0]), int(sizes[1][0])] #预测目标框、类别、分数存入list pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict( imgs, [sizes]) #将真实的目标框、类别、difficults存入list # print(pred_labels_) print('第' + str(ii + 1) + '个图片预测结果个数:' + str(len(pred_labels_[0]))) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) #将预测的目标框、类别、分数存入list pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break # if ii == 5: break #返回dictz字典,两个key值:AP、mAP # print # for i in range(len(pred_labels)): # print(len(pred_labels[i])) # print('pre num is ', len(pred_labels)) results = [] for iou_thresh in [0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95]: result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=False, iou_thresh=iou_thresh) results.append(result) return results
def eval(dataloader, faster_rcnn, test_num=10000): pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(enumerate(dataloader)): sizes = [sizes[0][0], sizes[1][0]] pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict(imgs, [sizes]) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break result = eval_detection_voc( pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) return result
def eval(seq_loader, faster_rcnn, signal_type, test_num=10000): carrada = download('Carrada') pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() # print('*** Evaluation ***') for n_seq, sequence_data in tqdm(enumerate(seq_loader)): seq_name, seq = sequence_data path_to_frames = os.path.join(carrada, seq_name[0]) frame_set = TestCarradaDataset(opt, seq, 'box', signal_type, path_to_frames) frame_loader = data_.DataLoader(frame_set, batch_size=1, shuffle=False, num_workers=opt.num_workers) for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(enumerate(frame_loader)): sizes = [sizes[0][0].item(), sizes[1][0].item()] imgs = normalize(imgs) pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict(imgs, [sizes]) gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ # if ii == test_num: break result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) ious = bbox_iou(gt_bboxes[0], pred_bboxes[0]) try: best_iou = ious.max() except ValueError: best_iou = 0 return result, best_iou
def eval(dataloader, faster_rcnn, test_num=10000): print('Running validation') # Each predicted box is organized as :`(y_{min}, x_{min}, y_{max}, x_{max}), # Where y corresponds to the height and x to the width pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() image_ids = list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_, image_ids_) in tqdm( enumerate(dataloader), total=test_num): sizes = [sizes[0].detach().numpy().tolist()[0], sizes[1].detach().numpy().tolist()[0]] pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict(imgs, [sizes]) # We have to add .copy() here to allow for the loaded image to be released after each iteration gt_bboxes += list(gt_bboxes_.numpy().copy()) gt_labels += list(gt_labels_.numpy().copy()) gt_difficults += list(gt_difficults_.numpy().copy()) image_ids += list(image_ids_.numpy().copy()) pred_bboxes += [pp.copy() for pp in pred_bboxes_] pred_labels += [pp.copy() for pp in pred_labels_] pred_scores += [pp.copy() for pp in pred_scores_] if ii == test_num: break result = eval_detection_voc( pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) if opt.validate_only: save_path = '{}_detections.npz'.format(opt.load_path) np.savez(save_path, pred_bboxes=pred_bboxes, pred_labels=pred_labels, pred_scores=pred_scores, gt_bboxes=gt_bboxes, gt_labels=gt_labels, gt_difficults=gt_difficults, image_ids=image_ids, result=result) return result
def eval(dataloader, faster_rcnn, test_num=10000): pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list for ii, (img, size, gt_bbox, gt_label, gt_difficult) in tqdm(enumerate(dataloader)): size = [size[0][0], size[1][0]] #why? pred_bbox, pred_label, pred_score = faster_rcnn.predict( img.numpy(), [size]) gt_bboxes += list(gt_bbox.numpy()) gt_labels += list(gt_label.numpy()) gt_difficults += list(gt_difficult.numpy()) pred_bboxes += pred_bbox pred_labels += pred_label pred_scores += pred_score if ii == test_num: break result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) return result
def eval(dataloader, faster_rcnn, test_num=10000): # 评价函数 pred_bboxes, pred_labels, pred_scores = list(), list(), list() # 预测结果 gt_bboxes, gt_labels, gt_difficults = list(), list(), list() # 真实结果 for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_) in tqdm(enumerate(dataloader)): # ii 用来指示当前进度 sizes = [sizes[0][0], sizes[1][0]] pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict( imgs, [sizes]) # 预测图片 gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) # 这哥们英语一般 pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) # 计算预测好坏情况 return result
def test(**kwargs): opt._parse(kwargs) device = opt.device cnn = AlexNet() in_features = cnn.classifier[6].in_features cnn.classifier[6] = t.nn.Linear(in_features, 21) cnn.load(opt.load_trained_path) cnn.to(device) svms = SVMs(method='load') lrs = LRs(method='load') dataset = NormalDataset(opt.voc_data_dir, split='test') dataloader = t.utils.data.DataLoader(dataset, batch_size=1, num_workers=0, shuffle=False) ipdb.set_trace() pred_bbox_last_, pred_label_last_, pred_score_last_, gt_bbox_, gt_label_ = [],[],[],[],[] for ii, (img, bbox_imgs, bboxes, gt_bbox, gt_label, _) in tqdm(enumerate(dataloader)): # for ii in tqdm(range(len(dataset))): # img,bbox_imgs,bboxes,gt_bbox,gt_label,_ = dataset.getitem(ii) img = img[0].numpy() bboxes = bboxes[0].numpy() gt_bbox = gt_bbox[0].numpy() gt_label = gt_label[0].numpy() inputs = bbox_imgs.to(device)[0] features = cnn.get_features(inputs).cpu().detach().numpy() ''' imOut = cv2.UMat(img.numpy().transpose((1,2,0))*255).get() for i, rect in enumerate(bboxes): ymin, xmin, ymax, xmax = rect cv2.rectangle(imOut, (xmin, ymin), (xmax, ymax), (0, 255, 0), 1, cv2.LINE_AA) cv2.imwrite('./pic/1.jpg',imOut) pred_label,pred_score,pred_bg = svms.predict(features) mask = np.where(pred_bg == False)[0] counts += 1 if mask.size == 0: null_counts += 1 if ii == 10: print("null per:{}".format(1.0*null_counts/counts)) break pred_label = pred_label[mask] pred_score = pred_score[mask] pred_bboxes_unregress = bboxes[mask] # np.ndarray bbox_features = features[mask] ''' pred_label, pred_score = svms.predict(features) pred_bboxes_unregress = bboxes bbox_features = features # mark # 获取每个种类的bboxes和score,为了nms pred_bboxes_2_nms = dict() pred_score_2_nms = dict() bbox_features_2_nms = dict() for lab in np.unique(pred_label): lab_mask = np.where(pred_label == lab)[0] pred_bboxes_2_nms[opt.VOC_BBOX_LABEL_NAMES[ lab]] = pred_bboxes_unregress[lab_mask] pred_score_2_nms[ opt.VOC_BBOX_LABEL_NAMES[lab]] = pred_score[lab_mask] bbox_features_2_nms[ opt.VOC_BBOX_LABEL_NAMES[lab]] = bbox_features[lab_mask] # nms & regression pred_bbox_last = [] pred_label_last = [] pred_score_last = [] for cat, bbox_nms in pred_bboxes_2_nms.items(): mask = np.where(pred_score_2_nms[cat] > opt.nms_thresh)[0] if mask.size == 0: continue else: bbox_nms = bbox_nms[mask] pre_score_2_nms[cat] = pre_score_2_nms[cat][mask] features_2_nms = features_2_nms[mask] keep_mask = nms( t.from_numpy(bbox_nms[:, [1, 0, 3, 2]]).float(), t.from_numpy(pred_score_2_nms[cat]).float(), opt.iou_thresh) loc = lrs.predict(cat, bbox_features_2_nms[cat][keep_mask]) pred_bbox_cat = loc2bbox(bbox_nms[keep_mask], loc) pred_score_last.append(pred_score_2_nms[cat][keep_mask]) pred_bbox_last.append(pred_bbox_cat) pred_label_last.extend([cat] * pred_bbox_cat.shape[0]) if len(pred_label_last) > 0: wrong_2_draw(img, np.vstack(pred_bbox_last), np.array(pred_label_last), gt_bbox, gt_label) pred_bbox_last_ += np.vstack(pred_bbox_last) pred_label_last_ += np.array(pred_label_last) pred_score_last_ += np.hstack(pred_Score_last) gt_bbox_ += gt_bbox gt_label_ += gt_label # evaluation res = eval_detection_voc(pred_bbox_last_, pred_label_last_, pred_score_last_, gt_bbox_, gt_label_) print(res) pd.DataFrame(res).to_excel('./res.xlsx')
def eval(dataloader, faster_rcnn, trainer, dataset, test_num=10000): with torch.no_grad(): print('Running validation') # Each predicted box is organized as :`(y_{min}, x_{min}, y_{max}, x_{max}), # Where y corresponds to the height and x to the width pred_bboxes, pred_labels, pred_scores = list(), list(), list() gt_bboxes, gt_labels, gt_difficults = list(), list(), list() image_ids = list() for ii, (imgs, sizes, gt_bboxes_, gt_labels_, gt_difficults_, image_ids_) in tqdm(enumerate(dataloader), total=test_num): sizes = [ sizes[0].detach().numpy().tolist()[0], sizes[1].detach().numpy().tolist()[0] ] pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict( imgs, [sizes]) # We have to add .copy() here to allow for the loaded image to be released after each iteration gt_bboxes += list(gt_bboxes_.numpy().copy()) gt_labels += list(gt_labels_.numpy().copy()) gt_difficults += list(gt_difficults_.numpy().copy()) image_ids += list(image_ids_.numpy().copy()) pred_bboxes += [pp.copy() for pp in pred_bboxes_] pred_labels += [pp.copy() for pp in pred_labels_] pred_scores += [pp.copy() for pp in pred_scores_] if ii == test_num: break result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) if opt.validate_only: save_path = '{}_detections.npz'.format(opt.load_path) np.savez(save_path, pred_bboxes=pred_bboxes, pred_labels=pred_labels, pred_scores=pred_scores, gt_bboxes=gt_bboxes, gt_labels=gt_labels, gt_difficults=gt_difficults, image_ids=image_ids, result=result) else: ori_img_ = inverse_normalize(at.tonumpy(imgs[0])) gt_img = visdom_bbox(ori_img_, at.tonumpy(gt_bboxes[-1]), at.tonumpy(gt_labels[-1]), label_names=dataset.get_class_names() + ['BG']) trainer.vis.img('test_gt_img', gt_img) # plot predicti bboxes pred_img = visdom_bbox(ori_img_, at.tonumpy(pred_bboxes[-1]), at.tonumpy(pred_labels[-1]).reshape(-1), at.tonumpy(pred_scores[-1]), label_names=dataset.get_class_names() + ['BG']) trainer.vis.img('test_pred_img', pred_img) del imgs, gt_bboxes_, gt_labels_, gt_difficults_, image_ids_, pred_bboxes_, pred_labels_, pred_scores_ torch.cuda.empty_cache() return result
sizes = [sizes[0][0], sizes[1][0]] <<<<<<< HEAD pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict(imgs,img_depth, [sizes]) ======= pred_bboxes_, pred_labels_, pred_scores_ = faster_rcnn.predict(imgs,[sizes]) >>>>>>> b43e1a358b5853ffb749ac931c9cd97a6dccf862 gt_bboxes += list(gt_bboxes_.numpy()) gt_labels += list(gt_labels_.numpy()) gt_difficults += list(gt_difficults_.numpy()) pred_bboxes += pred_bboxes_ pred_labels += pred_labels_ pred_scores += pred_scores_ if ii == test_num: break result = eval_detection_voc( pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) return result def train(**kwargs): opt._parse(kwargs) dataset = Dataset(opt) print('load data') dataloader = data_.DataLoader(dataset, \ batch_size=1, \ shuffle=True, \ #pin_memory=True, num_workers=opt.num_workers) testset = TestDataset(opt)