IntelLabs / coach

Reinforcement Learning Coach by Intel AI Lab enables easy experimentation with state of the art Reinforcement Learning algorithms
https://intellabs.github.io/coach/
Apache License 2.0
2.32k stars 460 forks source link

Issue rendering mp4/gif due to global `experiment_path` #404

Closed philwinder closed 4 years ago

philwinder commented 4 years ago

Hi there, When trying to dump videos in gym environments I receive the following error when trying to dump a gif:

...
  File "/Users/phil/source/winderresearch/rlplayground/.venv/lib/python3.7/site-packages/rl_coach/environments/environment.py", line 442, in dump_video_of_last_episode
    logger.create_gif(self.last_episode_images[::frame_skipping], name=file_name, fps=fps)
  File "/Users/phil/source/winderresearch/rlplayground/.venv/lib/python3.7/site-packages/rl_coach/logger.py", line 329, in create_gif
    output_dir = os.path.join(experiment_path, 'gifs')
...
TypeError: expected str, bytes or os.PathLike object, not NoneType

You encounter the same problem with mp4s too. This is due to a global experiment_path which is set to None earlier in the file. In the past I was able to override it, but now that doesn't seem to work any more.

Here is a simple example that can recreate the problem:

from rl_coach.base_parameters import VisualizationParameters
from rl_coach.core_types import TrainingSteps, EnvironmentEpisodes, EnvironmentSteps
from rl_coach.graph_managers.graph_manager import ScheduleParameters
from rl_coach.graph_managers.basic_rl_graph_manager import BasicRLGraphManager
from rl_coach.environments.gym_environment import GymVectorEnvironment
from rl_coach.agents.clipped_ppo_agent import ClippedPPOAgentParameters

# Custom schedule to speed up training. We don't really care about the results.
schedule_params = ScheduleParameters()
schedule_params.improve_steps = TrainingSteps(1)
schedule_params.steps_between_evaluation_periods = EnvironmentSteps(1)
schedule_params.evaluation_steps = EnvironmentEpisodes(1)
schedule_params.heatup_steps = EnvironmentSteps(0)

graph_manager = BasicRLGraphManager(
    agent_params=ClippedPPOAgentParameters(),
    env_params=GymVectorEnvironment(level='CartPole-v0'),
    schedule_params=schedule_params,
    vis_params=VisualizationParameters(
        dump_mp4=True)  # So we can dump the video
)

graph_manager.improve()

Any ideas how to work around this?

Thanks, Phil

philwinder commented 4 years ago

I've found a workaround. You can call:

from rl_coach import logger
logger.get_experiment_path("", None)

Which sets the experiment_path global.

I would still recommend removing the use of the global, however. Removing it should mitigate against bugs like this.

Thanks.

gal-leibovich commented 4 years ago

Hi @philwinder,

Thanks a lot for catching this issue. We have only tested dumping gifs and mp4s while running Coach from the command line. I have just tested it again now, and this path seems to work well.

It was not tested, though, when running through the "Coach as a library" Python API (which is fairly new).

I agree that the use of a global variable for the experiment_path is a less than ideal programming scheme. We definitely will consider changing it in a future update to the framework. Specifically, in this case, I think that the issue is that the 'experiment_path' was initialized to None instead of and empty string. I have pushed PR #405, which changes the default value of the global experiment_path to an empty string. It seems to workaround the issue for me.

philwinder commented 4 years ago

Perfect, thanks.