commit
309edb59c1
11 changed files with 39 additions and 856 deletions
@ -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) |
|
@ -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.<br /> |
|
||||||
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 |
|
@ -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 |
|
||||||
) |
|
@ -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 |
|
||||||
) |
|
@ -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<float>& 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<CaffeConverter> 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<float>& results) = 0; |
|
||||||
}; |
|
||||||
|
|
||||||
//! @}
|
|
||||||
} // namespace dnn2
|
|
||||||
} // namespace cv
|
|
||||||
#endif |
|
||||||
|
|
||||||
/* End of file. */ |
|
@ -1,93 +0,0 @@ |
|||||||
#include <opencv2/dnn_modern.hpp> |
|
||||||
#include <opencv2/imgcodecs.hpp> |
|
||||||
|
|
||||||
#include <iostream> |
|
||||||
#include <fstream> |
|
||||||
|
|
||||||
|
|
||||||
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 <model_file> <trained_file> <mean_file>\n" |
|
||||||
<< " <label_file> <image_file>\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<string> get_label_list(const string& label_file); |
|
||||||
void print_n_labels(const vector<string>& labels, |
|
||||||
const vector<float_t>& result, |
|
||||||
const int top_n); |
|
||||||
|
|
||||||
vector<string> 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<string> lines; |
|
||||||
while (getline(ifs, line)) lines.push_back(line); |
|
||||||
|
|
||||||
return lines; |
|
||||||
} |
|
||||||
|
|
||||||
void print_n_labels(const vector<string>& labels, |
|
||||||
const vector<float_t>& result, |
|
||||||
const int top_n) { |
|
||||||
vector<float_t> sorted(result.begin(), result.end()); |
|
||||||
|
|
||||||
partial_sort(sorted.begin(), sorted.begin()+top_n, sorted.end(), greater<float_t>()); |
|
||||||
|
|
||||||
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<CaffeConverter> caffe_ptr = CaffeConverter::create( |
|
||||||
model_file, trained_file, mean_file); |
|
||||||
|
|
||||||
// load input image
|
|
||||||
cv::Mat img = cv::imread(img_file, -1); |
|
||||||
|
|
||||||
// inference !
|
|
||||||
vector<float_t> scores; |
|
||||||
caffe_ptr->eval(img, scores); |
|
||||||
|
|
||||||
// retrieve n labels
|
|
||||||
const int n = 5; |
|
||||||
vector<string> labels = get_label_list(label_file); |
|
||||||
|
|
||||||
print_n_labels(labels, scores, n); |
|
||||||
|
|
||||||
return 0; |
|
||||||
} |
|
@ -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 <opencv2/imgproc.hpp> |
|
||||||
|
|
||||||
#include <tiny_dnn/tiny_dnn.h> |
|
||||||
#include <tiny_dnn/io/caffe/caffe.pb.cc> |
|
||||||
|
|
||||||
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<float>& 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<Mat>* input_channels); |
|
||||||
|
|
||||||
Mat mean_; |
|
||||||
std::shared_ptr<network<sequential>> 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<Mat> 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<Mat>* 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<float>& 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<Mat> input_channels; |
|
||||||
vector<float> 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<tiny_dnn::float_t> 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> CaffeConverter::create(const String& model_file, |
|
||||||
const String& trained_file, |
|
||||||
const String& mean_file) { |
|
||||||
return makePtr<CaffeConverter_Impl>(model_file, trained_file, mean_file); |
|
||||||
} |
|
||||||
|
|
||||||
} // namespace dnn2
|
|
||||||
} // namespace cv
|
|
@ -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 <tiny_cnn/tiny_cnn.h>
|
|
@ -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 |
|
Loading…
Reference in new issue