-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_dist_error.py
executable file
·161 lines (134 loc) · 5 KB
/
gen_dist_error.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
#!/usr/bin/env python
"""Compute horizontal and vertical 3D localization errors."""
from query_utils import precision_recall, overlap
import scores
import logging
import numpy
from nyc3dcars import SESSION, Detection, Vehicle, Photo, Model
import argparse
from sqlalchemy import func, desc, and_
def get_labels(session, score, detection_filters, vehicle_filters,
model, threshold):
"""Retrieves all possible detection-annotation pairings
that satify the VOC criterion."""
overlap_score = overlap(Detection, Vehicle)
# pylint: disable-msg=E1101
dist_x = (func.ST_X(func.ST_Transform(Detection.lla, 102718))
- func.ST_X(func.ST_Transform(Vehicle.lla, 102718))) \
* 0.3048
dist_y = (func.ST_Y(func.ST_Transform(Detection.lla, 102718))
- func.ST_Y(func.ST_Transform(Vehicle.lla, 102718))) \
* 0.3048
dist = func.sqrt(dist_x * dist_x + dist_y * dist_y)
height_diff = func.abs(
func.ST_Z(Detection.lla) - func.ST_Z(Vehicle.lla))
labels = session.query(
overlap_score.label('overlap'),
Vehicle.id.label('vid'),
Detection.id.label('did'),
dist.label('dist'),
height_diff.label('height_diff'),
score.label('score')) \
.select_from(Detection) \
.join(Photo) \
.join(Vehicle) \
.join(Model) \
.filter(Model.filename == model) \
.filter(Photo.test == True) \
.filter(overlap_score > 0.5) \
.filter(score > threshold)
# pylint: enable-msg=E1101
for query_filter in detection_filters:
labels = labels.filter(query_filter)
for query_filter in vehicle_filters:
labels = labels.filter(query_filter)
labels = labels.order_by(desc(overlap_score)).all()
return labels
def gen_dist_error(model, methods, dataset_id):
"""Generates the horizontal and vertical 3D error statistics."""
session = SESSION()
try:
# pylint: disable-msg=E1101
model_id, = session.query(Model.id) \
.filter_by(filename=model) \
.one()
todo, = session.query(func.count(Photo.id)) \
.outerjoin((
Detection,
and_(
Detection.pid == Photo.id,
Detection.mid == model_id
)
)) \
.filter(Photo.test == True) \
.filter(Detection.id == None) \
.filter(Photo.dataset_id == dataset_id) \
.one()
# pylint: enable-msg=E1101
if todo > 0:
msg = '%s is not ready. %d photos remaining' % (model, todo)
logging.info(msg)
return
not_ready = False
for name in methods:
nms_method = scores.METHODS[name]
# pylint: disable-msg=E1101
todo, = session.query(func.count(Detection.id)) \
.join(Model) \
.join(Photo) \
.filter(Photo.test == True) \
.filter(Model.filename == model) \
.filter(Photo.dataset_id == dataset_id) \
.filter(nms_method.output == None) \
.one()
# pylint: enable-msg=E1101
if todo > 0:
msg = '%s is not ready. %d %s NMS remaining' % (
model, todo, name)
logging.info(msg)
not_ready = True
if not_ready:
return
# pylint: disable-msg=E1101
dataset_id = [Photo.dataset_id == dataset_id]
# pylint: enable-msg=E1101
for method in methods:
nms_method = scores.METHODS[method]
selected = [nms_method.output == True]
msg = '%s method: %s' % (model, method)
logging.info(msg)
points = precision_recall(
session,
nms_method.score,
dataset_id + selected,
dataset_id, model
)
idx = numpy.abs(points[:, 0] - 0.9).argmin()
logging.info(points[idx, :])
labels = get_labels(
session, nms_method.score, selected, [], model, points[idx, 2])
dists = [l.dist for l in labels]
dist_zs = [l.height_diff for l in labels]
logging.info(
(numpy.array(dists).mean(), numpy.array(dist_zs).mean()))
logging.info('done')
except:
raise
finally:
session.close()
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
PARSER = argparse.ArgumentParser()
PARSER.add_argument('--model', required=True)
PARSER.add_argument('--dataset-id', required=True, type=int)
PARSER.add_argument('--methods', nargs='+',
default=['reference', 'coverage',
'horizon', 'height2',
'angle2', 'all2']
)
ARGS = PARSER.parse_args()
gen_dist_error(
model=ARGS.model,
methods=ARGS.methods,
dataset_id=ARGS.dataset_id,
)