-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy patheval.py
111 lines (96 loc) · 3.71 KB
/
eval.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#! /usr/bin/env python
# coding=utf-8
# ================================================================
#
# Author : miemie2013
# Created date: 2020-10-15 14:50:03
# Description : pytorch_ppyolo
#
# ================================================================
import copy
from config import *
from tools.cocotools import get_classes, catid2clsid, clsid2catid
import json
import os
import argparse
from tools.cocotools import eval
from model.decode_np import Decode
from model.ppyolo import PPYOLO
from tools.argparser import ArgParser
from tools.cocotools import get_classes
import logging
FORMAT = '%(asctime)s-%(levelname)s: %(message)s'
logging.basicConfig(level=logging.INFO, format=FORMAT)
logger = logging.getLogger(__name__)
if __name__ == '__main__':
parser = ArgParser()
use_gpu = parser.get_use_gpu()
cfg = parser.get_cfg()
print(torch.__version__)
import platform
sysstr = platform.system()
print(torch.cuda.is_available())
# 禁用cudnn就能解决Windows报错问题。Windows用户如果删掉之后不报CUDNN_STATUS_EXECUTION_FAILED,那就可以删掉。
if sysstr == 'Windows':
torch.backends.cudnn.enabled = False
# 读取的模型
model_path = cfg.eval_cfg['model_path']
# 是否给图片画框。
draw_image = cfg.eval_cfg['draw_image']
draw_thresh = cfg.eval_cfg['draw_thresh']
# 验证时的批大小
eval_batch_size = cfg.eval_cfg['eval_batch_size']
# 打印,确认一下使用的配置
print('\n=============== config message ===============')
print('config file: %s' % str(type(cfg)))
print('model_path: %s' % model_path)
print('target_size: %d' % cfg.eval_cfg['target_size'])
print('use_gpu: %s' % str(use_gpu))
print()
# 验证集图片的相对路径
eval_pre_path = cfg.val_pre_path
anno_file = cfg.val_path
from pycocotools.coco import COCO
val_dataset = COCO(anno_file)
val_img_ids = val_dataset.getImgIds()
images = [] # 只跑有gt的图片,跟随PaddleDetection
for img_id in val_img_ids:
ins_anno_ids = val_dataset.getAnnIds(imgIds=img_id, iscrowd=False) # 读取这张图片所有标注anno的id
if len(ins_anno_ids) == 0:
continue
img_anno = val_dataset.loadImgs(img_id)[0]
images.append(img_anno)
# 种类id
_catid2clsid = {}
_clsid2catid = {}
_clsid2cname = {}
with open(cfg.val_path, 'r', encoding='utf-8') as f2:
dataset_text = ''
for line in f2:
line = line.strip()
dataset_text += line
eval_dataset = json.loads(dataset_text)
categories = eval_dataset['categories']
for clsid, cate_dic in enumerate(categories):
catid = cate_dic['id']
cname = cate_dic['name']
_catid2clsid[catid] = clsid
_clsid2catid[clsid] = catid
_clsid2cname[clsid] = cname
class_names = []
num_classes = len(_clsid2cname.keys())
for clsid in range(num_classes):
class_names.append(_clsid2cname[clsid])
# 创建模型
Backbone = select_backbone(cfg.backbone_type)
backbone = Backbone(**cfg.backbone)
Head = select_head(cfg.head_type)
head = Head(yolo_loss=None, nms_cfg=cfg.nms_cfg, **cfg.head)
model = PPYOLO(backbone, head)
if use_gpu:
model = model.cuda()
model.load_state_dict(torch.load(model_path))
model.eval() # 必须调用model.eval()来设置dropout和batch normalization layers在运行推理前,切换到评估模式。
head.set_dropblock(is_test=True)
_decode = Decode(model, class_names, use_gpu, cfg, for_test=False)
box_ap = eval(_decode, images, eval_pre_path, anno_file, eval_batch_size, _clsid2catid, draw_image, draw_thresh)