I used the code below to create the video
# Function that tests the model in the given environment
def test_model(env, model, video=None, msg=None):
# Reset environment
obs, info = env.reset()
frame = env.render()
ep_len = 0
ep_rew = 0
# Run episode until complete
while True:
# Provide observation to policy to predict the next action
action, _ = model.predict(obs)
# Perform action, update total reward
obs, reward, terminated, truncated, info = env.step(action)
ep_rew += reward
# Record frame to video
if video:
frame = env.render()
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
frame = cv2.putText(
frame, # Image
msg, # Text to add
(10, 25), # Origin of text in image
cv2.FONT_HERSHEY_SIMPLEX, # Font
1, # Font scale
(0, 0, 0,), # Color
2, # Thickness
cv2.LINE_AA # Line type
)
video.write(frame)
# Increase step counter
ep_len += 1
# Check to see if episode has ended
if terminated or truncated:
return ep_len, ep_rew
# Training and testing hyperparameters
NUM_ROUNDS = 100
NUM_TRAINING_STEPS_PER_ROUND = 10000
NUM_TESTS_PER_ROUND = 10
MODEL_FILENAME_BASE = "carracing-dqn"
VIDEO_FILENAME = "2-training.mp4"
# Create recorder
video = cv2.VideoWriter(VIDEO_FILENAME, FOURCC, FPS, (width, height))
model = sb3.DQN.load("carracing-dqn_43", env=env)
# Train and test the model for a number of rounds
avg_ep_lens = []
avg_ep_rews = []
for rnd in range(44, NUM_ROUNDS):
print("Round:", rnd)
# Train the model
model.learn(total_timesteps=NUM_TRAINING_STEPS_PER_ROUND, reset_num_timesteps=False, tb_log_name="DQN")
# Save the model
model.save(f"{MODEL_FILENAME_BASE}_{rnd}")
# Test the model in several episodes
avg_ep_len = 0
avg_ep_rew = 0
for ep in range(NUM_TESTS_PER_ROUND):
print("Ep:", ep)
# Only record the first test
if ep == 0:
ep_len, ep_rew = test_model(env, model, video, f"Round {rnd}")
else:
ep_len, ep_rew = test_model(env, model)
# Accumulate average length and reward
avg_ep_len += ep_len
avg_ep_rew += ep_rew
# Record and dieplay average episode length and reward
avg_ep_len /= NUM_TESTS_PER_ROUND
avg_ep_lens.append(avg_ep_len)
avg_ep_rew /= NUM_TESTS_PER_ROUND
avg_ep_rews.append(avg_ep_rew)
print(f"Round {rnd} | average test length: {avg_ep_len}, average test reward: {avg_ep_rew}")
# Close the video writer
video.release()
# Training and testing hyperparameters
NUM_ROUNDS = 100
NUM_TRAINING_STEPS_PER_ROUND = 10000
NUM_TESTS_PER_ROUND = 10
MODEL_FILENAME_BASE = "carracing-dqn"
VIDEO_FILENAME = "2-training.mp4"
# Create recorder
video = cv2.VideoWriter(VIDEO_FILENAME, FOURCC, FPS, (width, height))
model = sb3.DQN.load("carracing-dqn_43", env=env)
# Train and test the model for a number of rounds
avg_ep_lens = []
avg_ep_rews = []
for rnd in range(44, NUM_ROUNDS):
print("Round:", rnd)
# Train the model
model.learn(total_timesteps=NUM_TRAINING_STEPS_PER_ROUND, reset_num_timesteps=False, tb_log_name="DQN")
# Save the model
model.save(f"{MODEL_FILENAME_BASE}_{rnd}")
# Test the model in several episodes
avg_ep_len = 0
avg_ep_rew = 0
for ep in range(NUM_TESTS_PER_ROUND):
print("Ep:", ep)
# Only record the first test
if ep == 0:
ep_len, ep_rew = test_model(env, model, video, f"Round {rnd}")
else:
ep_len, ep_rew = test_model(env, model)
# Accumulate average length and reward
avg_ep_len += ep_len
avg_ep_rew += ep_rew
# Record and dieplay average episode length and reward
avg_ep_len /= NUM_TESTS_PER_ROUND
avg_ep_lens.append(avg_ep_len)
avg_ep_rew /= NUM_TESTS_PER_ROUND
avg_ep_rews.append(avg_ep_rew)
print(f"Round {rnd} | average test length: {avg_ep_len}, average test reward: {avg_ep_rew}")
# Close the video writer
video.release()
The code is all fine. The problem is that the code crashed and the video never got “released”, because video.release() never got executed because of the crash.
How can I recover the file, or is it even possible to do so?
The filetype is mpeg-4 movie
I tried importing it again using opencv and releasing it and using ffmpeg and other tools, but unfortunatly didnt work.