-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Doc] Add Custom Options for VideoRecorder (#2259)
- Loading branch information
Showing
4 changed files
with
92 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Customising Video Renders | ||
|
||
## Tweaking Video Rendering Settings | ||
TorchRL relies heavily on the [torchvision.io](https://pytorch.org/vision/main/io.html) | ||
and [PyAV](https://github.com/PyAV-Org/PyAV) modules for its video logging | ||
capabilities. Though these libraries are quite convenient and powerful, it is | ||
not easy to access the variety of knobs and settings at your disposal. | ||
|
||
This guide hopes to clarify what appear to be the general principles behind | ||
customising video rendering, and show you how you can manually adjust your | ||
rollouts' rendering settings to your liking. | ||
|
||
## General Principles | ||
Ultimately, [torchvision.io](https://pytorch.org/vision/main/io.html) and | ||
[PyAV](https://github.com/PyAV-Org/PyAV) make calls to [FFmpeg](https://ffmpeg.org/) | ||
libraries in order to render videos. | ||
|
||
In other words: | ||
|
||
- Whatever can be fed into [FFmpeg](https://ffmpeg.org/), we can also feed | ||
into TorchRL's `Loggers`. | ||
- For any custom settings we wish to use, we must reference them from | ||
[FFmpeg's documentation](https://trac.ffmpeg.org/) | ||
|
||
## Video Rendering Customization Example | ||
|
||
Suppose the following snippet gave us extremely blurry videos, even though | ||
we provided it clear, frame-by-frame images to stitch together: | ||
```python | ||
from torchrl.envs import GymEnv, TransformedEnv | ||
from torchrl.record import CSVLogger, VideoRecorder | ||
|
||
logger = CSVLogger(exp_name="my_exp") | ||
env = GymEnv("CartPole-v1", from_pixels=True, pixels_only=False) | ||
|
||
recorder = VideoRecorder(logger, tag="my_video") | ||
record_env = TransformedEnv(env, recorder) | ||
rollout = record_env.rollout(max_steps=3) | ||
recorder.dump() | ||
``` | ||
|
||
Since TorchRL's default video codec is [H264](https://trac.ffmpeg.org/wiki/Encode/H.264), | ||
the settings that we must change should be in there. | ||
|
||
For the purposes of this example, let us choose a | ||
[Constant Rate Factor (CRF)](https://trac.ffmpeg.org/wiki/Encode/H.264#crf) of | ||
`17` and a [preset](https://trac.ffmpeg.org/wiki/Encode/H.264#Preset) of `slow`, | ||
as advised by the documentation. | ||
|
||
We can improve the video quality by appending all our desired settings | ||
(as keyword arguments) to `recorder` like so: | ||
```python | ||
# The arguments' types don't appear to matter too much, as long as they are | ||
# appropriate for Python. | ||
# For example, this would work as well: | ||
# logger = CSVLogger(exp_name="my_exp", crf=17, preset="slow") | ||
logger = CSVLogger(exp_name="my_exp", crf="17", preset="slow") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters