[{"data":1,"prerenderedAt":135},["ShallowReactive",2],{"content-query-bOul6ZuVgz":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":129,"_id":130,"_source":131,"_file":132,"_stem":133,"_extension":134},"/technology-blogs/zh/1641","zh",false,"","YOLOv3人体目标检测模型实现（四）","模型评估","2022-07-20","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/07/25/93af40f7a1f7480aaa4f8c898f14a40e.png","technology-blogs","实践",{"type":15,"children":16,"toc":124},"root",[17,25,32,38,48,53,61,66,74,79,87,92,97,102,110,115],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"yolov3人体目标检测模型实现四",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":29},"h2",{"id":28},"_5模型评估",[30],{"type":24,"value":31},"5.模型评估",{"type":18,"tag":33,"props":34,"children":35},"p",{},[36],{"type":24,"value":37},"首先是导包以及设置context：",{"type":18,"tag":39,"props":40,"children":42},"pre",{"code":41},"from mindspore import context\n\nfrom dataset.voc2012_dataset import create_voc2012_dataset\nfrom model.yolo import YOLOV3DarkNet53\nfrom utils.utils_yolov3 import DetectionEngine, load_yolov3\nfrom yolov3_eval_conf import EvalYOLOv3Conf\n\n# set context\ncontext.set_context(mode=context.GRAPH_MODE,\n                    device_target=\"GPU\", save_graphs=False)\n# set_graph_kernel_context\ncontext.set_context(enable_graph_kernel=True)\ncontext.set_context(graph_kernel_flags=\"--enable_parallel_fusion \"\n                                       \"--enable_trans_op_optimize \"\n                                       \"--disable_cluster_ops=ReduceMax,Reshape \"\n                                       \"--enable_expand_ops=Conv2D\")\n# Set mempool block size for improving memory utilization, which will not take effect in GRAPH_MODE\nif context.get_context(\"mode\") == context.PYNATIVE_MODE:\n    context.set_context(mempool_block_size=\"31GB\")\n# config\nconfig = EvalYOLOv3Conf()\n",[43],{"type":18,"tag":44,"props":45,"children":46},"code",{"__ignoreMap":7},[47],{"type":24,"value":41},{"type":18,"tag":33,"props":49,"children":50},{},[51],{"type":24,"value":52},"评估模型时的配置如下（也可见 附件\\yolov3_eval_conf.py）：",{"type":18,"tag":39,"props":54,"children":56},{"code":55},"class EvalYOLOv3Conf():\n    def __init__(self):\n        \n        # ---- dataset ----\n        \n        self.data_path = \"VOCdevkit/VOC2012/\"\n        self.data_usage = \"my_person_val\"       # 评估数据集\n        self.data_training = False\n        \n        self.num_classes = 1\n        \n        self.class_to_idx = {}\n        \n        self.anchor_scales = [[15, 38],\n                              [34, 86],\n                              [84, 127],\n                                \n                              [51, 192],\n                              [91, 257],\n                              [173, 195],\n                                \n                              [142, 319],\n                              [221, 339],\n                              [351, 365]]\n        \n        self.batch_size = 48                      # 评估时的batch\n        self.max_box = 32\n        \n        # test\n        self.test_img_shape = [416, 416]          # 图片缩放大小\n        \n        # ---- Model ----\n        \n        self.out_channel = 3 * ( 5 + self.num_classes)\n        \n        self.keep_detect = True\n        \n        self.ckpt_path = \"./train_ckpt/yolov3-???.ckpt\" # 评估的模型\n        \n        # ---- Detect ----\n        \n        self.nms_thresh = 0.5                     # nms 算法去重的IoU阈值\n        self.eval_ignore_threshold = 0.001        # 检测置信度阈值\n        \n        self.detcte_result_dir = \"det_res/\"       # 分类检测框的临时结果存放\n        \n        self.image_id_idx = []                    # xml编号的列表，与图片序号对应，eval_yolov3.py中设置\n        \n        self.anno_path = \"VOCdevkit/VOC2012/Annotations/{}.xml\"               # Annotations目录的位置，注意最后的格式\n        self.val_path = \"VOCdevkit/VOC2012/ImageSets/Main/my_person_val.txt\"  # 模型评估xml文件\n",[57],{"type":18,"tag":44,"props":58,"children":59},{"__ignoreMap":7},[60],{"type":24,"value":55},{"type":18,"tag":33,"props":62,"children":63},{},[64],{"type":24,"value":65},"读数据集：",{"type":18,"tag":39,"props":67,"children":69},{"code":68},"# dataset\nvoc2012_dat, data_size = create_voc2012_dataset(config, 2)\nconfig.steps_per_epoch = int(data_size / config.batch_size)\nimage_id_idx = {}\nwith open(config.val_path) as f:\n    lines = f.readlines()\n    for i, line in enumerate(lines):\n        image_id_idx[i] = line.strip()\nconfig.image_id_idx = image_id_idx\nprint(\"dataset size: \",data_size)\nprint(\"bath num in 1 epoch: \", config.steps_per_epoch)\n",[70],{"type":18,"tag":44,"props":71,"children":72},{"__ignoreMap":7},[73],{"type":24,"value":68},{"type":18,"tag":33,"props":75,"children":76},{},[77],{"type":24,"value":78},"定义网络和检测器：",{"type":18,"tag":39,"props":80,"children":82},{"code":81},"# network\nnetwork = YOLOV3DarkNet53(is_training=config.data_training, config=config)\nload_yolov3(network,config.ckpt_path)\nnetwork.set_train(False)\n\n# init detection engine\ndetection = DetectionEngine(config)\n",[83],{"type":18,"tag":44,"props":84,"children":85},{"__ignoreMap":7},[86],{"type":24,"value":81},{"type":18,"tag":33,"props":88,"children":89},{},[90],{"type":24,"value":91},"这里我们不需要损失函数，所以只用了网络结构YOLOV3DarkNet53，然后用load_yolov3将前面训练好的模型加载进来。",{"type":18,"tag":33,"props":93,"children":94},{},[95],{"type":24,"value":96},"DetectionEngine类主要处理模型前向传播后的输出，包括置信度阈值筛选、NMS算法去除重叠等等工作，最后计算AP的工作也是它完成，对这些计算感兴趣的朋友可以看 附件\\utils\\utils_yolov3.py 以及 附件\\utils\\eval_utils.py。",{"type":18,"tag":33,"props":98,"children":99},{},[100],{"type":24,"value":101},"最后对模型进行测试评估：",{"type":18,"tag":39,"props":103,"children":105},{"code":104},"print('Start inference....')\nfor i, data in enumerate(voc2012_dat.create_dict_iterator(num_epochs=1)):\n    image = data[\"image\"]\n\n    image_shape = data[\"image_shape\"]\n    image_id = data[\"img_id\"]\n\n    prediction = network(image)\n    output_big, output_me, output_small = prediction\n    output_big = output_big.asnumpy()\n    output_me = output_me.asnumpy()\n    output_small = output_small.asnumpy()\n    image_id = image_id.asnumpy()\n    image_shape = image_shape.asnumpy()\n\n    detection.detect([output_small, output_me, output_big], config.batch_size, image_shape, image_id)\n    if i % 2 == 0:\n        print('Processing... {:.2f}% '.format(i * config.batch_size / data_size * 100))\n\nprint(\"Finish\")\n\nprint('Calculating mAP...')\ndetection.do_nms_for_results()\nresult_file_path = detection.write_result()\nprint('result file path: ', result_file_path)\n\ndetection.get_eval_result()\n\nfor k, t in detection.eval_res.items():\n    print(k, \" AP : \", t['ap'])\n\nimport matplotlib.pyplot as plt\n\nplt.title(\"P-R curve\",fontsize=14)\nplt.xlabel(\"recall\", fontsize=14)\nplt.ylabel(\"precision\", fontsize=14)\nres = detection.eval_res['person']\nplt.plot(res['prec'], res['rec'])\n\nplt.savefig(\"p_r.png\")\n",[106],{"type":18,"tag":44,"props":107,"children":108},{"__ignoreMap":7},[109],{"type":24,"value":104},{"type":18,"tag":33,"props":111,"children":112},{},[113],{"type":24,"value":114},"最后我设置检测置信度的阈值为 0.01，nms 算法中的 IoU 阈值为 0.5时，得到的结果是AP为0.609，P-R曲线如下：",{"type":18,"tag":33,"props":116,"children":117},{},[118],{"type":18,"tag":119,"props":120,"children":123},"img",{"alt":121,"src":122},"10.jpg","https://bbs-img.huaweicloud.com/data/forums/attachment/forum/20227/12/1657637603602497489.jpg",[],{"title":7,"searchDepth":125,"depth":125,"links":126},4,[127],{"id":28,"depth":128,"text":31},2,"markdown","content:technology-blogs:zh:1641.md","content","technology-blogs/zh/1641.md","technology-blogs/zh/1641","md",1776506114650]