-
Notifications
You must be signed in to change notification settings - Fork 1
/
replay_construction_job.py
135 lines (113 loc) · 3.88 KB
/
replay_construction_job.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
import os
import hickle
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import smart_settings
from mujoco_py import GlfwContext
from plotting_utils import setup_video
from mbrl.environments import env_from_string
from mbrl.rair_utils import model_relational_rair
matplotlib.use("Qt5Agg")
# Needed for the recording only if LD_PRELOAD is not unset!
GlfwContext(offscreen=True)
### ------- INPUT THE RUN NAME -------- ###
run_name = "gt_model/gt_rair_relational_6obj"
### ------ INPUT VIDEO OR IMAGE MODE ------ ###
mode = "img" # video or img
working_dir = f"results/rair/construction/{run_name}"
# Load settings!
params = smart_settings.load(
os.path.join(working_dir, "settings.json"), make_immutable=False
)
# Make environment!
env = env_from_string(params.env, **params["env_params"])
# Set figure path!
fig_path = f"results/rair/construction/{run_name}/{mode}s"
os.makedirs(fig_path, exist_ok=True)
# Load buffer!
buffer = hickle.load(os.path.join(working_dir, "checkpoints_latest/rollouts"))
ep_length = buffer[0]["observations"].shape[0]
assert len(buffer) < 50 # Specify iterations for free play runs!
# Set camera settings!
render_width = 512
render_height = 512
frame = env.render("rgb_array", render_width, render_height)
# Feel free to change these values to modify the camera angle!
env.viewer.cam.azimuth = 180.16790799446062
env.viewer.cam.distance = 1.0
env.viewer.cam.elevation = -10
env.sim.forward()
if mode == "img":
fig = plt.figure(figsize=(4, 4))
data = np.zeros((render_width, render_height))
im = plt.imshow(data, vmin=0, vmax=1)
plt.axis("off")
fig.tight_layout()
for i in range(len(buffer)):
env.reset()
if mode == "video":
# File name in setup video is: "{name_prefix}rollout{name_suffix}.mp4"
video, video_path = setup_video(
fig_path,
name_suffix=f"_{i}",
name_prefix="construction_",
fps=env.get_fps(),
)
for t in range(ep_length):
env.set_GT_state(buffer[i]["env_states"][t, :])
frame = env.render("rgb_array", render_width, render_height)
video.append_data(frame)
if env.name == "FetchPickAndPlaceConstruction" and env.case == "Slide":
del env.viewer._markers[:]
video.close()
else:
env.set_GT_state(buffer[i]["env_states"][-1, :])
data = env.render("rgb_array", render_width, render_height)
im.set_data(data)
fig.savefig(
os.path.join(fig_path, f"rollout{i}_end.png"),
bbox_inches="tight",
dpi=300,
)
env.set_GT_state(buffer[i]["env_states"][0, :])
data = env.render("rgb_array", render_width, render_height)
im.set_data(data)
fig.savefig(
os.path.join(fig_path, f"rollout{i}_beginning.png"),
bbox_inches="tight",
dpi=300,
)
keys_to_extract = [
"compression_ndim",
"granularity",
"precision",
"bidirectional",
"distance",
"rounding",
]
input_dict = {
key: params["controller_params"][key]
for key in keys_to_extract
if key in params["controller_params"]
}
costs_rollout = model_relational_rair(
buffer[i]["observations"],
env,
non_dist=True
if params["controller"] == "mpc-relational-rair-icem-torch"
else False,
**input_dict,
)
t = np.argsort(costs_rollout)[0]
env.set_GT_state(buffer[i]["env_states"][t, :])
data = env.render("rgb_array", render_width, render_height)
im.set_data(data)
fig.savefig(
os.path.join(fig_path, f"rollout{i}_highest_rair_t{t}.png"),
bbox_inches="tight",
dpi=300,
)
if mode == "img":
plt.close(fig)
env.close()