-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathibrl_robosuite_image.py
More file actions
376 lines (321 loc) · 13.1 KB
/
Copy pathibrl_robosuite_image.py
File metadata and controls
376 lines (321 loc) · 13.1 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import os
from functools import partial
import gym
import h5py
import jax
import numpy as np
import tqdm
from absl import app, flags, logging
from flax.training import checkpoints
from ml_collections import config_flags
from backend.agents import agents
from backend.common import wandb
from backend.common.evaluation import evaluate_and_record_videos_ibrl_robosuite
from backend.common.wandb import WandBLogger
from backend.data.replay_buffer import ReplayBuffer, ReplayBuffer_IBRL, ReplayBuffer_Q2RL
from backend.utils.timer_utils import Timer
from backend.utils.train_utils import concatenate_batches
import robomimic.utils.file_utils as FileUtils
import robomimic.utils.torch_utils as TorchUtils
import robomimic.utils.obs_utils as ObsUtils
import robomimic.envs.env_base as EB
import robomimic.utils.env_utils as EnvUtils
from backend.envs.wrappers import RobosuiteImageWrapper
import collections
import pickle
from copy import deepcopy
# import os
# os.environ['CUDA_VISIBLE_DEVICES'] = '1'
FLAGS = flags.FLAGS
# env
flags.DEFINE_string("env", "antmaze-large-diverse-v2", "Environemnt to use")
flags.DEFINE_float("reward_scale", 1.0, "Reward scale.")
flags.DEFINE_float("reward_bias", -1.0, "Reward bias.")
flags.DEFINE_float("discount", 0.99, "discount")
flags.DEFINE_float("max_traj_length", 200, "Maximum trajectory length.")
flags.DEFINE_float(
"clip_action",
0.99999,
"Clip actions to be between [-n, n]. This is needed for tanh policies.",
)
# training
flags.DEFINE_integer("num_eval_rollouts", 1_000_000, "Number of offline epochs.")
flags.DEFINE_integer("num_q_est_steps", 1_000_000, "Number of policy training steps.")
flags.DEFINE_integer("num_online_steps", 1_000_000, "Number of policy training steps.")
# agent
flags.DEFINE_string("bc_agent", "calql", "what BC agent to use")
flags.DEFINE_string("agent", "calql", "what RL agent to use")
# experiment house keeping
flags.DEFINE_integer("seed", 0, "Random seed.")
flags.DEFINE_string(
"save_dir",
os.path.expanduser("~/q2rl_log"),
"Directory to save the logs and checkpoints",
)
flags.DEFINE_string("resume_path_bc", "", "Path to resume for bc policies")
flags.DEFINE_string("resume_path_agent", "", "Path to resume for RL agent")
flags.DEFINE_integer("log_interval", 5_000, "Log every n steps")
flags.DEFINE_integer("eval_interval", 20_000, "Evaluate every n steps")
flags.DEFINE_integer("video_interval", 20_000, "Evaluate every n steps")
flags.DEFINE_integer("save_interval", 50_000, "Evaluate every n steps")
flags.DEFINE_integer("batch_size", 256, "batch size for training")
flags.DEFINE_integer("utd", 1, "update-to-data ratio of the critic")
flags.DEFINE_integer(
"n_eval_trajs", 20, "Number of trajectories to use for each evaluation."
)
# wandb
flags.DEFINE_string("exp_name", "", "Experiment name for wandb logging")
flags.DEFINE_string("project", "q2rl", "Wandb project folder")
flags.DEFINE_string("group", None, "Wandb group of the experiment")
flags.DEFINE_bool("debug", False, "If true, no logging to wandb")
flags.DEFINE_string("replay_path", None, "path to load the replay buffer from")
flags.DEFINE_integer("replay_buffer_capacity", int(2e6), "Replay buffer capacity")
flags.DEFINE_bool("render", False, "Render the environment")
flags.DEFINE_string("demo_path", None, "path to load the demo replay buffer from")
flags.DEFINE_string("data_filter_key", None, "Key to filter data")
flags.DEFINE_bool("get_demo_buffer", False, "Load demo trajs into demo buffer")
flags.DEFINE_string("encoder_type", "small", "Type of image encoder")
config_flags.DEFINE_config_file(
"config",
None,
"File path to the training hyperparameter configuration.",
lock_config=False,
)
def print_green(x):
return print("\033[92m {}\033[00m".format(x))
def main(_):
"""
wandb and logging
"""
wandb_config = WandBLogger.get_default_config()
wandb_config.update(
{
"project": FLAGS.project,
"group": FLAGS.group,
"exp_descriptor": f"{FLAGS.group}_{FLAGS.env}_{FLAGS.agent}_seed{FLAGS.seed}",
}
)
wandb_logger = WandBLogger(
wandb_config=wandb_config,
variant=dict(),
random_str_in_identifier=True,
disable_online_logging=FLAGS.debug,
)
save_dir = os.path.join(
FLAGS.save_dir,
f"{FLAGS.group}",
f"{wandb_logger.config.exp_descriptor}_{wandb_logger.config.unique_identifier}",
)
"""
env
"""
##Robosuite env
torch_device = TorchUtils.get_torch_device(try_to_use_cuda=True)
robomimic_agent, ckpt_dict = FileUtils.policy_from_checkpoint(ckpt_path=FLAGS.resume_path_bc, device=torch_device, verbose=True)
rm_env, _ = FileUtils.env_from_checkpoint(
ckpt_dict=ckpt_dict,
render=FLAGS.render,
render_offscreen=True,
verbose=True,
)
rm_env.env.ignore_done = (
False # Fix a hardcoded ignore_done=True in robomimic env init
)
rm_env.env.reward_shaping = (
False #Reward shaping should be false
)
rm_env.env.horizon = (
FLAGS.max_traj_length #Changing horizon of the env
)
suite_env = rm_env.env
finetune_env = RobosuiteImageWrapper(suite_env, reward_scale=FLAGS.reward_scale, reward_bias=FLAGS.reward_bias)
##Robosuite eval env
_, ckpt_dict = FileUtils.policy_from_checkpoint(ckpt_path=FLAGS.resume_path_bc, device=torch_device, verbose=True)
eval_rm_env, _ = FileUtils.env_from_checkpoint(
ckpt_dict=ckpt_dict,
render=FLAGS.render,
render_offscreen=True,
verbose=True,
)
eval_rm_env.env.ignore_done = (
False # Fix a hardcoded ignore_done=True in robomimic env init
)
eval_rm_env.env.reward_shaping = (
True #Reward shaping should be true for eval env
)
eval_rm_env.env.horizon = (
FLAGS.max_traj_length #Changing horizon of the env
)
eval_suite_env = eval_rm_env.env
eval_env = RobosuiteImageWrapper(eval_suite_env, reward_scale=FLAGS.reward_scale, reward_bias=FLAGS.reward_bias)
def get_robomimic_obs(obs):
robomimic_obs = collections.OrderedDict()
robomimic_obs["robot0_eef_pos"] = obs["state"][:3]
robomimic_obs["robot0_eef_quat"] = obs["state"][3:7]
robomimic_obs["robot0_gripper_qpos"] = obs["state"][7:9]
robomimic_obs["agentview_image"] = obs["front"]
robomimic_obs["robot0_eye_in_hand_image"] = obs["wrist"]
robomimic_obs = ObsUtils.process_obs_dict(robomimic_obs)
return robomimic_obs
robomimic_agent.start_episode()
observation, info = finetune_env.reset()
action, log_prob, entropy = robomimic_agent(ob=get_robomimic_obs(observation), log_prob=True)
"""
Initialize agent
"""
rng = jax.random.PRNGKey(FLAGS.seed)
rng, construct_rng = jax.random.split(rng)
online_replay_buffer_type = ReplayBuffer_IBRL
online_replay_buffer = online_replay_buffer_type(
finetune_env.observation_space,
finetune_env.action_space,
capacity=FLAGS.replay_buffer_capacity,
seed=FLAGS.seed,
discount=FLAGS.discount,
)
agent = agents[FLAGS.agent].create(
rng=construct_rng,
observations=finetune_env.observation_space.sample(),
actions=finetune_env.action_space.sample(),
encoder_def=None,
shared_encoder=False,
**FLAGS.config.agent_kwargs,
image_keys=["front","wrist"],
)
if FLAGS.resume_path_agent != "":
assert os.path.exists(FLAGS.resume_path_agent), "resume path does not exist"
agent = checkpoints.restore_checkpoint(FLAGS.resume_path_agent, target=agent)
"""
eval function
"""
def evaluate_and_log_results(
eval_env,
policy_fn,
bc_fn,
eval_func,
step_number,
wandb_logger,
n_eval_trajs=FLAGS.n_eval_trajs,
):
stats, trajs = eval_func(
policy_fn,
bc_fn,
eval_env,
n_eval_trajs,
)
eval_info = {
"average_return": stats["average_return"],
"average_traj_length": stats["average_traj_length"],
"success_rate": stats["success_rate"],
"bc_actions": stats["bc_actions"],
}
wandb_logger.log({"evaluation": eval_info}, step=step_number)
## If we want to initialize IBRL replay buffer with demo data
if FLAGS.get_demo_buffer:
print_green("Loading demo data into the replay buffer...")
f = h5py.File(FLAGS.demo_path, "r")
for demo in f["mask"][FLAGS.data_filter_key][:]:
obs_robot0_eef_pos = f["data"][demo]["obs"]["robot0_eef_pos"][:]
obs_robot0_eef_quat = f["data"][demo]["obs"]["robot0_eef_quat"][:]
obs_robot0_gripper_qpos = f["data"][demo]["obs"]["robot0_gripper_qpos"][:]
state = np.concatenate([obs_robot0_eef_pos, obs_robot0_eef_quat, obs_robot0_gripper_qpos], axis=-1)
front = f["data"][demo]["obs"]["agentview_image"][:]
wrist = f["data"][demo]["obs"]["robot0_eye_in_hand_image"][:]
obs = []
for i in range(len(state)):
observation = collections.OrderedDict()
observation["state"] = state[i]
observation["front"] = front[i]
observation["wrist"] = wrist[i]
obs.append(observation)
actions = f["data"][demo]["actions"][:]
rewards = f["data"][demo]["rewards"][:] * FLAGS.reward_scale + FLAGS.reward_bias
dones = f["data"][demo]["dones"][:]
for i in range(len(actions)):
rl_obs = obs[i]
next_obs = obs[i+1] if i < len(actions) - 1 else obs[i]
rl_next_obs = next_obs
transition = dict(
observations=rl_obs,
next_observations=rl_next_obs,
actions=actions[i],
rewards=rewards[i],
masks=1.0 - dones[i],
dones=dones[i],
bc_actions=actions[i],
next_bc_actions=actions[i+1] if i < len(actions) - 1 else actions[i],
)
online_replay_buffer.insert(transition)
observation, info = finetune_env.reset()
done = False
step = agent.state.step
timer = Timer()
for _ in tqdm.tqdm(range(step, FLAGS.num_online_steps)):
rng, action_rng = jax.random.split(rng)
bc_action, bc_log_prob, bc_entropy = robomimic_agent(ob=get_robomimic_obs(observation), log_prob=True)
action,_ = agent.sample_actions(observation, bc_actions=bc_action, seed=action_rng)
next_observation, reward, done, truncated, info = finetune_env.step(
action
)
next_bc_action = robomimic_agent(ob=get_robomimic_obs(next_observation))
transition = dict(
observations=observation,
next_observations=next_observation,
actions=action,
rewards=reward,
masks=1.0 - done,
dones=1.0 if (done or truncated) else 0,
bc_actions=bc_action,
next_bc_actions=next_bc_action,
)
online_replay_buffer.insert(transition)
observation = next_observation
if done or truncated:
observation, info = finetune_env.reset()
done = False
batch = online_replay_buffer.sample(FLAGS.batch_size)
agent, update_info = agent.update_high_utd(
batch,
utd_ratio=FLAGS.utd,
)
if step % FLAGS.log_interval == 0:
update_info = jax.device_get(update_info)
wandb_logger.log({"training": update_info}, step=step)
if step % FLAGS.eval_interval == 0:
timer.tick("total")
with timer.context("eval step"):
policy_fn = partial(
agent.sample_actions, argmax=True
)
bc_fn = robomimic_agent
eval_func = partial(
evaluate_and_record_videos_ibrl_robosuite,
clip_action=FLAGS.clip_action,
step_number=step,
record_video=True if step % FLAGS.video_interval == 0 else False,
wandb_logger=wandb_logger,
img_obs=True
)
evaluate_and_log_results(
eval_env=eval_env,
policy_fn=policy_fn,
bc_fn=bc_fn,
eval_func=eval_func,
step_number=step,
wandb_logger=wandb_logger,
)
timer.tock("total")
wandb_logger.log({"timer": timer.get_average_times()}, step=step)
if step % FLAGS.save_interval == 0:
logging.info("Saving checkpoint...")
checkpoint_path = checkpoints.save_checkpoint(
save_dir, agent, step=step, keep=30
)
logging.info("Saved checkpoint to %s", checkpoint_path)
"""
Advance Step
"""
step += 1
print("Done training!")
if __name__ == "__main__":
app.run(main)