-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathutils.py
More file actions
274 lines (236 loc) · 9.66 KB
/
utils.py
File metadata and controls
274 lines (236 loc) · 9.66 KB
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
from collections import Counter
import contextlib
import json
import logging
import os
from pathlib import Path
from pydoc import locate
from calvin_agent.models.mcil import MCIL
from calvin_agent.utils.utils import add_text, format_sftp_path
import cv2
import hydra
import numpy as np
from numpy import pi
from omegaconf import OmegaConf
import mmh3
import torch
logger = logging.getLogger(__name__)
def get_default_model_and_env(train_folder, dataset_path, checkpoint, env=None, device_id=0):
train_cfg_path = Path(train_folder) / ".hydra/config.yaml"
train_cfg_path = format_sftp_path(train_cfg_path)
cfg = OmegaConf.load(train_cfg_path)
lang_folder = cfg.datamodule.datasets.lang_dataset.lang_folder
if not hydra.core.global_hydra.GlobalHydra.instance().is_initialized():
hydra.initialize("../../conf/datamodule/datasets")
# we don't want to use shm dataset for evaluation
datasets_cfg = hydra.compose("vision_lang.yaml", overrides=["lang_dataset.lang_folder=" + lang_folder])
# since we don't use the trainer during inference, manually set up data_module
cfg.datamodule.datasets = datasets_cfg
cfg.datamodule.root_data_dir = dataset_path
data_module = hydra.utils.instantiate(cfg.datamodule, num_workers=0)
data_module.prepare_data()
data_module.setup()
dataloader = data_module.val_dataloader()
dataset = dataloader.dataset.datasets["lang"]
device = torch.device(f"cuda:{device_id}")
if env is None:
rollout_cfg = OmegaConf.load(Path(__file__).parents[2] / "conf/callbacks/rollout/default.yaml")
env = hydra.utils.instantiate(rollout_cfg.env_cfg, dataset, device, show_gui=False)
checkpoint = format_sftp_path(checkpoint)
print(f"Loading model from {checkpoint}")
# import the model class that was used for the training
model_cls = locate(cfg.model._target_)
model = model_cls.load_from_checkpoint(checkpoint)
model.load_lang_embeddings(dataset.abs_datasets_dir / dataset.lang_folder / "embeddings.npy")
model.freeze()
if cfg.model.action_decoder.get("load_action_bounds", False):
model.action_decoder._setup_action_bounds(cfg.datamodule.root_data_dir, None, None, True)
model = model.cuda(device)
print("Successfully loaded model.")
return model, env, data_module
def collect_plan(model, plans, subtask):
try:
plans[subtask].append((model.plan.cpu(), model.latent_goal.cpu()))
except AttributeError:
return
def join_vis_lang(img, lang_text):
"""Takes as input an image and a language instruction and visualizes them with cv2"""
img = img[:, :, ::-1].copy()
img = cv2.resize(img, (500, 500))
add_text(img, lang_text)
cv2.imshow("simulation cam", img)
cv2.waitKey(1)
def count_success(results):
count = Counter(results)
step_success = []
for i in range(1, 6):
n_success = sum(count[j] for j in reversed(range(i, 6)))
sr = n_success / len(results)
step_success.append(sr)
return step_success
def print_and_save(results, sequences, log_dir, epoch=None):
current_data = {}
print(f"Results for Epoch {epoch}:")
avg_seq_len = np.mean(results)
chain_sr = {i + 1: sr for i, sr in enumerate(count_success(results))}
print(f"Average successful sequence length: {avg_seq_len}")
print("Success rates for i instructions in a row:")
for i, sr in chain_sr.items():
print(f"{i}: {sr * 100:.1f}%")
cnt_success = Counter()
cnt_fail = Counter()
for result, (_, sequence) in zip(results, sequences):
for successful_tasks in sequence[:result]:
cnt_success[successful_tasks] += 1
if result < len(sequence):
failed_task = sequence[result]
cnt_fail[failed_task] += 1
total = cnt_success + cnt_fail
task_info = {}
for task in total:
task_info[task] = {"success": cnt_success[task], "total": total[task]}
print(f"{task}: {cnt_success[task]} / {total[task]} | SR: {cnt_success[task] / total[task] * 100:.1f}%")
data = {"avg_seq_len": avg_seq_len, "chain_sr": chain_sr, "task_info": task_info}
current_data[epoch] = data
print()
previous_data = {}
try:
with open(log_dir / "results.json", "r") as file:
previous_data = json.load(file)
except FileNotFoundError:
pass
json_data = {**previous_data, **current_data}
with open(log_dir / "results.json", "w") as file:
json.dump(json_data, file)
print(
f"Best model: epoch {max(json_data, key=lambda x: json_data[x]['avg_seq_len'])} "
f"with average sequences length of {max(map(lambda x: x['avg_seq_len'], json_data.values()))}"
)
def create_tsne(plan_dict, log_dir, epoch):
ids, labels, plans, latent_goals = zip(
*[
(i, label, latent_goal, plan)
for i, (label, plan_list) in enumerate(plan_dict.items())
for latent_goal, plan in plan_list
]
)
latent_goals = torch.cat(latent_goals)
plans = torch.cat(plans)
np.savez(f"{log_dir / f'tsne_data_{epoch}.npz'}", ids=ids, labels=labels, plans=plans, latent_goals=latent_goals)
def get_log_dir(log_dir):
if log_dir is not None:
log_dir = Path(log_dir)
os.makedirs(log_dir, exist_ok=True)
else:
log_dir = Path(__file__).parents[3] / "evaluation"
if not log_dir.exists():
log_dir = Path("/tmp/evaluation")
os.makedirs(log_dir, exist_ok=True)
print(f"logging to {log_dir}")
return log_dir
def imshow_tensor(window, img_tensor, wait=0, resize=True, keypoints=None, text=None):
img_tensor = img_tensor.squeeze()
img = np.transpose(img_tensor.cpu().numpy(), (1, 2, 0))
img = np.clip(((img / 2) + 0.5) * 255, 0, 255).astype(np.uint8)
if keypoints is not None:
key_coords = np.clip(keypoints * 200 + 100, 0, 200)
key_coords = key_coords.reshape(-1, 2)
cv_kp1 = [cv2.KeyPoint(x=pt[1], y=pt[0], _size=1) for pt in key_coords]
img = cv2.drawKeypoints(img, cv_kp1, None, color=(255, 0, 0))
if text is not None:
add_text(img, text)
if resize:
cv2.imshow(window, cv2.resize(img[:, :, ::-1], (500, 500)))
else:
cv2.imshow(window, img[:, :, ::-1])
cv2.waitKey(wait)
def print_task_log(demo_task_counter, live_task_counter, mod):
print()
logger.info(f"Modality: {mod}")
for task in demo_task_counter:
logger.info(
f"{task}: SR = {(live_task_counter[task] / demo_task_counter[task]) * 100:.0f}%"
+ f" | {live_task_counter[task]} of {demo_task_counter[task]}"
)
logger.info(
f"Average Success Rate {mod} = "
+ f"{(sum(live_task_counter.values()) / s if (s := sum(demo_task_counter.values())) > 0 else 0) * 100:.0f}% "
)
logger.info(
f"Success Rates averaged throughout classes = {np.mean([live_task_counter[task] / demo_task_counter[task] for task in demo_task_counter]) * 100:.0f}%"
)
@contextlib.contextmanager
def temp_seed(seed):
state = np.random.get_state()
np.random.seed(seed)
try:
yield
finally:
np.random.set_state(state)
def get_env_state_for_initial_condition(initial_condition):
robot_obs = np.array(
[
0.02586889,
-0.2313129,
0.5712808,
3.09045411,
-0.02908596,
1.50013585,
0.07999963,
-1.21779124,
1.03987629,
2.11978254,
-2.34205014,
-0.87015899,
1.64119093,
0.55344928,
1.0,
]
)
block_rot_z_range = (pi / 2 - pi / 8, pi / 2 + pi / 8)
block_slider_left = np.array([-2.40851662e-01, 9.24044687e-02, 4.60990009e-01])
block_slider_right = np.array([7.03416330e-02, 9.24044687e-02, 4.60990009e-01])
block_table = [
np.array([5.00000896e-02, -1.20000177e-01, 4.59990009e-01]),
np.array([2.29995412e-01, -1.19995140e-01, 4.59990010e-01]),
]
# we want to have a "deterministic" random seed for each initial condition
seed = mmh3.hash(str(initial_condition.values()))
with temp_seed(seed):
np.random.shuffle(block_table)
scene_obs = np.zeros(24)
if initial_condition["slider"] == "left":
scene_obs[0] = 0.28
if initial_condition["drawer"] == "open":
scene_obs[1] = 0.22
if initial_condition["lightbulb"] == 1:
scene_obs[3] = 0.088
scene_obs[4] = initial_condition["lightbulb"]
scene_obs[5] = initial_condition["led"]
# red block
if initial_condition["red_block"] == "slider_right":
scene_obs[6:9] = block_slider_right
elif initial_condition["red_block"] == "slider_left":
scene_obs[6:9] = block_slider_left
else:
scene_obs[6:9] = block_table[0]
scene_obs[11] = np.random.uniform(*block_rot_z_range)
# blue block
if initial_condition["blue_block"] == "slider_right":
scene_obs[12:15] = block_slider_right
elif initial_condition["blue_block"] == "slider_left":
scene_obs[12:15] = block_slider_left
elif initial_condition["red_block"] == "table":
scene_obs[12:15] = block_table[1]
else:
scene_obs[12:15] = block_table[0]
scene_obs[17] = np.random.uniform(*block_rot_z_range)
# pink block
if initial_condition["pink_block"] == "slider_right":
scene_obs[18:21] = block_slider_right
elif initial_condition["pink_block"] == "slider_left":
scene_obs[18:21] = block_slider_left
else:
scene_obs[18:21] = block_table[1]
scene_obs[23] = np.random.uniform(*block_rot_z_range)
return robot_obs, scene_obs