Last active
January 7, 2021 15:54
-
-
Save DanielTakeshi/fec9a5cd957eb05b04b6d06a16cc88ae to your computer and use it in GitHub Desktop.
How to make a GIF from MuJoCo environment observations programmatically.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gym | |
import numpy as np | |
import imageio | |
# Make the environment and the initial observation `o`. | |
env_name = 'Walker2d-v3' | |
env = gym.make(env_name) | |
obs_dim = env.observation_space.shape | |
act_dim = env.action_space.shape[0] | |
act_limit = env.action_space.high[0] | |
o = env.reset() | |
# Book-keeping. | |
num_episodes = 3 | |
ep_ret = 0 | |
ep_len = 0 | |
ep_done = 0 | |
ep_obs = [] | |
# For video / GIF. | |
dur = 0.01 | |
width = 250 | |
height = 200 | |
while ep_done < num_episodes: | |
obs = env.render(mode='rgb_array', width=width, height=height) | |
assert obs.shape == (height, width, 3), obs.shape # height first! | |
ep_obs.append(obs) | |
# Take action, step into environment, etc. | |
a = np.random.randn(act_dim) | |
a = np.clip(a, -act_limit, act_limit) | |
o, r, d, _ = env.step(a) | |
ep_ret += r | |
ep_len += 1 | |
if d: | |
# Form GIF. imageio should read from numpy: https://imageio.github.io/ | |
print(f'Episode {ep_done}, cum. return: {ep_ret:0.1f}, length: {ep_len}.') | |
ep_name = f'ep_{env_name}_{str(ep_done).zfill(2)}_dur_{dur}_len_{str(ep_len).zfill(3)}.gif' | |
with imageio.get_writer(ep_name, mode='I', duration=dur) as writer: | |
for obs_np in ep_obs: | |
writer.append_data(obs_np) | |
# Reset information. | |
o = env.reset() | |
ep_ret = 0 | |
ep_len = 0 | |
ep_done += 1 | |
ep_obs = [] |
Mostly installed from spinningup conda env.
(spinningup) daniel@takeshi:~ $ conda list
# packages in environment at /home/daniel/miniconda3/envs/spinningup:
#
# Name Version Build Channel
_libgcc_mutex 0.1 main
absl-py 0.11.0 pypi_0 pypi
astor 0.8.1 pypi_0 pypi
atari-py 0.2.6 pypi_0 pypi
attrs 20.3.0 pypi_0 pypi
backcall 0.2.0 pypi_0 pypi
box2d-py 2.3.8 pypi_0 pypi
ca-certificates 2020.10.14 0
cached-property 1.5.2 pypi_0 pypi
certifi 2020.6.20 pyhd3eb1b0_3
cloudpickle 1.2.1 pypi_0 pypi
cycler 0.10.0 pypi_0 pypi
decorator 4.4.2 pypi_0 pypi
future 0.18.2 pypi_0 pypi
gast 0.2.2 pypi_0 pypi
google-pasta 0.2.0 pypi_0 pypi
grpcio 1.33.2 pypi_0 pypi
gym 0.15.7 pypi_0 pypi
h5py 3.1.0 pypi_0 pypi
importlib-metadata 2.0.0 pypi_0 pypi
iniconfig 1.1.1 pypi_0 pypi
ipython 7.16.1 pypi_0 pypi
ipython-genutils 0.2.0 pypi_0 pypi
jedi 0.17.2 pypi_0 pypi
joblib 0.17.0 pypi_0 pypi
keras-applications 1.0.8 pypi_0 pypi
keras-preprocessing 1.1.2 pypi_0 pypi
kiwisolver 1.3.1 pypi_0 pypi
ld_impl_linux-64 2.33.1 h53a641e_7
libedit 3.1.20191231 h14c3975_1
libffi 3.3 he6710b0_2
libgcc-ng 9.1.0 hdf63c60_0
libstdcxx-ng 9.1.0 hdf63c60_0
markdown 3.3.3 pypi_0 pypi
matplotlib 3.1.1 pypi_0 pypi
mpi4py 3.0.3 pypi_0 pypi
mujoco-py 2.0.2.13 pypi_0 pypi
ncurses 6.2 he6710b0_1
opencv-python 4.4.0.46 pypi_0 pypi
openssl 1.1.1h h7b6447c_0
opt-einsum 3.3.0 pypi_0 pypi
packaging 20.4 pypi_0 pypi
pandas 1.1.4 pypi_0 pypi
parso 0.7.1 pypi_0 pypi
patchelf 0.12 he6710b0_0
pexpect 4.8.0 pypi_0 pypi
pickleshare 0.7.5 pypi_0 pypi
pip 20.2.4 py36h06a4308_0
pluggy 0.13.1 pypi_0 pypi
prompt-toolkit 3.0.8 pypi_0 pypi
protobuf 3.13.0 pypi_0 pypi
psutil 5.7.3 pypi_0 pypi
ptyprocess 0.6.0 pypi_0 pypi
py 1.9.0 pypi_0 pypi
pyglet 1.5.0 pypi_0 pypi
pygments 2.7.2 pypi_0 pypi
pyparsing 2.4.7 pypi_0 pypi
pytest 6.1.2 pypi_0 pypi
python 3.6.12 hcff3b4d_2
python-dateutil 2.8.1 pypi_0 pypi
pytz 2020.4 pypi_0 pypi
readline 8.0 h7b6447c_0
scipy 1.5.4 pypi_0 pypi
seaborn 0.8.1 pypi_0 pypi
setuptools 50.3.1 py36h06a4308_1
spinup 0.2.0 dev_0 <develop>
sqlite 3.33.0 h62c20be_0
tensorboard 1.15.0 pypi_0 pypi
tensorflow 1.15.4 pypi_0 pypi
tensorflow-estimator 1.15.1 pypi_0 pypi
termcolor 1.1.0 pypi_0 pypi
tk 8.6.10 hbc83047_0
toml 0.10.2 pypi_0 pypi
torch 1.3.1 pypi_0 pypi
tqdm 4.51.0 pypi_0 pypi
traitlets 4.3.3 pypi_0 pypi
wcwidth 0.2.5 pypi_0 pypi
werkzeug 1.0.1 pypi_0 pypi
wheel 0.35.1 py_0
wrapt 1.12.1 pypi_0 pypi
xz 5.2.5 h7b6447c_0
zipp 3.4.0 pypi_0 pypi
zlib 1.2.11 h7b6447c_3
Note: I do NOT do export LD_PRELOAD=...
as in some solutions for getting MuJoCo to work. That will cause problems with RGB array mode.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Three episodes for each, with a random policy so it's bad. :D
HalfCheetah-v3
(Oops, file sizes are slightly bigger than 10MB, since episode lengths are 1000)
Hopper-v3
Walker2d-v3