Skip to content

Commit

Permalink
Add WebNN backend for OpenCV DNN Module
Browse files Browse the repository at this point in the history
Update dnn.cpp

Update dnn.cpp

Update dnn.cpp

Update dnn.cpp

Add WebNN head files into OpenCV 3rd partiy files

Create webnn.hpp

update cmake

Complete README and add OpenCVDetectWebNN.cmake file

add webnn.cpp

Modify webnn.cpp

Can successfully compile the codes for creating a MLContext

Update webnn.cpp

Update README.md

Update README.md

Update README.md

Update README.md

Update cmake files and

update README.md

Update OpenCVDetectWebNN.cmake and README.md

Update OpenCVDetectWebNN.cmake

Fix OpenCVDetectWebNN.cmake and update README.md

Add source webnn_cpp.cpp and libary libwebnn_proc.so

Update dnn.cpp

Update dnn.cpp

Update dnn.cpp

Update dnn.cpp

update dnn.cpp

update op_webnn

update op_webnn

Update op_webnn.hpp

update op_webnn.cpp & hpp

Update op_webnn.hpp

Update op_webnn

update the skeleton

Update op_webnn.cpp

Update op_webnn

Update op_webnn.cpp

Update op_webnn.cpp

Update op_webnn.hpp

update op_webnn

update op_webnn

Solved the problems of released variables.

Fixed the bugs in op_webnn.cpp

Implement op_webnn

Implement Relu by WebNN API

Update dnn.cpp for better test

Update elementwise_layers.cpp

Implement ReLU6

Update elementwise_layers.cpp

Implement SoftMax using WebNN API

Implement Reshape by WebNN API

Implement PermuteLayer by WebNN API

Implement PoolingLayer using WebNN API

Update pooling_layer.cpp

Update pooling_layer.cpp

Update pooling_layer.cpp

Update pooling_layer.cpp

Update pooling_layer.cpp

Update pooling_layer.cpp

Implement poolingLayer by WebNN API and add more detailed logs

Update dnn.cpp

Update dnn.cpp

Remove redundant codes and add more logs for poolingLayer

Add more logs in the pooling layer implementation

Fix the indent issue and resolve the compiling issue

Fix the build problems

Fix the build issue

FIx the build issue

Update dnn.cpp

Update dnn.cpp
  • Loading branch information
Hanxi Guo committed Aug 2, 2021
1 parent f30f1af commit 89eed5d
Show file tree
Hide file tree
Showing 18 changed files with 1,083 additions and 4 deletions.
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ OCV_OPTION(WITH_INF_ENGINE "Include Intel Inference Engine support" OFF
OCV_OPTION(WITH_NGRAPH "Include nGraph support" WITH_INF_ENGINE
VISIBLE_IF TRUE
VERIFY TARGET ngraph::ngraph)
OCV_OPTION(WITH_WEBNN "Include WebNN support" OFF
VISIBLE_IF TRUE
VERIFY HAVE_WEBNN)
OCV_OPTION(WITH_JASPER "Include JPEG2K support (Jasper)" ON
VISIBLE_IF NOT IOS
VERIFY HAVE_JASPER)
Expand Down Expand Up @@ -784,6 +787,11 @@ if(WITH_VULKAN)
include(cmake/OpenCVDetectVulkan.cmake)
endif()

# --- WebNN ---
if(WITH_WEBNN)
include(cmake/OpenCVDetectWebNN.cmake)
endif()

# --- Inference Engine ---
if(WITH_INF_ENGINE)
include(cmake/OpenCVDetectInferenceEngine.cmake)
Expand Down Expand Up @@ -1600,6 +1608,15 @@ if(WITH_VULKAN OR HAVE_VULKAN)
endif()
endif()

if(WITH_WEBNN OR HAVE_WEBNN)
status("")
status(" WebNN:" HAVE_WEBNN THEN "YES" ELSE "NO")
if(HAVE_WEBNN)
status(" Include path:" WEBNN_HEADER_DIRS THEN "${WEBNN_HEADER_DIRS}" ELSE "NO")
status(" Link libraries:" WEBNN_LIBRARIES THEN "${WEBNN_LIBRARIES}" ELSE "NO")
endif()
endif()

if(WITH_OPENCL OR HAVE_OPENCL)
ocv_build_features_string(opencl_features
IF HAVE_OPENCL_SVM THEN "SVM"
Expand Down
23 changes: 23 additions & 0 deletions cmake/OpenCVDetectWebNN.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ocv_clear_vars(HAVE_WEBNN)
ocv_clear_vars(WEBNN_EMSDK)
if(WITH_WEBNN)
set(WEBNN_HEADER_DIRS "$ENV{WEBNN_NATIVE_DIR}/gen/src/include")
set(WEBNN_INCLUDE_DIRS "$ENV{WEBNN_NATIVE_DIR}/../../src/include")
set(WEBNN_LIBRARIES "$ENV{WEBNN_NATIVE_DIR}/libwebnn_native.so;$ENV{WEBNN_NATIVE_DIR}/libwebnn_proc.so")
endif()

try_compile(VALID_WEBNN
"${OpenCV_BINARY_DIR}"
SOURCES "${OpenCV_SOURCE_DIR}/cmake/checks/webnn.cpp"
"$ENV{WEBNN_NATIVE_DIR}/gen/src/webnn/webnn_cpp.cpp"
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES:STRING=${WEBNN_INCLUDE_DIRS}\;${WEBNN_HEADER_DIRS}"
"-DLINK_LIBRARIES:STRING=${WEBNN_LIBRARIES}"
OUTPUT_VARIABLE TRY_OUT
)
if(NOT ${VALID_WEBNN})
message(WARNING "Can't use WebNN-native")
return()
endif()
message(AUTHOR_WARNING "Use WebNN-native")

set(HAVE_WEBNN 1)
13 changes: 13 additions & 0 deletions cmake/checks/webnn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <webnn/webnn_cpp.h>
#include <webnn/webnn.h>
#include <webnn/webnn_proc.h>
#include <webnn_native/WebnnNative.h>


int main(int /*argc*/, char** /*argv*/)
{
WebnnProcTable backendProcs = webnn_native::GetProcs();
webnnProcSetProcs(&backendProcs);
ml::Context ml_context = ml::Context(webnn_native::CreateContext());
return 0;
}
3 changes: 3 additions & 0 deletions cmake/templates/cvconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
/* Vulkan support */
#cmakedefine HAVE_VULKAN

/* Webnn support */
#cmakedefine HAVE_WEBNN

/* Define to 1 if you have the <inttypes.h> header file. */
#cmakedefine HAVE_INTTYPES_H 1

Expand Down
10 changes: 9 additions & 1 deletion modules/dnn/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ if(HAVE_TENGINE)
list(APPEND libs -Wl,--whole-archive ${TENGINE_LIBRARIES} -Wl,--no-whole-archive)
endif()

set(webnn_srcs "")
if(HAVE_WEBNN)
list(APPEND include_dirs ${WEBNN_HEADER_DIRS})
list(APPEND include_dirs ${WEBNN_INCLUDE_DIRS})
list(APPEND libs -Wl,--whole-archive ${WEBNN_LIBRARIES} -Wl,--no-whole-archive)
list(APPEND webnn_srcs $ENV{WEBNN_NATIVE_DIR}/gen/src/webnn/webnn_cpp.cpp)
endif()

ocv_module_include_directories(${include_dirs})
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
ocv_append_source_files_cxx_compiler_options(fw_srcs "-Wno-suggest-override") # GCC
Expand Down Expand Up @@ -162,7 +170,7 @@ if(HAVE_NGRAPH)
list(APPEND dnn_runtime_libs ngraph::ngraph)
endif()

ocv_glob_module_sources(${sources_options} SOURCES ${fw_srcs})
ocv_glob_module_sources(${sources_options} SOURCES ${fw_srcs} ${webnn_srcs})
ocv_create_module(${libs} ${dnn_runtime_libs})
ocv_add_samples()
ocv_add_accuracy_tests(${dnn_runtime_libs})
Expand Down
3 changes: 3 additions & 0 deletions modules/dnn/include/opencv2/dnn/dnn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ CV__DNN_INLINE_NS_BEGIN
DNN_BACKEND_OPENCV,
DNN_BACKEND_VKCOM,
DNN_BACKEND_CUDA,
DNN_BACKEND_WEBNN,
#ifdef __OPENCV_BUILD
DNN_BACKEND_INFERENCE_ENGINE_NGRAPH = 1000000, // internal - use DNN_BACKEND_INFERENCE_ENGINE + setInferenceEngineBackendType()
DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019, // internal - use DNN_BACKEND_INFERENCE_ENGINE + setInferenceEngineBackendType()
Expand Down Expand Up @@ -298,6 +299,8 @@ CV__DNN_INLINE_NS_BEGIN

virtual Ptr<BackendNode> initVkCom(const std::vector<Ptr<BackendWrapper> > &inputs);

virtual Ptr<BackendNode> initWebnn(const std::vector<Ptr<BackendWrapper> > &inputs, const std::vector<Ptr<BackendNode> >& nodes);

/**
* @brief Returns a CUDA backend node
*
Expand Down
Loading

0 comments on commit 89eed5d

Please sign in to comment.