Render() challenges in final lab

Trying to get the lab working outside of the environment so I can continue to build upon the foundation. I’m challenged with the rendering…

Any chance you can point me in the right direction or at least maybe how to troubleshoot?

1 Like

Hello @wacyclingnerd, I suspect you are using a different version of gym on your machine. gym is the python library the lunar lander is based on. To verify this, please create a new code cell on coursera’s notebook and on your machine’s notebook, then paste and run the following code:

import gym
print(gym.__version__)

If the versions are indeed different, then there are 2 choices I can suggest: either (1) up/down-grade your gym to the version as used on coursera (which is 0.24.0), or (2) upgrade to the latest version (which is 0.26.2) and edit relevant code.

For up/down-grade, please google for how to do it in your machine’s environment.

If you choose to upgrade to the latest version (which would be my personal preference), you may follow the official documentation on how to use the latest version of gym. It will be different from how gym is used in your lab’s notebook, but I think you won’t need to change more than 5 or 10 lines of code. For example, as in the documentation, the render_mode argument that is causing you a problem has been moved to gym.make:

Screenshot from 2022-10-06 09-49-12

Raymond

1 Like

Thanks for the quick response. It motivated me to do a little homework. I have it working… just need to sort out the rendering of the video at the end.

Now I’m motivated to figure out how to watch this thing as it’s being trained and / or start putting some obstacles in it’s path and see if it can navigate them.

If anyone wants the solution for getting this working on a mac with the latest version of gym just reach out. Or if I’m allowed I could just post the changes here.

Cheers,

Kevin

3 Likes

Sounds interesting! Let us know how it goes.

You may post any code as long as it is not an exercise of an assignment :wink:

Raymond

1 Like

Well… I’ll try to simplify as it’s kind of all over. None have anything to do with the assignment portion.

1.) You will run into a ton of modules that you will need to import into your environment. These are fairly easy to figure out as you’ll just keep getting more and more warnings. Install the latest of everything. I have requirements.txt at the end.

2.) For Macs… you need to install XQuartz…

brew install XQuartz

Major Gotcha - you need to reboot to get this to actually finalize the install.

3.) You’ll need to give root permissions to create a folder in your tmp directory and give root access to it. You can use these three lines of code:

mkdir /tmp/.X11-unix
sudo chmod 1777 /tmp/.X11-unix
sudo chown root /tmp/.X11-unix

4.) In Section 4- Load the Environment you need to swap the following:

#env = gym.make('LunarLander-v2')
env = gym.make('LunarLander-v2', render_mode="rgb_array")

print(gym.__version__)`
env.reset()
#PIL.Image.fromarray(env.render(mode='rgb_array'))
PIL.Image.fromarray(env.render())

5.) In section 5.1 you need to deal with the fact they add a 5th item to the tuple returned.

#next_state, reward, done, _ = env.step(action)
next_state, reward, done, _, _ = env.step(action)

6.) Section 9 - you need to deal with the fact they added a 2nd item being returned when you run env.reset(). Line 19:

    #state = env.reset()
    state = env.reset()[0]

7.) The same types of modifications need to be done in the “utils” code… same stuff as above again.

def create_video(filename, env, q_network, fps=30):
    with imageio.get_writer(filename, fps=fps) as video:
        done = False
#        state = env.reset()
#        frame = env.render(mode="rgb_array")
        state = env.reset()[0]
        frame = env.render()
        video.append_data(frame)
        while not done:    
            state = np.expand_dims(state, axis=0)
            q_values = q_network(state)
            action = np.argmax(q_values.numpy()[0])
#            state, _, done, _ = env.step(action)
#            frame = env.render(mode="rgb_array")
            state, _, done, _, _ = env.step(action)
            frame = env.render()
            video.append_data(frame)

Worth doing as it runs about 20 minutes faster on my laptop. :slight_smile:

Also, if you’re interested I’ve included my requirements.txt below that has everything for the entire specialization. I’m not positive which modules I loaded towards the end, but this should handle everything.

absl-py==1.2.0
anyio==3.6.1
appnope==0.1.3
argon2-cffi==21.3.0
argon2-cffi-bindings==21.2.0
asttokens==2.0.8
astunparse==1.6.3
attrs==22.1.0
av==9.2.0
Babel==2.10.3
backcall==0.2.0
beautifulsoup4==4.11.1
bleach==5.0.1
box2d-py==2.3.5
cachetools==5.2.0
certifi==2022.6.15.1
cffi==1.15.1
charset-normalizer==2.1.1
cloudpickle==2.2.0
cycler==0.11.0
debugpy==1.6.3
decorator==5.1.1
defusedxml==0.7.1
entrypoints==0.4
executing==1.0.0
fastjsonschema==2.16.1
flatbuffers==22.9.24
fonttools==4.37.1
gast==0.4.0
google-auth==2.12.0
google-auth-oauthlib==0.4.6
google-pasta==0.2.0
grpcio==1.49.1
gym==0.26.2
gym-notices==0.0.8
h5py==3.7.0
idna==3.3
imageio==2.22.1
imageio-ffmpeg==0.4.7
ipykernel==6.15.3
ipympl==0.9.2
ipython==8.5.0
ipython-genutils==0.2.0
ipywidgets==8.0.2
jedi==0.18.1
Jinja2==3.1.2
joblib==1.1.0
json5==0.9.10
jsonschema==4.16.0
jupyter-core==4.11.1
jupyter-server==1.18.1
jupyter_client==7.3.5
jupyterlab==3.4.7
jupyterlab-pygments==0.2.2
jupyterlab-widgets==3.0.3
jupyterlab_server==2.15.1
keras==2.10.0
Keras-Preprocessing==1.1.2
kiwisolver==1.4.4
libclang==14.0.6
lxml==4.9.1
Markdown==3.4.1
MarkupSafe==2.1.1
matplotlib==3.5.3
matplotlib-inline==0.1.6
mistune==2.0.4
nbclassic==0.4.3
nbclient==0.6.8
nbconvert==7.0.0
nbformat==5.4.0
nest-asyncio==1.5.5
networkx==2.8.7
notebook==6.4.12
notebook-shim==0.1.0
numpy==1.23.3
oauthlib==3.2.1
opt-einsum==3.3.0
packaging==21.3
pandas==1.5.0
pandocfilters==1.5.0
parso==0.8.3
patsy==0.5.2
pexpect==4.8.0
pickle5==0.0.11
pickleshare==0.7.5
Pillow==9.2.0
prometheus-client==0.14.1
prompt-toolkit==3.0.31
protobuf==3.19.5
psutil==5.9.2
ptyprocess==0.7.0
pure-eval==0.2.2
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.21
pydot==1.4.2
pygame==2.1.0
Pygments==2.13.0
pyparsing==3.0.9
pyrsistent==0.18.1
python-dateutil==2.8.2
pytz==2022.2.1
PyVirtualDisplay==3.0
pyzmq==23.2.1
requests==2.28.1
requests-oauthlib==1.3.1
rsa==4.9
scikit-learn==1.1.2
scipy==1.9.1
Send2Trash==1.8.0
six==1.16.0
sklearn==0.0
sniffio==1.3.0
soupsieve==2.3.2.post1
stack-data==0.5.0
statsmodels==0.13.2
swig==4.0.2
tabulate==0.8.10
tensorboard==2.10.1
tensorboard-data-server==0.6.1
tensorboard-plugin-wit==1.8.1
tensorflow-estimator==2.10.0
tensorflow-macos==2.10.0
termcolor==2.0.1
terminado==0.15.0
threadpoolctl==3.1.0
tinycss2==1.1.1
tomli==2.0.1
tornado==6.2
traitlets==5.4.0
typing_extensions==4.3.0
urllib3==1.26.12
wcwidth==0.2.5
webencodings==0.5.1
websocket-client==1.4.1
Werkzeug==2.2.2
widgetsnbextension==4.0.3
wrapt==1.14.1
xvfbwrapper==0.2.9
3 Likes

@wacyclingnerd Thank you for sharing!

1 Like

@wacyclingnerd Thank you so much

Since “gym” is no longer supported, you could consider switching to its successor, “gymnasium”.

There is a thread in the forum about how to make the conversion (there are some incompatibilities. Use the forum search tool for “gymnasium”.