Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
pride829 opened this issue May 4, 2023 · 6 comments
Closed
1 task done
Labels
bug Something isn't working

Comments

@pride829
Copy link

pride829 commented May 4, 2023

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

  • I have checked that there is no similar issue in the repo
@pride829 pride829 added the bug Something isn't working label May 4, 2023
@elliottower
Copy link
Contributor

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
Copy link
Contributor

elliottower commented Jul 16, 2023

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
Copy link
Contributor

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

@pride829
Copy link
Author

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
Copy link
Contributor

elliottower commented Jul 16, 2023

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
Copy link
Author

Ok, I will be sure to look into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants