def main(): img_seq = [] # Get img_ids from lvis, instead. img_ids = coco.getImgIds() num_images = len(img_ids) # import ipdb; ipdb.set_trace() dets = [] dets.append(coco.loadRes(PRE_PATH)) prog_bar = mmcv.ProgressBar(len(img_ids)) for i, img_id in enumerate(img_ids): # ipdb.set_trace() if DEBUG_ is True: if i == DEBUG_FRAMES: break img_info = coco.loadImgs(ids=[img_id])[0] img_path = IMG_PATH + img_info['file_name'] img = cv2.imread(img_path) # Create a gt labeled img. gt_ids = coco.getAnnIds(imgIds=[img_id]) gts = coco.loadAnns(gt_ids) gt_img = img for j, pred in enumerate(gts): bbox = _coco_box_to_bbox(pred['bbox']) cat_id = pred['category_id'] gt_img = add_box(gt_img, bbox, 1.00, cat_id) gt_img = add_to_canvas(gt_img) # Create a predictions labeled img. for k in range(len(dets)): pred_ids = dets[k].getAnnIds(imgIds=[img_id]) preds = dets[k].loadAnns(pred_ids) pred_img = img.copy() for j, pred in enumerate(preds): bbox = _coco_box_to_bbox(pred['bbox']) sc = pred['score'] cat_id = pred['category_id'] if sc > VIS_THR: pred_img = add_box(pred_img, bbox, sc, cat_id) # ipdb.set_trace() pred_img = add_to_canvas(pred_img) # Create a super image and save it. sup_img = np.concatenate((gt_img, pred_img), axis=1) sup_img_name = 'pre_{}.jpg'.format(str(img_id).zfill(12)) cv2.imwrite(os.path.join(OUT_PATH, sup_img_name), sup_img) img_seq.append(sup_img) prog_bar.update()
def evaluate_coco(model, dataset, coco, eval_type="bbox", limit=0, image_ids=None): """Runs official COCO evaluation. dataset: A Dataset object with valiadtion data eval_type: "bbox" or "segm" for bounding box or segmentation evaluation limit: if not 0, it's the number of images to use for evaluation """ # Pick COCO images from the dataset image_ids = image_ids or dataset.image_ids # Limit to a subset if limit: image_ids = image_ids[:limit] # Get corresponding COCO image IDs. coco_image_ids = [dataset.image_info[id]["id"] for id in image_ids] t_prediction = 0 t_start = time.time() results = [] for i, image_id in enumerate(image_ids): # Load image image = dataset.load_image(image_id) # Run detection t = time.time() r = model.detect([image], verbose=0)[0] t_prediction += (time.time() - t) # Convert results to COCO format image_results = build_coco_results(dataset, coco_image_ids[i:i + 1], r["rois"], r["class_ids"], r["scores"], r["masks"]) results.extend(image_results) # Load results. This modifies results with additional attributes. coco_results = coco.loadRes(results) # Evaluate cocoEval = COCOeval(coco, coco_results, eval_type) cocoEval.params.imgIds = coco_image_ids cocoEval.evaluate() cocoEval.accumulate() cocoEval.summarize() print("Prediction time: {}. Average {}/image".format( t_prediction, t_prediction / len(image_ids))) print("Total time: ", time.time() - t_start)
cv2.putText(image, txt, (bbox[0], bbox[1] - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), thickness=1) cv2.rectangle(image, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 2) return image if __name__ == '__main__': dets = [] img_ids = coco.getImgIds() num_images = len(img_ids) for k in range(1, len(sys.argv)): pred_path = sys.argv[k] dets.append(coco.loadRes(pred_path)) # import pdb; pdb.set_trace() for i, img_id in enumerate(img_ids): img_info = coco.loadImgs(ids=[img_id])[0] img_path = IMG_PATH + img_info['file_name'] img = cv2.imread(img_path) gt_ids = coco.getAnnIds(imgIds=[img_id]) gts = coco.loadAnns(gt_ids) gt_img = img.copy() for j, pred in enumerate(gts): bbox = _coco_box_to_bbox(pred['bbox']) cat_id = pred['category_id'] gt_img = add_box(gt_img, bbox, 0, cat_id) for k in range(len(dets)): pred_ids = dets[k].getAnnIds(imgIds=[img_id]) preds = dets[k].loadAnns(pred_ids)
thickness=1) cv2.rectangle(image, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 2) return image if __name__ == '__main__': dets = [] img_ids = coco.getImgIds() num_images = len(img_ids) ipdb.set_trace() #for k in range(1, len(sys.argv)): # pred_path = sys.argv[k] # dets.append(coco.loadRes(pred_path)) dets.append(coco.loadRes(ANN_PATH)) # import pdb; pdb.set_trace() for i, img_id in enumerate(img_ids): img_info = coco.loadImgs(ids=[img_id])[0] img_path = IMG_PATH + img_info['file_name'] img = cv2.imread(img_path) gt_ids = coco.getAnnIds(imgIds=[img_id]) gts = coco.loadAnns(gt_ids) gt_img = img.copy() for j, pred in enumerate(gts): bbox = _coco_box_to_bbox(pred['bbox']) cat_id = pred['category_id'] gt_img = add_box(gt_img, bbox, 0, cat_id) for k in range(len(dets)): pred_ids = dets[k].getAnnIds(imgIds=[img_id])
return iou def _box_inside(box2, box1): inside = (box2[0] >= box1[0] and box2[1] >= box1[1] and \ box2[2] <= box1[2] and box2[3] <= box1[3]) return inside if __name__ == '__main__': if len(sys.argv) > 2: ANN_PATH = sys.argv[2] coco = coco.COCO(ANN_PATH) pred_path = sys.argv[1] out_path = pred_path[:-5] + '_no_ghost.json' dets = coco.loadRes(pred_path) img_ids = coco.getImgIds() num_images = len(img_ids) thresh = 4 out = [] for i, img_id in enumerate(img_ids): if i % 500 == 0: print(i) pred_ids = dets.getAnnIds(imgIds=[img_id]) preds = dets.loadAnns(pred_ids) num_preds = len(preds) for j in range(num_preds): overlap_score = 0 if preds[j]['score'] > 0.2: for k in range(num_preds): if preds[j]['category_id'] == preds[k]['category_id'] and \
from __future__ import absolute_import from __future__ import division from __future__ import print_function import pycocotools.coco as coco from pycocotools.cocoeval import COCOeval import sys import cv2 import numpy as np import pickle import os print("eval-coco") this_dir = os.path.dirname(__file__) # ANN_PATH = this_dir + '../../data/coco/annotations/instances_val2017.json' ANN_PATH = this_dir + '/store/datasets/UA-Detrac/COCO-format/test.json' print(ANN_PATH) if __name__ == '__main__': pred_path = sys.argv[1] coco = coco.COCO(ANN_PATH) dets = coco.loadRes(pred_path) img_ids = coco.getImgIds() num_images = len(img_ids) coco_eval = COCOeval(coco, dets, "bbox") coco_eval.evaluate() coco_eval.accumulate() coco_eval.summarize()
'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush' ] annot_path = "/home/user/home/user/Xinyuan/work/CenterNet-1/data/coco/annotations/instances_val2017.json" coco = coco.COCO(annot_path) save_dir = "/home/user/home/user/Xinyuan/work/CenterNet-1/exp/ctdet/coco_hg/" coco_dets = coco.loadRes('{}/results.json'.format(save_dir)) coco_eval = COCOeval(coco, coco_dets, "bbox") print(dir(coco_eval)) save_path = "src/cache/fail/" coco_dets = coco.loadRes('{}/results.json'.format(save_dir)) coco_eval = COCOeval(coco, coco_dets, "bbox") p = coco_eval.params p.imgIds = list(np.unique(p.imgIds)) p.maxDets = sorted(p.maxDets) coco_eval.params = p coco_eval._prepare() cat_dict = coco.cats # print(cat_dict) catIds = coco_eval.params.catIds computeIoU = coco_eval.computeIoU coco_eval.ious = {(imgId, catId): coco_eval.computeIoU(imgId, catId) \
det['bbox'] = [ ji[0], ji[1], ji[2] - ji[0] + 1, ji[3] - ji[1] + 1 ] dets.append(det) return dets ann_path = '/media/leo/data/code/pangnet/detection/object_as_point/Git/CenterNet/data/voc/annotations/pascal_test2007.json' # pred_path = '/media/leo/data/code/pangnet/detection/object_as_point/Git/CenterNet/exp/ctdet/fatnet_frn_pascal_192_daspp_dcn_dla/results.json' # pred_path = '/media/leo/data/code/pangnet/detection/object_as_point/Git/CenterNet/exp/ctdet/resnet_18_dcn_lr_1x/results.json' pred_path = '/media/leo/data/code/pangnet/detection/object_as_point/Git/CenterNet/exp/ctdet/fatnet_frn_pascal_192_93/results.json' ann_json = json.load(open(ann_path)) pred = json.load(open(pred_path)) jcoco = json.load( open( '/media/leo/data/code/mm_ped/work_dirs/faster_rcnn_r50_fpn_baseline/12.json' )) pred_convert = convert(pred, ann_json) pred_convert_path = pred_path[:-4] + '_convert.json' json.dump(pred_convert, open(pred_convert_path, 'w')) coco = coco.COCO(ann_path) dets = coco.loadRes(pred_convert_path) img_ids = coco.getImgIds() num_images = len(img_ids) coco_eval = COCOeval(coco, dets, "bbox") coco_eval.evaluate() coco_eval.accumulate() coco_eval.summarize()
def eval(model_arch, model_path, img_dir, gt_annot_path): output = "output" if os.path.exists(output): shutil.rmtree(output) os.mkdir(output) os.environ['CUDA_VISIBLE_DEVICES'] = "0" if LoadImagesAndLabels.num_classes <= 5: colors = [(55, 55, 250), (255, 155, 50), (128, 0, 0), (255, 0, 255), (128, 255, 128), (255, 0, 0)] else: colors = [(v // 32 * 64 + 64, (v // 8) % 4 * 64, v % 8 * 32) for v in range(1, LoadImagesAndLabels.num_classes + 1)][::-1] detector = CtdetDetector(model_arch, model_path) print('\n/****************** Eval ****************/\n') import tqdm import pycocotools.coco as coco from pycocotools.cocoeval import COCOeval print("gt path: {}".format(gt_annot_path)) result_file = '../evaluation/instances_det.json' coco = coco.COCO(gt_annot_path) images = coco.getImgIds() num_samples = len(images) print('find {} samples in {}'.format(num_samples, gt_annot_path)) #------------------------------------------------ coco_res = [] for index in tqdm.tqdm(range(num_samples)): img_id = images[index] file_name = coco.loadImgs(ids=[img_id])[0]['file_name'] image_path = os.path.join(img_dir, file_name) img = cv2.imread(image_path) results, hm = detector.work(img) # 返回检测结果和置信度图 class_num = {} for res in results: cls, conf, bbox = res[0], res[1], res[2] coco_res.append({ 'bbox': [bbox[0], bbox[1], bbox[2] - bbox[0], bbox[3] - bbox[1]], 'category_id': LoadImagesAndLabels.class_name.index(cls), 'image_id': img_id, 'score': conf }) if cls in class_num: class_num[cls] += 1 else: class_num[cls] = 1 color = colors[LoadImagesAndLabels.class_name.index(cls)] # 绘制标签&置信度 label_ = '{}:{:.1f}'.format(cls, conf) plot_one_box(bbox, img, color=color, label=label_, line_thickness=2) cv2.imwrite(output + "/" + os.path.basename(image_path), img) cv2.namedWindow("heatmap", 0) cv2.imshow("heatmap", np.hstack(hm[0].cpu().numpy())) cv2.namedWindow("img", 0) cv2.imshow("img", img) key = cv2.waitKey(1) #------------------------------------------------- with open(result_file, 'w') as f_dump: json.dump(coco_res, f_dump, cls=NpEncoder) cocoDt = coco.loadRes(result_file) cocoEval = COCOeval(coco, cocoDt, 'bbox') # cocoEval.params.imgIds = imgIds cocoEval.params.catIds = [1] # 1代表’Hand’类,你可以根据需要增减类别 cocoEval.evaluate() print('\n/***************************/\n') cocoEval.accumulate() print('\n/***************************/\n') cocoEval.summarize() draw_pr(cocoEval)
import torch.nn import torch.nn.functional as F import matplotlib.pyplot as plt import pycocotools.coco as coco from pycocotools.cocoeval import COCOeval # GT coco = coco.COCO( '/media/vincent/856c2c04-3976-4948-ba47-5539ecaa24be/vincent/Code/CenterNet-cuda10-multi-spectral/data/rgb/annotations/test.json' ) # Prediction coco_dets = coco.loadRes('{}/results.json'.format( '/media/vincent/856c2c04-3976-4948-ba47-5539ecaa24be/vincent/Checkpoint/CenterNet-CentralNet/ctdet/default/' )) coco_eval = COCOeval(coco, coco_dets, "bbox") coco_eval.evaluate() print('') coco_eval.accumulate() print('') coco_eval.summarize() # 返回mAP值 print(coco_eval.stats[1])
category_id = _valid_ids[cat_id] bbox = item['bbox'] bbox[2] -= bbox[0] bbox[3] -= bbox[1] bbox_out = list(map(_to_float, bbox[0:4])) detection = { "image_id": int(image_id), "category_id": int(category_id), "bbox": bbox_out, "score": float("{:.2f}".format(item['score'])) } detections.append(detection) return detections if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--gt_path', type=str, default='') parser.add_argument('--result_path', type=str, default='') opt = parser.parse_args() # path = 'C:/Users/zhuoyuhe/Desktop/incepio/CenterTrack/exp/results/' coco = coco.COCO(opt.gt_path) with open(opt.result_path + '/save_results_nuscenes.json') as f: result = json.load(f) det = convert_eval_format(result) json.dump(det, open('{}/results_coco.json'.format(opt.result_path), 'w')) coco_dets = coco.loadRes(opt.result_path + '/results_coco.json') coco_eval = COCOeval(coco, coco_dets, "bbox") coco_eval.evaluate() coco_eval.accumulate() coco_eval.summarize()