Skip to content

Add support for Tensorflow dnn_backends in dnn_processing, sr & derain filters #172

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

Merged

Conversation

bbgdzxng1
Copy link
Contributor

This branch/patch adds TensorFlow support for general dnn_processing and application-specific sr and derain filters, using brew's standard libtensorflow formula.

These filters can be used with conventional neural network (CNN) and deep neural network (DNN) techniques and can be used with independent models for model-based frame processing. A common application for dnn processing is Super Resolution using a trained model.

Background

There are very few public builds (static or docker) that currently support these filters with TensorFlow. One docker build https://github.com/MIR-MU/ffmpeg-tensorflow does support TensorFlow, with the following observations:

  • The container is built with FFmpeg 5.x, so does not support alternate dnn_processing backends such as OpenVINO and PyTorch.
  • The above container does not support Apple Silicon
  • The above container requires Docker, whereas homebrew-ffmeg is native
  • The above container, does however, kindly provide some pre-trained sample models {espcn.pb, srcnn.pb, vespcn.pb, vsrnet.pb} which can be extracted from the container.

This homebrew-ffmpeg branch does not include or plan to include any pre-trained models. A user is expected to train their own model or source pre-trained models from elsewhere (modelzoo, openmodeldb or similar). A user is expected to place their model in a easily accessible folder and reference it when invoking the FFmpeg filter, eg $HOME/tensorflow-models. This branch does not offer any opinion on the quality or suitability of models.

The awesome homebrew-ffmpeg already includes optional support for OpenVINO backend for the dnn_processing filter. This patch adds support for TensorFlow as an alternative backend for dnn_processing, and with the addition of TensorFlow, supports the application-specific sr and derain filters.

Assuming that this patch is accepted, homebrew-ffmpeg would support two out of the three active backends. The FFmpeg 'Native' backend was deprecated & removed in April 2023

  • TensorFlow. This branch/patch.
  • OpenVINO. Already supported.
  • PyTorch.

Installation using the homebrew-ffmpeg option

$ brew install homebrew-ffmpeg/ffmpeg/ffmpeg --with-tensorflow

Validation

After compilation & installation, FFmpeg command-line help lists TensorFlow as an available dnn_backend for each of the following filters.

$ ffmpeg -h filter=sr
$ ffmpeg -h filter=dnn_processing
$ ffmpeg -h filter=dnn_detect
$ ffmpeg -h filter=derain

It is not expected that TensorFlow would be available in the dnn_clasify filter. [https://ffmpeg.org/ffmpeg-all.html#dnn_005fclassify](Formal FFmpeg documentation) for dnn_classify states that "This option accepts only openvino now, tensorflow backends will be added.".

✅ Compilation and installation was successful using this branch on macOS (13.7 Apple Silicon). Not tested on homebrew linux.

Tests

Wow! it actually JustWorked(TM) using brew's "standard" libtensorflow and borrowing some models for testing.

sr filter with Super Resolution Conventional Neural Network (srcnn) model

A pre-trained srcnn.pb model can be extracted from https://github.com/MIR-MU/ffmpeg-tensorflow Docker container

$ ffmpeg -f 'lavfi' -i mandelbrot=size='ntsc':rate='ntsc',trim=end_frame=30 -filter:v sr=dnn_backend='tensorflow':scale_factor=4:model="$HOME/tensorflow-models/srcnn.pb":input='x':output='y',format=pix_fmts='yuv420p' -f 'null' -

✅ The above FFmpeg command was successful on macOS (13.7 Apple Silicon) 2024-10-23. Not tested on homebrew linux.

At the time of writing, the HTML docs for the sr filter are outdated (and do not reference mandatory options such as input='x':output='y'). Users are advised to use command-line help $ ffmpeg -h filter=sr for up to date command-line options.

Derain filter with derain & dehaze models

$ curl --location https://github.com/XueweiMeng/derain_filter/raw/refs/heads/master/models_for_test/derain/derain_RESCAN.pb -o $HOME/tensorflow-models/derain_RESCAN.pb 
$ ffmpeg -f 'lavfi' -i mandelbrot=size='ntsc':rate='ntsc',trim=end_frame=30 -filter:v derain=dnn_backend='tensorflow':model="$HOME/tensorflow-models/derain_RESCAN.pb":filter_type='derain':input='x':output='y',format=pix_fmts='yuv420p' -f 'null' -

$ curl --location https://github.com/XueweiMeng/derain_filter/raw/refs/heads/master/models_for_test/dehaze/dehaze_RESCAN.pb -o $HOME/tensorflow-models/dehaze_RESCAN.pb
$ ffmpeg -f 'lavfi' -i mandelbrot=size='ntsc':rate='ntsc',trim=end_frame=30 -filter:v derain=dnn_backend='tensorflow':model="$HOME/tensorflow-models/dehaze_RESCAN.pb":filter_type='dehaze':input='x':output='y',format=pix_fmts='yuv420p' -f 'null' -

✅ The above FFmpeg command was successful on macOS (13.7 Apple Silicon) 2024-10-23. Not tested on homebrew linux.

At the time of writing, the HTML docs for the derain filter are outdated and do not reference mandatory options such as input='x':output='y'. Users are advised to use command-line help $ ffmpeg -h filter=derain for up to date command-line options.

General dnn_processing filter

A pre-trained espcn.pb model can be extracted from https://github.com/MIR-MU/ffmpeg-tensorflow Docker container

$ ffmpeg -f 'lavfi' -i mandelbrot=size='ntsc':rate='ntsc',trim=end_frame=30 -filter:v dnn_processing=dnn_backend='tensorflow':model="$HOME/tensorflow-models/espcn.pb":input='x':output='y',format=pix_fmts='yuv420p' -f 'null' -

✅ The above FFmpeg command was successful on macOS (13.7 Apple Silicon) 2024-10-23. Not tested on homebrew linux.

Caveat Emptor

I'm not a developer; I'm not very good with git branches or commits; I can barely write a homebrew formula. If I have broken any git or contribution conventions, please forgive me. I shall aim to learn and improve.

…ilters

Install:
```
$ brew install homebrew-ffmpeg/ffmpeg/ffmpeg --with-libtensorflow
```

Test case / validation that it compiles:

```
$ ffmpeg -h filter=sr

sr AVOptions:
   dnn_backend       <int>        ..FV....... DNN backend used for model execution (from 0 to 1) (default tensorflow)
     tensorflow      1            ..FV....... tensorflow backend flag
```

```
$ ffmpeg -h filter=dnn_processing 

dnn_processing AVOptions:
   dnn_backend       <int>        ..FV....... DNN backend (from INT_MIN to INT_MAX) (default tensorflow)
     tensorflow      1            ..FV....... tensorflow backend flag
```

Tensorflow is Apache License 2.0 https://github.com/tensorflow/tensorflow/blob/master/LICENSE
`--with-tensorflow` is the proposed option name in the formula
`--enable-libtensorflow` is the FFmpeg compile-time flag

[If there is a preferred formula convention that contradicts this proposal, then easy to change.]
After submitting PR, Github CI claimed:

> "FormulaAudit/DependencyOrder: dependency "libtensorflow" (line 105) should be put before dependency "svt-av1" (line 111)"

This commit reorders the dependencies.
After submitting PR, Github CI claimed:

> FormulaAudit/DependencyOrder: dependency "libtensorflow" (line 104) should be put before dependency "srt" (line 111)

This commit reorders the dependencies.

I hope that this is is the last error.  I doubt that neither svt-av1 nor srt is actually a dependency of libtensorflow, but I commit this change to try to appease the CI deity.
Github CI reports...

>FormulaAudit/DependencyOrder: dependency "libtensorflow" (line 104) should be put before dependency "speex" (line 111)

Moving libtensorflow into absolute alphabetical order, rather than in option-name order.

Apologies to any watchers for the level of noise.
@bbgdzxng1
Copy link
Contributor Author

The DependencyOrder error generated by Github CI was resolved by ensuring "depends_on" was in strict alphabetical order. Apologies to any watchers for the additional noise in this PR - I was unaware of this requirement prior to PR submission.

I believe other that the other CI errors are expected.

@retokromer
Copy link
Contributor

I believe other that the other CI errors are expected.

😂 I would prefer CI to work better, but I really don’t have time to look into this …

@slhck
Copy link
Contributor

slhck commented Oct 24, 2024

I don't know any alternative other than removing the chromaprint dependency altogether...

@retokromer
Copy link
Contributor

retokromer commented Oct 24, 2024

Me neither, but I would not remove chromaprint. Maybe there is a possibility to ignore it for testing.

@retokromer retokromer requested a review from slhck October 24, 2024 05:42
@retokromer
Copy link
Contributor

@slhck Works fine on my end with M3 on macOS 15.0.1. on one of our old test datasets. I can test only next week on Linux.

@retokromer retokromer merged commit 35f43f1 into homebrew-ffmpeg:master Oct 24, 2024
3 checks passed
@retokromer
Copy link
Contributor

Thank you again, @bbgdzxng1 !

@bbgdzxng1 bbgdzxng1 deleted the homebrew-ffmpeg-tensorflow branch October 24, 2024 11:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants