question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[feature request] Support Tensorboard video logging (convenience)

See original GitHub issue

When investigating RL algorithms I’ve found it quite useful to report videos of a trajectory to Tensorboard, to directly relate it to other performance measures and look at the agent at different stages.

The implementation would be quite simple I think and if you decide this to be a useful feature I’d be happy to implement it. As a matter of fact I’ve already done so in my own fork of this repo. Here is how I envisioned its usage:

def render_trajectory(env, model):
    obs = env.reset()
    rewards = []
    done = False
    screens = []
    while not done:
        actions, _ = model.predict(obs, deterministic=True)
        obs, _, done, _ = env.step(actions)
        screen = env.render(mode='rgb_array')
        screens.append(screen.transpose(2, 0, 1))

    return Video(th.ByteTensor([screens]), fps=40)

class ReportTrajectoryCallback(BaseCallback):
    def __init__(self, eval_env, check_freq):
        super().__init__()
        self._eval_env = eval_env
        self._check_freq = check_freq

    def _on_step(self):
        if self.n_calls % self._check_freq == 0:
            video = render_trajectory(self._eval_env, self.model)
            self.logger.record("trajectory/video", video)
        return True

env = gym.make('CartPole-v1')
model = A2C('MlpPolicy', env)
model.learn(total_timesteps=int(1e4), callback=ReportTrajectoryCallback(env, 1000))

I’d suggest introducing a Video object and a simple extension of the logger to record the video. The TensorBoardOutputFormat would simply check for a video object and forward it to the SummaryWriter. All other KVWriter classes would just ignore it. As mentioned I’ve done this already for me self. Here is the relevant commit: https://github.com/SwamyDev/stable-baselines3/commit/d6b15416000011a5aeddd4af1bfb9f9a2d79d22f

Of course I’m open to any different approaches. For instance checking the type directly in the Formater classes is not good object oriented style and maybe an explicit video logging function would be better. However, it is relatively simple and get’s the job done.

What do you think? Would this is be a small but useful contribution?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
araffincommented, Oct 14, 2020

In fact, part of it should be included inside SB3 repo, because we need to change the tensorboard writer. But I agree that the callback and the helper should go in the contrib folder.

And there is a simpler way to use it: record("trajectory/video", video, exclude=("json", "stdout", "log", "csv"))

1reaction
SwamyDevcommented, Oct 14, 2020

Ah thanks for the suggestion that makes my code a lot simpler 😃

Ok, I’ll file a proper bug report and everything for the other issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Feature request: Log scale for x-axis · Issue #6085 - GitHub
Hi! It would be great to have a way to toggle log scale on the x-axis. See tensorflow/tensorflow#6532 This would allow to check...
Read more >
Tensorboard Integration - Stable Baselines3 - Read the Docs
TensorBoard supports periodic logging of video data, which helps evaluating agents at various stages during training. Warning. To support video logging ...
Read more >
[P] tbparse: Load tensorboard event logs as pandas ... - Reddit
Hi all,. Tensorboard is a nice tool to visualize experiment results. However, it is quite difficult to parse the event logs into raw...
Read more >
Tracking Module - Polyaxon Experimentation
Tracking module is an extension of Polyaxon client with tracking capabilities for logging parameters, code versions, metrics, and outputs when running your ...
Read more >
How to Train Your Own Object Detector Using TensorFlow ...
In this article we will focus on the second generation of the TensorFlow Object Detection API, which: supports TensorFlow 2,; lets you employ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found