-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnavid_agent.py
347 lines (234 loc) · 11.8 KB
/
navid_agent.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import json
import numpy as np
from habitat import Env
from habitat.core.agent import Agent
from tqdm import trange
import os
import re
import torch
import cv2
import imageio
from habitat.utils.visualizations import maps
import random
from navid.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN
from navid.conversation import conv_templates, SeparatorStyle
from navid.model.builder import load_pretrained_model
from navid.mm_utils import tokenizer_image_token, get_model_name_from_path, KeywordsStoppingCriteria
def evaluate_agent(config, split_id, dataset, model_path, result_path) -> None:
env = Env(config.TASK_CONFIG, dataset)
agent = NaVid_Agent(model_path, result_path)
num_episodes = len(env.episodes)
EARLY_STOP_ROTATION = config.EVAL.EARLY_STOP_ROTATION
EARLY_STOP_STEPS = config.EVAL.EARLY_STOP_STEPS
target_key = {"distance_to_goal", "success", "spl", "path_length", "oracle_success"}
count = 0
for _ in trange(num_episodes, desc=config.EVAL.IDENTIFICATION+"-{}".format(split_id)):
obs = env.reset()
iter_step = 0
agent.reset()
continuse_rotation_count = 0
last_dtg = 999
while not env.episode_over:
info = env.get_metrics()
if info["distance_to_goal"] != last_dtg:
last_dtg = info["distance_to_goal"]
continuse_rotation_count=0
else :
continuse_rotation_count +=1
action = agent.act(obs, info, env.current_episode.episode_id)
if continuse_rotation_count > EARLY_STOP_ROTATION or iter_step>EARLY_STOP_STEPS:
action = {"action": 0}
iter_step+=1
obs = env.step(action)
info = env.get_metrics()
result_dict = dict()
result_dict = {k: info[k] for k in target_key if k in info}
result_dict["id"] = env.current_episode.episode_id
count+=1
with open(os.path.join(os.path.join(result_path, "log"),"stats_{}.json".format(env.current_episode.episode_id)), "w") as f:
json.dump(result_dict, f, indent=4)
class NaVid_Agent(Agent):
def __init__(self, model_path, result_path, require_map=True):
print("Initialize NaVid")
self.result_path = result_path
self.require_map = require_map
self.conv_mode = "vicuna_v1"
os.makedirs(self.result_path, exist_ok=True)
os.makedirs(os.path.join(self.result_path, "log"), exist_ok=True)
os.makedirs(os.path.join(self.result_path, "video"), exist_ok=True)
self.model_name = get_model_name_from_path(model_path)
self.tokenizer, self.model, self.image_processor, self.context_len = load_pretrained_model(model_path, None, get_model_name_from_path(model_path))
print("Initialization Complete")
self.promt_template = "Imagine you are a robot programmed for navigation tasks. You have been given a video of historical observations and an image of the current observation <image>. Your assigned task is: '{}'. Analyze this series of images to decide your next move, which could involve turning left or right by a specific degree or moving forward a certain distance."
self.rgb_list = []
self.topdown_map_list = []
self.count_id = 0
self.reset()
def process_images(self, rgb_list):
batch_image = np.asarray(rgb_list)
video = self.image_processor.preprocess(batch_image, return_tensors='pt')['pixel_values'].half().cuda()
return [video]
def predict_inference(self, prompt):
question = prompt.replace(DEFAULT_IMAGE_TOKEN, '').replace('\n', '')
qs = prompt
VIDEO_START_SPECIAL_TOKEN = "<video_special>"
VIDEO_END_SPECIAL_TOKEN = "</video_special>"
IMAGE_START_TOKEN = "<image_special>"
IMAGE_END_TOKEN = "</image_special>"
NAVIGATION_SPECIAL_TOKEN = "[Navigation]"
IAMGE_SEPARATOR = "<image_sep>"
image_start_special_token = self.tokenizer(IMAGE_START_TOKEN, return_tensors="pt").input_ids[0][1:].cuda()
image_end_special_token = self.tokenizer(IMAGE_END_TOKEN, return_tensors="pt").input_ids[0][1:].cuda()
video_start_special_token = self.tokenizer(VIDEO_START_SPECIAL_TOKEN, return_tensors="pt").input_ids[0][1:].cuda()
video_end_special_token = self.tokenizer(VIDEO_END_SPECIAL_TOKEN, return_tensors="pt").input_ids[0][1:].cuda()
navigation_special_token = self.tokenizer(NAVIGATION_SPECIAL_TOKEN, return_tensors="pt").input_ids[0][1:].cuda()
image_seperator = self.tokenizer(IAMGE_SEPARATOR, return_tensors="pt").input_ids[0][1:].cuda()
if self.model.config.mm_use_im_start_end:
qs = DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_TOKEN + DEFAULT_IM_END_TOKEN + '\n' + qs.replace('<image>', '')
else:
qs = DEFAULT_IMAGE_TOKEN + '\n' + qs.replace('<image>', '')
conv = conv_templates[self.conv_mode].copy()
conv.append_message(conv.roles[0], qs)
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
token_prompt = tokenizer_image_token(prompt, self.tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt').cuda()
indices_to_replace = torch.where(token_prompt == -200)[0]
new_list = []
while indices_to_replace.numel() > 0:
idx = indices_to_replace[0]
new_list.append(token_prompt[:idx])
new_list.append(video_start_special_token)
new_list.append(image_seperator)
new_list.append(token_prompt[idx:idx + 1])
new_list.append(video_end_special_token)
new_list.append(image_start_special_token)
new_list.append(image_end_special_token)
new_list.append(navigation_special_token)
token_prompt = token_prompt[idx + 1:]
indices_to_replace = torch.where(token_prompt == -200)[0]
if token_prompt.numel() > 0:
new_list.append(token_prompt)
input_ids = torch.cat(new_list, dim=0).unsqueeze(0)
stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
keywords = [stop_str]
stopping_criteria = KeywordsStoppingCriteria(keywords, self.tokenizer, input_ids)
imgs = self.process_images(self.rgb_list)
cur_prompt = question
with torch.inference_mode():
self.model.update_prompt([[cur_prompt]])
output_ids = self.model.generate(
input_ids,
images=imgs,
do_sample=True,
temperature=0.2,
max_new_tokens=1024,
use_cache=True,
stopping_criteria=[stopping_criteria])
input_token_len = input_ids.shape[1]
n_diff_input_output = (input_ids != output_ids[:, :input_token_len]).sum().item()
if n_diff_input_output > 0:
print(f'[Warning] {n_diff_input_output} output_ids are not the same as the input_ids')
outputs = self.tokenizer.batch_decode(output_ids[:, input_token_len:], skip_special_tokens=True)[0]
outputs = outputs.strip()
if outputs.endswith(stop_str):
outputs = outputs[:-len(stop_str)]
outputs = outputs.strip()
return outputs
def extract_result(self, output):
# id: 0-stop, 1 move forward, 2 turn left, 3 turn right
if "stop" in output:
return 0, None
elif "forward" in output:
match = re.search(r'-?\d+', output)
if match is None:
return None, None
match = match.group()
return 1, float(match)
elif "left" in output:
match = re.search(r'-?\d+', output)
if match is None:
return None, None
match = match.group()
return 2, float(match)
elif "right" in output:
match = re.search(r'-?\d+', output)
if match is None:
return None, None
match = match.group()
return 3, float(match)
return None, None
def addtext(self, image, instuction, navigation):
h, w = image.shape[:2]
new_height = h + 150
new_image = np.zeros((new_height, w, 3), np.uint8)
new_image.fill(255)
new_image[:h, :w] = image
font = cv2.FONT_HERSHEY_SIMPLEX
textsize = cv2.getTextSize(instuction, font, 0.5, 2)[0]
textY = h + (50 + textsize[1]) // 2
y_line = textY + 0 * textsize[1]
words = instuction.split(' ')
max_width = new_image.shape[1]
x = 10
line = ""
for word in words:
test_line = line + ' ' + word if line else word
test_line_size, _ = cv2.getTextSize(test_line, font, 0.5, 2)
if test_line_size[0] > image.shape[1] - x:
cv2.putText(new_image, line, (x, y_line ), font, 0.5, (0, 0, 0), 2)
line = word
y_line += textsize[1]+5
else:
line = test_line
if line:
cv2.putText(new_image, line, (x, y_line), font, 0.5, (0, 0, 0), 2)
y_line = y_line + 1 * textsize[1] + 10
new_image = cv2.putText(new_image, navigation, (x, y_line), font, 0.5, (0, 0, 0), 2)
return new_image
def reset(self):
if self.require_map:
if len(self.topdown_map_list)!=0:
output_video_path = os.path.join(self.result_path, "video","{}.gif".format(self.episode_id))
imageio.mimsave(output_video_path, self.topdown_map_list)
self.transformation_list = []
self.rgb_list = []
self.topdown_map_list = []
self.last_action = None
self.count_id += 1
self.count_stop = 0
self.pending_action_list = []
self.first_forward = False
def act(self, observations, info, episode_id):
self.episode_id = episode_id
rgb = observations["rgb"]
self.rgb_list.append(rgb)
if self.require_map:
top_down_map = maps.colorize_draw_agent_and_fit_to_height(info["top_down_map_vlnce"], rgb.shape[0])
output_im = np.concatenate((rgb, top_down_map), axis=1)
if len(self.pending_action_list) != 0 :
temp_action = self.pending_action_list.pop(0)
if self.require_map:
img = self.addtext(output_im, observations["instruction"]["text"], "Pending action: {}".format(temp_action))
self.topdown_map_list.append(img)
return {"action": temp_action}
navigation_qs = self.promt_template.format(observations["instruction"]["text"])
navigation = self.predict_inference(navigation_qs)
if self.require_map:
img = self.addtext(output_im, observations["instruction"]["text"], navigation)
self.topdown_map_list.append(img)
action_index, num = self.extract_result(navigation[:-1])
if action_index == 0:
self.pending_action_list.append(0)
elif action_index == 1:
for _ in range(min(3, int(num/25))):
self.pending_action_list.append(1)
elif action_index == 2:
for _ in range(min(3,int(num/30))):
self.pending_action_list.append(2)
elif action_index == 3:
for _ in range(min(3,int(num/30))):
self.pending_action_list.append(3)
if action_index is None or len(self.pending_action_list)==0:
self.pending_action_list.append(random.randint(1, 3))
# Primarily unused, intended to complete the pipeline logic.
return {"action": self.pending_action_list.pop(0)}