Skip to content

Video-enabled find_avoid example project #108

@TarekSaati

Description

@TarekSaati

Video-enabled example from deepworlds repository

This is an example implementing video logging to tensorboard to monitor agent performance during trainig in Webots simulator.

  • Here are the changes I did to training.py script in "find_and_avoid_v2" example project in deepworlds dev banch.

Todo:

1- Import Video from logger:
in training script:

from stable_baselines3.common.logger import HParam, Video

2- Add necessary variables to AdditionalInfoCallback class:
add in the constructor:

        self.frames = []  # List to store frames for GIF creation
        self.episode_cnt = 1
        self.record = False
        self.render_interval=render_interval

3- Implement the on_step() event handler to record frames periodically:

if self.env.done:
            if self.episode_cnt % self.render_interval == 0:
                self.env.camera.enable(self.env.timestep * 10) # basic time step = 32
                self.record = True
            else:
                self.env.camera.disable()
                self.record = False
            self.episode_cnt+=1
            self.frames = []
            print(f'Starting Episode {self.episode_cnt} ...')  

4- Add video creation and logging to tensorboard to on_rollout_end() event:
if self.record:

            # Save the frames to tensorboard
            frame = self.env.render(mode='rgb_array') # (c, h, w)
            self.frames.append(frame) 
            video = np.asarray([self.frames])
            self.logger.record("visualization",
                                Video(torch.from_numpy(video), fps=30),
                                exclude=("stdout", "log", "json", "csv"))

5- Add render parameters to run() function:

run(... ,
        log_interval=4,
        render_interval=100)

Results:

"Visualization" tab is added to Tensorboard monitoring & is updated after a predefined sequence of episodes, hence more ability to track the agent performance across different difficulty levels.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions