Farama-Foundation / PettingZoo

An API standard for multi-agent reinforcement learning environments, with popular reference environments and related utilities
https://pettingzoo.farama.org
Other
2.59k stars 410 forks source link

[Bug Report] render() returns only 0. under MPE env with render_mode='rgb_array' #963

Closed pride829 closed 1 year ago

pride829 commented 1 year ago

Describe the bug

I was trying to render MPE using cv2. The reason for this is that I am running pettingzoo on a remote docker container.

However, env.render() always returns all zeros with shape (700, 700, 3). I have tested this for several MPE environments and got the same results. Knights Archers Zombies on the other hand returns the correct RGB array and hence can be rendered using cv2.imshow().

Also, env.render() displays a correct result if I try to run this on my local machine with render_mode='human'

Looks like #874 was supposed to fix this issue, however, I still encounter it. Adding env.env.env.draw() fix it.

Advanced apologize if I missed anything.

Code example

from pettingzoo.mpe import simple_adversary_v2
import cv2

env = simple_adversary_v2.env(N=2, render_mode='rgb_array')
env.reset()

for agent in env.agent_iter():
    observation, reward, termination, truncation, info = env.last()
    cv2.imshow("animation", env.render())
    cv2.waitKey(1)
    if termination or truncation:
        action = None
    else:
        action = env.action_space(agent).sample()
    env.step(action)

env.close()

System info

PettingZoo 1.22.3 was installed by pip. Python 3.10.6, Ubuntu 22.04.2 on the local machine. Python 3.8.5, Ubuntu 22.04.2 on docker container.

Additional context

No response

Checklist

elliottower commented 1 year ago

Would you be interested in contributing a PR to fix this? I can look into it at some point in the future but haven't had much time with other things

elliottower commented 1 year ago

Just tested this locally and it it looks like it's been fixed. It returns values of 255 and values of 0, and if you export a given frame and render it, it looks correct. I suspect you're running an older version of PettingZoo or having issues due to opencv.

Just for simplicity and consistency's sake, I'm going to put code below which doesn't require opencv and tries to reproduce the error, and a command to ensure you have the most up to date environment (can also just clone the repo and install locally). Run pip install "pettingzoo[mpe] @ git+https://github.com/Farama-Foundation/PettingZoo.git" and then run the following script (note that there is now a v3 version of the env as well, but only fixing minor physics related things):

from pettingzoo.mpe import simple_adversary_v3
import numpy as np
from matplotlib import pyplot as plt

env = simple_adversary_v3.env(N=2, render_mode='rgb_array')
env.reset()

for agent in env.agent_iter():
    observation, reward, termination, truncation, info = env.last()
    frame = env.render()
    print(np.amin(frame)) # 0
    print(np.amax(frame)) # 255
    plt.imshow(frame)
    plt.show()
    if termination or truncation:
        action = None
    else:
        action = env.action_space(agent).sample()
    env.step(action)

env.close()

On my machine I get this:

Screen Shot 2023-07-16 at 2 01 35 PM
elliottower commented 1 year ago

Feel free to reopen if I've made a mistake or you find another problem

pride829 commented 1 year ago

Hi, sorry for the late reply. I would happily contribute a PR in the future. However I am currently working on my thesis(about reinforcement learning) right now. I believe that I am using the newest Pettingzoo so it might be OpenCV that cause the problem. I will try to dig in more in the future. Thank you!

elliottower commented 1 year ago

Sounds good, to figure out the error you might find my script helpful since it seems to isolate the rendering and show that they aren't just black screens. Just FYI if you're interested in examples that use PettingZoo and CV here's a few resources:

pride829 commented 1 year ago

Ok, I will be sure to look into it.