diff --git a/modules/dnn/CMakeLists.txt b/modules/dnn/CMakeLists.txt index fc1857d99..cfd4e846b 100644 --- a/modules/dnn/CMakeLists.txt +++ b/modules/dnn/CMakeLists.txt @@ -14,6 +14,7 @@ else() message(STATUS "PROTOBUF not found. Caffe import function will be disabled.") set(HAVE_PROTOBUF OFF) set(PROTOBUF_LIBRARIES "") + add_definitions(-DHAVE_PROTOBUF=0) endif() endmacro(_dnn_find_protobuf) @@ -24,7 +25,7 @@ set(OPENCV_MODULE_IS_PART_OF_WORLD OFF) _dnn_find_protobuf() ocv_add_module(dnn opencv_imgproc opencv_core opencv_highgui WRAP python matlab) -ocv_warnings_disable(CMAKE_CXX_FLAGS -Wno-shadow -Wno-parentheses) +ocv_warnings_disable(CMAKE_CXX_FLAGS -Wno-shadow -Wno-parentheses -Wmaybe-uninitialized -Wsign-promo -Wmissing-declarations -Wmissing-prototypes) ocv_glob_module_sources(${PROTO_SRCS} ${PROTO_HDRS}) ocv_source_group("Src\\protobuf" FILES ${PROTO_SRCS} ${PROTO_HDRS}) ocv_module_include_directories(include src/caffe ${PROTOBUF_INCLUDE_DIR}) diff --git a/modules/dnn/README.md b/modules/dnn/README.md new file mode 100644 index 000000000..6e7e9c408 --- /dev/null +++ b/modules/dnn/README.md @@ -0,0 +1,2 @@ +Deep Neural Network module +========================== \ No newline at end of file diff --git a/modules/dnn/include/opencv2/dnn.hpp b/modules/dnn/include/opencv2/dnn.hpp index eea4fc2bb..9bc062c53 100644 --- a/modules/dnn/include/opencv2/dnn.hpp +++ b/modules/dnn/include/opencv2/dnn.hpp @@ -5,7 +5,14 @@ // We are free to change headers layout in dnn subfolder, so please include // this header for future compartibility + +/** @defgroup dnn Deep Neural Network module + @{ + This module contain tools to load artifical neural network models and to make forward test passes. + @} +*/ + #include -#endif /* __OPENCV_DNN_HPP__ */ \ No newline at end of file +#endif /* __OPENCV_DNN_HPP__ */ diff --git a/modules/dnn/samples/alexnet.cpp b/modules/dnn/samples/alexnet.cpp index 831852c7c..1b55de253 100644 --- a/modules/dnn/samples/alexnet.cpp +++ b/modules/dnn/samples/alexnet.cpp @@ -53,6 +53,7 @@ int main(void) CV_Assert(!img.empty()); cvtColor(img, img, COLOR_BGR2RGB); img.convertTo(img, CV_32F); + resize(img, img, Size(227, 227)); subtract(img, cv::mean(img), img); Blob imgBlob(img); diff --git a/modules/dnn/src/caffe/common.hpp b/modules/dnn/src/caffe/common.hpp index d38b5dea2..bf20da198 100644 --- a/modules/dnn/src/caffe/common.hpp +++ b/modules/dnn/src/caffe/common.hpp @@ -19,17 +19,6 @@ #include // pair #include -//// Disable the copy and assignment operator for a class. -//#define DISABLE_COPY_AND_ASSIGN(classname) \ -//private:\ -// classname(const classname&);\ -// classname& operator=(const classname&) -// -//// A simple macro to mark codes that are not implemented, so that when the code -//// is executed we will see a fatal log. -//#define NOT_IMPLEMENTED LOG(FATAL) << "Not Implemented Yet" -// - namespace caffe { // Common functions and classes from std that caffe often uses. diff --git a/modules/dnn/src/caffe/glog_emulator.hpp b/modules/dnn/src/caffe/glog_emulator.hpp index 718d297a2..887bd7a2a 100644 --- a/modules/dnn/src/caffe/glog_emulator.hpp +++ b/modules/dnn/src/caffe/glog_emulator.hpp @@ -1,10 +1,11 @@ #pragma once #include #include +#include #include #define CHECK(cond) cv::GLogWrapper(__FILE__, CV_Func, __LINE__, "CHECK", #cond, cond) -#define CHECK_EQ(a, b) cv::GLogWrapper(__FILE__, CV_Func, __LINE__, "CHECK", #a #b, ((a) == (b))) +#define CHECK_EQ(a, b) cv::GLogWrapper(__FILE__, CV_Func, __LINE__, "CHECK", #a"="#b, ((a) == (b))) #define LOG(TYPE) cv::GLogWrapper(__FILE__, CV_Func, __LINE__, #TYPE) namespace cv @@ -12,18 +13,10 @@ namespace cv class GLogWrapper { - const char *type, *cond_str, *file, *func; + std::stringstream stream; + const char *file, *func, *type, *cond_str; int line; bool cond_staus; - std::ostream &stream; - - static std::ostream &selectStream(const char *type) - { - if (!strcmp(type, "INFO")) - return std::cout; - else - return std::cerr; - } public: @@ -31,9 +24,8 @@ public: const char *_type, const char *_cond_str = NULL, bool _cond_status = true ) : - stream(selectStream(_type)), - file(_file), func(_func), line(_line), - type(_type), cond_str(_cond_str), cond_staus(_cond_status) {} + file(_file), func(_func), type(_type), cond_str(_cond_str), + line(_line), cond_staus(_cond_status) {} template GLogWrapper &operator<<(const T &v) @@ -47,13 +39,16 @@ public: { if (cond_str && !cond_staus) { - cv::error(cv::Error::StsAssert, cond_str, func, file, line); + cv::error(cv::Error::StsError, "FAILED: " + String(cond_str) + "." + stream.str(), func, file, line); + } + else if (!cond_str && strcmp(type, "CHECK")) + { + if (!strcmp(type, "INFO")) + std::cout << stream.str(); + else + std::cerr << stream.str(); } - //else if (!cond_str && strcmp(type, "INFO")) - //{ - // cv::error(cv::Error::StsAssert, type, func, file, line); - //} } }; -} \ No newline at end of file +} diff --git a/modules/dnn/src/caffe/util/io.cpp b/modules/dnn/src/caffe/util/io.cpp index c4832aac6..434ca8d9c 100644 --- a/modules/dnn/src/caffe/util/io.cpp +++ b/modules/dnn/src/caffe/util/io.cpp @@ -1,4 +1,5 @@ -#ifdef HAVE_PROTOBUF +#if HAVE_PROTOBUF +#include "io.hpp" #include #include #include @@ -29,7 +30,7 @@ using google::protobuf::Message; bool ReadProtoFromTextFile(const char* filename, Message* proto) { std::ifstream fs(filename, std::ifstream::in); - CV_Assert(fs.is_open()); + CHECK(fs.is_open()) << "Can't open \"" << filename << "\""; IstreamInputStream input(&fs); bool success = google::protobuf::TextFormat::Parse(&input, proto); fs.close(); @@ -47,7 +48,7 @@ bool ReadProtoFromTextFile(const char* filename, Message* proto) { // bool ReadProtoFromBinaryFile(const char* filename, Message* proto) { std::ifstream fs(filename, std::ifstream::in | std::ifstream::binary); - CV_Assert(fs.is_open()); + CHECK(fs.is_open()) << "Can't open \"" << filename << "\""; ZeroCopyInputStream* raw_input = new IstreamInputStream(&fs); CodedInputStream* coded_input = new CodedInputStream(raw_input); coded_input->SetTotalBytesLimit(kProtoReadBytesLimit, 536870912); @@ -66,4 +67,4 @@ bool ReadProtoFromBinaryFile(const char* filename, Message* proto) { //} } // namespace caffe -#endif \ No newline at end of file +#endif diff --git a/modules/dnn/src/caffe/util/io.hpp b/modules/dnn/src/caffe/util/io.hpp index 0c56997cb..1a2c9905b 100644 --- a/modules/dnn/src/caffe/util/io.hpp +++ b/modules/dnn/src/caffe/util/io.hpp @@ -1,5 +1,6 @@ #ifndef CAFFE_UTIL_IO_H_ #define CAFFE_UTIL_IO_H_ +#if HAVE_PROTOBUF //instead of GLOG #include "../glog_emulator.hpp" @@ -141,4 +142,5 @@ void CVMatToDatum(const cv::Mat& cv_img, Datum* datum); } // namespace caffe +#endif #endif // CAFFE_UTIL_IO_H_ diff --git a/modules/dnn/src/caffe/util/upgrade_proto.cpp b/modules/dnn/src/caffe/util/upgrade_proto.cpp index 388f6d6eb..853b78869 100644 --- a/modules/dnn/src/caffe/util/upgrade_proto.cpp +++ b/modules/dnn/src/caffe/util/upgrade_proto.cpp @@ -1,4 +1,4 @@ -#ifdef HAVE_PROTOBUF +#if HAVE_PROTOBUF #include #include #include @@ -939,4 +939,4 @@ void ReadNetParamsFromBinaryFileOrDie(const string& param_file, } } // namespace caffe -#endif \ No newline at end of file +#endif diff --git a/modules/dnn/src/caffe/util/upgrade_proto.hpp b/modules/dnn/src/caffe/util/upgrade_proto.hpp index ace27f25e..7dbfc24fc 100644 --- a/modules/dnn/src/caffe/util/upgrade_proto.hpp +++ b/modules/dnn/src/caffe/util/upgrade_proto.hpp @@ -1,5 +1,6 @@ #ifndef CAFFE_UTIL_UPGRADE_PROTO_H_ #define CAFFE_UTIL_UPGRADE_PROTO_H_ +#if HAVE_PROTOBUF #include #include "caffe/common.hpp" @@ -61,4 +62,5 @@ void ReadNetParamsFromBinaryFileOrDie(const string& param_file, } // namespace caffe +#endif #endif // CAFFE_UTIL_UPGRADE_PROTO_H_ diff --git a/modules/dnn/src/dnn.cpp b/modules/dnn/src/dnn.cpp index 3eacf5458..d71402edd 100644 --- a/modules/dnn/src/dnn.cpp +++ b/modules/dnn/src/dnn.cpp @@ -335,7 +335,7 @@ struct Net::Impl } std::cout << "\nNet Outputs(" << netOutputs.size() << "):\n"; - for (int i = 0; i < netOutputs.size(); i++) + for (size_t i = 0; i < netOutputs.size(); i++) std::cout << layers[netOutputs[i]].name << std::endl; } @@ -459,10 +459,10 @@ int Net::addLayer(const String &name, const String &type, LayerParams ¶ms) return id; } -void Net::connect(BlobId input, BlobId output) -{ +//void Net::connect(BlobId input, BlobId output) +//{ -} +//} void Net::setOutputNames(LayerId layer, const std::vector &outputNames) { @@ -605,4 +605,4 @@ Ptr LayerRegister::createLayerInstance(const String &_type, LayerParams& } } -} \ No newline at end of file +} diff --git a/modules/dnn/src/layers/convolution_layer.cpp b/modules/dnn/src/layers/convolution_layer.cpp index 0aa7e2f43..3a3b6214f 100644 --- a/modules/dnn/src/layers/convolution_layer.cpp +++ b/modules/dnn/src/layers/convolution_layer.cpp @@ -50,7 +50,7 @@ namespace dnn if (bias) { Blob &biasBlob = learnedParams[1]; - CV_Assert(biasBlob.total() == numOutput); + CV_Assert(biasBlob.total() == (size_t)numOutput); } } @@ -159,4 +159,4 @@ namespace dnn outW = (inW + 2 * padW - kernelW) / strideW + 1; } } -} \ No newline at end of file +} diff --git a/modules/dnn/src/layers/fully_connected_layer.cpp b/modules/dnn/src/layers/fully_connected_layer.cpp index c7e184175..444061fbc 100644 --- a/modules/dnn/src/layers/fully_connected_layer.cpp +++ b/modules/dnn/src/layers/fully_connected_layer.cpp @@ -30,7 +30,7 @@ namespace dnn bias = params.get("bias_term", true); CV_Assert(params.learnedBlobs.size() >= 1); - CV_Assert(!bias || (params.learnedBlobs.size() >= 2 && params.learnedBlobs[1].total() == numOutputs)); + CV_Assert(!bias || (params.learnedBlobs.size() >= 2 && (int)params.learnedBlobs[1].total() == numOutputs)); learnedParams.resize(bias ? 2 : 1); learnedParams[0] = params.learnedBlobs[0]; @@ -49,7 +49,7 @@ namespace dnn inW = inputs[0]->cols(); inSize = inC * inH * inW; - CV_Assert(inSize * numOutputs == learnedParams[0].total()); + CV_Assert((size_t)inSize * (size_t)numOutputs == learnedParams[0].total()); outputs.resize(inputs.size()); for (size_t i = 0; i < inputs.size(); i++) @@ -84,4 +84,4 @@ namespace dnn } } } -} \ No newline at end of file +}