forked from OpenGHz/Imitate-All
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize_episodes.py
249 lines (222 loc) · 7.02 KB
/
visualize_episodes.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
import os
import numpy as np
import cv2
import h5py
import argparse
import matplotlib.pyplot as plt
from data_process.convert_all import Compresser
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def load_hdf5(dataset_dir, dataset_name):
dataset_path = os.path.join(dataset_dir, dataset_name + ".hdf5")
print(f"Loading dataset from: {dataset_path}")
if not os.path.isfile(dataset_path):
raise FileNotFoundError(f"Dataset does not exist at \n{dataset_path}\n")
with h5py.File(dataset_path, "r") as root:
qpos = root["/observations/qpos"][()]
# qvel = root["/observations/qvel"][()]
qvel = None
action = root["/action"][()]
image_dict = dict()
for cam_name in root[f"/observations/images/"].keys():
image_dict[cam_name] = root[f"/observations/images/{cam_name}"][()]
return qpos, qvel, action, image_dict
def save_videos(video, dt, video_path=None, swap_channel=False, decompress=False):
if isinstance(video, list):
cam_names = list(video[0].keys())
h, w, _ = video[0][cam_names[0]].shape
w = w * len(cam_names)
fps = int(1 / dt)
out = cv2.VideoWriter(video_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
for ts, image_dict in enumerate(video):
images = []
for cam_name in cam_names:
image = image_dict[cam_name]
if swap_channel:
image = image[:, :, [2, 1, 0]] # swap B and R channel
if decompress:
image = Compresser.decompress(image, "jpg")
images.append(image)
images = np.concatenate(images, axis=1)
out.write(images)
out.release()
print(f"Saved video to: {video_path}")
elif isinstance(video, dict):
cam_names = list(video.keys())
all_cam_videos = []
for cam_name in cam_names:
all_cam_videos.append(video[cam_name])
all_cam_videos = np.concatenate(all_cam_videos, axis=2) # width dimension
n_frames, h, w, _ = all_cam_videos.shape
fps = int(1 / dt)
out = cv2.VideoWriter(video_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
for t in range(n_frames):
image = all_cam_videos[t]
image = image[:, :, [2, 1, 0]] # swap B and R channel
out.write(image)
out.release()
print(f"Saved video to: {video_path}")
def visualize_joints(
state_list,
action_list,
plot_path=None,
ylim=None,
label_overwrite=None,
joint_names=None,
):
if label_overwrite:
label1, label2 = label_overwrite
else:
label1, label2 = "State", "Action"
qpos = np.array(state_list) # ts, dim
action = np.array(action_list)
if qpos.shape != action.shape:
logger.warning(f"qpos and action have different shapes: {qpos.shape} vs {action.shape}")
num_ts, num_dim = qpos.shape
h, w = 2, num_dim
num_figs = num_dim
fig, axs = plt.subplots(num_figs, 1, figsize=(w, h * num_figs))
assert num_dim == len(
joint_names
), "joint_names must match the number of dimensions"
# plot joint state
for dim_idx in range(num_dim):
ax = axs[dim_idx]
ax.plot(qpos[:, dim_idx], label=label1)
ax.set_title(f"{joint_names[dim_idx]}")
ax.legend()
# plot arm command
for dim_idx in range(num_dim):
ax = axs[dim_idx]
ax.plot(action[:, dim_idx], label=label2)
ax.legend()
if ylim:
for dim_idx in range(num_dim):
ax = axs[dim_idx]
ax.set_ylim(ylim)
plt.tight_layout()
plt.savefig(plot_path)
print(f"Saved qpos plot to: {plot_path}")
plt.close()
def visualize_timestamp(t_list, dataset_path):
plot_path = dataset_path.replace(".pkl", "_timestamp.png")
h, w = 4, 10
fig, axs = plt.subplots(2, 1, figsize=(w, h * 2))
# process t_list
t_float = []
for secs, nsecs in t_list:
t_float.append(secs + nsecs * 10e-10)
t_float = np.array(t_float)
ax = axs[0]
ax.plot(np.arange(len(t_float)), t_float)
ax.set_title(f"Camera frame timestamps")
ax.set_xlabel("timestep")
ax.set_ylabel("time (sec)")
ax = axs[1]
ax.plot(np.arange(len(t_float) - 1), t_float[:-1] - t_float[1:])
ax.set_title(f"dt")
ax.set_xlabel("timestep")
ax.set_ylabel("time (sec)")
plt.tight_layout()
plt.savefig(plot_path)
print(f"Saved timestamp plot to: {plot_path}")
plt.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-tn",
"--task_name",
action="store",
type=str,
help="task_name",
required=False,
)
parser.add_argument(
"-dt",
"--sample_freq",
action="store",
type=float,
help="Time step.",
required=True,
)
parser.add_argument(
"-dir",
"--dataset_dir",
action="store",
type=str,
help="Dataset dir.",
required=False,
default="data",
)
parser.add_argument(
"-dn",
"--dataset_name",
action="store",
type=str,
help="Dataset name.",
required=True,
)
parser.add_argument(
"-jn",
"--joint_names",
action="store",
nargs="+",
type=str,
help="State names.",
required=False,
default=("joint1", "joint2", "joint3", "joint4", "joint5", "joint6", "joint7"),
)
parser.add_argument(
"-sv",
"--save_video",
action="store_true",
help="Save video.",
)
parser.add_argument(
"-sj",
"--save_joints",
action="store_true",
help="Save joint states and acitons.",
)
parser.add_argument(
"-od",
"--output_dir",
action="store",
type=str,
help="Output dir.",
required=False,
default="visualizations",
)
parser.add_argument(
"-dc",
"--decompress",
action="store_true",
help="Decompress image.",
)
args = vars(parser.parse_args())
dt = args["sample_freq"]
task_name = args["task_name"]
dataset_dir = args["dataset_dir"] + "/" + task_name
dataset_name = args["dataset_name"]
joint_names = args["joint_names"]
output_dir = args["output_dir"]
decompress = args["decompress"]
qpos, qvel, action, image_dict = load_hdf5(dataset_dir, dataset_name)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
if args["save_video"]:
save_videos(
image_dict,
dt,
video_path=os.path.join(output_dir, dataset_name + "_video.mp4"),
decompress=decompress,
)
if args["save_joints"]:
visualize_joints(
qpos,
action,
plot_path=os.path.join(output_dir, dataset_name + "_joint.png"),
joint_names=joint_names,
)
# visualize_timestamp(t_list, dataset_path) # TODO addn timestamp back