|
| 1 | +.. _augmentation: |
| 2 | + |
| 3 | +Using filters for augmentation |
| 4 | +============================== |
| 5 | + |
| 6 | +When using FFmpeg-based decoders, FFmpeg can also apply preprocessing via |
| 7 | +`filters <https://ffmpeg.org/ffmpeg-filters.html>`_. |
| 8 | + |
| 9 | +Client code can pass a custom filter string to decoding functions. |
| 10 | +There are helper fuctions to facilitate the construction of filter expression |
| 11 | +for common usecases. |
| 12 | + |
| 13 | +- :py:func:`spdl.io.get_audio_filter_desc` |
| 14 | +- :py:func:`spdl.io.get_video_filter_desc` |
| 15 | +- :py:func:`spdl.io.get_filter_desc` |
| 16 | + |
| 17 | +.. note:: |
| 18 | + |
| 19 | + For image/video data, it is often more efficient to apply augmentation |
| 20 | + before converting them to RGB format. |
| 21 | + (i.e. applying the same augmentation on an RGB array) |
| 22 | + |
| 23 | + Many image/video files are encoded with YUV420 format, which has half the number |
| 24 | + of pixels compared to RGB24. |
| 25 | + |
| 26 | + The :py:func:`~spdl.io.get_video_filter_desc` function performs custom |
| 27 | + filtering before changing the pixel format. |
| 28 | + |
| 29 | +Custom Filtering |
| 30 | +---------------- |
| 31 | + |
| 32 | +Filter description is a simple ``str`` object. |
| 33 | +You can write a custom filter description by yourself or |
| 34 | +insert a part of filter descriptions to the previous helper functions. |
| 35 | + |
| 36 | +.. note:: |
| 37 | + |
| 38 | + The filtering is also used for trimming the packets with the user-specified |
| 39 | + timestamp. |
| 40 | + When demuxing and decoding audio/video for a specific time window, |
| 41 | + (i.e. using ``timestamp`` option when using ``get_audio_filter_desc`` / |
| 42 | + ``get_video_filter_desc``) |
| 43 | + packets returned by demuxers contain frames outside of the window, |
| 44 | + because they are necessary to correctly decode frames. |
| 45 | + This process also creates frames outside of the window. |
| 46 | + Filterings such as `trim` and `atrim` are used to remove these frames. |
| 47 | + |
| 48 | + Therefore, when you create a custom filter for audio/video, |
| 49 | + make sure that the resulting filter removes frames outside of specified window. |
| 50 | + |
| 51 | +Image augmentation |
| 52 | +------------------ |
| 53 | + |
| 54 | +Using filters like ``hflip``, ``vflip``, ``rotate``, ``scale`` and ``crop``, |
| 55 | +we can compose an augmentation pipeline. |
| 56 | + |
| 57 | +For the detail of each filter, please refer to |
| 58 | +`the filter documentation <https://ffmpeg.org/ffmpeg-filters.html>`_. |
| 59 | + |
| 60 | +.. code-block:: |
| 61 | +
|
| 62 | + >>> import random |
| 63 | + >>> |
| 64 | + >>> def random_augmentation(): |
| 65 | + ... filters = [] |
| 66 | + ... |
| 67 | + ... # random_hflip |
| 68 | + ... if bool(random.getrandbits(1)): |
| 69 | + ... filters.append("hflip") |
| 70 | + ... |
| 71 | + ... # random_vflip |
| 72 | + ... if bool(random.getrandbits(1)): |
| 73 | + ... filters.append("vflip") |
| 74 | + ... |
| 75 | + ... # random_rotate +/- 30 deg |
| 76 | + ... angle = (60 * random.random() - 30) * 3.14 / 180 |
| 77 | + ... filters.append(f"rotate=angle={angle:.2f}") |
| 78 | + ... |
| 79 | + ... # resize |
| 80 | + ... filters.append("scale=256:256") |
| 81 | + ... |
| 82 | + ... # random_crop |
| 83 | + ... x_pos, y_pos = random.random(), random.random() |
| 84 | + ... filters.append(f"crop=224:224:x={x_pos:.2f}*(iw-ow):y={y_pos:.2f}*(ih-oh)") |
| 85 | + ... |
| 86 | + ... filter_desc = ",".join(filters) |
| 87 | + ... |
| 88 | + ... return filter_desc |
| 89 | +
|
| 90 | + |
| 91 | + >>> def load_with_augmentation(src): |
| 92 | + ... packets = spdl.io.demux_image(src) |
| 93 | + ... |
| 94 | + ... frames = spdl.io.decode_packets( |
| 95 | + ... packets, |
| 96 | + ... filter_desc=get_video_filter_desc(filter_desc=filter_desc, pix_fmt="rgb24"), |
| 97 | + ... ) |
| 98 | + ... |
| 99 | + ... return spdl.io.convert_frames(frames) |
| 100 | +
|
| 101 | +
|
| 102 | +This generates filter descriptions like the following |
| 103 | + |
| 104 | +.. code-block:: |
| 105 | +
|
| 106 | + "hflip,rotate=angle=-0.05,scale=256:256,crop=224:224:x=0.18*(iw-ow):y=0.17*(ih-oh)" |
| 107 | + "hflip,vflip,rotate=angle=-0.37,scale=256:256,crop=224:224:x=0.09*(iw-ow):y=0.96*(ih-oh)" |
| 108 | + "rotate=angle=0.33,scale=256:256,crop=224:224:x=0.58*(iw-ow):y=0.57*(ih-oh)" |
| 109 | + "hflip,vflip,rotate=angle=0.30,scale=256:256,crop=224:224:x=0.80*(iw-ow):y=0.35*(ih-oh)" |
| 110 | + "hflip,vflip,rotate=angle=0.02,scale=256:256,crop=224:224:x=0.01*(iw-ow):y=0.25*(ih-oh)" |
| 111 | + "vflip,rotate=angle=0.35,scale=256:256,crop=224:224:x=0.42*(iw-ow):y=0.69*(ih-oh)" |
| 112 | + "hflip,rotate=angle=0.22,scale=256:256,crop=224:224:x=0.10*(iw-ow):y=0.03*(ih-oh)" |
| 113 | + "hflip,rotate=angle=-0.18,scale=256:256,crop=224:224:x=0.65*(iw-ow):y=0.31*(ih-oh)" |
| 114 | + "rotate=angle=-0.13,scale=256:256,crop=224:224:x=0.37*(iw-ow):y=0.75*(ih-oh)" |
| 115 | + "hflip,vflip,rotate=angle=0.01,scale=256:256,crop=224:224:x=0.27*(iw-ow):y=0.84*(ih-oh)" |
| 116 | + "hflip,rotate=angle=-0.31,scale=256:256,crop=224:224:x=0.43*(iw-ow):y=0.92*(ih-oh)" |
| 117 | + "hflip,rotate=angle=-0.27,scale=256:256,crop=224:224:x=0.96*(iw-ow):y=0.92*(ih-oh)" |
| 118 | + "vflip,rotate=angle=-0.28,scale=256:256,crop=224:224:x=0.61*(iw-ow):y=0.04*(ih-oh)" |
| 119 | + "hflip,vflip,rotate=angle=0.08,scale=256:256,crop=224:224:x=0.84*(iw-ow):y=0.57*(ih-oh)" |
| 120 | + "hflip,vflip,rotate=angle=0.41,scale=256:256,crop=224:224:x=0.24*(iw-ow):y=0.92*(ih-oh)" |
| 121 | + "hflip,rotate=angle=-0.02,scale=256:256,crop=224:224:x=0.47*(iw-ow):y=0.87*(ih-oh)" |
| 122 | + "hflip,rotate=angle=-0.15,scale=256:256,crop=224:224:x=0.73*(iw-ow):y=0.30*(ih-oh)" |
| 123 | + "vflip,rotate=angle=-0.13,scale=256:256,crop=224:224:x=0.91*(iw-ow):y=0.85*(ih-oh)" |
| 124 | + "vflip,rotate=angle=0.28,scale=256:256,crop=224:224:x=0.62*(iw-ow):y=0.02*(ih-oh)" |
| 125 | + "rotate=angle=0.24,scale=256:256,crop=224:224:x=0.85*(iw-ow):y=0.61*(ih-oh)" |
| 126 | + "vflip,rotate=angle=-0.52,scale=256:256,crop=224:224:x=0.61*(iw-ow):y=0.59*(ih-oh)" |
| 127 | + "vflip,rotate=angle=0.06,scale=256:256,crop=224:224:x=0.08*(iw-ow):y=0.04*(ih-oh)" |
| 128 | + "hflip,rotate=angle=0.50,scale=256:256,crop=224:224:x=0.23*(iw-ow):y=0.42*(ih-oh)" |
| 129 | + "vflip,rotate=angle=0.18,scale=256:256,crop=224:224:x=0.54*(iw-ow):y=0.34*(ih-oh)" |
| 130 | +
|
| 131 | +and here are the resulting images. |
| 132 | + |
| 133 | +.. image:: ../../_static/data/io_preprocessing_random_aug.png |
| 134 | + |
0 commit comments