From 3059b16a02e9c7cb91b22ed4d99aaf1c6ac8d4ab Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Mon, 16 Apr 2018 15:42:05 +0300 Subject: [PATCH 1/3] ximgproc: fix weightedMedianFilter() hang - eliminated "float th = 1e-5f" magic value (replaced to explicit checks) - eliminate manual memory allocation - avoid out of range access (i == alls) --- .../ximgproc/src/weighted_median_filter.cpp | 62 ++++++++++++------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/modules/ximgproc/src/weighted_median_filter.cpp b/modules/ximgproc/src/weighted_median_filter.cpp index d438fb962..2c1771b65 100644 --- a/modules/ximgproc/src/weighted_median_filter.cpp +++ b/modules/ximgproc/src/weighted_median_filter.cpp @@ -55,51 +55,64 @@ using namespace cv::ximgproc; void from32FTo32S(Mat &img, Mat &outImg, int nI, float *mapping) { int rows = img.rows, cols = img.cols; - int alls = rows * cols; + size_t alls = (size_t)rows * cols; + CV_Assert(img.isContinuous()); float *imgPtr = img.ptr(); typedef pair pairFI; - pairFI *data = (pairFI *)malloc(alls*sizeof(pairFI)); + std::vector data(alls); // Sort all pixels of the image by ascending order of pixel value - for(int i=0;i th) + while (r > l) { - float m = (r+l)*0.5f; + float m = (r + l) * 0.5f; + if (m == r || m == l) + break; // bailout on numeric accuracy limit bool suc = true; float base = (float)minVal; - int cnt=0; - for(int i=0;ibase+m) + if (data[i].first > base + m) { cnt++; base = data[i].first; - if(cnt==nI) + if (cnt == nI) { suc = false; break; } } } - if(suc)r=m; - else l=m; + if (suc) + r = m; + else + l = m; } Mat retImg(img.size(),CV_32SC1); + CV_Assert(retImg.isContinuous()); int *retImgPtr = retImg.ptr(); // In the sorted list, divide pixel values into clusters according to the minimum error bound @@ -108,23 +121,22 @@ void from32FTo32S(Mat &img, Mat &outImg, int nI, float *mapping) float base = (float)minVal; int baseI = 0; int cnt = 0; - for(int i=0;i<=alls;i++) + for (size_t i = 0; i < alls; i++) { - if(i==alls || data[i].first>base+r) + if (data[i].first > base + r) { mapping[cnt] = data[(baseI+i-1)>>1].first; //median - if(i==alls)break; cnt++; base = data[i].first; baseI = i; } retImgPtr[data[i].second] = cnt; } - - free(data); + // tail: i == alls + mapping[cnt] = data[(baseI+alls-1)>>1].first; // median //end of the function - outImg = retImg; + swap(outImg, retImg); } /***************************************************************/ @@ -134,6 +146,8 @@ void from32FTo32S(Mat &img, Mat &outImg, int nI, float *mapping) void from32STo32F(Mat &img, Mat &outImg, float *mapping) { Mat retImg(img.size(),CV_32F); + CV_Assert(img.isContinuous()); + CV_Assert(retImg.isContinuous()); int rows = img.rows, cols = img.cols, alls = rows*cols; float *retImgPtr = retImg.ptr(); int *imgPtr = img.ptr(); From eebc869baab941524210cf7c45b2af934ca6d252 Mon Sep 17 00:00:00 2001 From: berak Date: Tue, 17 Apr 2018 07:48:32 +0200 Subject: [PATCH 2/3] face: fix missing faceDetectorData arg in facemarkLBF.cpp --- modules/face/src/facemarkLBF.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/face/src/facemarkLBF.cpp b/modules/face/src/facemarkLBF.cpp index 61ee4b28d..501922865 100644 --- a/modules/face/src/facemarkLBF.cpp +++ b/modules/face/src/facemarkLBF.cpp @@ -466,7 +466,7 @@ Rect FacemarkLBFImpl::getBBox(Mat &img, const Mat_ shape) { if(!faceDetector){ defaultFaceDetector(img, rects); }else{ - faceDetector(img, rects,0); + faceDetector(img, rects, faceDetectorData); } if (rects.size() == 0) return Rect(-1, -1, -1, -1); From f7ca9ef279bbb9d7bbb791548725d91e46725550 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Fri, 20 Apr 2018 12:50:03 +0300 Subject: [PATCH 3/3] cleanup: drop dnn_modern - almost all functionality of this module is covered by OpenCV dnn module - module build has been broken for the long time due strong conflict with OpenCV dnn module - requires external tinydnn library - there are no tests --- modules/dnn_modern/CMakeLists.txt | 180 ---------------- modules/dnn_modern/README.md | 50 ----- modules/dnn_modern/cmake/FindNNPACK.cmake | 54 ----- modules/dnn_modern/cmake/FindTinyDNN.cmake | 47 ---- .../dnn_modern/include/opencv2/dnn_modern.hpp | 106 --------- modules/dnn_modern/samples/simple_test.cpp | 93 -------- modules/dnn_modern/src/caffe_converter.cpp | 204 ------------------ modules/dnn_modern/src/dnn_modern_init.cpp | 47 ---- modules/dnn_modern/src/precomp.hpp | 50 ----- 9 files changed, 831 deletions(-) delete mode 100644 modules/dnn_modern/CMakeLists.txt delete mode 100644 modules/dnn_modern/README.md delete mode 100644 modules/dnn_modern/cmake/FindNNPACK.cmake delete mode 100644 modules/dnn_modern/cmake/FindTinyDNN.cmake delete mode 100644 modules/dnn_modern/include/opencv2/dnn_modern.hpp delete mode 100644 modules/dnn_modern/samples/simple_test.cpp delete mode 100644 modules/dnn_modern/src/caffe_converter.cpp delete mode 100644 modules/dnn_modern/src/dnn_modern_init.cpp delete mode 100644 modules/dnn_modern/src/precomp.hpp diff --git a/modules/dnn_modern/CMakeLists.txt b/modules/dnn_modern/CMakeLists.txt deleted file mode 100644 index 8e8f1b848..000000000 --- a/modules/dnn_modern/CMakeLists.txt +++ /dev/null @@ -1,180 +0,0 @@ -set(the_description "Modern Deep Learning module") - -if(${CMAKE_VERSION} VERSION_LESS 3.2) - message(STATUS "Module opencv_dnn_modern disabled because CMake version is less than 3.2") - ocv_module_disable(dnn_modern) - return() -endif() - -# Using cmake scripts and modules -list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) - -# ---------------------------------------------------------------------------- -# MODULE REQUIREMENTS -# ---------------------------------------------------------------------------- - -set(TINY_DNN_CPP_PATH "${OpenCV_BINARY_DIR}/3rdparty/tinydnn") -set(TINY_DNN_CPP_ROOT "${TINY_DNN_CPP_PATH}/tiny-dnn-1.0.0a3") -ocv_download(FILENAME "v1.0.0a3.tar.gz" - HASH "adb1c512e09ca2c7a6faef36f9c53e59" - URL - "${OPENCV_TINY_DNN_URL}" - "$ENV{OPENCV_TINY_DNN_URL}" - "https://github.com/tiny-dnn/tiny-dnn/archive/" - DESTINATION_DIR "${TINY_DNN_CPP_PATH}" - STATUS TINY_DNN_DOWNLOAD_SUCCESS - ID "tiny-dnn" - UNPACK RELATIVE_URL) - -if(NOT TINY_DNN_DOWNLOAD_SUCCESS) - message(STATUS "Failed to download tiny-dnn sources") -endif() - -find_package(TinyDNN QUIET) - -include(CheckCXXCompilerFlag) -CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11) - -if(NOT TinyDNN_FOUND) - message(STATUS "Module opencv_dnn_modern disabled because tiny-dnn was not found") - ocv_module_disable(dnn_modern) - return() -elseif(NOT COMPILER_SUPPORTS_CXX11) - message(STATUS "Module opencv_dnn_modern disabled because your compiler does not support C++11") - ocv_module_disable(dnn_modern) - return() -elseif(APPLE_FRAMEWORK OR ANDROID) - message(STATUS "Module opencv_dnn_modern disabled because you are not under Linux or Win") - ocv_module_disable(dnn_modern) - return() -endif() - -# ---------------------------------------------------------------------------- -# OPTIMIZATION OPTIONS -# ---------------------------------------------------------------------------- - -option(TINYDNN_USE_SSE "Build tiny-dnn with SSE library support" ON) -option(TINYDNN_USE_AVX "Build tiny-dnn with AVX library support" ON) -option(TINYDNN_USE_TBB "Build tiny-dnn with TBB library support" OFF) -option(TINYDNN_USE_OMP "Build tiny-dnn with OMP library support" OFF) -option(TINYDNN_USE_NNPACK "Build tiny-dnn with NNPACK library support" OFF) - -if(TINYDNN_USE_TBB AND HAVE_TBB) - add_definitions(-DCNN_USE_TBB) -elseif(NOT TINYDNN_USE_TBB AND - TINYDNN_USE_OMP AND HAVE_OPENMP) - add_definitions(-DCNN_USE_OMP) -endif() - -if(TINYDNN_USE_NNPACK) - find_package(NNPACK REQUIRED) - add_definitions(-DCNN_USE_NNPACK) - include_directories(SYSTEM ${NNPACK_INCLUDE_DIR}) - include_directories(SYSTEM ${NNPACK_INCLUDE_DIR}/../third-party/pthreadpool/include) - list(APPEND REQUIRED_LIBRARIES ${NNPACK_LIB}) -endif() - -# we need to disable seializer unless we import cereal and we gonna use caffe converter -add_definitions(-DCNN_NO_SERIALIZATION -DCNN_USE_CAFFE_CONVERTER) - -# NOTE: In case that proto files already exist, -# this is not needed anymore. -if(NOT BUILD_PROTOBUF) - find_package(Protobuf QUIET) -endif() - -if(NOT ${Protobuf_FOUND}) - message(STATUS "Module opencv_dnn_modern disabled because Protobuf is not found") - ocv_module_disable(dnn_modern) - return() -endif() - -if(DEFINED PROTOBUF_PROTOC_EXECUTABLE AND EXISTS ${PROTOBUF_PROTOC_EXECUTABLE}) - execute_process(COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} caffe.proto --cpp_out=./ - WORKING_DIRECTORY ${TINYDNN_INCLUDE_DIRS}/tiny_dnn/io/caffe) -else() - message(STATUS "The protocol buffer compiler is not found (PROTOBUF_PROTOC_EXECUTABLE='${PROTOBUF_PROTOC_EXECUTABLE}')") - ocv_module_disable(dnn_modern) - return() -endif() - -list(APPEND REQUIRED_LIBRARIES ${PROTOBUF_LIBRARIES}) -include_directories(SYSTEM ${PROTOBUF_INCLUDE_DIRS}) - -#### -# Setup the compiler options - -# set c++ standard to c++11. -# Note: not working on CMake 2.8. We assume that user has -# a compiler with C++11 support. - -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_STANDARD_REQUIRED ON) - -# Unix -if(CMAKE_COMPILER_IS_GNUCXX OR MINGW OR - CMAKE_CXX_COMPILER_ID MATCHES "Clang") - include(CheckCXXCompilerFlag) - check_cxx_compiler_flag("-msse3" COMPILER_HAS_SSE_FLAG) - check_cxx_compiler_flag("-mavx" COMPILER_HAS_AVX_FLAG) - check_cxx_compiler_flag("-mavx2" COMPILER_HAS_AVX2_FLAG) - check_cxx_compiler_flag("-mfma" COMPILER_HAS_AVX2_FLAG) - - # set Streaming SIMD Extension (SSE) instructions - if(USE_SSE AND COMPILER_HAS_SSE_FLAG) - add_definitions(-DCNN_USE_SSE) - set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} -msse3") - endif(USE_SSE AND COMPILER_HAS_SSE_FLAG) - # set Advanced Vector Extensions (AVX) - if(USE_AVX AND COMPILER_HAS_AVX_FLAG) - add_definitions(-DCNN_USE_AVX) - set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} -mavx") - endif(USE_AVX AND COMPILER_HAS_AVX_FLAG) - # set Advanced Vector Extensions 2 (AVX2) - if(USE_AVX2 AND COMPILER_HAS_AVX2_FLAG) - add_definitions(-DCNN_USE_AVX2) - set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} -mavx2 -mfma -march=core-avx2") - endif(USE_AVX2 AND COMPILER_HAS_AVX2_FLAG) - - # include extra flags to the compiler - # TODO: add info about those flags. - set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} -Wall -Wpedantic -Wno-narrowing") - set(EXTRA_C_FLAGS_RELEASE "${EXTRA_C_FLAGS_RELEASE} -O3") - set(EXTRA_C_FLAGS_DEBUG "${EXTRA_C_FLAGS_DEBUG} -g3 -pthread") -elseif(MSVC) - if(USE_SSE) - add_definitions(-DCNN_USE_SSE) - set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} /arch:SSE2") - endif(USE_SSE) - if(USE_AVX) - add_definitions(-DCNN_USE_AVX) - set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} /arch:AVX") - endif(USE_AVX) - if(USE_AVX2) - add_definitions(-DCNN_USE_AVX2) - set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} /arch:AVX2") - endif(USE_AVX2) - # include specific flags for release and debug modes. - set(EXTRA_C_FLAGS_RELEASE "${EXTRA_C_FLAGS_RELEASE} - /Ox /Oi /Ot /Oy /GL /fp:fast /GS-") - set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG") - set(EXTRA_C_FLAGS_DEBUG "${EXTRA_C_FLAGS_DEBUG}") - set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} /W4 /bigobj") - # this is fine - add_definitions(-D _CRT_SECURE_NO_WARNINGS) - add_definitions(-D _SCL_SECURE_NO_WARNINGS) - add_definitions(-D NO_STRICT) - # prolly powerless with header-only project - set(EXTRA_C_FLAGS "${EXTRA_C_FLAGS} /MP") -endif() - -# ---------------------------------------------------------------------------- -# DNN-MODERN MODULE -# ---------------------------------------------------------------------------- - -ocv_define_module(dnn_modern opencv_core opencv_imgproc WRAP python) -ocv_target_link_libraries(${the_module} ${REQUIRED_LIBRARIES}) -ocv_target_include_directories(${the_module} ${TINYDNN_INCLUDE_DIRS}) -ocv_warnings_disable(CMAKE_CXX_FLAGS - -Wnon-virtual-dtor -Wunused-parameter -Wshadow -Wundef - /wd4265 /wd4100 /wd4458 /wd4244 /wd4456) diff --git a/modules/dnn_modern/README.md b/modules/dnn_modern/README.md deleted file mode 100644 index 6588734bd..000000000 --- a/modules/dnn_modern/README.md +++ /dev/null @@ -1,50 +0,0 @@ -Modern Deep Learning Module -=========================== - -The module is wrapper to [tiny-dnn](https://github.com/tiny-dnn/tiny-dnn), -a header only, dependency-free deep learning framework in C++11. - -Installation ------------- - -**Required Dependencies** - - System under Unix or Windows - - C++11 compiler - - tiny-dnn headers - - protoc compiler (part of the [protobuf](https://developers.google.com/protocol-buffers/docs/overview) library) - -**How to install tiny-dnn?** - CMake will try to download a certain version of tiny-dnn which was tested to build with OpenCV. - If the latest version is needed, location of the tiny-dnn folder can be specified manually: - -- Download tiny-dnn project somewhere in your system -``` - cd /opt - git clone https://github.com/tiny-dnn/tiny-dnn.git -``` - -- Run your OpenCV CMake pointing to your tiny-dnn headers location -``` - cd /opt/opencv/build - cmake -DTINYDNN_ROOT=/opt/tiny-dnn .. - make -j4 -``` - -**Extra** - - You can enable some optimizations just for tiny-dnn backend - - cmake -DTINYDNN_USE_SSE=ON .. - cmake -DTINYDNN_USE_AVX=ON .. - - Use third-party multithreading libs: TBB or OMP. - - cmake -DTINYDNN_USE_TBB=ON .. // then disable OMP - cmake -DTINYDNN_USE_OMP=ON .. // then disable TBB - - NNPACK: Acceleration package for neural networks on multi-core CPUs.
- Check project site for installation: [https://github.com/Maratyszcza/NNPACK](https://github.com/Maratyszcza/NNPACK) - - cmake -DTINYDNN_USE_NNPACK=ON .. // not supported yet for Caffe loader - -See detailed module API documentation in http://docs.opencv.org/trunk/d1/df7/group__dnn__modern.html diff --git a/modules/dnn_modern/cmake/FindNNPACK.cmake b/modules/dnn_modern/cmake/FindNNPACK.cmake deleted file mode 100644 index c296de6d8..000000000 --- a/modules/dnn_modern/cmake/FindNNPACK.cmake +++ /dev/null @@ -1,54 +0,0 @@ -SET(NNPACK_INCLUDE_SEARCH_PATHS - /usr/include - /usr/local/include - /opt/NNPACK/include - $ENV{NNPACK_ROOT} - $ENV{NNPACK_ROOT}/include -) - -SET(NNPACK_LIB_SEARCH_PATHS - /lib/ - /lib64/ - /usr/lib - /usr/lib64 - /usr/local/lib - /usr/local/lib64 - /opt/NNPACK/lib - $ENV{NNPACK_ROOT} - $ENV{NNPACK_ROOT}/lib - ) - -FIND_PATH(NNPACK_INCLUDE_DIR NAMES nnpack.h PATHS ${NNPACK_INCLUDE_SEARCH_PATHS}) -FIND_LIBRARY(NNPACK_LIB NAMES nnpack PATHS ${NNPACK_LIB_SEARCH_PATHS}) - -SET(NNPACK_FOUND ON) - -# Check include files -IF(NOT NNPACK_INCLUDE_DIR) - SET(NNPACK_FOUND OFF) - MESSAGE(STATUS "Could not find NNPACK include. Turning NNPACK_FOUND off") -ENDIF() - -# Check libraries -IF(NOT NNPACK_LIB) - SET(NNPACK_FOUND OFF) - MESSAGE(STATUS "Could not find NNPACK lib. Turning NNPACK_FOUND off") -ENDIF() - -IF (NNPACK_FOUND) - add_definitions(-DUSE_NNPACK) - IF (NOT NNPACK_FIND_QUIETLY) - MESSAGE(STATUS "Found NNPACK libraries: ${NNPACK_LIB}") - MESSAGE(STATUS "Found NNPACK include: ${NNPACK_INCLUDE_DIR}") - ENDIF (NOT NNPACK_FIND_QUIETLY) -ELSE (NNPACK_FOUND) - IF (NNPACK_FIND_REQUIRED) - MESSAGE(FATAL_ERROR "Could not find NNPACK") - ENDIF (NNPACK_FIND_REQUIRED) -ENDIF (NNPACK_FOUND) - -MARK_AS_ADVANCED( - NNPACK_INCLUDE_DIR - NNPACK_LIB - NNPACK -) diff --git a/modules/dnn_modern/cmake/FindTinyDNN.cmake b/modules/dnn_modern/cmake/FindTinyDNN.cmake deleted file mode 100644 index ac4dcaa96..000000000 --- a/modules/dnn_modern/cmake/FindTinyDNN.cmake +++ /dev/null @@ -1,47 +0,0 @@ -# Locate the tiny-dnn library. -# -# Defines the following variables: -# -# TinyDNN_FOUND - TRUE if the tiny-dnn headers are found -# TINYDNN_INCLUDE_DIRS - The path to tiny-dnn headers -# -# Accepts the following variables as input: -# -# TinyDNN_ROOT - (as a CMake or environment variable) -# The root directory of the tiny-dnn install prefix - -message(STATUS "Looking for tiny_dnn.h") - -set(TINYDNN_INCLUDE_SEARCH_PATHS - /usr/include/tiny_dnn - /usr/local/include/tiny_dnn - /opt/tiny_dnn - $ENV{TINYDNN_ROOT} - ${TINYDNN_ROOT} - ${TINYDNN_ROOT}/tiny_dnn - ${TINY_DNN_CPP_ROOT} -) - -find_path(TINYDNN_INCLUDE_DIR - NAMES tiny_dnn/tiny_dnn.h - HINTS ${TINYDNN_INCLUDE_SEARCH_PATHS} -) - -# handle the QUIETLY and REQUIRED arguments and set TinyDNN_FOUND to TRUE if -# all listed variables are TRUE -include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(TinyDNN - FOUND_VAR TinyDNN_FOUND - REQUIRED_VARS TINYDNN_INCLUDE_DIR) - -if(TinyDNN_FOUND) - set(TINYDNN_INCLUDE_DIRS ${TINYDNN_INCLUDE_DIR}) - message(STATUS "Looking for tiny_dnn.h - found") - message(STATUS "Found tiny-dnn in: ${TINYDNN_INCLUDE_DIRS}") -else() - message(STATUS "Looking for tiny_dnn.h - not found") -endif() - -mark_as_advanced( - TINYDNN_INCLUDE_DIRS -) diff --git a/modules/dnn_modern/include/opencv2/dnn_modern.hpp b/modules/dnn_modern/include/opencv2/dnn_modern.hpp deleted file mode 100644 index 335989034..000000000 --- a/modules/dnn_modern/include/opencv2/dnn_modern.hpp +++ /dev/null @@ -1,106 +0,0 @@ -/* - By downloading, copying, installing or using the software you agree to this license. - If you do not agree to this license, do not download, install, - copy or use the software. - - - License Agreement - For Open Source Computer Vision Library - (3-clause BSD License) - - Copyright (C) 2000-2016, Intel Corporation, all rights reserved. - Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. - Copyright (C) 2009-2016, NVIDIA Corporation, all rights reserved. - Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. - Copyright (C) 2015-2016, OpenCV Foundation, all rights reserved. - Copyright (C) 2015-2016, Itseez Inc., all rights reserved. - Third party copyrights are property of their respective owners. - - Redistribution and use in source and binary forms, with or without modification, - are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the names of the copyright holders nor the names of the contributors - may be used to endorse or promote products derived from this software - without specific prior written permission. - - This software is provided by the copyright holders and contributors "as is" and - any express or implied warranties, including, but not limited to, the implied - warranties of merchantability and fitness for a particular purpose are disclaimed. - In no event shall copyright holders or contributors be liable for any direct, - indirect, incidental, special, exemplary, or consequential damages - (including, but not limited to, procurement of substitute goods or services; - loss of use, data, or profits; or business interruption) however caused - and on any theory of liability, whether in contract, strict liability, - or tort (including negligence or otherwise) arising in any way out of - the use of this software, even if advised of the possibility of such damage. - */ - -#ifndef __OPENCV_DNN_M_HPP__ -#define __OPENCV_DNN_M_HPP__ - -#include "opencv2/core.hpp" - -/** @defgroup dnn_modern Deep Learning Modern Module - * This module is based on the [tiny-dnn](https://github.com/tiny-dnn/tiny-dnn) framework. - * The module uses tiny-dnn to load and run pre-trained Caffe models. - * tiny-dnn's converter only supports single input/single output network without branches. -*/ - - -namespace cv { -namespace dnn2 { - -//! @addtogroup dnn_modern -//! @{ - -/** @brief Base class for tiny-dnn converter. - */ -class CV_EXPORTS_W BaseConverter -{ -public: - virtual ~BaseConverter() {}; - - /** - @brief Evaluates single model output on single model input. - @param image input image. - @param results output form model. - */ - CV_WRAP virtual void eval(InputArray image, std::vector& results) = 0; -}; - -/** @brief Class implementing the CaffeConverter. - -Implementation of tiny-dnn Caffe converter. -Loads a pretrained Caffe model. Only support simple sequential models. - - */ -class CV_EXPORTS_W CaffeConverter : public BaseConverter { - public: - - /** - @brief Creates a CaffeConverter object. - - @param model_file path to the prototxt file. - @param trained_file path to the caffemodel file. - @param mean_file path to binaryproto file. - */ - CV_WRAP static Ptr create(const String& model_file, - const String& trained_file, - const String& mean_file = String()); - - CV_WRAP virtual void eval(InputArray image, CV_OUT std::vector& results) = 0; -}; - -//! @} -} // namespace dnn2 -} // namespace cv -#endif - -/* End of file. */ diff --git a/modules/dnn_modern/samples/simple_test.cpp b/modules/dnn_modern/samples/simple_test.cpp deleted file mode 100644 index 965a2afbe..000000000 --- a/modules/dnn_modern/samples/simple_test.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include -#include - -#include -#include - - -using namespace std; -using namespace cv; -using namespace cv::dnn2; - -static void help() { - cout - << "\n----------------------------------------------------------------------------\n" - << " This program shows how to import a Caffe model using the \n" - << " OpenCV Modern Deep Learning module (DNN2).\n" - << " Usage:\n" - << " example_dnn_modern_simple_test \n" - << " \n" - << " where: model_file is the path to the *.prototxt\n" - << " trained_file is the path to the *.caffemodel\n" - << " mean_file is the path to the *.binaryproto\n" - << " label_file is the path to the labels file\n" - << " image_file is the path to the image to evaluate\n" - << "----------------------------------------------------------------------------\n\n" - << endl; -} - -vector get_label_list(const string& label_file); -void print_n_labels(const vector& labels, - const vector& result, - const int top_n); - -vector get_label_list(const string& label_file) { - string line; - ifstream ifs(label_file.c_str()); - - if (ifs.fail() || ifs.bad()) { - throw runtime_error("failed to open:" + label_file); - } - - vector lines; - while (getline(ifs, line)) lines.push_back(line); - - return lines; -} - -void print_n_labels(const vector& labels, - const vector& result, - const int top_n) { - vector sorted(result.begin(), result.end()); - - partial_sort(sorted.begin(), sorted.begin()+top_n, sorted.end(), greater()); - - for (int i = 0; i < top_n; i++) { - size_t idx = distance(result.begin(), find(result.begin(), result.end(), sorted[i])); - cout << labels[idx] << "," << sorted[i] << endl; - } -} - -int main(int argc, char* argv[]) { - - if (argc < 6) { - help(); - exit(0); - } - - int arg_channel = 1; - string model_file = argv[arg_channel++]; - string trained_file = argv[arg_channel++]; - string mean_file = argv[arg_channel++]; - string label_file = argv[arg_channel++]; - string img_file = argv[arg_channel++]; - - // load Caffe model - Ptr caffe_ptr = CaffeConverter::create( - model_file, trained_file, mean_file); - - // load input image - cv::Mat img = cv::imread(img_file, -1); - - // inference ! - vector scores; - caffe_ptr->eval(img, scores); - - // retrieve n labels - const int n = 5; - vector labels = get_label_list(label_file); - - print_n_labels(labels, scores, n); - - return 0; -} diff --git a/modules/dnn_modern/src/caffe_converter.cpp b/modules/dnn_modern/src/caffe_converter.cpp deleted file mode 100644 index b8762892c..000000000 --- a/modules/dnn_modern/src/caffe_converter.cpp +++ /dev/null @@ -1,204 +0,0 @@ -/* - By downloading, copying, installing or using the software you agree to this license. - If you do not agree to this license, do not download, install, - copy or use the software. - - - License Agreement - For Open Source Computer Vision Library - (3-clause BSD License) - - Copyright (C) 2000-2016, Intel Corporation, all rights reserved. - Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. - Copyright (C) 2009-2016, NVIDIA Corporation, all rights reserved. - Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. - Copyright (C) 2015-2016, OpenCV Foundation, all rights reserved. - Copyright (C) 2015-2016, Itseez Inc., all rights reserved. - Third party copyrights are property of their respective owners. - - Redistribution and use in source and binary forms, with or without modification, - are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the names of the copyright holders nor the names of the contributors - may be used to endorse or promote products derived from this software - without specific prior written permission. - - This software is provided by the copyright holders and contributors "as is" and - any express or implied warranties, including, but not limited to, the implied - warranties of merchantability and fitness for a particular purpose are disclaimed. - In no event shall copyright holders or contributors be liable for any direct, - indirect, incidental, special, exemplary, or consequential damages - (including, but not limited to, procurement of substitute goods or services; - loss of use, data, or profits; or business interruption) however caused - and on any theory of liability, whether in contract, strict liability, - or tort (including negligence or otherwise) arising in any way out of - the use of this software, even if advised of the possibility of such damage. - */ - -#include "precomp.hpp" -#include - -#include -#include - -using namespace tiny_dnn; -using namespace tiny_dnn::activation; -using namespace std; - -namespace cv { -namespace dnn2 { - -/* - !CaffeConverter Implementation - */ -class CaffeConverter_Impl : public CaffeConverter { - public: - explicit CaffeConverter_Impl(const String& model_file, - const String& trained_file, - const String& mean_file) { - net_ = create_net_from_caffe_prototxt(model_file); - reload_weight_from_caffe_protobinary(trained_file, net_.get()); - - const size_t width = (*net_)[0]->in_data_shape()[0].width_; - const size_t height = (*net_)[0]->in_data_shape()[0].height_; - - mean_ = compute_mean(mean_file, width, height); - } - - ~CaffeConverter_Impl() {} - - virtual void eval(InputArray image, std::vector& results); - - private: - Mat compute_mean(const string& mean_file, const size_t width, - const size_t height); - - ColorConversionCodes get_cvt_codes(const int src_channels, - const int dst_channels); - - void preprocess(const Mat& img, const Mat& mean, - const int num_channels, const Size& geometry, - vector* input_channels); - - Mat mean_; - std::shared_ptr> net_; -}; - -Mat -CaffeConverter_Impl::compute_mean(const string& mean_file, - const size_t width, - const size_t height) { - caffe::BlobProto blob; - ::detail::read_proto_from_binary(mean_file, &blob); - - vector channels; - auto data = blob.mutable_data()->mutable_data(); - - const size_t offset = blob.height() * blob.width(); - - for (int i = 0; i < blob.channels(); i++, data += offset) { - channels.emplace_back(blob.height(), blob.width(), CV_32FC1, data); - } - - Mat meanChannel; - merge(channels, meanChannel); - - return Mat(Size(width, height), meanChannel.type(), mean(meanChannel)); -} - -ColorConversionCodes -CaffeConverter_Impl::get_cvt_codes(const int src_channels, - const int dst_channels) { - assert(src_channels != dst_channels); - - if (dst_channels == 3) { - return src_channels == 1 ? COLOR_GRAY2BGR : COLOR_BGRA2BGR; - } else if (dst_channels == 1) { - return src_channels == 3 ? COLOR_BGR2GRAY : COLOR_BGRA2GRAY; - } else { - throw runtime_error("unsupported color code"); - } -} - -void CaffeConverter_Impl::preprocess(const Mat& img, - const Mat& mean, - const int num_channels, - const Size& geometry, - vector* input_channels) { - Mat sample; - - // convert color - if (img.channels() != num_channels) { - cvtColor(img, sample, - get_cvt_codes(img.channels(), num_channels)); - } else { - sample = img; - } - - // resize - Mat sample_resized; - resize(sample, sample_resized, geometry); - - Mat sample_float; - sample_resized.convertTo(sample_float, - num_channels == 3 ? CV_32FC3 : CV_32FC1); - - // subtract mean - if (mean.size().width > 0) { - Mat sample_normalized; - subtract(sample_float, mean, sample_normalized); - split(sample_normalized, *input_channels); - } - else { - split(sample_float, *input_channels); - } -} - -void CaffeConverter_Impl::eval(InputArray image, - std::vector& results) { - const Mat img = image.getMat(); - - const size_t channels = (*net_)[0]->in_data_shape()[0].depth_; - const size_t width = (*net_)[0]->in_data_shape()[0].width_; - const size_t height = (*net_)[0]->in_data_shape()[0].height_; - - vector input_channels; - vector inputvec(width*height*channels); - - for (size_t i = 0; i < channels; i++) { - input_channels.emplace_back(height, width, CV_32FC1, - &inputvec[width*height*i]); - } - - // subtract mean from input - preprocess(img, mean_, 3, Size(width, height), &input_channels); - - const vector vec(inputvec.begin(), inputvec.end()); - - // perform inderence - auto result = net_->predict(vec); - - // allocate output - results.clear(); - results.reserve(result.size()); - - for (size_t i = 0; i < result.size(); i++) { - results.push_back(result[i]); - } -} - -Ptr CaffeConverter::create(const String& model_file, - const String& trained_file, - const String& mean_file) { - return makePtr(model_file, trained_file, mean_file); -} - -} // namespace dnn2 -} // namespace cv diff --git a/modules/dnn_modern/src/dnn_modern_init.cpp b/modules/dnn_modern/src/dnn_modern_init.cpp deleted file mode 100644 index 441ec4310..000000000 --- a/modules/dnn_modern/src/dnn_modern_init.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* - By downloading, copying, installing or using the software you agree to this license. - If you do not agree to this license, do not download, install, - copy or use the software. - - - License Agreement - For Open Source Computer Vision Library - (3-clause BSD License) - - Copyright (C) 2000-2016, Intel Corporation, all rights reserved. - Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. - Copyright (C) 2009-2016, NVIDIA Corporation, all rights reserved. - Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. - Copyright (C) 2015-2016, OpenCV Foundation, all rights reserved. - Copyright (C) 2015-2016, Itseez Inc., all rights reserved. - Third party copyrights are property of their respective owners. - - Redistribution and use in source and binary forms, with or without modification, - are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the names of the copyright holders nor the names of the contributors - may be used to endorse or promote products derived from this software - without specific prior written permission. - - This software is provided by the copyright holders and contributors "as is" and - any express or implied warranties, including, but not limited to, the implied - warranties of merchantability and fitness for a particular purpose are disclaimed. - In no event shall copyright holders or contributors be liable for any direct, - indirect, incidental, special, exemplary, or consequential damages - (including, but not limited to, procurement of substitute goods or services; - loss of use, data, or profits; or business interruption) however caused - and on any theory of liability, whether in contract, strict liability, - or tort (including negligence or otherwise) arising in any way out of - the use of this software, even if advised of the possibility of such damage. - */ - -#include "precomp.hpp" - -//#include diff --git a/modules/dnn_modern/src/precomp.hpp b/modules/dnn_modern/src/precomp.hpp deleted file mode 100644 index 8ac69e2a6..000000000 --- a/modules/dnn_modern/src/precomp.hpp +++ /dev/null @@ -1,50 +0,0 @@ -/* - By downloading, copying, installing or using the software you agree to this license. - If you do not agree to this license, do not download, install, - copy or use the software. - - - License Agreement - For Open Source Computer Vision Library - (3-clause BSD License) - - Copyright (C) 2000-2016, Intel Corporation, all rights reserved. - Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved. - Copyright (C) 2009-2016, NVIDIA Corporation, all rights reserved. - Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved. - Copyright (C) 2015-2016, OpenCV Foundation, all rights reserved. - Copyright (C) 2015-2016, Itseez Inc., all rights reserved. - Third party copyrights are property of their respective owners. - - Redistribution and use in source and binary forms, with or without modification, - are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the names of the copyright holders nor the names of the contributors - may be used to endorse or promote products derived from this software - without specific prior written permission. - - This software is provided by the copyright holders and contributors "as is" and - any express or implied warranties, including, but not limited to, the implied - warranties of merchantability and fitness for a particular purpose are disclaimed. - In no event shall copyright holders or contributors be liable for any direct, - indirect, incidental, special, exemplary, or consequential damages - (including, but not limited to, procurement of substitute goods or services; - loss of use, data, or profits; or business interruption) however caused - and on any theory of liability, whether in contract, strict liability, - or tort (including negligence or otherwise) arising in any way out of - the use of this software, even if advised of the possibility of such damage. - */ - -#ifndef __OPENCV_PRECOMP_H__ -#define __OPENCV_PRECOMP_H__ - -#include "opencv2/dnn_modern.hpp" - -#endif