-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_coco2.py
161 lines (119 loc) · 5.6 KB
/
eval_coco2.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
151
152
153
154
155
156
157
158
159
160
161
from utils import *
from datasets import *
from torchcv.datasets.transforms import *
import torch.nn.functional as F
from tqdm import tqdm
from pprint import PrettyPrinter
import torch
import torch.utils.data as data
import json
import os
import os.path
import sys
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont
from utils import *
if sys.version_info[0] == 2:
import xml.etree.cElementTree as ET
else:
import xml.etree.ElementTree as ET
import pdb
from collections import namedtuple
from torchcv.utils import Timer, kaist_results_file as write_result, write_coco_format as write_result_coco
### Evaluation
from torchcv.evaluations.coco import COCO
from torchcv.evaluations.eval_MR_multisetup import COCOeval
annType = 'bbox'
DB_ROOT = './kaist-rgbt'
JSON_GT_FILE = os.path.join( DB_ROOT, 'kaist_annotations_test20.json' )
cocoGt = COCO(JSON_GT_FILE)
# Parameters
data_folder = './kaist-rgbt/'
batch_size = 4
workers = 4
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
for i in range(24,27):
path=os.path.join('./jobs/2020-05-21_14h19m_spec+eff+norm+fpn','checkpoint_ssd300.pth.tar0'+str(i))
checkpoint_root = './jobs/2020-05-21_14h19m_spec+eff+norm+fpn'
checkpoint = path
checkpoint_name = 'checkpoint_ssd300.pth.tar0'+str(i)
input_size = [512., 640.]
# Load model checkpoint that is to be evaluated
checkpoint = torch.load(checkpoint)
model = checkpoint['model']
model = model.to(device)
# Switch to eval mode
model.eval()
# Load test data
preprocess1 = Compose([ ])
transforms1 = Compose([ \
ToTensor(), \
Normalize([0.3465,0.3219,0.2842], [0.2358,0.2265,0.2274], 'R'), \
#Normalize([0.1598], [0.0813], 'T')
])
test_dataset = KAISTPed('test-all-20.txt',img_transform=preprocess1, co_transform=transforms1)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False,
num_workers=workers,
collate_fn=test_dataset.collate_fn,
pin_memory=True)
def evaluate_coco(test_loader, model):
"""
Evaluate.
:param test_loader: DataLoader for test data
:param model: model
"""
fig_test, ax_test = plt.subplots(figsize=(18,15))
# Make sure it's in eval mode
model.eval()
# Lists to store detected and true boxes, labels, scores
det_boxes = list()
det_labels = list()
det_scores = list()
true_boxes = list()
true_labels = list()
#For CoCo
results = []
with torch.no_grad():
# Batches
for i, (images, boxes, labels, index) in enumerate(tqdm(test_loader, desc='Evaluating')):
images = images.to(device)
# Forward prop.
predicted_locs, predicted_scores = model(images)
# Detect objects in SSD output
det_boxes_batch, det_labels_batch, det_scores_batch = model.detect_objects(predicted_locs, predicted_scores,
min_score=0.1, max_overlap=0.45,
top_k=50)
# Evaluation MUST be at min_score=0.01, max_overlap=0.45, top_k=200 for fair comparision with the paper's results and other repos
# Store this batch's results for mAP calculation
boxes = [b.to(device) for b in boxes]
labels = [l.to(device) for l in labels]
for box_t, label_t, score_t, ids in zip(det_boxes_batch ,det_labels_batch, det_scores_batch, index):
for box, label, score in zip(box_t, label_t, score_t) :
bb = box.cpu().numpy().tolist()
# if score.item() > 0.1 :
results.append( {\
'image_id': ids.item(), \
'category_id': label.item(), \
'bbox': [bb[0]*input_size[1], bb[1]*input_size[0], (bb[2]-bb[0])*input_size[1], (bb[3]-bb[1])*input_size[0]], \
'score': score.item()} )
rstFile = os.path.join(checkpoint_root, './COCO_TEST_det_{:s}.json'.format(checkpoint_name))
write_result_coco(results, rstFile)
# rstFile = os.path.join('./jobs/2019-03-26_16h07m_[SSDPed_512x640][KAISTPed_train-all-02]video_make_test_full/SSDPed_512x640_epoch_0022_det.json')
try:
cocoDt = cocoGt.loadRes(rstFile)
imgIds = sorted(cocoGt.getImgIds())
cocoEval = COCOeval(cocoGt,cocoDt,annType)
cocoEval.params.imgIds = imgIds
cocoEval.params.catIds = [1]
cocoEval.evaluate(0)
cocoEval.accumulate()
curPerf = cocoEval.summarize(0)
cocoEval.draw_figure(ax_test, rstFile.replace('json', 'jpg'))
#writer.add_scalars('LAMR/fppi', {'test': curPerf}, epoch)
print('Recall: {:}'.format( 1-cocoEval.eval['yy'][0][-1] ) )
except:
import torchcv.utils.trace_error
print('[Error] cannot evaluate by cocoEval. ')
if __name__ == '__main__':
evaluate_coco(test_loader, model)