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]: VecVideoRecorder overwrites previous video at each save #2061

Closed
5 tasks done
curtiscjohnson opened this issue Dec 18, 2024 · 4 comments · Fixed by #2063
Closed
5 tasks done

[Bug]: VecVideoRecorder overwrites previous video at each save #2061

curtiscjohnson opened this issue Dec 18, 2024 · 4 comments · Fixed by #2063
Labels
bug Something isn't working

Comments

@curtiscjohnson
Copy link

curtiscjohnson commented Dec 18, 2024

🐛 Bug

Due to changes that happened with commit #1837 in vec_video_recorder.py, the video_name is no longer updated at each recording, which leads to the previous video being overwritten as self.video_name every time.

To Reproduce

import gymnasium as gym
from stable_baselines3.common.vec_env import VecVideoRecorder, DummyVecEnv
from stable_baselines3 import PPO
import glob


def test_video_save():
    env_id = "CartPole-v1"
    video_folder = "videos"
    video_length = 100

    vec_env = DummyVecEnv([lambda: gym.make(env_id, render_mode="rgb_array")])

    # Record the video starting at the first step
    vec_env = VecVideoRecorder(vec_env,
                               video_folder,
                               record_video_trigger=lambda x: x % 1024 == 0,
                               video_length=video_length,
                               name_prefix=f"agent-{env_id}")

    vec_env.reset()

    model = PPO("MlpPolicy", vec_env, verbose=0, device="cpu")

    model.learn(total_timesteps=2048)

    #print all videos in video_folder, should be multiple step 0-100, step 1024-1124
    video_files = glob.glob(f"{video_folder}/*.mp4")

    # Clean up
    vec_env.close()

    print(f"All video files saved in {video_folder}: {video_files}")


if __name__ == '__main__':
    test_video_save()

Relevant log output / Error message

Moviepy - Building video /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4.
Moviepy - Writing video /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4

Moviepy - Done !                                                                                        
Moviepy - video ready /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4
Saving video to /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4
Moviepy - Building video /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4.
Moviepy - Writing video /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4

Moviepy - Done !                                                                                        
Moviepy - video ready /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4
Saving video to /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4
Moviepy - Building video /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4.
Moviepy - Writing video /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4

Moviepy - Done !                                                                                        
Moviepy - video ready /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4
Moviepy - Building video /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4.
Moviepy - Writing video /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4

Moviepy - Done !                                                                                        
Moviepy - video ready /home/curtis/baloo/baloo-gym/videos/agent-CartPole-v1-step-0-to-step-100.mp4
All video files saved in videos: ['videos/agent-CartPole-v1-step-0-to-step-100.mp4']

System Info

  • OS: Linux-5.15.0-124-generic-x86_64-with-glibc2.29 # 134~20.04.1-Ubuntu SMP Tue Oct 1 15:27:33 UTC 2024
  • Python: 3.8.10
  • Stable-Baselines3: 2.4.0
  • PyTorch: 2.4.1+cu121
  • GPU Enabled: True
  • Numpy: 1.24.3
  • Cloudpickle: 3.1.0
  • Gymnasium: 1.0.0

Checklist

  • My issue does not relate to a custom gym environment. (Use the custom gym env template instead)
  • I have checked that there is no similar issue in the repo
  • I have read the documentation
  • I have provided a minimal and working example to reproduce the bug
  • I've used the markdown code blocks for both code and stack traces.
@curtiscjohnson curtiscjohnson added the bug Something isn't working label Dec 18, 2024
@araffin
Copy link
Member

araffin commented Dec 18, 2024

Hello,
thanks for reporting the issue, could you do a PR to fix it?

Edit: defining video name as a property should be an easy fix

@araffin araffin added the help wanted Help from contributors is welcomed label Dec 18, 2024
@curtiscjohnson
Copy link
Author

Hello, thanks for reporting the issue, could you do a PR to fix it?

Sure thing. I'll get a PR submitted in the next few days.

I'll also investigate whether this is indeed related to #2055 and let you know what I find.

@araffin
Copy link
Member

araffin commented Dec 20, 2024

@curtiscjohnson I found some time today, could you give #2063 a try?
(and also check if it fixes the W&B issue)

@araffin araffin removed the help wanted Help from contributors is welcomed label Dec 20, 2024
@curtiscjohnson
Copy link
Author

@curtiscjohnson I found some time today, could you give #2063 a try? (and also check if it fixes the W&B issue)

Sounds good!

#2063 does indeed fix the issue. Using the same MWE as above, the output now matches the expected behavior.

MoviePy - Building video /home/curtis/testing-sb3-temp/stable-baselines3/videos/agent-CartPole-v1-step-0-to-step-100.mp4.
MoviePy - Writing video /home/curtis/testing-sb3-temp/stable-baselines3/videos/agent-CartPole-v1-step-0-to-step-100.mp4

MoviePy - Done !                                                                                              
MoviePy - video ready /home/curtis/testing-sb3-temp/stable-baselines3/videos/agent-CartPole-v1-step-0-to-step-100.mp4
Saving video to /home/curtis/testing-sb3-temp/stable-baselines3/videos/agent-CartPole-v1-step-0-to-step-100.mp4
MoviePy - Building video /home/curtis/testing-sb3-temp/stable-baselines3/videos/agent-CartPole-v1-step-0-to-step-100.mp4.
MoviePy - Writing video /home/curtis/testing-sb3-temp/stable-baselines3/videos/agent-CartPole-v1-step-0-to-step-100.mp4

MoviePy - Done !                                                                                              
MoviePy - video ready /home/curtis/testing-sb3-temp/stable-baselines3/videos/agent-CartPole-v1-step-0-to-step-100.mp4
Saving video to /home/curtis/testing-sb3-temp/stable-baselines3/videos/agent-CartPole-v1-step-1024-to-step-1124.mp4
MoviePy - Building video /home/curtis/testing-sb3-temp/stable-baselines3/videos/agent-CartPole-v1-step-1024-to-step-1124.mp4.
MoviePy - Writing video /home/curtis/testing-sb3-temp/stable-baselines3/videos/agent-CartPole-v1-step-1024-to-step-1124.mp4

MoviePy - Done !                                                                                              
MoviePy - video ready /home/curtis/testing-sb3-temp/stable-baselines3/videos/agent-CartPole-v1-step-1024-to-step-1124.mp4
MoviePy - Building video /home/curtis/testing-sb3-temp/stable-baselines3/videos/agent-CartPole-v1-step-2048-to-step-2148.mp4.
MoviePy - Writing video /home/curtis/testing-sb3-temp/stable-baselines3/videos/agent-CartPole-v1-step-2048-to-step-2148.mp4

MoviePy - Done !                                                                                              
MoviePy - video ready /home/curtis/testing-sb3-temp/stable-baselines3/videos/agent-CartPole-v1-step-2048-to-step-2148.mp4
All video files saved in videos: ['videos/agent-CartPole-v1-step-1024-to-step-1124.mp4', 'videos/agent-CartPole-v1-step-0-to-step-100.mp4']

Regarding the W&B issue in #2055, still the same issue as mentioned in here. So I agree with your comment.

@araffin araffin mentioned this issue Jan 7, 2025
16 tasks
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
2 participants