Skip to content

Commit 1a26f99

Browse files
edgarribaalalek
authored andcommitted
Merge pull request #720 from edgarriba:dnn-modern
GSOC'16 - DNN modern (#720) * dnn_modern module sketelon add FindTinyCNN.cmake and setup CMakeLists.txt add find TBB and NNPACK to CMakeLists.txt add caffe converter skeleton add simple sample eval method return scores Update README.md Update README.md Update README.md * update for tiny-dnn package * add QUIET attribute to CMake to find tiny-dnn * update files due to library rename * disable warning in cmake * replace cnn_size_t -> size_t * disable serializer, remove Caffe dependency and remove tiny-dnn target * check CMake version to enable/disable the module * add -Wno-erro in module target * update README.md with tiny-dnn * remove redunadant CMake codes for TBB and OMP * update CMake to enable C++11 compiler * sync CMake compiler options with upstream project * remove unused FindTBB.cmake * minor fix to add const variables * tiny-cnn -> tiny-dnn
1 parent 11a0721 commit 1a26f99

File tree

9 files changed

+775
-0
lines changed

9 files changed

+775
-0
lines changed

modules/dnn_modern/CMakeLists.txt

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
set(the_description "Modern Deep Learning module")
2+
3+
if(${CMAKE_VERSION} VERSION_LESS 3.2)
4+
message(STATUS "Module opencv_dnn_modern disabled because CMake version is less than 3.2")
5+
ocv_module_disable(dnn_modern)
6+
return()
7+
endif()
8+
9+
cmake_policy(SET CMP0028 OLD)
10+
11+
# Using cmake scripts and modules
12+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
13+
14+
# ----------------------------------------------------------------------------
15+
# MODULE REQUIREMENTS
16+
# ----------------------------------------------------------------------------
17+
18+
find_package(TinyDNN QUIET)
19+
20+
include(CheckCXXCompilerFlag)
21+
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
22+
23+
if(NOT TinyDNN_FOUND)
24+
message(STATUS "Module opencv_dnn_modern disabled because tiny-dnn was not found")
25+
ocv_module_disable(dnn_modern)
26+
return()
27+
elseif(NOT COMPILER_SUPPORTS_CXX11)
28+
message(STATUS "Module opencv_dnn_modern disabled because your compiler does not support C++11")
29+
ocv_module_disable(dnn_modern)
30+
return()
31+
elseif(APPLE_FRAMEWORK OR ANDROID)
32+
message(STATUS "Module opencv_dnn_modern disabled because you are not under Linux or Win")
33+
ocv_module_disable(dnn_modern)
34+
return()
35+
endif()
36+
37+
# ----------------------------------------------------------------------------
38+
# OPTIMIZATION OPTIONS
39+
# ----------------------------------------------------------------------------
40+
41+
option(TINYDNN_USE_SSE "Build tiny-dnn with SSE library support" ON)
42+
option(TINYDNN_USE_AVX "Build tiny-dnn with AVX library support" ON)
43+
option(TINYDNN_USE_TBB "Build tiny-dnn with TBB library support" OFF)
44+
option(TINYDNN_USE_OMP "Build tiny-dnn with OMP library support" OFF)
45+
option(TINYDNN_USE_NNPACK "Build tiny-dnn with NNPACK library support" OFF)
46+
47+
if(TINYDNN_USE_TBB AND HAVE_TBB)
48+
add_definitions(-DCNN_USE_TBB)
49+
elseif(NOT TINYDNN_USE_TBB AND
50+
TINYDNN_USE_OMP AND HAVE_OPENMP)
51+
add_definitions(-DCNN_USE_OMP)
52+
endif()
53+
54+
if(TINYDNN_USE_NNPACK)
55+
find_package(NNPACK REQUIRED)
56+
add_definitions(-DCNN_USE_NNPACK)
57+
include_directories(SYSTEM ${NNPACK_INCLUDE_DIR})
58+
include_directories(SYSTEM ${NNPACK_INCLUDE_DIR}/../third-party/pthreadpool/include)
59+
list(APPEND REQUIRED_LIBRARIES ${NNPACK_LIB})
60+
endif()
61+
62+
# we need to disable seializer unless we import cereal
63+
add_definitions(-DCNN_NO_SERIALIZATION)
64+
65+
# NOTE: In case that proto files already exist,
66+
# this is not needed anymore.
67+
find_package(Protobuf)
68+
list(APPEND REQUIRED_LIBRARIES ${PROTOBUF_LIBRARIES})
69+
70+
####
71+
# Setup the compiler options
72+
73+
# set c++ standard to c++11.
74+
# Note: not working on CMake 2.8. We assume that user has
75+
# a compiler with C++11 support.
76+
77+
set(CMAKE_CXX_STANDARD 11)
78+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
79+
message(STATUS "C++11 support has been enabled by default.")
80+
81+
# Unix
82+
if(CMAKE_COMPILER_IS_GNUCXX OR MINGW OR
83+
CMAKE_CXX_COMPILER_ID MATCHES "Clang")
84+
include(CheckCXXCompilerFlag)
85+
check_cxx_compiler_flag("-msse3" COMPILER_HAS_SSE_FLAG)
86+
check_cxx_compiler_flag("-mavx" COMPILER_HAS_AVX_FLAG)
87+
check_cxx_compiler_flag("-mavx2" COMPILER_HAS_AVX2_FLAG)
88+
check_cxx_compiler_flag("-mfma" COMPILER_HAS_AVX2_FLAG)
89+
90+
# set Streaming SIMD Extension (SSE) instructions
91+
if(USE_SSE AND COMPILER_HAS_SSE_FLAG)
92+
add_definitions(-DCNN_USE_SSE)
93+
set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} -msse3")
94+
endif(USE_SSE AND COMPILER_HAS_SSE_FLAG)
95+
# set Advanced Vector Extensions (AVX)
96+
if(USE_AVX AND COMPILER_HAS_AVX_FLAG)
97+
add_definitions(-DCNN_USE_AVX)
98+
set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} -mavx")
99+
endif(USE_AVX AND COMPILER_HAS_AVX_FLAG)
100+
# set Advanced Vector Extensions 2 (AVX2)
101+
if(USE_AVX2 AND COMPILER_HAS_AVX2_FLAG)
102+
add_definitions(-DCNN_USE_AVX2)
103+
set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} -mavx2 -mfma -march=core-avx2")
104+
endif(USE_AVX2 AND COMPILER_HAS_AVX2_FLAG)
105+
106+
# include extra flags to the compiler
107+
# TODO: add info about those flags.
108+
set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} -Wall -Wpedantic -Wno-narrowing")
109+
set(EXTRA_C_FLAGS_RELEASE "${EXTRA_C_FLAGS_RELEASE} -O3")
110+
set(EXTRA_C_FLAGS_DEBUG "${EXTRA_C_FLAGS_DEBUG} -g3 -pthread")
111+
elseif(MSVC)
112+
if(USE_SSE)
113+
add_definitions(-DCNN_USE_SSE)
114+
set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} /arch:SSE2")
115+
endif(USE_SSE)
116+
if(USE_AVX)
117+
add_definitions(-DCNN_USE_AVX)
118+
set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} /arch:AVX")
119+
endif(USE_AVX)
120+
if(USE_AVX2)
121+
add_definitions(-DCNN_USE_AVX2)
122+
set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} /arch:AVX2")
123+
endif(USE_AVX2)
124+
# include specific flags for release and debug modes.
125+
set(EXTRA_C_FLAGS_RELEASE "${EXTRA_C_FLAGS_RELEASE}
126+
/Ox /Oi /Ot /Oy /GL /fp:fast /GS-")
127+
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG")
128+
set(EXTRA_C_FLAGS_DEBUG "${EXTRA_C_FLAGS_DEBUG}")
129+
set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} /W4 /bigobj")
130+
# this is fine
131+
add_definitions(-D _CRT_SECURE_NO_WARNINGS)
132+
add_definitions(-D _SCL_SECURE_NO_WARNINGS)
133+
# prolly powerless with header-only project
134+
set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} /MP")
135+
endif()
136+
137+
# ----------------------------------------------------------------------------
138+
# DNN-MODERN MODULE
139+
# ----------------------------------------------------------------------------
140+
141+
ocv_define_module(dnn_modern opencv_core opencv_imgproc opencv_imgcodecs WRAP python)
142+
ocv_target_link_libraries(${the_module} ${REQUIRED_LIBRARIES})
143+
ocv_target_include_directories(${the_module} ${TINYDNN_INCLUDE_DIRS})
144+
target_compile_options(${the_module} PRIVATE "-Wno-error=non-virtual-dtor")

modules/dnn_modern/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Modern Deep Learning Module
2+
===========================
3+
4+
The module is wrapper to [tiny-dnn](https://github.com/tiny-dnn/tiny-dnn)
5+
6+
A header only, dependency-free deep learning framework in C++11
7+
8+
Installation
9+
------------
10+
11+
**Required Dependencies**
12+
- System under Unix or Windows
13+
- C++11 compiler
14+
- tiny-dnn headers
15+
16+
**How to install tiny-dnn?**
17+
18+
Download tiny-dnn project somewhere in your system
19+
20+
cd /opt
21+
git clone https://github.com/tiny-dnn/tiny-dnn.git
22+
23+
Run your OpenCV CMake pointing to your tiny-dnn headers location
24+
25+
cd /opt/opencv/build
26+
cmake -DTINYDNN_ROOT=/opt/tiny-dnn ..
27+
make -j4
28+
29+
**Extra**
30+
31+
You can enable some optimizations just for tiny-dnn backend
32+
33+
cmake -DTINYDNN_USE_SSE=ON ..
34+
cmake -DTINYDNN_USE_AVX=ON ..
35+
36+
Use third-party multithreading libs: TBB or OMP.
37+
38+
cmake -DTINYDNN_USE_TBB=ON .. // then disable OMP
39+
cmake -DTINYDNN_USE_OMP=ON .. // then disable TBB
40+
41+
NNPACK: Acceleration package for neural networks on multi-core CPUs.<br />
42+
Check project site for installation: [https://github.com/Maratyszcza/NNPACK](https://github.com/Maratyszcza/NNPACK)
43+
44+
cmake -DTINYDNN_USE_NNPACK=ON .. // not supported yet for Caffe loader
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
SET(NNPACK_INCLUDE_SEARCH_PATHS
2+
/usr/include
3+
/usr/local/include
4+
/opt/NNPACK/include
5+
$ENV{NNPACK_ROOT}
6+
$ENV{NNPACK_ROOT}/include
7+
)
8+
9+
SET(NNPACK_LIB_SEARCH_PATHS
10+
/lib/
11+
/lib64/
12+
/usr/lib
13+
/usr/lib64
14+
/usr/local/lib
15+
/usr/local/lib64
16+
/opt/NNPACK/lib
17+
$ENV{NNPACK_ROOT}
18+
$ENV{NNPACK_ROOT}/lib
19+
)
20+
21+
FIND_PATH(NNPACK_INCLUDE_DIR NAMES nnpack.h PATHS ${NNPACK_INCLUDE_SEARCH_PATHS})
22+
FIND_LIBRARY(NNPACK_LIB NAMES nnpack PATHS ${NNPACK_LIB_SEARCH_PATHS})
23+
24+
SET(NNPACK_FOUND ON)
25+
26+
# Check include files
27+
IF(NOT NNPACK_INCLUDE_DIR)
28+
SET(NNPACK_FOUND OFF)
29+
MESSAGE(STATUS "Could not find NNPACK include. Turning NNPACK_FOUND off")
30+
ENDIF()
31+
32+
# Check libraries
33+
IF(NOT NNPACK_LIB)
34+
SET(NNPACK_FOUND OFF)
35+
MESSAGE(STATUS "Could not find NNPACK lib. Turning NNPACK_FOUND off")
36+
ENDIF()
37+
38+
IF (NNPACK_FOUND)
39+
add_definitions(-DUSE_NNPACK)
40+
IF (NOT NNPACK_FIND_QUIETLY)
41+
MESSAGE(STATUS "Found NNPACK libraries: ${NNPACK_LIB}")
42+
MESSAGE(STATUS "Found NNPACK include: ${NNPACK_INCLUDE_DIR}")
43+
ENDIF (NOT NNPACK_FIND_QUIETLY)
44+
ELSE (NNPACK_FOUND)
45+
IF (NNPACK_FIND_REQUIRED)
46+
MESSAGE(FATAL_ERROR "Could not find NNPACK")
47+
ENDIF (NNPACK_FIND_REQUIRED)
48+
ENDIF (NNPACK_FOUND)
49+
50+
MARK_AS_ADVANCED(
51+
NNPACK_INCLUDE_DIR
52+
NNPACK_LIB
53+
NNPACK
54+
)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Locate the tiny-dnn library.
2+
#
3+
# Defines the following variables:
4+
#
5+
# TinyDNN_FOUND - TRUE if the tiny-dnn headers are found
6+
# TINYDNN_INCLUDE_DIRS - The path to tiny-dnn headers
7+
#
8+
# Accepts the following variables as input:
9+
#
10+
# TinyDNN_ROOT - (as a CMake or environment variable)
11+
# The root directory of the tiny-dnn install prefix
12+
13+
message(STATUS "Looking for tiny_dnn.h")
14+
15+
set(TINYDNN_INCLUDE_SEARCH_PATHS
16+
/usr/include/tiny_dnn
17+
/usr/local/include/tiny_dnn
18+
/opt/tiny_dnn
19+
$ENV{TINYDNN_ROOT}
20+
${TINYDNN_ROOT}
21+
${TINYDNN_ROOT}/tiny_dnn
22+
)
23+
24+
find_path(TINYDNN_INCLUDE_DIR
25+
NAMES tiny_dnn/tiny_dnn.h
26+
HINTS ${TINYDNN_INCLUDE_SEARCH_PATHS}
27+
)
28+
29+
# handle the QUIETLY and REQUIRED arguments and set TinyDNN_FOUND to TRUE if
30+
# all listed variables are TRUE
31+
include(FindPackageHandleStandardArgs)
32+
find_package_handle_standard_args(TinyDNN
33+
FOUND_VAR TinyDNN_FOUND
34+
REQUIRED_VARS TINYDNN_INCLUDE_DIR)
35+
36+
if(TinyDNN_FOUND)
37+
set(TINYDNN_INCLUDE_DIRS ${TINYDNN_INCLUDE_DIR})
38+
message(STATUS "Looking for tiny_dnn.h - found")
39+
message(STATUS "Found tiny-dnn in: ${TINYDNN_INCLUDE_DIRS}")
40+
else()
41+
message(STATUS "Looking for tiny_dnn.h - not found")
42+
endif()
43+
44+
mark_as_advanced(
45+
TINYDNN_INCLUDE_DIRS
46+
)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
By downloading, copying, installing or using the software you agree to this license.
3+
If you do not agree to this license, do not download, install,
4+
copy or use the software.
5+
6+
7+
License Agreement
8+
For Open Source Computer Vision Library
9+
(3-clause BSD License)
10+
11+
Copyright (C) 2000-2016, Intel Corporation, all rights reserved.
12+
Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
13+
Copyright (C) 2009-2016, NVIDIA Corporation, all rights reserved.
14+
Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
15+
Copyright (C) 2015-2016, OpenCV Foundation, all rights reserved.
16+
Copyright (C) 2015-2016, Itseez Inc., all rights reserved.
17+
Third party copyrights are property of their respective owners.
18+
19+
Redistribution and use in source and binary forms, with or without modification,
20+
are permitted provided that the following conditions are met:
21+
22+
* Redistributions of source code must retain the above copyright notice,
23+
this list of conditions and the following disclaimer.
24+
25+
* Redistributions in binary form must reproduce the above copyright notice,
26+
this list of conditions and the following disclaimer in the documentation
27+
and/or other materials provided with the distribution.
28+
29+
* Neither the names of the copyright holders nor the names of the contributors
30+
may be used to endorse or promote products derived from this software
31+
without specific prior written permission.
32+
33+
This software is provided by the copyright holders and contributors "as is" and
34+
any express or implied warranties, including, but not limited to, the implied
35+
warranties of merchantability and fitness for a particular purpose are disclaimed.
36+
In no event shall copyright holders or contributors be liable for any direct,
37+
indirect, incidental, special, exemplary, or consequential damages
38+
(including, but not limited to, procurement of substitute goods or services;
39+
loss of use, data, or profits; or business interruption) however caused
40+
and on any theory of liability, whether in contract, strict liability,
41+
or tort (including negligence or otherwise) arising in any way out of
42+
the use of this software, even if advised of the possibility of such damage.
43+
*/
44+
45+
#ifndef __OPENCV_DNN_M_HPP__
46+
#define __OPENCV_DNN_M_HPP__
47+
48+
#include "opencv2/core.hpp"
49+
#include "opencv2/imgcodecs.hpp"
50+
#include "opencv2/imgproc.hpp"
51+
52+
/** @defgroup dnn_modern Deep Learning Modern Module
53+
@{
54+
55+
Base class for tiny-dnn converter
56+
57+
@}
58+
*/
59+
60+
namespace cv {
61+
namespace dnn2 {
62+
63+
class CV_EXPORTS_W BaseConverter
64+
{
65+
public:
66+
virtual ~BaseConverter() {};
67+
virtual void eval(const cv::InputArray image, std::vector<float_t>* results) = 0;
68+
};
69+
70+
/** @brief Class implementing the CaffeConverter.
71+
72+
Implementation of tiny-dnn Caffe converter.
73+
Loads a pretrained Caffe model. Only support simple sequential models.
74+
75+
*/
76+
class CV_EXPORTS_W CaffeConverter : public BaseConverter {
77+
public:
78+
79+
/**
80+
@param model_file path to the prototxt file.
81+
@param trained_file path to the caffemodel file.
82+
@param mean_file path to binaryproto file.
83+
*/
84+
CV_WRAP static Ptr<CaffeConverter> create(const cv::String& model_file,
85+
const cv::String& trained_file,
86+
const cv::String& mean_file=cv::String());
87+
88+
virtual void eval(const cv::InputArray image, std::vector<float_t>* results) = 0;
89+
};
90+
91+
} // namespace dnn2
92+
} // namespace cv
93+
94+
#endif
95+
96+
/* End of file. */

0 commit comments

Comments
 (0)