-
Notifications
You must be signed in to change notification settings - Fork 0
/
postprocess.py
74 lines (56 loc) · 2.17 KB
/
postprocess.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
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Evaluation for retinanet"""
import os
import numpy as np
from PIL import Image
from src.coco_eval import metrics
from src.model_utils.config import config
def get_pred(result_path, img_id):
boxes_file = os.path.join(result_path, img_id + '_0.bin')
scores_file = os.path.join(result_path, img_id + '_1.bin')
boxes = np.fromfile(boxes_file, dtype=np.float32).reshape(67995, 4)
scores = np.fromfile(scores_file, dtype=np.float32).reshape(67995, config.num_classes)
return boxes, scores
def get_img_size(file_name):
img = Image.open(file_name)
return img.size
def get_img_id(img_id_file):
f = open(img_id_file)
lines = f.readlines()
ids = []
for line in lines:
ids.append(int(line))
return ids
def cal_acc(result_path, img_path, img_id_file):
"""Calculate acc"""
ids = get_img_id(img_id_file)
imgs = os.listdir(img_path)
pred_data = []
for img in imgs:
img_id = img.split('.')[0]
if int(img_id) not in ids:
continue
boxes, box_scores = get_pred(result_path, img_id)
w, h = get_img_size(os.path.join(img_path, img))
img_shape = np.array((h, w), dtype=np.float32)
pred_data.append({"boxes": boxes,
"box_scores": box_scores,
"img_id": int(img_id),
"image_shape": img_shape})
mAP = metrics(pred_data)
print(f"mAP: {mAP}")
if __name__ == '__main__':
cal_acc(config.result_path, config.img_path, config.img_id_file)