How to render?
There is a render module but I don't know how to use it. Could you please provide some examples of how to render?
you can use this:
from dm_control import suite
import matplotlib.pyplot as plt
import numpy as np
max_frame = 90
width = 480
height = 480
video = np.zeros((90, height, 2 * width, 3), dtype=np.uint8)
# Load one task:
env = suite.load(domain_name="cartpole", task_name="swingup")
# Step through an episode and print out reward, discount and observation.
action_spec = env.action_spec()
time_step = env.reset()
while not time_step.last():
for i in range(max_frame):
action = np.random.uniform(action_spec.minimum,
action_spec.maximum,
size=action_spec.shape)
time_step = env.step(action)
video[i] = np.hstack([env.physics.render(height, width, camera_id=0),
env.physics.render(height, width, camera_id=1)])
#print(time_step.reward, time_step.discount, time_step.observation)
for i in range(max_frame):
img = plt.imshow(video[i])
plt.pause(0.01) # Need min display time > 0.0.
plt.draw()
Hi @Hwhitetooth the render module is used internally within dm_control. We'll add a README so this is clearer, sorry for any confusion. Thanks @wwxFromTju for the snippet - see also page 10 of the tech report.
Hi all, we're currently working on adding more rendering options to the open-source codebase, including headless + software rendering support.
Hi @yotam , could you provide an ETA for headless rendering?
Hi @yotam
Is there any progress in this?
Hi @yotam any progress?
Support for headless rendering was added in 1a31a7ab5c356f72c90719e86d82e25f3625500d (currently Linux only).
TL;DR for who might find this by Googling:
env.physics.render() # [height, width, 3]
env.physics.render(height, width, camera_id=1) # [height, width, 3]
Signature:
def render(self, height=240, width=320, camera_id=-1, overlays=(),
depth=False, segmentation=False, scene_option=None):
@wwxFromTju, thanks for the code sample. On my machine (Windows 10, matplotlib==3.5.2, dm-control==1.0.3.post1), it rendered very slowly, with each frame taking about 2x longer than the previous frame (added timing code on each frame). It seems plot.imshow() creates an additional plot on each call. I changed the plot.imshow to img.set_array() and it worked a bit better (starting at 13 FPS and then decaying every 90 frames to about 7 FPS). Here's the updated code (2nd update to follow):
# dm_control_render.py: test of posted solution to render dm_control environment
from dm_control import suite
import matplotlib.pyplot as plt
import numpy as np
import time
max_frame = 90
frame_count = 0
started = time.time()
width = 480
height = 480
video = np.zeros((90, height, 2 * width, 3), dtype=np.uint8)
# Load one task:
env = suite.load(domain_name="cartpole", task_name="swingup")
# Step through an episode and print out reward, discount and observation.
action_spec = env.action_spec()
time_step = env.reset()
while not time_step.last():
for i in range(max_frame):
action = np.random.uniform(action_spec.minimum,
action_spec.maximum,
size=action_spec.shape)
time_step = env.step(action)
video[i] = np.hstack([env.physics.render(height, width, camera_id=0),
env.physics.render(height, width, camera_id=1)])
#print(time_step.reward, time_step.discount, time_step.observation)
img = plt.imshow(video[0])
for i in range(max_frame):
#img = plt.imshow(video[i])
img.set_array(video[i])
plt.pause(0.01) # Need min display time > 0.0.
#plt.draw()
frame_count += max_frame
elapsed = time.time() - started
print("frame: {}, elapsed: {:.2f} secs, fps: {:.0f}".format(frame_count, elapsed, max_frame/elapsed))
started = time.time()
Hi @wwxFromTju, here is update #2 which uses matplotlilb's animation.FuncAnimation. It seems to run at a steady 29 FPS (RTX 3090, Intel i9-9900k), but has the downside of having to restructure the code a bit to put each RL step into a callback.
# dm_control_env2.py: test dm_control creation of an RL environment
import time
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from dm_control import suite
# Load one task:
env = suite.load(domain_name="cartpole", task_name="swingup")
action_spec = env.action_spec()
time_step = env.reset()
width = 480
height = 480
fig = plt.figure()
video = np.zeros((1, height, 2 * width, 3), dtype=np.uint8)
im = plt.imshow(video[0], animated=True)
max_frame = 90
frame_count = 0
started = time.time()
def one_step(args):
action = np.random.uniform(action_spec.minimum, action_spec.maximum, size=action_spec.shape)
time_step = env.step(action)
frame = np.hstack([env.physics.render(height, width, camera_id=0),
env.physics.render(height, width, camera_id=1)])
im.set_array(frame)
# update stats
global frame_count, started
frame_count += 1
if frame_count % max_frame == 0:
elapsed = time.time() - started
print("frame: {}, elapsed: {:.2f} secs, fps: {:.0f}".format(frame_count, elapsed, max_frame/elapsed))
started = time.time()
return im,
ani = animation.FuncAnimation(fig, one_step, interval=0, blit=True, repeat=False)
plt.show()
Just wanting to clarify, is this a regression specifically on dm_control==1.0.3.post1 and mujoco==2.2.0, as in things were working fine in version 1.0.2?
Good question: this is the first time I have tried it. I also forgot to mention a likely candidate for the difference: I am running on Windows 10.
Adjustment to first update above: I moved "img = plt.imshow(video[0])" from line 31 to line 18, and now it runs at a steady 17 FPS. The continual adding of a imshow() call was gradually slowing things down.
FYI: FPS for a quick rendering callback with python/matplotlib runs at top speed of 80 FPS on my machine. It seems the native rendering in mujoco is much faster, judging from running basic.exe on the 22_humanioids.xml model. It would be great to be able to play the animation of the environment natively thru mujoco from python.