Skip to content

[GSoC] Develop OpenCV.js DNN modules for promising web use cases together with their tutorials#18064

Merged
alalek merged 31 commits intoopencv:3.4from
akineeic:gsoc_2020_dnn
Nov 29, 2020
Merged

[GSoC] Develop OpenCV.js DNN modules for promising web use cases together with their tutorials#18064
alalek merged 31 commits intoopencv:3.4from
akineeic:gsoc_2020_dnn

Conversation

@akineeic
Copy link
Copy Markdown
Contributor

@akineeic akineeic commented Aug 10, 2020

Overview

Introduction

OpenCV.js comes out as a JavaScript binding for a selected subset of OpenCV functions for the open web platform. It allows emerging web applications involving multimedia processing to benefit from the wide variety of vision functions available in OpenCV. OpenCV.js leverages Emscripten to compile OpenCV functions into ASM.js or WebAssembly targets, and provides JavaScript APIs for web developers.

The dnn module of OpenCV supports several frameworks including caffe, tensorflow, torch, darknet, onnx. It also supports so many types of layers as shown in the wiki, which reveal its potential to develop deep learning applications. OpenCV.js also binds some dnn API which is enough to do deep learning inference on the Web. At the same time, with the optimization through multi-threads and SIMD, the performance of the dnn module in OpenCV.js is impressive. All of these means that OpenCV.js also has the ability and potential to develop attractive deep learning applications on the Web.

But there are lack of tutorials to show the ability or potential of the dnn module of OpenCV.js. Very little information a developer can get from the tutorial even if he is interested in the dnn module of OpenCV.js. Lack of documentation and tutorials becomes a major issue that prevents widespread use of the dnn module in OpenCV.js.

Therefore, the main goal of this project is to develop several deep learning example with OpenCV.js and add them into the tutorial.

Work structure

Develop DNN(Deep Neural Networks) examples

This project added several DNN examples into OpenCV.js tutorial, including image classification, object detection, style transfer, semantic segmentation and pose estimation. The process flow of each example is similar. So condensed a function main to do inference once, which could be divided into several parts as follows:

  1. Load labels from txt file and process it into an array.
  2. Fetch model file and save into emscripten file system.
  3. Get blob from image as input for net, and standardize it with parameter like mean, std according to model.
  4. Use binding functions setInput and forward to get the output data.
  5. Post-processing to get the final result from the output data.

The first three part have been written into a JS file as help functions since these parts can be re-use between examples. You should only re-write the post-processing function to develop different example and change the parameters to run differents models.

Usage of DNN example

Follow the tutorial to build OpenCV.js and append option --build_doc to build documents. To try the DNN examples, launch a local web server in <build_dir>/doc/doxygen/html folder. Then click OpenCV.js Tutorials and Deep Neural Networks (dnn module) example in turn will navigate into the example webpage. You can download models according to the table in the bottom of the webpage or use their own models. Click the modelFile button (and configFile button if neeed) to upload model and modify the parameters in the first codeSnippet according to the uploaded model. Finally click Try it button will run function main to do inference once and show the result in the webpage.

Result

Test Environment

OS: Ubuntu 18.04

Emscripten: 1.39.15, LLVM upstream backend

Browser: Google Chrome 85.0.4183.83 (Official Build) (64-bit)

Hardware: Core(TM) i7-5960X CPU @ 3.00GHz with 16 logical cores

Test Result

Example Model Scalar(ms) SIMD(ms) Threads(ms) SIMD && Threads(ms)
Image Classifiction googleNet caffe 1139.86 285.73 325.77 93.17
Object Detection MobileNet-SSD 1069.60 343.23 403.13 184.10
Style Transfer mosaic 1776.86 476.94 456.84 156.38
Semantic Segmentation deepLab v3 513 7224.29 2214.53 1989.64 601.87
Pose Estimation openpose 82167.64 16722.71 19900.37 4291.09

Examples show(test in scalar)

  • Image Classification

  • Object Detection

  • Style Transfer

  • Semantic Segmentation

  • Pose Estimation

Future Work

  1. Investigate more models and add their infermation into the tutorials for each example.
  2. Try to enable DetectionModel for object detection example with the new version of OpenCV.
force_builders_only=docs,Custom
buildworker:Custom=linux-1,linux-2,linux-4
build_image:Docs=docs-js
build_image:Custom=javascript

akineeic and others added 21 commits June 21, 2020 17:10
[Opencv.js doc] Init commit to add image classification example in op…
[Opencv.js doc] Add image classification example with camera
[Opencv.js doc] Init commit to add semantic segmentation example
Fix the utils.loadOpenCv for promise module
[Opencv.js doc] Add object detection example.
[Opencv.js doc] Add fast neural style transfer example with opencv.js
[Opencv.js doc] Add pose estimation example
@dkurt
Copy link
Copy Markdown
Member

dkurt commented Aug 10, 2020

To do
Enable object detection example with camera and add model information json file to show in the tutorial to every example.

Please try to enable DetectionModel - t must be more efficient than js postprocessing

@akineeic
Copy link
Copy Markdown
Contributor Author

Please try to enable DetectionModel - t must be more efficient than js postprocessing

I found this function before in the wiki. But this project is based on Opencv 3.4, DetectionModel seems not in this version.

@huningxin
Copy link
Copy Markdown
Contributor

Please try to enable DetectionModel - t must be more efficient than js postprocessing

I found this function before in the wiki. But this project is based on Opencv 3.4, DetectionModel seems not in this version.

@dkurt , I agree DetectionModel should be more efficient than js postprocessing. As @akineeic mentioned, this PR is based on OpenCV 3.4, so DetectionModel is not available. Another gap is the JS binding of DetectionModel needs to be added if re-target this PR to the master.

@huningxin
Copy link
Copy Markdown
Contributor

@akineeic , please help fix the buildbot failures.

@akineeic
Copy link
Copy Markdown
Contributor Author

akineeic commented Aug 31, 2020

The onnx repo doesn't host the models in the github any more. Now they use LFS(large file system) to store instead, for example squeezeNet. So loading model from the url doesn't work in that case. So I'm wondering if we can add a button for users to upload models.

@huningxin
Copy link
Copy Markdown
Contributor

huningxin commented Aug 31, 2020

So I'm wondering if we can add a button for users to upload models.

It sounds like a good solution to me. It can solve two restrictions here: 1) docs.opencv.org would not host any external models; 2) cross-origin issue of the external models. The usage would be like: user follows the links in your table to download models, then upload models to the tutorial for inference.

@akineeic , please go ahead to try this solution and let us know the results.

@dkurt @alalek , any thoughts?

@akineeic akineeic force-pushed the gsoc_2020_dnn branch 2 times, most recently from 779aa1d to f3df32e Compare September 1, 2020 13:42
@akineeic
Copy link
Copy Markdown
Contributor Author

akineeic commented Sep 1, 2020

@akineeic , please go ahead to try this solution and let us know the results.

I have done the work. Users can use their own models download the models according to the table in the bottom of the webpage, and then click the input buttons to upload the models. Finally click the Try it will do inference once.

@huningxin @dkurt @alalek You can try the online demo without build(but it will take several minutes to load opencv.js). Any suggestion is welcomed sincerely.

"needSoftmax": "false",
"labelsUrl": "https://raw.githubusercontent.com/petewarden/tf_ios_makefile_example/master/data/imagenet_comp_graph_label_strings.txt",
"modelUrl": "https://raw.githubusercontent.com/petewarden/tf_ios_makefile_example/master/data/tensorflow_inception_graph.pb"
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do you remove the ONNX models? You list models in #18064 (comment)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It seems these models can't run in this example with opencv.js. I test successfully with opencv python before. This may caused by the difference version between python and opencv.js. I'll investigate these models later.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please investigate whether the root cause is the version of latest ONNX models. Did you try some old models?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That may be the root cause. I'll try to find the old model and have a try.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There is a number of tested models here: https://github.com/opencv/opencv_extra/blob/master/testdata/dnn/download_models.py

But they are not very interesting - almost all the models just ports to Caffe2

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@akineeic, could you try the ONNX models listed in download_models.py?

@huningxin
Copy link
Copy Markdown
Contributor

You can try the online demo without build(but it will take several minutes to load opencv.js). Any suggestion is welcomed sincerely.

Thanks @akineeic , the model upload function works great.

I found some issues and left some comments in the code. Please take a look.

@akineeic
Copy link
Copy Markdown
Contributor Author

akineeic commented Sep 5, 2020

I found some issues and left some comments in the code. Please take a look.

Thanks for your reviewer! I have modify the code according to your comments. And I update the code in my repo at hte same code. You can clone the code to launch a local web server or just try the online demo.

@alalek
Copy link
Copy Markdown
Member

alalek commented Sep 5, 2020

online demo?

BTW, what is wrong with preview pages: https://pullrequest.opencv.org/buildbot/export/pr/18064/docs/d5/d10/tutorial_js_root.html ?

@akineeic
Copy link
Copy Markdown
Contributor Author

akineeic commented Sep 5, 2020

BTW, what is wrong with preview pages: https://pullrequest.opencv.org/buildbot/export/pr/18064/docs/d5/d10/tutorial_js_root.html ?

I just create a subpage to hold the examples.

@huningxin
Copy link
Copy Markdown
Contributor

BTW, what is wrong with preview pages: https://pullrequest.opencv.org/buildbot/export/pr/18064/docs/d5/d10/tutorial_js_root.html ?

@alalek , thanks for this link. It is very convenient for review. We'll use that.

We didn't know the buildbot can host the preview pages, so I suggest @akineeic to host on his own github repo.

@akineeic akineeic changed the title WIP: [GSoC] Develop OpenCV.js DNN modules for promising web use cases together with their tutorials [GSoC] Develop OpenCV.js DNN modules for promising web use cases together with their tutorials Sep 7, 2020
@akineeic
Copy link
Copy Markdown
Contributor Author

akineeic commented Sep 7, 2020

@huningxin @alalek @dkurt Thanks for your review! I have finished the coding according to your conments. And my GSoC report can be found here. I'm wondering do you have any suggestion to the code or the webpage?

}
}
}
}
Copy link
Copy Markdown
Member

@dkurt dkurt Sep 7, 2020

Choose a reason for hiding this comment

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

Why we need to implement NMS? There is already NMSBoxes in dnn module

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I guess the reason is current dnn module of OpenCV.js doesn't expose NMSBoxes. @akineeic could you clarify that?

Copy link
Copy Markdown
Contributor Author

@akineeic akineeic Sep 15, 2020

Choose a reason for hiding this comment

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

Sorry for the later reply. I missed the mail since I forgot to change the primary email of my github account. The reason is mentioned by @huningxin that the JS binding of NMSBoxes is not implemented yet.

Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

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

Great job! Thank you for contribution 👍

@alalek alalek merged commit 4e44584 into opencv:3.4 Nov 29, 2020
@alalek alalek mentioned this pull request Dec 4, 2020
@alalek alalek mentioned this pull request Apr 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants