Toni-SM / skrl

Modular reinforcement learning library (on PyTorch and JAX) with support for NVIDIA Isaac Gym, Omniverse Isaac Gym and Isaac Lab
https://skrl.readthedocs.io/
MIT License
518 stars 47 forks source link

"pygame.error:display Surface quit" in train.eval under render_mode="human" #58

Closed fangkuoyu closed 1 year ago

fangkuoyu commented 1 year ago

Change sarsa_gym_taxi.py or sarsa_gymnasium_taxi.py to reproduce the problems:

  1. env = gym.make("Taxi-v3", render_mode="human") # set the render mode
  2. cfg_trainer = {"timesteps": 100, "headless": True} # set a smaller timestep
  3. trainer.eval() # add the eval method after trainer.train()

Expected outcome: Show the environment under the train method and the eval method

Actual outcome: The environment under the train method is ok. But, the eval method gets the following error: Traceback (most recent call last): File ".\sarsa_gym_taxi.py", line 82, in trainer.eval() File "C:\Users\hp\Anaconda3\envs\skrl-env\lib\site-packages\skrl\trainers\torch\sequential.py", line 145, in eval self.single_agent_eval() File "C:\Users\hp\Anaconda3\envs\skrl-env\lib\site-packages\skrl\trainers\torch\base.py", line 211, in single_agent_eval states, infos = self.env.reset() File "C:\Users\hp\Anaconda3\envs\skrl-env\lib\site-packages\skrl\envs\torch\wrappers.py", line 471, in reset observation, info = self._env.reset() File "C:\Users\hp\Anaconda3\envs\skrl-env\lib\site-packages\gym\wrappers\time_limit.py", line 68, in reset return self.env.reset(kwargs) File "C:\Users\hp\Anaconda3\envs\skrl-env\lib\site-packages\gym\wrappers\order_enforcing.py", line 42, in reset return self.env.reset(kwargs) File "C:\Users\hp\Anaconda3\envs\skrl-env\lib\site-packages\gym\wrappers\env_checker.py", line 47, in reset return self.env.reset(**kwargs) File "C:\Users\hp\Anaconda3\envs\skrl-env\lib\site-packages\gym\envs\toy_text\taxi.py", line 277, in reset self.render() File "C:\Users\hp\Anaconda3\envs\skrl-env\lib\site-packages\gym\envs\toy_text\taxi.py", line 290, in render return self._render_gui(self.render_mode) File "C:\Users\hp\Anaconda3\envs\skrl-env\lib\site-packages\gym\envs\toy_text\taxi.py", line 366, in _render_gui self.window.blit(self.background_img, cell) pygame.error: display Surface quit

Comment: Because display is ok if eval without train, I think that the problem might be related to 'pygame stop in train' and 'pygame start in eval'. Thanks for any advice.

Toni-SM commented 1 year ago

Hi @fangkuoyu

This problem is because the environment is closed at the end of the execution of both the train() and eval() methods. I will program a solution for enabling/disabling environment closing after training/eval and push it to the develop branch soon

In the meanwhile, you can comment/remove the line that closes the environment after training in skrl/trainers/torch/base.py: https://github.com/Toni-SM/skrl/blob/b760dc7d49aad6b53a8ab2a81be704bb8a6f10dd/skrl/trainers/torch/base.py#L196

Let me know, please, if everything goes well after change :)

fangkuoyu commented 1 year ago

Removing the close statement in base.py works. Thanks for the workaround. I will use the current version to explore the package, so you can take your time to fix the problem. Thanks again for the quick response.

Toni-SM commented 1 year ago

Hi @fangkuoyu

I have pushed to the develop branch the changes for closing the environment at the end of script execution instead of after training/evaluation methods

https://github.com/Toni-SM/skrl/blob/e596a7642d9675e08db7579e7e5a8b42354e3cc9/skrl/trainers/torch/base.py#L70-L76

Many thanks for reporting this issue :)

fangkuoyu commented 1 year ago

Thanks for the fix. It works.