forked from PaulKMueller/llama_traffic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrag_retriever.py
266 lines (226 loc) · 9.06 KB
/
trag_retriever.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import numpy as np
import json
from npz_utils import list_vehicle_files_absolute
import torch
from uae_explore import encode_with_uae
from sklearn.metrics import roc_curve, roc_auc_score
import matplotlib.pyplot as plt
class TRAGRetriever:
def __init__(self):
self.direction_index_mapping = {
"Left": 0,
"Right": 1,
"Stationary": 2,
"Straight": 3,
"Straight-Left": 4,
"Straight-Right": 5,
"Right-U-Turn": 6,
"Left-U-Turn": 7,
}
self.index_direction_mapping = {
0: "Left",
1: "Right",
2: "Stationary",
3: "Straight",
4: "Straight-Left",
5: "Straight-Right",
6: "Right-U-Turn",
7: "Left-U-Turn",
}
print("Loading bucket embedding cache...")
with open("datasets/uae_buckets_cache.json") as bucket_cache:
self.bucket_embedding_cache = json.load(bucket_cache)
print("Finished")
print("Loading Synonym embedding cache...")
with open("datasets/synonyms_uae_cache.json") as synonym_cache:
self.synonym_embedding_cache = json.load(synonym_cache)
print("Finished")
print("Loading trajectory encoder output...")
self.encoded_trajectories_indirect = np.load(
"datasets/encoder_output_a_mse.npy"
)
print("Finished")
with open("datasets/synonym_bucket_mapping.json") as synonym_bucket_mapping:
self.synonym_bucket_mapping = json.load(synonym_bucket_mapping)
print("Loading trajectory buckets...")
self.trajectory_buckets = np.load("datasets/raw_direction_labels.npy")
print("Finished")
print("Loading direct encoded trajectory buckets...")
self.encoded_trajectories_direct = np.zeros(
(self.trajectory_buckets.size, 1024)
)
for i, index in enumerate(self.trajectory_buckets):
self.encoded_trajectories_direct[i] = (
self.get_bucket_encoding_for_direction_index(index)
)
print(self.encoded_trajectories_direct.shape)
print("Finished")
self.vehicle_list = list_vehicle_files_absolute()
def retrieve_trajectory_direct(self, user_input: str, k: int = 1):
embedded_user_input = torch.Tensor(encode_with_uae(user_input))
cos_sim = torch.nn.CosineSimilarity()
similarities = cos_sim(
torch.Tensor(self.encoded_trajectories_direct), embedded_user_input
)
torch.clamp(similarities, min=-1, max=1)
values, indices = torch.topk(similarities, 10)
vehicles = [self.get_vehicle_for_index(index) for index in indices]
values_list = values.tolist()
indices_list = indices.tolist()
# Print the values and indices
# print("Indices of top 10 values:", indices_list)
# print("Top 10 values:", values_list)
# print("Vehicles:", vehicles)
# print(similarities.shape)
return values, indices, vehicles
def retrieve_trajectory_indirect(self, user_input: str, k: int = 1):
embedded_user_input = torch.Tensor(encode_with_uae(user_input))
cos_sim = torch.nn.CosineSimilarity()
similarities = cos_sim(
torch.Tensor(self.encoded_trajectories_indirect), embedded_user_input
)
torch.clamp(similarities, min=-1, max=1)
values, indices = torch.topk(similarities, k)
vehicles = np.array([self.get_vehicle_for_index(index) for index in indices])
return values, indices, vehicles
def retrieve_scenario_direct(self, user_input: str, k: int = 1):
pass
def get_vehicle_for_index(self, index: int):
return self.vehicle_list[index].split("/")[-1]
def get_bucket_encoding_for_direction_index(self, index: int):
return np.array(
self.bucket_embedding_cache[self.index_direction_mapping[index]]
)
def benchmark_indirect_trajectory_retrieval(self):
occurences = {
"Left": 79230,
"Right": 68302,
"Stationary": 26222,
"Straight": 221035,
"Straight-Left": 35270,
"Straight-Right": 34643,
"Left-U-Turn": 2787,
"Right-U-Turn": 619,
}
cos_sim = torch.nn.CosineSimilarity()
for key in list(self.synonym_embedding_cache.keys()):
correct_bucket = self.synonym_bucket_mapping[key]
occurence = occurences[self.index_direction_mapping[correct_bucket]]
embedded_user_input = torch.Tensor(self.synonym_embedding_cache[key])
similarities = cos_sim(
torch.Tensor(self.encoded_trajectories_indirect), embedded_user_input
)
torch.clamp(similarities, min=-1, max=1)
values, indices = torch.topk(similarities, 468108)
vehicles = np.array(
[self.get_vehicle_for_index(index) for index in indices]
)
correct = 0
for i in indices[:occurence]:
if correct_bucket == self.trajectory_buckets[i]:
correct += 1
print(key)
print(correct / occurence)
def collect_scores_and_labels(self, key):
scores = []
labels = []
cos_sim = torch.nn.CosineSimilarity()
occurences = {
"Left": 79230,
"Right": 68302,
"Stationary": 26222,
"Straight": 221035,
"Straight-Left": 35270,
"Straight-Right": 34643,
"Left-U-Turn": 2787,
"Right-U-Turn": 619,
}
correct_bucket = self.synonym_bucket_mapping[key]
occurence = occurences[self.index_direction_mapping[correct_bucket]]
embedded_user_input = torch.Tensor(self.synonym_embedding_cache[key])
similarities = cos_sim(
torch.Tensor(self.encoded_trajectories_indirect), embedded_user_input
)
similarities = torch.clamp(similarities, min=-1, max=1)
_, indices = torch.topk(similarities, occurence)
preds = np.ones(occurence)
labels = np.array(self.trajectory_buckets[indices] == correct_bucket)
print(key)
print(f"Accuracy: {np.count_nonzero(labels)/occurence}")
# print(labels)
return len(labels), labels
def plot_roc_and_calculate_auc(self):
for key in list(self.synonym_embedding_cache.keys()):
occurence, labels = self.collect_scores_and_labels(key)
# Convert lists to numpy arrays for compatibility with scikit-learn functions
# scores = np.array(scores)
# print(scores[:10])
# labels = np.array(labels)
x, y = 0, 0
points_x = [x]
points_y = [y]
# Iterate through the boolean array
for value in labels:
if value:
y += 1 # Move up for True
else:
x += 1 # Move right for False
points_x.append(x)
points_y.append(y)
points_x = np.array(points_x) / (labels.shape[0] - np.count_nonzero(labels))
points_y = np.array(points_y) / np.count_nonzero(labels)
auc = np.trapz(points_y, points_x)
# Plot the ROC curve
plt.figure()
plt.plot(
points_x,
points_y,
color="darkorange",
lw=2,
label="ROC curve (area = %0.2f)" % auc,
)
plt.plot([0, 1], [0, 1], color="navy", lw=2, linestyle="--")
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.title(f"Receiver Operating Characteristic ({key})")
plt.legend(loc="lower right")
plt.savefig(f"output/roc_{key}.png")
# retriever = TRAGRetriever()
# print(retriever.retrieve_trajectory_direct("Turn right")[1])
# print(retriever.retrieve_trajectory_indirect("Turn right")[1])
# retriever.benchmark_indirect_trajectory_retrieval()
# retriever.plot_roc_and_calculate_auc()
with open("datasets/results_indirect_retrieval.json") as results_indirect:
results = json.load(results_indirect)
with open("datasets/synonym_bucket_mapping.json") as synonym_bucket_mapping:
mapping = json.load(synonym_bucket_mapping)
direction_index_mapping = {
"Left": 0,
"Right": 1,
"Stationary": 2,
"Straight": 3,
"Straight-Left": 4,
"Straight-Right": 5,
"Right-U-Turn": 6,
"Left-U-Turn": 7,
}
accuracies = {}
output = {}
keys = list(direction_index_mapping.keys())
for key in keys:
accuracies = []
bucket_number = direction_index_mapping[key]
sum = 0
for entry in results.items():
synonym, accuracy = entry
if mapping[synonym] == bucket_number:
accuracies.append(accuracy)
output[key] = accuracies
sum += accuracy
mean = np.array(output[key]).mean()
std_dev = np.array(output[key]).std()
print(key)
print(mean)
print(std_dev)