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

Add detection filter #1

Merged
merged 1 commit into from
Jun 22, 2021
Merged

Conversation

oscar-davids
Copy link

Add dnn filter for detection(livepeer/lpms#181).

To use this filter, need to install the TensorFlow for C library.
See https://www.tensorflow.org/install/install_c and configure FFmpeg with --enable-libtensorflow.
We can write the result of the detection filter in a file.
Added "lavfi.lvpdnn.text" metadata.

filter name

lvpdnn

parameter

  • model: trained tensorflow model path.(file extention is .pb)

  • input: tensorflow input layer name(in our model is input_1)

  • output: tensorflow output layer name(in our model is reshape_3/Reshape)

  • sample: detect every sample frames.

  • threshold: threshold for inference confidence.

  • log: path for save inference results.

Benchmark and example

  1. Software transcoding:

ffmpeg -i input.mp4 -vf lvpdnn=model=tmodel.pb:input=input:output=outshape:sample=1:threshold=0.5:log=outlog.txt out.mp4

  1. Hardware transcoding:

ffmpeg -vsync 0 -hwaccel cuvid -c:v h264_cuvid -i input.mp4 -c:a copy -vf lvpdnn=model=tmodel.pb:input=input:output=outshape:sample=1:threshold=0.5:log=outlog.txt -c:v h264_nvenc -b:v 5M out.mp4

  1. Mixed transcoding:

ffmpeg -vsync 0 -c:v h264_cuvid -i input.mp4 -vf lvpdnn=model=tmodel.pb:input=input:output=outshape:sample=1:threshold=0.5:log=outlog.txt -c:v h264_nvenc out.mp4

  1. ffprobe example:

ffprobe -show_entries frame_tags=lavfi.lvpdnn.text -f lavfi -i "movie=input.mp4,lvpdnn=model=tmodel.pb:input=input:output=outshape:sample=1:threshold=5.0" > lvptext.txt

@yondonfu yondonfu requested review from yondonfu and j0sh April 14, 2020 21:09
@yondonfu yondonfu changed the base branch from master to livepeer April 14, 2020 21:19
@j0sh
Copy link

j0sh commented Apr 14, 2020

Is the blob at 62164c7 a compile time or a run time requirement?

Rather than checking a 25+ MB binary blob into the tree (git does not handle such blobs well), can we make it a temporary requirement to have that blob locally available? Then later on we can explore alternative solutions, perhaps using git-lfs or even downloading a default model from somewhere if necessary.

@oscar-davids
Copy link
Author

Not need in compile time. This blob is run time requirement and can't replace it with a temporary.
Maybe we should use git-lfs.

@j0sh
Copy link

j0sh commented Apr 14, 2020

This blob is run time requirement

If it's runtime, then just make a note that the user needs to be sure to download the blob prior to running the filter. Checking into the ffmpeg tree doesn't seem to be a strict requirement either, since it'd have to be shipped over to the user (LPMS) somehow.

For now, we can store the model in a GCS storage bucket and add a link to that somewhere in the documentation.

Copy link

@jailuthra jailuthra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after fixing/answering minor comments.

Needs to be rebased on top of latest livepeer branch before merge.

libavfilter/vf_lvpdnn.c Outdated Show resolved Hide resolved
libavfilter/vf_lvpdnn.c Show resolved Hide resolved
libavcodec/nvenc.h Outdated Show resolved Hide resolved
Copy link
Member

@yondonfu yondonfu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly small nit comments and a few clarifying questions.

I think it would be helpful to describe in a comment what the additional steps in the filter that currently involve sws_scale are doing and why they are necessary (as already mentioned in another comment).

doc/filters.texi Show resolved Hide resolved
doc/filters.texi Outdated Show resolved Hide resolved
doc/filters.texi Outdated Show resolved Hide resolved
libavfilter/dnn/dnn_backend_tf.c Outdated Show resolved Hide resolved
libavfilter/dnn/dnn_backend_tf.c Outdated Show resolved Hide resolved
libavfilter/vf_lvpdnn.c Outdated Show resolved Hide resolved
libavfilter/vf_lvpdnn.c Outdated Show resolved Hide resolved
libavfilter/vf_lvpdnn.c Outdated Show resolved Hide resolved
libavfilter/vf_lvpdnn.c Outdated Show resolved Hide resolved
libavfilter/vf_lvpdnn.c Show resolved Hide resolved
libavfilter/dnn/dnn_backend_tf.c Outdated Show resolved Hide resolved
libavfilter/vf_lvpdnn.c Show resolved Hide resolved
libavfilter/vf_lvpdnn.c Outdated Show resolved Hide resolved
libavfilter/vf_lvpdnn.c Outdated Show resolved Hide resolved
libavfilter/vf_lvpdnn.c Outdated Show resolved Hide resolved
@yondonfu
Copy link
Member

yondonfu commented Jun 4, 2021

@oscar-davids I see that 3be1a59 sets the gpu_options.visible_device_list for TF in the set_input_output_tf function of dnn_backend_tf.c. Did the suggestion in this comment of setting the GPU options when the model is loaded via ff_dnn_load_model_tf (which seems to accept an options arg) not work out? If not, could you explain why it didn't work?

@oscar-davids
Copy link
Author

As we know there were many changes in the original FFmpeg DNN module in 2~3 months, I reviewed the code and thought that have to update a lot of parts of our codebase now It may cause safety issues.

The method they are using is to create a sess_config(i.e 0x10022805320e09cdccccccccccec3f20012a01303801) with an external python code(tf_sess_config.py) and use it as an argument. Overall, our goal is to use multiple GPUs.
For this purpose, I thought it would be more advantageous for the user to select the GPU id as a filter argument.

@oscar-davids oscar-davids removed the request for review from j0sh June 7, 2021 14:00
libavfilter/dnn/dnn_backend_tf.c Show resolved Hide resolved
libavfilter/dnn/dnn_backend_tf.c Show resolved Hide resolved
libavfilter/vf_lvpdnn.c Outdated Show resolved Hide resolved
@yondonfu
Copy link
Member

yondonfu commented Jun 9, 2021

The go-livepeer install_ffmpeg.sh script currently disables the majority of features and then selectively enables only the features that are currently required. I believe the motivation for this is to avoid unnecessarily increasing the size of the resulting binary by including code for additional features that are not used.

When I tried compiling go-livepeer using the code from this branch, I ran into compilation errors indicating missing references for ff_get_dnn_module. I was able to workaround this compilation error by also enabling the SR filter.

My best guess as to why enabling the SR filter addressed the compilation error is that enabling the SR filter pulled in the required DNN dependencies required by the lvpdnn filter.

In the configure file, _select seems to indicate that something is a "selected dependency". For example, the SR filter has the value of sr_filter_select set to dnn. The derain filter has the value of derain_filter_select set to dnn. Perhaps this means that each filter needs to specify what its selected dependencies are. If this is the case, then we'd want to specify the "dnn" dependency for the lvpdnn filter as well so that if it is enabled on its own it will pull in the required DNN dependencies.

Copy link

@jailuthra jailuthra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM for merge after adding the dnn dependency in configure script as mentioned by Yondon.

@yondonfu
Copy link
Member

@oscar-davids Saw that I was tagged for a follow up review, but I can't seem to find the changes relevant to this comment. Could you point to the changes that address that comment?

@oscar-davids
Copy link
Author

oscar-davids commented Jun 19, 2021

I think that there is nothing to change in the code because it is ffmpeg build and config options.

@jailuthra
Copy link

I think that there is nothing to change in the code because it is ffmpeg build and config options.

We can add a line within this PR

FFmpeg/configure

Line 3483 in b34f2e1

derain_filter_select="dnn"

lvpdnn_filter_select="dnn"

@jailuthra
Copy link

Confirmed that after changes in a10074d we no longer need to enable sr-filter in go-livepeer livepeer/go-livepeer@a121c1e 👍

Copy link
Member

@yondonfu yondonfu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jailuthra
Copy link

LGTM for merge after -

  1. Rebasing over latest livepeer branch (so that unused commit 1da1787 is dropped)
  2. Cleaning up the log by squashing temp commits + editing commit log messages to match ffmpeg style guidelines

@oscar-davids oscar-davids force-pushed the dev-lpvdnn branch 2 times, most recently from 7dfce00 to 447450a Compare June 22, 2021 19:07
@jailuthra jailuthra merged commit b344789 into livepeer:livepeer Jun 22, 2021
@jailuthra
Copy link

Merged ✔️ 🚢

jailuthra pushed a commit that referenced this pull request Jul 13, 2021
CID 1460603 (#1 of 1): Improper use of negative value (NEGATIVE_RETURNS)
ad-astra-video pushed a commit to ad-astra-video/FFmpeg that referenced this pull request Feb 25, 2024
In close_output(), a dummy frame is created with format NONE passed
to enc_open(), which isn't prepared for it. The NULL pointer
dereference happened at
av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth.

When fgt.graph is NULL, skip fg_output_frame() since there is
nothing to output.

frame #0: 0x0000005555bc34a4 ffmpeg_g`enc_open(opaque=0xb400007efe2db690, frame=0xb400007efe2d9f70) at ffmpeg_enc.c:235:44
frame livepeer#1: 0x0000005555bef250 ffmpeg_g`enc_open(sch=0xb400007dde2d4090, enc=0xb400007e4e2daad0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:1462:11
frame livepeer#2: 0x0000005555bee094 ffmpeg_g`send_to_enc(sch=0xb400007dde2d4090, enc=0xb400007e4e2daad0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:1571:19
frame livepeer#3: 0x0000005555bee01c ffmpeg_g`sch_filter_send(sch=0xb400007dde2d4090, fg_idx=0, out_idx=0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:2154:12
frame livepeer#4: 0x0000005555bcf124 ffmpeg_g`close_output(ofp=0xb400007e4e2d85b0, fgt=0x0000007d1790eb08) at ffmpeg_filter.c:2225:15
frame livepeer#5: 0x0000005555bcb000 ffmpeg_g`fg_output_frame(ofp=0xb400007e4e2d85b0, fgt=0x0000007d1790eb08, frame=0x0000000000000000) at ffmpeg_filter.c:2317:16
frame livepeer#6: 0x0000005555bc7e48 ffmpeg_g`filter_thread(arg=0xb400007eae2ce7a0) at ffmpeg_filter.c:2836:15
frame livepeer#7: 0x0000005555bee568 ffmpeg_g`task_wrapper(arg=0xb400007d8e2db478) at ffmpeg_sched.c:2200:21

Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
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.

None yet

4 participants