-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathevaluate_calvin.py
266 lines (243 loc) · 10.5 KB
/
evaluate_calvin.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
# MIT License
# Copyright (c) 2021 Oier Mees
# Copyright (c) 2024 Bytedance Ltd. and/or its affiliates
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Code to evaluate Calvin."""
import argparse
import json
import logging
import os
from pathlib import Path
import sys
import time
import copy
from moviepy.editor import ImageSequenceClip
from accelerate import Accelerator
from datetime import timedelta
from accelerate.utils import InitProcessGroupKwargs
# This is for using the locally installed repo clone when using slurm
from calvin_agent.models.calvin_base_model import CalvinBaseModel
sys.path.insert(0, Path(__file__).absolute().parents[2].as_posix())
from calvin_agent.evaluation.multistep_sequences import get_sequences
from calvin_agent.evaluation.utils import (
count_success,
get_env_state_for_initial_condition,
get_log_dir,
)
import hydra
import numpy as np
from omegaconf import OmegaConf
from pytorch_lightning import seed_everything
from termcolor import colored
import torch
from tqdm.auto import tqdm
from evaluation.calvin_evaluation import GR1CalvinEvaluation
from utils.calvin_utils import print_and_save
import clip
from PreProcess import PreProcess
import models.vision_transformer as vits
from models.gr1 import GR1
logger = logging.getLogger(__name__)
os.environ["FFMPEG_BINARY"] = "auto-detect"
CALVIN_ROOT = os.environ['CALVIN_ROOT']
def make_env(dataset_path, observation_space, device):
val_folder = Path(dataset_path) / "validation"
from evaluation.calvin_env_wrapper_raw import CalvinEnvWrapperRaw
env = CalvinEnvWrapperRaw(val_folder, observation_space, device)
return env
def evaluate_policy(model, env, eval_sr_path, eval_result_path, ep_len, num_sequences, num_procs, procs_id, eval_dir=None, debug=False):
conf_dir = Path(f"{CALVIN_ROOT}/calvin_models") / "conf"
task_cfg = OmegaConf.load(conf_dir / "callbacks/rollout/tasks/new_playtable_tasks.yaml")
task_oracle = hydra.utils.instantiate(task_cfg)
val_annotations = OmegaConf.load(conf_dir / "annotations/new_playtable_validation.yaml")
eval_dir = get_log_dir(eval_dir)
eval_sequences = get_sequences(num_sequences)
num_seq_per_procs = num_sequences // num_procs
eval_sequences = eval_sequences[num_seq_per_procs*procs_id:num_seq_per_procs*(procs_id+1)]
results = []
if not debug:
eval_sequences = tqdm(eval_sequences, position=0, leave=True)
sequence_i = 0
for initial_state, eval_sequence in eval_sequences:
result = evaluate_sequence(env, model, task_oracle, initial_state, eval_sequence, val_annotations, debug, eval_dir, sequence_i, ep_len)
results.append(result)
if not debug:
success_list = count_success(results)
with open(eval_sr_path, 'a') as f:
line =f"{sequence_i}/{num_sequences}: "
for sr in success_list:
line += f"{sr:.3f} | "
sequence_i += 1
line += "\n"
f.write(line)
eval_sequences.set_description(
" ".join([f"{i + 1}/5 : {v * 100:.1f}% |" for i, v in enumerate(success_list)]) + "|"
)
else:
sequence_i += 1
print_and_save(results, eval_sequences, eval_result_path, None)
return results
def evaluate_sequence(env, model, task_checker, initial_state, eval_sequence, val_annotations, debug, eval_dir, sequence_i, ep_len):
robot_obs, scene_obs = get_env_state_for_initial_condition(initial_state)
env.reset(robot_obs=robot_obs, scene_obs=scene_obs)
success_counter = 0
if debug:
time.sleep(1)
print()
print()
print(f"Evaluating sequence: {' -> '.join(eval_sequence)}")
print("Subtask: ", end="")
for subtask_i, subtask in enumerate(eval_sequence):
success = rollout(env, model, task_checker, subtask, val_annotations, debug, eval_dir, subtask_i, sequence_i, ep_len)
if success:
success_counter += 1
else:
return success_counter
return success_counter
def rollout(env, model, task_oracle, subtask, val_annotations, debug, eval_dir, subtask_i, sequence_i, ep_len):
if debug:
print(f"{subtask} ", end="")
time.sleep(0.5)
obs = env.get_obs()
lang_annotation = val_annotations[subtask][0]
model.reset()
start_info = env.get_info()
if debug:
img_dict = {
'static': [],
'gripper': [],
'pred_static': [],
'pred_gripper': [],
}
unfinished = 0
for step in range(ep_len):
if unfinished == 0:
output = model.step(obs, lang_annotation)
action = output['action_pred']
unfinished = action.shape[0]
obs, _, _, current_info = env.step(action[-unfinished])
unfinished -= 1
if debug:
img_dict['static'].append(copy.deepcopy(obs['rgb_obs']['rgb_static']))
img_dict['gripper'].append(copy.deepcopy(obs['rgb_obs']['rgb_gripper']))
img_dict['pred_static'].append(copy.deepcopy(output['obs_preds'][0, -1].astype(np.uint8)))
img_dict['pred_gripper'].append(copy.deepcopy(output['obs_hand_preds'][0, -1].astype(np.uint8)))
# check if current step solves a task
current_task_info = task_oracle.get_task_info_for_set(start_info, current_info, {subtask})
if len(current_task_info) > 0:
if debug:
print(colored("success", "green"), end=" ")
for key in img_dict.keys():
clip = ImageSequenceClip(img_dict[key], fps=30)
clip.write_gif(os.path.join(eval_dir, f'{sequence_i}-{subtask_i}-{subtask}-{key}-succ.gif'), fps=30)
return True
if debug:
print(colored("fail", "red"), end=" ")
for key in img_dict.keys():
clip = ImageSequenceClip(img_dict[key], fps=30)
clip.write_gif(os.path.join(eval_dir, f'{sequence_i}-{subtask_i}-{subtask}-{key}-fail.gif'), fps=30)
return False
def main():
# Preparation
cfg = json.load(open('configs.json'))
# The timeout here is 3600s to wait for other processes to finish the simulation
kwargs = InitProcessGroupKwargs(timeout=timedelta(seconds=3600))
acc = Accelerator(kwargs_handlers=[kwargs])
device = acc.device
preprocessor = PreProcess(
cfg['rgb_static_pad'],
cfg['rgb_gripper_pad'],
cfg['rgb_shape'],
cfg['rgb_mean'],
cfg['rgb_std'],
device,
)
model_clip, _ = clip.load(cfg['clip_backbone'], device=device)
model_mae = vits.__dict__['vit_base'](patch_size=16, num_classes=0).to(device)
checkpoint = torch.load(cfg['mae_ckpt'])
model_mae.load_state_dict(checkpoint['model'], strict=False)
model = GR1(
model_clip,
model_mae,
rgb_shape=cfg['rgb_shape'],
patch_size=cfg['patch_size'],
state_dim=cfg['state_dim'],
act_dim=cfg['act_dim'],
hidden_size=cfg['embed_dim'],
sequence_length=cfg['seq_len'],
chunk_size=cfg['chunk_size'],
training_target=['act_pred', 'fwd_pred', 'fwd_pred_hand'],
img_feat_dim=cfg['img_feat_dim'],
patch_feat_dim=cfg['patch_feat_dim'],
lang_feat_dim=cfg['lang_feat_dim'],
resampler_params={
'depth': cfg['resampler_depth'],
'dim_head': cfg['resampler_dim_head'],
'heads': cfg['resampler_heads'],
'num_latents': cfg['resampler_num_latents'],
'num_media_embeds': cfg['resampler_num_media_embeds'],
},
without_norm_pixel_loss=cfg['without_norm_pixel_loss'],
skip_frame=cfg['skip_frame'],
use_hand_rgb=True,
n_layer=cfg['n_layer'],
n_head=cfg['n_head'],
n_inner=4*cfg['embed_dim'],
activation_function=cfg['activation_function'],
n_positions=cfg['n_positions'],
resid_pdrop=cfg['dropout'],
attn_pdrop=cfg['dropout'],
).to(device) # for fused optimizer
if cfg['load_bytedance_ckpt']:
model.load_state_dict(torch.load(cfg['bytedance_ckpt_path'])['state_dict'], strict=False)
acc.print('load ', cfg['bytedance_ckpt_path'] )
elif os.path.isfile(cfg['save_path']+'GR1_{}.pth'.format(cfg['load_epoch'])):
state_dict = torch.load(cfg['save_path']+'GR1_{}.pth'.format(cfg['load_epoch']))['state_dict']
model.load_state_dict(state_dict, strict=False)
acc.print('load ', cfg['save_path']+'GR1_{}.pth'.format(cfg['load_epoch']))
if cfg['compile_model']:
model = torch.compile(model)
model = acc.prepare(model, device_placement=[True])
observation_space = {
'rgb_obs': ['rgb_static', 'rgb_gripper'],
'depth_obs': [],
'state_obs': ['robot_obs'],
'actions': ['rel_actions'],
'language': ['language']}
eval_dir = cfg['save_path']+f'eval{torch.cuda.current_device()}/'
os.makedirs(eval_dir, exist_ok=True)
env = make_env('./fake_dataset', observation_space, device)
eva = GR1CalvinEvaluation(model, cfg, preprocessor, device)
model.eval()
avg_reward = torch.tensor(evaluate_policy(
eva,
env,
cfg['save_path']+'success_rate.txt',
cfg['save_path']+'result.txt',
cfg['ep_len'],
cfg['num_sequences'],
acc.num_processes,
acc.process_index,
eval_dir,
debug=cfg['record_evaluation_video'],
)).float().mean().to(device)
acc.wait_for_everyone()
avg_reward = acc.gather_for_metrics(avg_reward).mean()
if acc.is_main_process:
print('average success rate ', avg_reward)
if __name__ == "__main__":
main()