def __calc_APs(self, iou_thresh=0.5, use_07_metric=False): """ Calculate ap values for each category :param iou_thresh: :param use_07_metric: :return:dict{cls:ap} """ filename = os.path.join(self.pred_result_path, "comp4_det_test_{:s}.txt") cachedir = os.path.join(self.pred_result_path, "cache") # annopath = os.path.join(self.val_data_path, 'Annotations', '{:s}.xml') annopath = os.path.join(self.val_data_path, "Annotations\\" + "{:s}.xml") imagesetfile = os.path.join(self.val_data_path, "ImageSets", "Main", "test.txt") APs = {} Recalls = {} Precisions = {} for i, cls in enumerate(self.classes): R, P, AP = voc_eval.voc_eval( filename, annopath, imagesetfile, cls, cachedir, iou_thresh, use_07_metric, ) Recalls[cls] = R Precisions[cls] = P APs[cls] = AP if os.path.exists(cachedir): shutil.rmtree(cachedir) return APs
def __calc_APs(self, iou_thresh=0.5, use_07_metric=False): """ 计算每个类别的ap值 :param iou_thresh: :param use_07_metric: :return:dict{cls:ap} """ filename = os.path.join(self.pred_result_path, 'voc', 'comp4_det_test_{:s}.txt') cachedir = os.path.join(self.pred_result_path, 'voc', 'cache') annopath = os.path.join(self.val_data_path, 'Annotations/{:s}.xml') imagesetfile = os.path.join(self.val_data_path, 'ImageSets', cfg.TEST["EVAL_NAME"] + '.txt') APs = {} Recalls = {} Precisions = {} for i, cls in enumerate(self.classes): R, P, AP = voc_eval.voc_eval(filename, annopath, imagesetfile, cls, cachedir, iou_thresh, use_07_metric) #调用voc_eval.py的函数进行计算 APs[cls] = AP Recalls[cls] = R Precisions[cls] = P if os.path.exists(cachedir): shutil.rmtree(cachedir) return APs
def evalMap(save_result_root, xmlRoot, savePath): """ save_result_root: root dir should only contain labels.txt. Such as: person.txt, car.txt, mobilephone.txt ... xmlRoot: should contain ground truth xml files. savePath: image list file, and the file contains only image name per line. """ print("***************************") print("***************************") mAP = [] for class_name_file in os.listdir(save_result_root): class_name = class_name_file[:-4] rec, prec, ap = voc_eval(save_result_root + '/{}.txt', xmlRoot + '/{}.xml', savePath, class_name, '.') print("{} :\t {} ".format(class_name, ap)) mAP.append(ap) mAP = tuple(mAP) print("***************************") print("mAP :\t {}".format( float( sum(mAP)/len(mAP)) )) print("***************************") print("***************************") os.remove('annots.pkl')
def __calc_APs(self, iou_thresh=0.5, use_07_metric=False): """ Calculate ap values for each category :param iou_thresh: :param use_07_metric: :return:dict{cls:ap} """ filename = os.path.join(self.pred_result_path, "comp4_det_test_{:s}.txt") cachedir = os.path.join(self.pred_result_path, "cache") annopath = os.path.join(self.val_data_path, 'label\\' + '{:s}.xml') val_data_expr = os.path.join(self.val_data_path, 'val', "*.jpg") imagesetfile = glob.glob(val_data_expr) imagesetfile = [path.split("\\")[-1][:-4] for path in imagesetfile] APs = {} Recalls = {} Precisions = {} for i, cls in enumerate(self.classes): R, P, AP = voc_eval.voc_eval( filename, annopath, imagesetfile, cls, cachedir, iou_thresh, use_07_metric, ) Recalls[cls] = R Precisions[cls] = P APs[cls] = AP if os.path.exists(cachedir): shutil.rmtree(cachedir) return APs
def __APs_calc(self, iou_thresh=0.5, use_07_metric=False): """ :param iou_thresh: 计算AP时用的iou阈值 :param use_07_metric: 是否使用07年的11点计算方式 :return: 所有类别的APs,元素是字典{cls:ap} """ filename = os.path.join(self._project_path, 'eval', 'results', 'VOC2007', 'Main', 'comp3_det_test_{:s}.txt') cachedir = os.path.join(self._project_path, 'eval', 'cache') annopath = os.path.join(self._dataset_path, 'VOC2007', 'Annotations', '{:s}.xml') imagesetfile = os.path.join(self._dataset_path, 'VOC2007', 'ImageSets', 'Main', 'test.txt') APs = {} for i, cls in enumerate(cfg.CLASSES): rec, prec, ap = voc_eval.voc_eval(filename, annopath, imagesetfile, cls, cachedir, iou_thresh, use_07_metric) APs[cls] = ap if os.path.exists(cachedir): shutil.rmtree(cachedir) return APs
def __APs_calc(self, iou_thresh=0.5, use_07_metric=False): filename = os.path.join(self._project_path, 'eval', 'results', 'VOC2007', 'Main', 'comp3_det_test_{:s}.txt') cachedir = os.path.join(self._project_path, 'eval', 'cache') #annopath = os.path.join(self._dataset_path, '2007_test', 'Annotations', '{:s}.xml') annopath = self._dataset_path + '/2007_test' + '/Annotations/' + '{:s}.xml' imagesetfile = os.path.join(self._dataset_path, '2007_test', 'ImageSets', 'Main', 'test.txt') APs = {} for i, cls in enumerate(cfg.CLASSES): rec, prec, ap = voc_eval.voc_eval(filename, annopath, imagesetfile, cls, cachedir, iou_thresh, use_07_metric) APs[cls] = ap if os.path.exists(cachedir): shutil.rmtree(cachedir) return APs
def __calc_APs(self, iou_thresh=0.5, use_07_metric=False): """ 计算每个类别的ap值 :param iou_thresh: :param use_07_metric: :return:dict{cls:ap} """ filename = os.path.join(self.pred_result_path, 'comp4_det_test_{:s}.txt') cachedir = os.path.join(self.pred_result_path, 'cache') annopath = os.path.join(self.val_data_path, 'Annotations', '{:s}.xml') imagesetfile = os.path.join(self.val_data_path, 'ImageSets', 'Main', 'test.txt') APs = {} for i, cls in enumerate(self.classes): R, P, AP = voc_eval.voc_eval(filename, annopath, imagesetfile, cls, cachedir, iou_thresh, use_07_metric) APs[cls] = AP if os.path.exists(cachedir): shutil.rmtree(cachedir) return APs
cachedir = './cachedir' imageset = ('2007', 'test') if not os.path.exists(cachedir): os.mkdir(cachedir) det_list = [ os.path.join(cachedir_predict, file) for file in os.listdir(cachedir_predict) ] det_classes = list() for file in det_list: classes = os.path.splitext(os.path.basename(file))[0].split('_')[-1] det_classes.append(classes) detpath = file.replace(classes, '%s') annopath = os.path.join(cfg.VOCroot, 'VOC' + imageset[0], 'Annotations', '%s.xml') imagesetfile = os.path.join(cfg.VOCroot, 'VOC' + imageset[0], 'ImageSets', 'Main', imageset[1] + '.txt') MAPList = list() for classname in det_classes: rec, prec, ap = voc_eval(detpath, annopath, imagesetfile, classname, cachedir) print '%s\t AP:%.4f' % (classname, ap) MAPList.append(ap) Map = np.array(MAPList) mean_Map = np.mean(Map) print '------ Map: %.4f' % (mean_Map)