forked from cjf8899/SSD_ResNet_Pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.py
154 lines (103 loc) · 4.26 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from voc_dataset import VOCDetection
from config import opt
import numpy as np
from lib.res_model import RES18_SSD, RES101_SSD
from lib.vgg_model import VGG_SSD
import torch
import torch.nn.functional as F
import os
from lib.multibox_encoder import MultiBoxEncoder
from lib.ssd_loss import MultiBoxLoss
import cv2
from lib.utils import nms
from lib.augmentations import preproc_for_test
import matplotlib.pyplot as plt
from lib.utils import detect
from voc_dataset import VOC_LABELS
import tqdm
import os
import argparse
import torch.nn as nn
from lib.voc_eval import voc_eval
from lib.utils import detection_collate
from collections import OrderedDict
parser = argparse.ArgumentParser()
parser.add_argument('--model',
default='./weights/loss-646.26.pth',
type=str,
help='model checkpoint used to eval VOC dataset')
parser.add_argument('--save_folder',
default='result',
type=str,
help='eval result save folder')
args = parser.parse_args()
output_dir = args.save_folder
checkpoint = args.model
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
voc_root = opt.VOC_ROOT
annopath = os.path.join(voc_root, 'VOCtest2007', 'VOC2007', 'Annotations', "%s.xml")
imgpath = os.path.join(voc_root, 'VOCtest2007', 'VOC2007', 'JPEGImages', '%s.jpg')
imgsetpath = os.path.join(voc_root, 'VOCtest2007', 'VOC2007', 'ImageSets', 'Main', '{:s}.txt')
cachedir = os.path.join( os.getcwd(), 'annotations_cache')
if __name__ == '__main__':
print('using {} to eval, use cpu may take an hour to complete !!'.format(device))
# model = RES18_SSD(opt.num_classes, opt.anchor_num, pretrain=False)
model = RES101_SSD(opt.num_classes, opt.anchor_num, pretrain=False)
# model = VGG_SSD(opt.num_classes, opt.anchor_num)
print('loading checkpoint from {}'.format(checkpoint))
state_dict = torch.load(checkpoint, map_location=None if torch.cuda.is_available() else 'cpu')
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[7:] # remove `module.`
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
model.to(device)
model = nn.DataParallel(model)
print('model loaded')
model.eval()
multibox_encoder = MultiBoxEncoder(opt)
image_sets = [['2007', 'test']]
test_dataset = VOCDetection(opt, image_sets=image_sets, is_train=False)
os.makedirs(output_dir, exist_ok=True)
files = [
open(
os.path.join(
output_dir, '{:s}.txt'.format(label)),
mode='w')
for label in VOC_LABELS]
print('start detect.........')
for i in tqdm.tqdm(range(len(test_dataset))):
src = test_dataset[i][0]
img_name = os.path.basename(test_dataset.ids[i][0]).split('.')[0]
image = preproc_for_test(src, opt.min_size, opt.mean)
image = torch.from_numpy(image).to(device)
with torch.no_grad():
loc, conf = model(image.unsqueeze(0))
loc = loc[0]
conf = conf[0]
conf = F.softmax(conf, dim=1)
conf = conf.cpu().numpy()
loc = loc.cpu().numpy()
decode_loc = multibox_encoder.decode(loc)
gt_boxes, gt_confs, gt_labels = detect(decode_loc, conf, nms_threshold=0.5, gt_threshold=0.01)
#no object detected
if len(gt_boxes) == 0:
continue
h, w = src.shape[:2]
gt_boxes[:, 0] = gt_boxes[:, 0] * w
gt_boxes[:, 1] = gt_boxes[:, 1] * h
gt_boxes[:, 2] = gt_boxes[:, 2] * w
gt_boxes[:, 3] = gt_boxes[:, 3] * h
for box, label, score in zip(gt_boxes, gt_labels, gt_confs):
print(img_name, "{:.3f}".format(score), "{:.1f} {:.1f} {:.1f} {:.1f}".format(*box), file=files[label])
for f in files:
f.close()
print('start cal MAP.........')
aps = []
for f in os.listdir(output_dir):
filename = os.path.join(output_dir, f)
class_name = f.split('.')[0]
rec, prec, ap = voc_eval(filename, annopath, imgsetpath.format('test'), class_name, cachedir, ovthresh=0.1, use_07_metric=True)
print(class_name, ap)
aps.append(ap)
print('mean MAP is : ', np.mean(aps))