Skip to content

Instantly share code, notes, and snippets.

@hiwonjoon
Last active December 24, 2019 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiwonjoon/4890be744845e4be89bfcac3de78c8ff to your computer and use it in GitHub Desktop.
Save hiwonjoon/4890be744845e4be89bfcac3de78c8ff to your computer and use it in GitHub Desktop.
Titan Setting for Mujoco OpenAI Env with Anaconda3

<<< Depreciated >>>

  1. Conda Install
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod +x Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
<answer appropriately>
<recommend to setup conda on /scratch/cluster/<username>. Since miniconda can become really big!>
conda install -y 'python==3.6.*' # Downgrade python version
  1. Install Cuda / Cudnn
<use conda if possible>
<if not, just download cuda and cudnn and extract it. then, add cuda directory to a path/ld_library_path>
  1. Install basic required libraries using conda
conda install pkg-config
conda install patchelf
conda install -c menpo osmesa
  1. Install basic required librarires using pip
pip insatll ipython
pip install pkgconfig
pip install tensorflow-gpu==1.13.1
pip install gym
  1. Install mujoco-py

Check whether osmesa library installed correctly by pkg-config --cflags osmesa. The return should be -I<your conda include directory>

< Download mujoco200_linux.zip and extract on ~/.mujoco/ also, add mjkey.txt' >
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<path to your mujoco dir>
wget https://launchpad.net/ubuntu/+archive/primary/+files/libgcrypt11_1.5.3-2ubuntu4.2_amd64.deb
dpkg -x libgcrypt11_1.5.3-2ubuntu4.2_amd64.deb
mv lib/x86_64-linux-gnu/* <your miniconda dir>/lib
git clone git@github.com:hiwonjoon/mujoco-py.git # For a detailed change I made, please see the commit
cd mujoco-py
pip install .
cd ..
rm -rf libgcrypt11_1.5.3-2ubuntu4.2_amd64.deb mujoco-py

Check the installation of mujoco-py by running a test script

import mujoco_py
import os
mj_path, _ = mujoco_py.utils.discover_mujoco()
xml_path = os.path.join(mj_path, 'model', 'humanoid.xml')
model = mujoco_py.load_model_from_path(xml_path)
sim = mujoco_py.MjSim(model)

print(sim.data.qpos)
# [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

sim.step()
print(sim.data.qpos)
  1. Test Gym Headless Rendering
import gym
env = gym.make('Reacher-v2')
env.reset()
env.render('rgb_array').shape
# should be [500,500,3]
from dm_control import mujoco
# Load a model from an MJCF XML string.
xml_string = """
<mujoco>
<worldbody>
<light name="top" pos="0 0 1.5"/>
<geom name="floor" type="plane" size="1 1 .1"/>
<body name="box" pos="0 0 .3">
<joint name="up_down" type="slide" axis="0 0 1"/>
<geom name="box" type="box" size=".2 .2 .2" rgba="1 0 0 1"/>
<geom name="sphere" pos=".2 .2 .2" size=".1" rgba="0 1 0 1"/>
</body>
</worldbody>
</mujoco>
"""
physics = mujoco.Physics.from_xml_string(xml_string)
# Render the default camera view as a numpy array of pixels.
while True:
pixels = physics.render()
import mujoco_py
import os
mj_path, _ = mujoco_py.utils.discover_mujoco()
xml_path = os.path.join(mj_path, 'model', 'humanoid.xml')
model = mujoco_py.load_model_from_path(xml_path)
sim = mujoco_py.MjSim(model)
viewer = mujoco_py.MjRenderContextOffscreen(sim, -1)
while True:
viewer.render(300, 300, camera_id=-1)
data = viewer.read_pixels(300, 300, depth=False)
print(data.shape)
from dm_control import mujoco
# Load a model from an MJCF XML string.
xml_string = """
<mujoco>
<worldbody>
<light name="top" pos="0 0 1.5"/>
<geom name="floor" type="plane" size="1 1 .1"/>
<body name="box" pos="0 0 .3">
<joint name="up_down" type="slide" axis="0 0 1"/>
<geom name="box" type="box" size=".2 .2 .2" rgba="1 0 0 1"/>
<geom name="sphere" pos=".2 .2 .2" size=".1" rgba="0 1 0 1"/>
</body>
</worldbody>
</mujoco>
"""
physics = mujoco.Physics.from_xml_string(xml_string)
# Render the default camera view as a numpy array of pixels.
pixels = physics.render()
import mujoco_py
import os
mj_path, _ = mujoco_py.utils.discover_mujoco()
xml_path = os.path.join(mj_path, 'model', 'humanoid.xml')
model = mujoco_py.load_model_from_path(xml_path)
sim = mujoco_py.MjSim(model)
viewer = mujoco_py.MjRenderContextOffscreen(sim, -1)
while True:
viewer.render(300, 300, camera_id=-1)
data = viewer.read_pixels(300, 300, depth=False)
print(data.shape)

Conda setup

conda create --no-default-packages -n tf2 conda activate tf2

Install Python

conda install -y 'python==3.7.*'

Install Cuda & Cudnn

conda install cudatoolkit=10.1 conda install cudnn

Install tensorflow

pip install tensorflow-gpu

Tensorflow GPU Test

export CUDA_VISIBLE_DEVICES=1 python tf-test.py

Install dm_control

Check GLEW is installed (required for EGL rendering)

dpkg -s libglew2.0 ldconfig -p | grep GLEW

pip install dm_control

The current version of dm_control has a PyOpenGL library compatibility issue. see the note below

vi /envs/tf2/lib/python3.7/site-packages/dm_control/_render/pyopengl/egl_ext.py

Test dm_control GPU rendering (watch nvidia-smi; the process should be seen on the process list,

and gpu usage should go up)

python dm_control_gpu_headless_rendering_test.py

Install mujoco-py

pip3 install -U 'mujoco-py<2.1,>=2.0'

surprisingly enough, there is no hickup. OPENAI start to manage their code I guess.

Test mujoco_py rendering... (watch nvidia-smi)

python mujoco_py_render_test.py

Nope. they won't render using GPU.

But, when you load proper library through dm_control, then... (watch nvidia-smi)

python mujoco_py_render_test_with_gpu.py

it will be rendered with GPU!

############# Side Note #################

dm_control OPENGL version compatibility issue:

change code here https://github.com/deepmind/dm_control/blob/30069ac11b60ee71acbd9159547d0bc334d63281/dm_control/_render/pyopengl/egl_ext.py#L42

ctypes.POINTER(EGLDeviceEXT), ctypes.POINTER(EGL.EGLint)) --> ctypes.POINTER(EGL.EGLDeviceEXT), ctypes.POINTER(EGL.EGLint))

from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Conv2D
from tensorflow.keras import Model
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# 채널 차원을 추가합니다.
x_train = x_train[..., tf.newaxis]
x_test = x_test[..., tf.newaxis]
train_ds = tf.data.Dataset.from_tensor_slices(
(x_train, y_train)).shuffle(10000).batch(32)
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
self.conv1 = Conv2D(32, 3, activation='relu')
self.flatten = Flatten()
self.d1 = Dense(128, activation='relu')
self.d2 = Dense(10, activation='softmax')
def call(self, x):
x = self.conv1(x)
x = self.flatten(x)
x = self.d1(x)
return self.d2(x)
model = MyModel()
loss_object = tf.keras.losses.SparseCategoricalCrossentropy()
optimizer = tf.keras.optimizers.Adam()
train_loss = tf.keras.metrics.Mean(name='train_loss')
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
test_loss = tf.keras.metrics.Mean(name='test_loss')
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(name='test_accuracy')
@tf.function
def train_step(images, labels):
with tf.GradientTape() as tape:
predictions = model(images)
loss = loss_object(labels, predictions)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
train_loss(loss)
train_accuracy(labels, predictions)
@tf.function
def test_step(images, labels):
predictions = model(images)
t_loss = loss_object(labels, predictions)
test_loss(t_loss)
test_accuracy(labels, predictions)
EPOCHS = 5
for epoch in range(EPOCHS):
for images, labels in train_ds:
train_step(images, labels)
for test_images, test_labels in test_ds:
test_step(test_images, test_labels)
template = 'epoch: {}, loss: {}, acc: {}, test_loss: {}, testacc: {}'
print (template.format(epoch+1,
train_loss.result(),
train_accuracy.result()*100,
test_loss.result(),
test_accuracy.result()*100))
@hiwonjoon
Copy link
Author

Other python dependencies:

pip install tqdm matplotlib imgcat

@hiwonjoon
Copy link
Author

<<< Update Dec 24, 2019 >>>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment