def verify_model(self): """ 处理标记文件夹 :param img_folder: 图片文件夹 :param out_folder: :return: """ yolo = YoloV3(model_path=self.model_path, classes_path=self.classes_path, anchors_path=self.anchors_path) # yolo算法类 img_dict = format_img_and_anno(self.img_folder) res_dict = dict() for count, img_name in enumerate(img_dict): print_info('-' * 50) print_info('图片: {}'.format(img_name)) (img_p, anno_p) = img_dict[img_name] _, precision, recall = self.detect_img(yolo, img_p, anno_p, self.out_folder) res_dict[img_name] = (precision, recall) if count == 10: break ap, ar = 0, 0 for name in res_dict.keys(): precision, recall = res_dict[name] ap += precision ar += recall mAp = safe_div(ap, len(res_dict.keys())) mAr = safe_div(ar, len(res_dict.keys())) print_info('平均精准率: {:.4f} %, 平均召回率: {:.4f} %'.format( mAp * 100, mAr * 100))
def iou_of_boxes(boxes1, boxes2): """ 框的IOU :param boxes1_: 真值 :param boxes2_: 预测 :return: 精准和召回 """ res_list = [] boxes1_ = copy.deepcopy(boxes1) boxes2_ = copy.deepcopy(boxes2) t1, t2 = len(boxes1_), len(boxes2_) for box1 in boxes1_: final_iou = 0 final_box = None for box2 in boxes2_: iou = bb_intersection_over_union(box1, box2) if iou > final_iou: final_iou = iou final_box = box2 if final_iou > 0.5: res_list.append((box1, final_box, final_iou)) boxes2_.remove(final_box) tr = len(res_list) if tr == 0: # 没有检测源, 则认为正确 if t1 == 0 and t2 != 0: recall, precision = 1.0, 0.0 elif t1 != 0 and t2 == 0: recall, precision = 0.0, 1.0 elif t1 != 0 and t2 != 0: recall, precision = 0.0, 0.0 else: recall, precision = 1.0, 1.0 else: recall = safe_div(tr, t1) # 召回率 precision = safe_div(tr, t2) # 精准率 print_info('精准: {:.4f}, 召回: {:.4f}, 匹配结果: {}'.format( precision, recall, res_list)) return [precision, recall]
def verify_model(self): """ 处理标记文件夹 :param img_folder: 图片文件夹 :param out_folder: :return: """ img_dict = format_img_and_anno(self.img_folder) res_list = [] for count, img_name in enumerate(img_dict): print_info('-' * 50) print_info('图片: {}'.format(img_name)) (img_p, anno_p) = img_dict[img_name] try: res_dict = self.detect_img(img_p, anno_p, self.out_folder) except Exception as e: print_ex('检测异常: {}'.format(e)) continue res_list.append(res_dict) # if count == 10: # break print_info('-' * 50) for target_name in (['all'] + self.targets_name): if target_name in ['truck', 'bus']: continue ap, ar, count = 0, 0, 0 for pr_dict in res_list: if target_name in pr_dict: tp, tr = pr_dict[target_name] ap += tp ar += tr count += 1 mAp = safe_div(ap, count) mAr = safe_div(ar, count) print_info('类: {} P: {:.4f} %, R: {:.4f} %'.format( target_name, mAp * 100, mAr * 100))
def iou_of_boxes(boxes1, boxes2): res_list = [] for box1 in boxes1: final_iou = 0 final_box = None for box2 in boxes2: iou = bb_intersection_over_union(box1, box2) if iou > final_iou: final_iou = iou final_box = box2 if final_iou > 0.5: res_list.append((box1, final_box, final_iou)) t1, t2, tr = len(boxes1), len(boxes2), len(res_list) if tr == 0: # 没有检测源, 则认为正确 return [1.0, 1.0] recall = safe_div(tr, t1) # 召回率 precision = safe_div(tr, t2) # 精准率 print_info('精准: {:.4f}, 召回: {:.4f}, 匹配结果: {}'.format( precision, recall, res_list)) return [precision, recall]
def get_batch_acc(outputs, labels): """ multiLabel acc, all values are same, is right :param outputs: predictions :param labels: ground truth :return: acc percent, num of right, num of all """ outputs = sigmoid(outputs) outputs = outputs.asnumpy() labels = labels.asnumpy().astype('int') print(labels[0]) outputs = np.where(outputs > 0.5, 1, 0) # 类别阈值0.5 print(outputs[0]) rights = np.sum(np.where(labels == outputs, 0, 1), axis=1) n_right = np.count_nonzero(rights == 0) # 全0的即全部相等 return safe_div(n_right, len(labels)), n_right, len(labels)
def main(): ym = Y3Model() img_folder = os.path.join(IMG_DATA, 'jiaotong-0727') right_folder = os.path.join(IMG_DATA, 'jiaotong-0727-right') wrong_folder = os.path.join(IMG_DATA, 'jiaotong-0727-wrong') none_folder = os.path.join(IMG_DATA, 'jiaotong-0727-none') mkdir_if_not_exist(right_folder, is_delete=True) mkdir_if_not_exist(wrong_folder, is_delete=True) mkdir_if_not_exist(none_folder, is_delete=True) img_dict = format_img_and_anno(img_folder) r_count = 0 all_count = 0 no_recall_count = 0 for count, img_name in enumerate(img_dict): (img_p, anno_p) = img_dict[img_name] # print(img_p) try: tag_res, img_box = ym.detect_img(img_p, True) except Exception as e: continue w_tags = [] for tag in tag_res.keys(): if tag_res[tag] <= 0.01: print_info('删除Tag {} {}'.format(tag, tag_res[tag])) w_tags.append(tag) for tag in w_tags: tag_res.pop(tag, None) # 小于1%的类别 all_count += 1 p_classes = set(tag_res.keys()) _, t_classes = read_anno_xml(anno_p) merge_dict = {'truck': 'car', 'bus': 'car', 'car': 'car'} # 合并类别 t_classes = map_classes(merge_dict, t_classes) # 合并类别 traffic_names = [ 'bicycle', 'car', 'motorbike', 'aeroplane', 'bus', 'train', 'truck', 'boat' ] t_classes = set(t_classes) & set(traffic_names) img_name = img_p.split('/')[-1] is_right = False if p_classes and p_classes.issubset(t_classes): # 检测正确 r_count += 1 img_box.save(os.path.join(right_folder, img_name + '.d.jpg')) is_right = True elif not p_classes and not t_classes: # 空,检测正确 r_count += 1 if not img_box: img_box = Image.open(img_p) img_box.save(os.path.join(right_folder, img_name + '.d.jpg')) is_right = True elif not p_classes and t_classes: # 检测为空,实际有类 if not img_box: img_box = Image.open(img_p) img_box.save(os.path.join(none_folder, img_name + '.d.jpg')) no_recall_count += 1 # 未召回 r_count += 1 is_right = True else: # 其他,检测错误 if not img_box: img_box = Image.open(img_p) img_box.save(os.path.join(wrong_folder, img_name + '.d.jpg')) print_info('P: {}, T: {}, {}'.format(list(p_classes), list(t_classes), '正确' if is_right else '错误')) right_ratio = safe_div(r_count, all_count) print_info('正确: {}, 全部: {}, 未召回: {}, 准确率: {}'.format( r_count, all_count, no_recall_count, right_ratio))