From c37771f01dcde4ff47867be563d1d335e7bbdc41 Mon Sep 17 00:00:00 2001 From: MichaelHsu171 Date: Tue, 2 Oct 2018 23:14:42 +0900 Subject: [PATCH 1/2] Get Intel Caffe working with OpenCV4 --- include/caffe/layers/video_data_layer.hpp | 4 ++-- src/caffe/layers/image_data_layer.cpp | 9 ++++++++- src/caffe/layers/video_data_layer.cpp | 5 +++++ src/caffe/util/bbox_util.cpp | 21 ++++++++++++++------- src/caffe/util/im_transforms.cpp | 6 +++++- 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/include/caffe/layers/video_data_layer.hpp b/include/caffe/layers/video_data_layer.hpp index 7d24bb8ed..ee3efade1 100644 --- a/include/caffe/layers/video_data_layer.hpp +++ b/include/caffe/layers/video_data_layer.hpp @@ -39,11 +39,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define CAFFE_VIDEO_DATA_LAYER_HPP_ #ifdef USE_OPENCV -#if OPENCV_VERSION == 3 +#if OPENCV_VERSION >= 3 #include #else #include -#endif // OPENCV_VERSION == 3 +#endif // OPENCV_VERSION >= 3 #include #include diff --git a/src/caffe/layers/image_data_layer.cpp b/src/caffe/layers/image_data_layer.cpp index b4fd48468..4236bfe9d 100644 --- a/src/caffe/layers/image_data_layer.cpp +++ b/src/caffe/layers/image_data_layer.cpp @@ -37,7 +37,14 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifdef USE_OPENCV #include + +#if CV_VERSION_MAJOR == 4 +#include +#define CV_BGR2RGB cv::COLOR_BGR2RGB +#define CV_FILLED cv::FILLED +#else #include +#endif #include // NOLINT(readability/streams) #include // NOLINT(readability/streams) @@ -237,7 +244,7 @@ void ImageDataLayer::load_batch(Batch* batch) { Blob tmp_data; tmp_data.Reshape(top_shape); tmp_data.set_cpu_data(prefetch_data + offset); - this->data_transformer_->Transform(cv_img, &tmp_data, + this->data_transformer_->Transform(cv_img, &tmp_data, precalculated_rand_numbers); } #endif diff --git a/src/caffe/layers/video_data_layer.cpp b/src/caffe/layers/video_data_layer.cpp index b53cf3e09..1722c6965 100644 --- a/src/caffe/layers/video_data_layer.cpp +++ b/src/caffe/layers/video_data_layer.cpp @@ -50,6 +50,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "caffe/layers/video_data_layer.hpp" #include "caffe/util/benchmark.hpp" +#if CV_VERSION_MAJOR == 4 +#define CV_CAP_PROP_POS_FRAMES cv::CAP_PROP_POS_FRAMES +#define CV_CAP_PROP_FRAME_COUNT cv::CAP_PROP_FRAME_COUNT +#endif + namespace caffe { template diff --git a/src/caffe/util/bbox_util.cpp b/src/caffe/util/bbox_util.cpp index 27f62bb42..37b6ac7f6 100644 --- a/src/caffe/util/bbox_util.cpp +++ b/src/caffe/util/bbox_util.cpp @@ -48,6 +48,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "boost/iterator/counting_iterator.hpp" #include "caffe/util/bbox_util.hpp" +#ifdef USE_OPENCV +#if CV_VERSION_MAJOR == 4 +#include +#define CV_FILLED cv::FILLED +#endif +#endif + #ifdef ENABLE_NMS_OPTIMIZATION #include #include "omp.h" @@ -96,13 +103,13 @@ static inline float min(float a, float b) { return (a) < (b) ? (a) : (b); } #define MASK_ELEM(x, y) \ ((uint64_t*)((char*)mask_tbl + x * blocks * sizeof(uint64_t)) + y) -void cpu_nms_avx512_parallize_inner(int* __restrict keep_out, - int* __restrict num_out, +void cpu_nms_avx512_parallize_inner(int* __restrict keep_out, + int* __restrict num_out, const float* __restrict x1, const float* __restrict y1, - const float* __restrict x2, + const float* __restrict x2, const float* __restrict y2, - int64_t boxes_num, + int64_t boxes_num, float thresh_f) { const int blocks = DIVUP(boxes_num, 64); @@ -179,7 +186,7 @@ void cpu_nms_avx512_parallize_inner(int* __restrict keep_out, __m512 Sb_w = _mm512_sub_ps(x2_b, x1_b); __m512 Sb_h = _mm512_sub_ps(y2_b, y1_b); - + __m512 Sb = _mm512_mul_ps(Sb_w, Sb_h); __m512 ovr = _mm512_add_ps(Sa, Sb); @@ -1844,7 +1851,7 @@ void GetConfidenceScores(const Dtype* conf_data, const int num, } } #ifdef _OPENMP - #pragma omp parallel for + #pragma omp parallel for #endif for (int i = 0; i < num; i++) { // #pragma omp parallel @@ -2185,7 +2192,7 @@ void GetPriorBBoxes(const Dtype* prior_data, const int num_priors, } vector var(4, 0); - + for (int i = 0; i < num_priors; ++i) { for (int j = 0; j < 4; ++j) { var.at(j) = (prior_data[(num_priors + i) * 4 + j]); diff --git a/src/caffe/util/im_transforms.cpp b/src/caffe/util/im_transforms.cpp index 1f20bd2c5..644fa1d0c 100644 --- a/src/caffe/util/im_transforms.cpp +++ b/src/caffe/util/im_transforms.cpp @@ -38,7 +38,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifdef USE_OPENCV #include -#if CV_VERSION_MAJOR == 3 +#if CV_VERSION_MAJOR >= 3 #include #define CV_GRAY2BGR cv::COLOR_GRAY2BGR #define CV_BGR2GRAY cv::COLOR_BGR2GRAY @@ -48,6 +48,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define CV_LOAD_IMAGE_COLOR cv::IMREAD_COLOR #define CV_THRESH_BINARY_INV cv::THRESH_BINARY_INV #define CV_THRESH_OTSU cv::THRESH_OTSU +#define CV_BGR2HSV cv::COLOR_BGR2HSV +#define CV_BGR2Lab cv::COLOR_BGR2Lab +#define CV_BGR2HSV cv::COLOR_BGR2HSV +#define CV_HSV2BGR cv::COLOR_HSV2BGR #endif #endif // USE_OPENCV From ad5fdeac50a9244a0fb9c202d504eb37f529c926 Mon Sep 17 00:00:00 2001 From: MichaelHsu171 Date: Tue, 2 Oct 2018 23:21:46 +0900 Subject: [PATCH 2/2] Added preprocessor condition to distinguish change for OpenCV4 --- src/caffe/util/im_transforms.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/caffe/util/im_transforms.cpp b/src/caffe/util/im_transforms.cpp index 644fa1d0c..9bf347732 100644 --- a/src/caffe/util/im_transforms.cpp +++ b/src/caffe/util/im_transforms.cpp @@ -48,11 +48,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define CV_LOAD_IMAGE_COLOR cv::IMREAD_COLOR #define CV_THRESH_BINARY_INV cv::THRESH_BINARY_INV #define CV_THRESH_OTSU cv::THRESH_OTSU +#if CV_VERSION_MAJOR == 4 #define CV_BGR2HSV cv::COLOR_BGR2HSV #define CV_BGR2Lab cv::COLOR_BGR2Lab #define CV_BGR2HSV cv::COLOR_BGR2HSV #define CV_HSV2BGR cv::COLOR_HSV2BGR #endif +#endif #endif // USE_OPENCV #include