forked from luo-yuanfu/GAMMA
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgamma_compute_accuracy.py
executable file
·183 lines (137 loc) · 5.48 KB
/
gamma_compute_accuracy.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
from __future__ import print_function
import numpy as np
import copy
import math
import glob
all_traj_total_gamma_ade = 0.0
all_traj_total_gamma_fde = 0.0
trajectory_count = 0.0
agent_id_list = []
M = [[55.8303947300378, -21.4969860492286, 1119.51180638000], [-38.7841290361716, 31.3801203371366, 694.848288076000], [0.0412819851646424, 0.0501909553699786, 1]]
M = np.array(M)
def transform_world_to_pixel(world_x_list, world_y_list):
uv_list = []
for i in range(0, len(world_x_list)):
uv = np.dot(M, np.array([world_x_list[i],world_y_list[i], 1.0]))
uv /= uv[2]
uv_list.append(uv)
return uv_list
def divide_trajectories(agent_id):
global trajectory_segment
global content
global prediction_frame_count
frame_start = -1
frame_end = -1
frame_tmp = 1
for line in content:
if "begin to predict at frame:" in line:
frame_tmp = int(line.split(' ')[5])
elif "there are" not in line:
if int(line.split(' ')[0]) == agent_id:
if frame_start == -1:
frame_start = frame_tmp
frame_end = frame_tmp
if frame_start != -1 and frame_end != -1:
while frame_start + prediction_frame_count <= frame_end:
trajectory_segment[agent_id].append(frame_start)
trajectory_segment[agent_id].append(frame_start + prediction_frame_count)
frame_start += 1 #prediction_frame_count #1
def compute_accuracy(agent_trajectories, agent_id):
traj_num = int(len(agent_trajectories) / 2)
global prediction_frame_count
total_gamma_ade = 0.0
total_gamma_fde = 0.0
start_a_new_traj = False
prediction_frame_count_itr = 0
gamma_x_list = []
gamma_y_list = []
gamma_all_x_list = []
gamma_all_y_list = []
ori_x_list = []
ori_y_list = []
ori_all_x_list = []
ori_all_y_list = []
for line in content:
if "begin to predict at frame:" in line and int(line.split(' ')[5]) > agent_trajectories[(traj_num-1)*2+1]:
if gamma_x_list != []:
gamma_all_x_list.append(gamma_x_list)
gamma_all_y_list.append(gamma_y_list)
ori_all_x_list.append(ori_x_list)
ori_all_y_list.append(ori_y_list)
gamma_x_list = []
gamma_y_list = []
ori_x_list = []
ori_y_list = []
break
elif "begin to predict at frame:" in line and int(line.split(' ')[5]) >= agent_trajectories[0]:
frame_tmp = int(line.split(' ')[5])
for i in range(traj_num):
if frame_tmp == agent_trajectories[i*2]:
start_a_new_traj = True
prediction_frame_count_itr = 0
if gamma_x_list != []:
gamma_all_x_list.append(gamma_x_list)
gamma_all_y_list.append(gamma_y_list)
gamma_x_list = []
gamma_y_list = []
ori_all_x_list.append(ori_x_list)
ori_all_y_list.append(ori_y_list)
ori_x_list = []
ori_y_list = []
break
else:
start_a_new_traj = False
prediction_frame_count_itr = 0
elif start_a_new_traj and "there are" not in line:
if agent_id == int(line.split(' ')[0]) and prediction_frame_count_itr < prediction_frame_count:
if prediction_frame_count_itr == prediction_frame_count - 1:
total_gamma_fde += dist(float(line.split(' ')[2]), float(line.split(' ')[3]), float(line.split(' ')[4]), float(line.split(' ')[5]))
gamma_x_list.append(float(line.split(' ')[2]))
gamma_y_list.append(float(line.split(' ')[3]))
ori_x_list.append(float(line.split(' ')[4]))
ori_y_list.append(float(line.split(' ')[5]))
total_gamma_ade += dist(float(line.split(' ')[2]), float(line.split(' ')[3]), float(line.split(' ')[4]), float(line.split(' ')[5]))
gamma_x_list.append(float(line.split(' ')[2]))
gamma_y_list.append(float(line.split(' ')[3]))
ori_x_list.append(float(line.split(' ')[4]))
ori_y_list.append(float(line.split(' ')[5]))
prediction_frame_count_itr += 1
if output_file_for_visualization:
for i in range(len(gamma_all_x_list)):
output_dir = "pos_in_image_space/"
file_name = "frame_"+str(agent_trajectories[0])+"_agent_"+str(agent_id)+"_"+str(i)+"_gamma.txt"
output_file = open(output_dir+file_name, "w")
output_file2 = open(output_dir+file_name[:-9]+"ori.txt", "w")
uv_list = transform_world_to_pixel(gamma_all_x_list[i], gamma_all_y_list[i])
start_frame = agent_trajectories[2*i]
for j in range(len(uv_list)):
print(agent_id, start_frame+j, uv_list[j][0], uv_list[j][1], file = output_file)
uv_list = transform_world_to_pixel(ori_all_x_list[i], ori_all_y_list[i])
start_frame = agent_trajectories[2*i]
for j in range(len(uv_list)):
print(agent_id, start_frame+j, uv_list[j][0], uv_list[j][1], file = output_file2)
output_file.close()
output_file2.close()
return traj_num, total_gamma_ade, total_gamma_fde
def dist(x1,y1,x2,y2):
return math.sqrt((x1-x2)**2+(y1-y2)**2)
total_agent_num = 450
filename = "gamma_output.txt"
file_handler = open(filename,'r')
content = file_handler.read().splitlines()
prediction_duration = 4.8
prediction_frame_count = int(prediction_duration * 2.5)
trajectory_segment = [copy.deepcopy([]) for i in range(450)]
output_file_for_visualization = False
for i in range(0, total_agent_num):
divide_trajectories(i)
for i in range(0, total_agent_num):
if trajectory_segment[i] != []:
traj_num, total_gamma_ade, total_gamma_fde = compute_accuracy(trajectory_segment[i], i)
all_traj_total_gamma_ade += total_gamma_ade
all_traj_total_gamma_fde += total_gamma_fde
trajectory_count += float(traj_num)
print ("total number of trajectories: ", trajectory_count)
print ("ade: ", all_traj_total_gamma_ade / trajectory_count / prediction_frame_count)
print ("fde: ", all_traj_total_gamma_fde / trajectory_count)
print("finished")