From 1629e29b98aba4b7f520ab639068b48e65fe6a2e Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Fri, 8 Feb 2019 16:27:59 +0300 Subject: [PATCH 01/12] core: clone convert.simd.hpp --- modules/core/src/{convert.cpp => convert.simd.hpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename modules/core/src/{convert.cpp => convert.simd.hpp} (100%) diff --git a/modules/core/src/convert.cpp b/modules/core/src/convert.simd.hpp similarity index 100% rename from modules/core/src/convert.cpp rename to modules/core/src/convert.simd.hpp From 5527c41468e4749a8ba6edb52edd7481c89054c0 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Fri, 8 Feb 2019 16:29:16 +0300 Subject: [PATCH 02/12] core: clone convert.dispatch.cpp --- modules/core/src/{convert.cpp => convert.dispatch.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename modules/core/src/{convert.cpp => convert.dispatch.cpp} (100%) diff --git a/modules/core/src/convert.cpp b/modules/core/src/convert.dispatch.cpp similarity index 100% rename from modules/core/src/convert.cpp rename to modules/core/src/convert.dispatch.cpp From 0af9e19be791ba40fe9681ac62c1656f3ed19ab8 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Fri, 8 Feb 2019 16:35:52 +0300 Subject: [PATCH 03/12] core: clone convert_scale.simd.hpp --- modules/core/src/{convert_scale.cpp => convert_scale.simd.hpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename modules/core/src/{convert_scale.cpp => convert_scale.simd.hpp} (100%) diff --git a/modules/core/src/convert_scale.cpp b/modules/core/src/convert_scale.simd.hpp similarity index 100% rename from modules/core/src/convert_scale.cpp rename to modules/core/src/convert_scale.simd.hpp From c4abdb80508ad6df4800745e050232a74bd0d301 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Fri, 8 Feb 2019 16:38:50 +0300 Subject: [PATCH 04/12] core: clone convert_scale.dispatch.cpp --- .../core/src/{convert_scale.cpp => convert_scale.dispatch.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename modules/core/src/{convert_scale.cpp => convert_scale.dispatch.cpp} (100%) diff --git a/modules/core/src/convert_scale.cpp b/modules/core/src/convert_scale.dispatch.cpp similarity index 100% rename from modules/core/src/convert_scale.cpp rename to modules/core/src/convert_scale.dispatch.cpp From 39b90ae9fbcb7d5761b1fbdfbfa01474f5aee0e1 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Fri, 8 Feb 2019 17:37:59 +0300 Subject: [PATCH 05/12] core: dispatch convert --- modules/core/CMakeLists.txt | 1 + modules/core/src/convert.dispatch.cpp | 305 +++----------------------- modules/core/src/convert.simd.hpp | 176 +++------------ 3 files changed, 53 insertions(+), 429 deletions(-) diff --git a/modules/core/CMakeLists.txt b/modules/core/CMakeLists.txt index 992d09de0b..a5a75efc41 100644 --- a/modules/core/CMakeLists.txt +++ b/modules/core/CMakeLists.txt @@ -3,6 +3,7 @@ set(the_description "The Core Functionality") ocv_add_dispatched_file(mathfuncs_core SSE2 AVX AVX2) ocv_add_dispatched_file(stat SSE4_2 AVX2) ocv_add_dispatched_file(arithm SSE2 SSE4_1 AVX2 VSX3) +ocv_add_dispatched_file(convert SSE2 AVX2) # dispatching for accuracy tests ocv_add_dispatched_file_force_all(test_intrin128 TEST SSE2 SSE3 SSSE3 SSE4_1 SSE4_2 AVX FP16 AVX2) diff --git a/modules/core/src/convert.dispatch.cpp b/modules/core/src/convert.dispatch.cpp index 42d0c7d468..89848c2513 100644 --- a/modules/core/src/convert.dispatch.cpp +++ b/modules/core/src/convert.dispatch.cpp @@ -4,239 +4,12 @@ #include "precomp.hpp" #include "opencl_kernels_core.hpp" -#include "convert.hpp" + +#include "convert.simd.hpp" +#include "convert.simd_declarations.hpp" // defines CV_CPU_DISPATCH_MODES_ALL=AVX2,...,BASELINE based on CMakeLists.txt content namespace cv { -/*namespace hal { - -void cvt16f32f( const float16_t* src, float* dst, int len ) -{ - int j = 0; -#if CV_SIMD - const int VECSZ = v_float32::nlanes; - for( ; j < len; j += VECSZ ) - { - if( j > len - VECSZ ) - { - if( j == 0 ) - break; - j = len - VECSZ; - } - v_store(dst + j, vx_load_expand(src + j)); - } -#endif - for( ; j < len; j++ ) - dst[j] = (float)src[j]; -} - -void cvt32f16f( const float* src, float16_t* dst, int len ) -{ - int j = 0; -#if CV_SIMD - const int VECSZ = v_float32::nlanes; - for( ; j < len; j += VECSZ ) - { - if( j > len - VECSZ ) - { - if( j == 0 ) - break; - j = len - VECSZ; - } - v_pack_store(dst + j, vx_load(src + j)); - } -#endif - for( ; j < len; j++ ) - dst[j] = float16_t(src[j]); -} - -/*void addRNGBias32f( float* arr, const float* scaleBiasPairs, int len ) -{ - // the loop is simple enough, so we let the compiler to vectorize it - for( int i = 0; i < len; i++ ) - arr[i] = scaleBiasPairs[i*2 + 1]; -} - -void addRNGBias64f( double* arr, const double* scaleBiasPairs, int len ) -{ - // the loop is simple enough, so we let the compiler to vectorize it - for( int i = 0; i < len; i++ ) - arr[i] = scaleBiasPairs[i*2 + 1]; -} - -}*/ - -template inline void -cvt_( const _Ts* src, size_t sstep, _Td* dst, size_t dstep, Size size ) -{ - sstep /= sizeof(src[0]); - dstep /= sizeof(dst[0]); - - for( int i = 0; i < size.height; i++, src += sstep, dst += dstep ) - { - int j = 0; -#if CV_SIMD - const int VECSZ = _Twvec::nlanes*2; - for( ; j < size.width; j += VECSZ ) - { - if( j > size.width - VECSZ ) - { - if( j == 0 || src == (_Ts*)dst ) - break; - j = size.width - VECSZ; - } - _Twvec v0, v1; - vx_load_pair_as(src + j, v0, v1); - v_store_pair_as(dst + j, v0, v1); - } -#endif - for( ; j < size.width; j++ ) - dst[j] = saturate_cast<_Td>(src[j]); - } -} - -// in order to reduce the code size, for (16f <-> ...) conversions -// we add a conversion function without loop unrolling -template inline void -cvt1_( const _Ts* src, size_t sstep, _Td* dst, size_t dstep, Size size ) -{ - sstep /= sizeof(src[0]); - dstep /= sizeof(dst[0]); - - for( int i = 0; i < size.height; i++, src += sstep, dst += dstep ) - { - int j = 0; -#if CV_SIMD - const int VECSZ = _Twvec::nlanes; - for( ; j < size.width; j += VECSZ ) - { - if( j > size.width - VECSZ ) - { - if( j == 0 || src == (_Ts*)dst ) - break; - j = size.width - VECSZ; - } - _Twvec v; - vx_load_as(src + j, v); - v_store_as(dst + j, v); - } - vx_cleanup(); -#endif - for( ; j < size.width; j++ ) - dst[j] = saturate_cast<_Td>(src[j]); - } -} - -static void cvtCopy( const uchar* src, size_t sstep, - uchar* dst, size_t dstep, Size size, size_t elemsize) -{ - size_t len = size.width*elemsize; - for( int i = 0; i < size.height; i++, src += sstep, dst += dstep ) - { - memcpy( dst, src, len ); - } -} - -#define DEF_CVT_FUNC(suffix, cvtfunc, _Ts, _Td, _Twvec) \ -static void cvt##suffix(const _Ts* src, size_t sstep, uchar*, size_t, \ - _Td* dst, size_t dstep, Size size, void*) \ -{ cvtfunc<_Ts, _Td, _Twvec>(src, sstep, dst, dstep, size); } - -////////////////////// 8u -> ... //////////////////////// - -DEF_CVT_FUNC(8u8s, cvt_, uchar, schar, v_int16) -DEF_CVT_FUNC(8u16u, cvt_, uchar, ushort, v_uint16) -DEF_CVT_FUNC(8u16s, cvt_, uchar, short, v_int16) -DEF_CVT_FUNC(8u32s, cvt_, uchar, int, v_int32) -DEF_CVT_FUNC(8u32f, cvt_, uchar, float, v_float32) -DEF_CVT_FUNC(8u64f, cvt_, uchar, double, v_int32) -//DEF_CVT_FUNC(8u16f, cvt1_, uchar, float16_t, v_float32) - -////////////////////// 8s -> ... //////////////////////// - -DEF_CVT_FUNC(8s8u, cvt_, schar, uchar, v_int16) -DEF_CVT_FUNC(8s16u, cvt_, schar, ushort, v_uint16) -DEF_CVT_FUNC(8s16s, cvt_, schar, short, v_int16) -DEF_CVT_FUNC(8s32s, cvt_, schar, int, v_int32) -DEF_CVT_FUNC(8s32f, cvt_, schar, float, v_float32) -DEF_CVT_FUNC(8s64f, cvt_, schar, double, v_int32) -//DEF_CVT_FUNC(8s16f, cvt1_, schar, float16_t, v_float32) - -////////////////////// 16u -> ... //////////////////////// - -DEF_CVT_FUNC(16u8u, cvt_, ushort, uchar, v_uint16) -DEF_CVT_FUNC(16u8s, cvt_, ushort, schar, v_uint16) -DEF_CVT_FUNC(16u16s, cvt_, ushort, short, v_int32) -DEF_CVT_FUNC(16u32s, cvt_, ushort, int, v_int32) -DEF_CVT_FUNC(16u32f, cvt_, ushort, float, v_float32) -DEF_CVT_FUNC(16u64f, cvt_, ushort, double, v_int32) -//DEF_CVT_FUNC(16u16f, cvt1_,ushort, float16_t, v_float32) - -////////////////////// 16s -> ... //////////////////////// - -DEF_CVT_FUNC(16s8u, cvt_, short, uchar, v_int16) -DEF_CVT_FUNC(16s8s, cvt_, short, schar, v_int16) -DEF_CVT_FUNC(16s16u, cvt_, short, ushort, v_int32) -DEF_CVT_FUNC(16s32s, cvt_, short, int, v_int32) -DEF_CVT_FUNC(16s32f, cvt_, short, float, v_float32) -DEF_CVT_FUNC(16s64f, cvt_, short, double, v_int32) -//DEF_CVT_FUNC(16s16f, cvt1_,short, float16_t, v_float32) - -////////////////////// 32s -> ... //////////////////////// - -DEF_CVT_FUNC(32s8u, cvt_, int, uchar, v_int32) -DEF_CVT_FUNC(32s8s, cvt_, int, schar, v_int32) -DEF_CVT_FUNC(32s16u, cvt_, int, ushort, v_int32) -DEF_CVT_FUNC(32s16s, cvt_, int, short, v_int32) -DEF_CVT_FUNC(32s32f, cvt_, int, float, v_float32) -DEF_CVT_FUNC(32s64f, cvt_, int, double, v_int32) -//DEF_CVT_FUNC(32s16f, cvt1_,int, float16_t, v_float32) - -////////////////////// 32f -> ... //////////////////////// - -DEF_CVT_FUNC(32f8u, cvt_, float, uchar, v_float32) -DEF_CVT_FUNC(32f8s, cvt_, float, schar, v_float32) -DEF_CVT_FUNC(32f16u, cvt_, float, ushort, v_float32) -DEF_CVT_FUNC(32f16s, cvt_, float, short, v_float32) -DEF_CVT_FUNC(32f32s, cvt_, float, int, v_float32) -DEF_CVT_FUNC(32f64f, cvt_, float, double, v_float32) -DEF_CVT_FUNC(32f16f, cvt1_,float, float16_t, v_float32) - -////////////////////// 64f -> ... //////////////////////// - -DEF_CVT_FUNC(64f8u, cvt_, double, uchar, v_int32) -DEF_CVT_FUNC(64f8s, cvt_, double, schar, v_int32) -DEF_CVT_FUNC(64f16u, cvt_, double, ushort, v_int32) -DEF_CVT_FUNC(64f16s, cvt_, double, short, v_int32) -DEF_CVT_FUNC(64f32s, cvt_, double, int, v_int32) -DEF_CVT_FUNC(64f32f, cvt_, double, float, v_float32) -//DEF_CVT_FUNC(64f16f, cvt1_,double, float16_t, v_float32) - -////////////////////// 16f -> ... //////////////////////// - -//DEF_CVT_FUNC(16f8u, cvt_, float16_t, uchar, v_float32) -//DEF_CVT_FUNC(16f8s, cvt_, float16_t, schar, v_float32) -//DEF_CVT_FUNC(16f16u, cvt1_, float16_t, ushort, v_float32) -//DEF_CVT_FUNC(16f16s, cvt1_, float16_t, short, v_float32) -//DEF_CVT_FUNC(16f32s, cvt1_, float16_t, int, v_float32) -DEF_CVT_FUNC(16f32f, cvt1_, float16_t, float, v_float32) -//DEF_CVT_FUNC(16f64f, cvt1_, float16_t, double, v_float32) - -///////////// "conversion" w/o conversion /////////////// - -static void cvt8u(const uchar* src, size_t sstep, uchar*, size_t, uchar* dst, size_t dstep, Size size, void*) -{ cvtCopy(src, sstep, dst, dstep, size, 1); } - -static void cvt16u(const ushort* src, size_t sstep, uchar*, size_t, ushort* dst, size_t dstep, Size size, void*) -{ cvtCopy((const uchar*)src, sstep, (uchar*)dst, dstep, size, 2); } - -static void cvt32s(const int* src, size_t sstep, uchar*, size_t, int* dst, size_t dstep, Size size, void*) -{ cvtCopy((const uchar*)src, sstep, (uchar*)dst, dstep, size, 4); } - -static void cvt64s(const int64* src, size_t sstep, uchar*, size_t, int64* dst, size_t dstep, Size size, void*) -{ cvtCopy((const uchar*)src, sstep, (uchar*)dst, dstep, size, 8); } - - /* [TODO] Recover IPP calls #if defined(HAVE_IPP) #define DEF_CVT_FUNC_F(suffix, stype, dtype, ippFavor) \ @@ -334,50 +107,22 @@ DEF_CPY_FUNC(64s, int64) BinaryFunc getConvertFunc(int sdepth, int ddepth) { - static BinaryFunc cvtTab[][8] = - { - { - (BinaryFunc)(cvt8u), (BinaryFunc)GET_OPTIMIZED(cvt8s8u), (BinaryFunc)GET_OPTIMIZED(cvt16u8u), - (BinaryFunc)GET_OPTIMIZED(cvt16s8u), (BinaryFunc)GET_OPTIMIZED(cvt32s8u), (BinaryFunc)GET_OPTIMIZED(cvt32f8u), - (BinaryFunc)GET_OPTIMIZED(cvt64f8u), 0 //(BinaryFunc)(cvt16f8u) - }, - { - (BinaryFunc)GET_OPTIMIZED(cvt8u8s), (BinaryFunc)cvt8u, (BinaryFunc)GET_OPTIMIZED(cvt16u8s), - (BinaryFunc)GET_OPTIMIZED(cvt16s8s), (BinaryFunc)GET_OPTIMIZED(cvt32s8s), (BinaryFunc)GET_OPTIMIZED(cvt32f8s), - (BinaryFunc)GET_OPTIMIZED(cvt64f8s), 0 //(BinaryFunc)(cvt16f8s) - }, - { - (BinaryFunc)GET_OPTIMIZED(cvt8u16u), (BinaryFunc)GET_OPTIMIZED(cvt8s16u), (BinaryFunc)cvt16u, - (BinaryFunc)GET_OPTIMIZED(cvt16s16u), (BinaryFunc)GET_OPTIMIZED(cvt32s16u), (BinaryFunc)GET_OPTIMIZED(cvt32f16u), - (BinaryFunc)GET_OPTIMIZED(cvt64f16u), 0 //(BinaryFunc)(cvt16f16u) - }, - { - (BinaryFunc)GET_OPTIMIZED(cvt8u16s), (BinaryFunc)GET_OPTIMIZED(cvt8s16s), (BinaryFunc)GET_OPTIMIZED(cvt16u16s), - (BinaryFunc)cvt16u, (BinaryFunc)GET_OPTIMIZED(cvt32s16s), (BinaryFunc)GET_OPTIMIZED(cvt32f16s), - (BinaryFunc)GET_OPTIMIZED(cvt64f16s), 0 //(BinaryFunc)(cvt16f16s) - }, - { - (BinaryFunc)GET_OPTIMIZED(cvt8u32s), (BinaryFunc)GET_OPTIMIZED(cvt8s32s), (BinaryFunc)GET_OPTIMIZED(cvt16u32s), - (BinaryFunc)GET_OPTIMIZED(cvt16s32s), (BinaryFunc)cvt32s, (BinaryFunc)GET_OPTIMIZED(cvt32f32s), - (BinaryFunc)GET_OPTIMIZED(cvt64f32s), 0 //(BinaryFunc)(cvt16f32s) - }, - { - (BinaryFunc)GET_OPTIMIZED(cvt8u32f), (BinaryFunc)GET_OPTIMIZED(cvt8s32f), (BinaryFunc)GET_OPTIMIZED(cvt16u32f), - (BinaryFunc)GET_OPTIMIZED(cvt16s32f), (BinaryFunc)GET_OPTIMIZED(cvt32s32f), (BinaryFunc)cvt32s, - (BinaryFunc)GET_OPTIMIZED(cvt64f32f), 0 //(BinaryFunc)(cvt16f32f) - }, - { - (BinaryFunc)GET_OPTIMIZED(cvt8u64f), (BinaryFunc)GET_OPTIMIZED(cvt8s64f), (BinaryFunc)GET_OPTIMIZED(cvt16u64f), - (BinaryFunc)GET_OPTIMIZED(cvt16s64f), (BinaryFunc)GET_OPTIMIZED(cvt32s64f), (BinaryFunc)GET_OPTIMIZED(cvt32f64f), - (BinaryFunc)(cvt64s), 0 //(BinaryFunc)(cvt16f64f) - }, - { - 0, 0, 0, 0, 0, 0, 0, 0 - //(BinaryFunc)(cvt8u16f), (BinaryFunc)(cvt8s16f), (BinaryFunc)(cvt16u16f), (BinaryFunc)(cvt16s16f), - //(BinaryFunc)(cvt32s16f), (BinaryFunc)(cvt32f16f), (BinaryFunc)(cvt64f16f), (BinaryFunc)(cvt16u) - } - }; - return cvtTab[CV_MAT_DEPTH(ddepth)][CV_MAT_DEPTH(sdepth)]; + CV_INSTRUMENT_REGION(); + CV_CPU_DISPATCH(getConvertFunc, (sdepth, ddepth), + CV_CPU_DISPATCH_MODES_ALL); +} + +static BinaryFunc get_cvt32f16f() +{ + CV_INSTRUMENT_REGION(); + CV_CPU_DISPATCH(get_cvt32f16f, (), + CV_CPU_DISPATCH_MODES_ALL); +} +static BinaryFunc get_cvt16f32f() +{ + CV_INSTRUMENT_REGION(); + CV_CPU_DISPATCH(get_cvt16f32f, (), + CV_CPU_DISPATCH_MODES_ALL); } #ifdef HAVE_OPENCL @@ -410,9 +155,7 @@ static bool ocl_convertFp16( InputArray _src, OutputArray _dst, int sdepth, int } #endif -} // cv:: - -void cv::Mat::convertTo(OutputArray _dst, int _type, double alpha, double beta) const +void Mat::convertTo(OutputArray _dst, int _type, double alpha, double beta) const { CV_INSTRUMENT_REGION(); @@ -467,7 +210,7 @@ void cv::Mat::convertTo(OutputArray _dst, int _type, double alpha, double beta) //================================================================================================== -void cv::convertFp16( InputArray _src, OutputArray _dst ) +void convertFp16(InputArray _src, OutputArray _dst) { CV_INSTRUMENT_REGION(); @@ -485,12 +228,12 @@ void cv::convertFp16( InputArray _src, OutputArray _dst ) } else ddepth = CV_16S; - func = (BinaryFunc)cvt32f16f; + func = (BinaryFunc)get_cvt32f16f(); break; case CV_16S: //case CV_16F: ddepth = CV_32F; - func = (BinaryFunc)cvt16f32f; + func = (BinaryFunc)get_cvt16f32f(); break; default: CV_Error(Error::StsUnsupportedFormat, "Unsupported input depth"); @@ -525,3 +268,5 @@ void cv::convertFp16( InputArray _src, OutputArray _dst ) func(ptrs[0], 0, 0, 0, ptrs[1], 0, sz, 0); } } + +} // namespace cv diff --git a/modules/core/src/convert.simd.hpp b/modules/core/src/convert.simd.hpp index 42d0c7d468..d9b3469e4c 100644 --- a/modules/core/src/convert.simd.hpp +++ b/modules/core/src/convert.simd.hpp @@ -3,10 +3,16 @@ // of this distribution and at http://opencv.org/license.html #include "precomp.hpp" -#include "opencl_kernels_core.hpp" #include "convert.hpp" namespace cv { +CV_CPU_OPTIMIZATION_NAMESPACE_BEGIN + +BinaryFunc getConvertFunc(int sdepth, int ddepth); +BinaryFunc get_cvt32f16f(); +BinaryFunc get_cvt16f32f(); + +#ifndef CV_CPU_OPTIMIZATION_DECLARATIONS_ONLY /*namespace hal { @@ -66,7 +72,7 @@ void addRNGBias64f( double* arr, const double* scaleBiasPairs, int len ) }*/ -template inline void +template static inline void cvt_( const _Ts* src, size_t sstep, _Td* dst, size_t dstep, Size size ) { sstep /= sizeof(src[0]); @@ -97,7 +103,7 @@ cvt_( const _Ts* src, size_t sstep, _Td* dst, size_t dstep, Size size ) // in order to reduce the code size, for (16f <-> ...) conversions // we add a conversion function without loop unrolling -template inline void +template static inline void cvt1_( const _Ts* src, size_t sstep, _Td* dst, size_t dstep, Size size ) { sstep /= sizeof(src[0]); @@ -140,7 +146,10 @@ static void cvtCopy( const uchar* src, size_t sstep, #define DEF_CVT_FUNC(suffix, cvtfunc, _Ts, _Td, _Twvec) \ static void cvt##suffix(const _Ts* src, size_t sstep, uchar*, size_t, \ _Td* dst, size_t dstep, Size size, void*) \ -{ cvtfunc<_Ts, _Td, _Twvec>(src, sstep, dst, dstep, size); } +{ \ + CV_INSTRUMENT_REGION(); \ + cvtfunc<_Ts, _Td, _Twvec>(src, sstep, dst, dstep, size); \ +} ////////////////////// 8u -> ... //////////////////////// @@ -225,16 +234,16 @@ DEF_CVT_FUNC(16f32f, cvt1_, float16_t, float, v_float32) ///////////// "conversion" w/o conversion /////////////// static void cvt8u(const uchar* src, size_t sstep, uchar*, size_t, uchar* dst, size_t dstep, Size size, void*) -{ cvtCopy(src, sstep, dst, dstep, size, 1); } +{ CV_INSTRUMENT_REGION(); cvtCopy(src, sstep, dst, dstep, size, 1); } static void cvt16u(const ushort* src, size_t sstep, uchar*, size_t, ushort* dst, size_t dstep, Size size, void*) -{ cvtCopy((const uchar*)src, sstep, (uchar*)dst, dstep, size, 2); } +{ CV_INSTRUMENT_REGION(); cvtCopy((const uchar*)src, sstep, (uchar*)dst, dstep, size, 2); } static void cvt32s(const int* src, size_t sstep, uchar*, size_t, int* dst, size_t dstep, Size size, void*) -{ cvtCopy((const uchar*)src, sstep, (uchar*)dst, dstep, size, 4); } +{ CV_INSTRUMENT_REGION(); cvtCopy((const uchar*)src, sstep, (uchar*)dst, dstep, size, 4); } static void cvt64s(const int64* src, size_t sstep, uchar*, size_t, int64* dst, size_t dstep, Size size, void*) -{ cvtCopy((const uchar*)src, sstep, (uchar*)dst, dstep, size, 8); } +{ CV_INSTRUMENT_REGION(); cvtCopy((const uchar*)src, sstep, (uchar*)dst, dstep, size, 8); } /* [TODO] Recover IPP calls @@ -380,148 +389,17 @@ BinaryFunc getConvertFunc(int sdepth, int ddepth) return cvtTab[CV_MAT_DEPTH(ddepth)][CV_MAT_DEPTH(sdepth)]; } -#ifdef HAVE_OPENCL -static bool ocl_convertFp16( InputArray _src, OutputArray _dst, int sdepth, int ddepth ) +BinaryFunc get_cvt32f16f() { - int type = _src.type(), cn = CV_MAT_CN(type); - - _dst.createSameSize( _src, CV_MAKETYPE(ddepth, cn) ); - int kercn = 1; - int rowsPerWI = 1; - String build_opt = format("-D HALF_SUPPORT -D srcT=%s -D dstT=%s -D rowsPerWI=%d%s", - sdepth == CV_32F ? "float" : "half", - sdepth == CV_32F ? "half" : "float", - rowsPerWI, - sdepth == CV_32F ? " -D FLOAT_TO_HALF " : ""); - ocl::Kernel k("convertFp16", ocl::core::halfconvert_oclsrc, build_opt); - if (k.empty()) - return false; - - UMat src = _src.getUMat(); - UMat dst = _dst.getUMat(); - - ocl::KernelArg srcarg = ocl::KernelArg::ReadOnlyNoSize(src), - dstarg = ocl::KernelArg::WriteOnly(dst, cn, kercn); - - k.args(srcarg, dstarg); - - size_t globalsize[2] = { (size_t)src.cols * cn / kercn, ((size_t)src.rows + rowsPerWI - 1) / rowsPerWI }; - return k.run(2, globalsize, NULL, false); + return (BinaryFunc)cvt32f16f; } + +BinaryFunc get_cvt16f32f() +{ + return (BinaryFunc)cvt16f32f; +} + #endif -} // cv:: - -void cv::Mat::convertTo(OutputArray _dst, int _type, double alpha, double beta) const -{ - CV_INSTRUMENT_REGION(); - - if( empty() ) - { - _dst.release(); - return; - } - - bool noScale = fabs(alpha-1) < DBL_EPSILON && fabs(beta) < DBL_EPSILON; - - if( _type < 0 ) - _type = _dst.fixedType() ? _dst.type() : type(); - else - _type = CV_MAKETYPE(CV_MAT_DEPTH(_type), channels()); - - int sdepth = depth(), ddepth = CV_MAT_DEPTH(_type); - if( sdepth == ddepth && noScale ) - { - copyTo(_dst); - return; - } - - Mat src = *this; - if( dims <= 2 ) - _dst.create( size(), _type ); - else - _dst.create( dims, size, _type ); - Mat dst = _dst.getMat(); - - BinaryFunc func = noScale ? getConvertFunc(sdepth, ddepth) : getConvertScaleFunc(sdepth, ddepth); - double scale[] = {alpha, beta}; - int cn = channels(); - CV_Assert( func != 0 ); - - if( dims <= 2 ) - { - Size sz = getContinuousSize2D(src, dst, cn); - func( src.data, src.step, 0, 0, dst.data, dst.step, sz, scale ); - } - else - { - const Mat* arrays[] = {&src, &dst, 0}; - uchar* ptrs[2] = {}; - NAryMatIterator it(arrays, ptrs); - Size sz((int)(it.size*cn), 1); - - for( size_t i = 0; i < it.nplanes; i++, ++it ) - func(ptrs[0], 1, 0, 0, ptrs[1], 1, sz, scale); - } -} - -//================================================================================================== - -void cv::convertFp16( InputArray _src, OutputArray _dst ) -{ - CV_INSTRUMENT_REGION(); - - int sdepth = _src.depth(), ddepth = 0; - BinaryFunc func = 0; - - switch( sdepth ) - { - case CV_32F: - if(_dst.fixedType()) - { - ddepth = _dst.depth(); - CV_Assert(ddepth == CV_16S /*|| ddepth == CV_16F*/); - CV_Assert(_dst.channels() == _src.channels()); - } - else - ddepth = CV_16S; - func = (BinaryFunc)cvt32f16f; - break; - case CV_16S: - //case CV_16F: - ddepth = CV_32F; - func = (BinaryFunc)cvt16f32f; - break; - default: - CV_Error(Error::StsUnsupportedFormat, "Unsupported input depth"); - return; - } - - CV_OCL_RUN(_src.dims() <= 2 && _dst.isUMat(), - ocl_convertFp16(_src, _dst, sdepth, ddepth)) - - Mat src = _src.getMat(); - - int type = CV_MAKETYPE(ddepth, src.channels()); - _dst.create( src.dims, src.size, type ); - Mat dst = _dst.getMat(); - int cn = src.channels(); - - CV_Assert( func != 0 ); - - if( src.dims <= 2 ) - { - Size sz = getContinuousSize2D(src, dst, cn); - func( src.data, src.step, 0, 0, dst.data, dst.step, sz, 0); - } - else - { - const Mat* arrays[] = {&src, &dst, 0}; - uchar* ptrs[2] = {}; - NAryMatIterator it(arrays, ptrs); - Size sz((int)(it.size*cn), 1); - - for( size_t i = 0; i < it.nplanes; i++, ++it ) - func(ptrs[0], 0, 0, 0, ptrs[1], 0, sz, 0); - } -} +CV_CPU_OPTIMIZATION_NAMESPACE_END +} // namespace From d32d576d6dbe226b6e42b3649c62c58d0c2dc6df Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Fri, 8 Feb 2019 17:56:25 +0300 Subject: [PATCH 06/12] core: dispatch convert_scale --- modules/core/CMakeLists.txt | 1 + modules/core/src/convert_scale.dispatch.cpp | 355 +------------------- modules/core/src/convert_scale.simd.hpp | 249 +------------- 3 files changed, 26 insertions(+), 579 deletions(-) diff --git a/modules/core/CMakeLists.txt b/modules/core/CMakeLists.txt index a5a75efc41..c4f073713c 100644 --- a/modules/core/CMakeLists.txt +++ b/modules/core/CMakeLists.txt @@ -4,6 +4,7 @@ ocv_add_dispatched_file(mathfuncs_core SSE2 AVX AVX2) ocv_add_dispatched_file(stat SSE4_2 AVX2) ocv_add_dispatched_file(arithm SSE2 SSE4_1 AVX2 VSX3) ocv_add_dispatched_file(convert SSE2 AVX2) +ocv_add_dispatched_file(convert_scale SSE2 AVX2) # dispatching for accuracy tests ocv_add_dispatched_file_force_all(test_intrin128 TEST SSE2 SSE3 SSSE3 SSE4_1 SSE4_2 AVX FP16 AVX2) diff --git a/modules/core/src/convert_scale.dispatch.cpp b/modules/core/src/convert_scale.dispatch.cpp index b17bed8f58..83376aa61d 100644 --- a/modules/core/src/convert_scale.dispatch.cpp +++ b/modules/core/src/convert_scale.dispatch.cpp @@ -5,348 +5,26 @@ #include "precomp.hpp" #include "opencl_kernels_core.hpp" -#include "convert.hpp" -/****************************************************************************************\ -* convertScale[Abs] * -\****************************************************************************************/ +#include "convert_scale.simd.hpp" +#include "convert_scale.simd_declarations.hpp" // defines CV_CPU_DISPATCH_MODES_ALL=AVX2,...,BASELINE based on CMakeLists.txt content + namespace cv { -template inline void -cvtabs_32f( const _Ts* src, size_t sstep, _Td* dst, size_t dstep, - Size size, float a, float b ) -{ -#if CV_SIMD - v_float32 va = vx_setall_f32(a), vb = vx_setall_f32(b); - const int VECSZ = v_float32::nlanes*2; -#endif - sstep /= sizeof(src[0]); - dstep /= sizeof(dst[0]); - - for( int i = 0; i < size.height; i++, src += sstep, dst += dstep ) - { - int j = 0; -#if CV_SIMD - for( ; j < size.width; j += VECSZ ) - { - if( j > size.width - VECSZ ) - { - if( j == 0 || src == (_Ts*)dst ) - break; - j = size.width - VECSZ; - } - v_float32 v0, v1; - vx_load_pair_as(src + j, v0, v1); - v0 = v_fma(v0, va, vb); - v1 = v_fma(v1, va, vb); - v_store_pair_as(dst + j, v_abs(v0), v_abs(v1)); - } -#endif - for( ; j < size.width; j++ ) - dst[j] = saturate_cast<_Td>(std::abs(src[j]*a + b)); - } -} - -// variant for convrsions 16f <-> ... w/o unrolling -template inline void -cvtabs1_32f( const _Ts* src, size_t sstep, _Td* dst, size_t dstep, - Size size, float a, float b ) -{ -#if CV_SIMD - v_float32 va = vx_setall_f32(a), vb = vx_setall_f32(b); - const int VECSZ = v_float32::nlanes*2; -#endif - sstep /= sizeof(src[0]); - dstep /= sizeof(dst[0]); - - for( int i = 0; i < size.height; i++, src += sstep, dst += dstep ) - { - int j = 0; -#if CV_SIMD - for( ; j < size.width; j += VECSZ ) - { - if( j > size.width - VECSZ ) - { - if( j == 0 || src == (_Ts*)dst ) - break; - j = size.width - VECSZ; - } - v_float32 v0; - vx_load_as(src + j, v0); - v0 = v_fma(v0, va, vb); - v_store_as(dst + j, v_abs(v0)); - } -#endif - for( ; j < size.width; j++ ) - dst[j] = saturate_cast<_Td>(src[j]*a + b); - } -} - -template inline void -cvt_32f( const _Ts* src, size_t sstep, _Td* dst, size_t dstep, - Size size, float a, float b ) -{ -#if CV_SIMD - v_float32 va = vx_setall_f32(a), vb = vx_setall_f32(b); - const int VECSZ = v_float32::nlanes*2; -#endif - sstep /= sizeof(src[0]); - dstep /= sizeof(dst[0]); - - for( int i = 0; i < size.height; i++, src += sstep, dst += dstep ) - { - int j = 0; -#if CV_SIMD - for( ; j < size.width; j += VECSZ ) - { - if( j > size.width - VECSZ ) - { - if( j == 0 || src == (_Ts*)dst ) - break; - j = size.width - VECSZ; - } - v_float32 v0, v1; - vx_load_pair_as(src + j, v0, v1); - v0 = v_fma(v0, va, vb); - v1 = v_fma(v1, va, vb); - v_store_pair_as(dst + j, v0, v1); - } -#endif - for( ; j < size.width; j++ ) - dst[j] = saturate_cast<_Td>(src[j]*a + b); - } -} - -// variant for convrsions 16f <-> ... w/o unrolling -template inline void -cvt1_32f( const _Ts* src, size_t sstep, _Td* dst, size_t dstep, - Size size, float a, float b ) -{ -#if CV_SIMD - v_float32 va = vx_setall_f32(a), vb = vx_setall_f32(b); - const int VECSZ = v_float32::nlanes; -#endif - sstep /= sizeof(src[0]); - dstep /= sizeof(dst[0]); - - for( int i = 0; i < size.height; i++, src += sstep, dst += dstep ) - { - int j = 0; -#if CV_SIMD - for( ; j < size.width; j += VECSZ ) - { - if( j > size.width - VECSZ ) - { - if( j == 0 || src == (_Ts*)dst ) - break; - j = size.width - VECSZ; - } - v_float32 v0; - vx_load_as(src + j, v0); - v0 = v_fma(v0, va, vb); - v_store_as(dst + j, v0); - } -#endif - for( ; j < size.width; j++ ) - dst[j] = saturate_cast<_Td>(src[j]*a + b); - } -} - - -template inline void -cvt_64f( const _Ts* src, size_t sstep, _Td* dst, size_t dstep, - Size size, double a, double b ) -{ -#if CV_SIMD_64F - v_float64 va = vx_setall_f64(a), vb = vx_setall_f64(b); - const int VECSZ = v_float64::nlanes*2; -#endif - sstep /= sizeof(src[0]); - dstep /= sizeof(dst[0]); - - for( int i = 0; i < size.height; i++, src += sstep, dst += dstep ) - { - int j = 0; -#if CV_SIMD_64F - for( ; j < size.width; j += VECSZ ) - { - if( j > size.width - VECSZ ) - { - if( j == 0 || src == (_Ts*)dst ) - break; - j = size.width - VECSZ; - } - v_float64 v0, v1; - vx_load_pair_as(src + j, v0, v1); - v0 = v_fma(v0, va, vb); - v1 = v_fma(v1, va, vb); - v_store_pair_as(dst + j, v0, v1); - } -#endif - for( ; j < size.width; j++ ) - dst[j] = saturate_cast<_Td>(src[j]*a + b); - } -} - -//================================================================================================== - -#define DEF_CVT_SCALE_ABS_FUNC(suffix, cvt, stype, dtype, wtype) \ -static void cvtScaleAbs##suffix( const stype* src, size_t sstep, const uchar*, size_t, \ - dtype* dst, size_t dstep, Size size, double* scale) \ -{ \ - cvt(src, sstep, dst, dstep, size, (wtype)scale[0], (wtype)scale[1]); \ -} - - -#define DEF_CVT_SCALE_FUNC(suffix, cvt, stype, dtype, wtype) \ -static void cvtScale##suffix( const stype* src, size_t sstep, const uchar*, size_t, \ - dtype* dst, size_t dstep, Size size, double* scale) \ -{ \ - cvt(src, sstep, dst, dstep, size, (wtype)scale[0], (wtype)scale[1]); \ -} - -DEF_CVT_SCALE_ABS_FUNC(8u, cvtabs_32f, uchar, uchar, float) -DEF_CVT_SCALE_ABS_FUNC(8s8u, cvtabs_32f, schar, uchar, float) -DEF_CVT_SCALE_ABS_FUNC(16u8u, cvtabs_32f, ushort, uchar, float) -DEF_CVT_SCALE_ABS_FUNC(16s8u, cvtabs_32f, short, uchar, float) -DEF_CVT_SCALE_ABS_FUNC(32s8u, cvtabs_32f, int, uchar, float) -DEF_CVT_SCALE_ABS_FUNC(32f8u, cvtabs_32f, float, uchar, float) -DEF_CVT_SCALE_ABS_FUNC(64f8u, cvtabs_32f, double, uchar, float) - -DEF_CVT_SCALE_FUNC(8u, cvt_32f, uchar, uchar, float) -DEF_CVT_SCALE_FUNC(8s8u, cvt_32f, schar, uchar, float) -DEF_CVT_SCALE_FUNC(16u8u, cvt_32f, ushort, uchar, float) -DEF_CVT_SCALE_FUNC(16s8u, cvt_32f, short, uchar, float) -DEF_CVT_SCALE_FUNC(32s8u, cvt_32f, int, uchar, float) -DEF_CVT_SCALE_FUNC(32f8u, cvt_32f, float, uchar, float) -DEF_CVT_SCALE_FUNC(64f8u, cvt_32f, double, uchar, float) -//DEF_CVT_SCALE_FUNC(16f8u, cvt_32f, float16_t, uchar, float) - -DEF_CVT_SCALE_FUNC(8u8s, cvt_32f, uchar, schar, float) -DEF_CVT_SCALE_FUNC(8s, cvt_32f, schar, schar, float) -DEF_CVT_SCALE_FUNC(16u8s, cvt_32f, ushort, schar, float) -DEF_CVT_SCALE_FUNC(16s8s, cvt_32f, short, schar, float) -DEF_CVT_SCALE_FUNC(32s8s, cvt_32f, int, schar, float) -DEF_CVT_SCALE_FUNC(32f8s, cvt_32f, float, schar, float) -DEF_CVT_SCALE_FUNC(64f8s, cvt_32f, double, schar, float) -//DEF_CVT_SCALE_FUNC(16f8s, cvt_32f, float16_t, schar, float) - -DEF_CVT_SCALE_FUNC(8u16u, cvt_32f, uchar, ushort, float) -DEF_CVT_SCALE_FUNC(8s16u, cvt_32f, schar, ushort, float) -DEF_CVT_SCALE_FUNC(16u, cvt_32f, ushort, ushort, float) -DEF_CVT_SCALE_FUNC(16s16u, cvt_32f, short, ushort, float) -DEF_CVT_SCALE_FUNC(32s16u, cvt_32f, int, ushort, float) -DEF_CVT_SCALE_FUNC(32f16u, cvt_32f, float, ushort, float) -DEF_CVT_SCALE_FUNC(64f16u, cvt_32f, double, ushort, float) -//DEF_CVT_SCALE_FUNC(16f16u, cvt1_32f, float16_t, ushort, float) - -DEF_CVT_SCALE_FUNC(8u16s, cvt_32f, uchar, short, float) -DEF_CVT_SCALE_FUNC(8s16s, cvt_32f, schar, short, float) -DEF_CVT_SCALE_FUNC(16u16s, cvt_32f, ushort, short, float) -DEF_CVT_SCALE_FUNC(16s, cvt_32f, short, short, float) -DEF_CVT_SCALE_FUNC(32s16s, cvt_32f, int, short, float) -DEF_CVT_SCALE_FUNC(32f16s, cvt_32f, float, short, float) -DEF_CVT_SCALE_FUNC(64f16s, cvt_32f, double, short, float) -//DEF_CVT_SCALE_FUNC(16f16s, cvt1_32f, float16_t, short, float) - -DEF_CVT_SCALE_FUNC(8u32s, cvt_32f, uchar, int, float) -DEF_CVT_SCALE_FUNC(8s32s, cvt_32f, schar, int, float) -DEF_CVT_SCALE_FUNC(16u32s, cvt_32f, ushort, int, float) -DEF_CVT_SCALE_FUNC(16s32s, cvt_32f, short, int, float) -DEF_CVT_SCALE_FUNC(32s, cvt_64f, int, int, double) -DEF_CVT_SCALE_FUNC(32f32s, cvt_32f, float, int, float) -DEF_CVT_SCALE_FUNC(64f32s, cvt_64f, double, int, double) -//DEF_CVT_SCALE_FUNC(16f32s, cvt1_32f, float16_t, int, float) - -DEF_CVT_SCALE_FUNC(8u32f, cvt_32f, uchar, float, float) -DEF_CVT_SCALE_FUNC(8s32f, cvt_32f, schar, float, float) -DEF_CVT_SCALE_FUNC(16u32f, cvt_32f, ushort, float, float) -DEF_CVT_SCALE_FUNC(16s32f, cvt_32f, short, float, float) -DEF_CVT_SCALE_FUNC(32s32f, cvt_32f, int, float, float) -DEF_CVT_SCALE_FUNC(32f, cvt_32f, float, float, float) -DEF_CVT_SCALE_FUNC(64f32f, cvt_64f, double, float, double) -//DEF_CVT_SCALE_FUNC(16f32f, cvt1_32f, float16_t, float, float) - -DEF_CVT_SCALE_FUNC(8u64f, cvt_64f, uchar, double, double) -DEF_CVT_SCALE_FUNC(8s64f, cvt_64f, schar, double, double) -DEF_CVT_SCALE_FUNC(16u64f, cvt_64f, ushort, double, double) -DEF_CVT_SCALE_FUNC(16s64f, cvt_64f, short, double, double) -DEF_CVT_SCALE_FUNC(32s64f, cvt_64f, int, double, double) -DEF_CVT_SCALE_FUNC(32f64f, cvt_64f, float, double, double) -DEF_CVT_SCALE_FUNC(64f, cvt_64f, double, double, double) -//DEF_CVT_SCALE_FUNC(16f64f, cvt_64f, float16_t, double, double) - -/*DEF_CVT_SCALE_FUNC(8u16f, cvt1_32f, uchar, float16_t, float) -DEF_CVT_SCALE_FUNC(8s16f, cvt1_32f, schar, float16_t, float) -DEF_CVT_SCALE_FUNC(16u16f, cvt1_32f, ushort, float16_t, float) -DEF_CVT_SCALE_FUNC(16s16f, cvt1_32f, short, float16_t, float) -DEF_CVT_SCALE_FUNC(32s16f, cvt1_32f, int, float16_t, float) -DEF_CVT_SCALE_FUNC(32f16f, cvt1_32f, float, float16_t, float) -DEF_CVT_SCALE_FUNC(64f16f, cvt_64f, double, float16_t, double) -DEF_CVT_SCALE_FUNC(16f, cvt1_32f, float16_t, float16_t, float)*/ - static BinaryFunc getCvtScaleAbsFunc(int depth) { - static BinaryFunc cvtScaleAbsTab[] = - { - (BinaryFunc)cvtScaleAbs8u, (BinaryFunc)cvtScaleAbs8s8u, (BinaryFunc)cvtScaleAbs16u8u, - (BinaryFunc)cvtScaleAbs16s8u, (BinaryFunc)cvtScaleAbs32s8u, (BinaryFunc)cvtScaleAbs32f8u, - (BinaryFunc)cvtScaleAbs64f8u, 0 - }; - - return cvtScaleAbsTab[depth]; + CV_INSTRUMENT_REGION(); + CV_CPU_DISPATCH(getCvtScaleAbsFunc, (depth), + CV_CPU_DISPATCH_MODES_ALL); } BinaryFunc getConvertScaleFunc(int sdepth, int ddepth) { - static BinaryFunc cvtScaleTab[][8] = - { - { - (BinaryFunc)GET_OPTIMIZED(cvtScale8u), (BinaryFunc)GET_OPTIMIZED(cvtScale8s8u), (BinaryFunc)GET_OPTIMIZED(cvtScale16u8u), - (BinaryFunc)GET_OPTIMIZED(cvtScale16s8u), (BinaryFunc)GET_OPTIMIZED(cvtScale32s8u), (BinaryFunc)GET_OPTIMIZED(cvtScale32f8u), - (BinaryFunc)cvtScale64f8u, 0 //(BinaryFunc)cvtScale16f8u - }, - { - (BinaryFunc)GET_OPTIMIZED(cvtScale8u8s), (BinaryFunc)GET_OPTIMIZED(cvtScale8s), (BinaryFunc)GET_OPTIMIZED(cvtScale16u8s), - (BinaryFunc)GET_OPTIMIZED(cvtScale16s8s), (BinaryFunc)GET_OPTIMIZED(cvtScale32s8s), (BinaryFunc)GET_OPTIMIZED(cvtScale32f8s), - (BinaryFunc)cvtScale64f8s, 0 //(BinaryFunc)cvtScale16f8s - }, - { - (BinaryFunc)GET_OPTIMIZED(cvtScale8u16u), (BinaryFunc)GET_OPTIMIZED(cvtScale8s16u), (BinaryFunc)GET_OPTIMIZED(cvtScale16u), - (BinaryFunc)GET_OPTIMIZED(cvtScale16s16u), (BinaryFunc)GET_OPTIMIZED(cvtScale32s16u), (BinaryFunc)GET_OPTIMIZED(cvtScale32f16u), - (BinaryFunc)cvtScale64f16u, 0 //(BinaryFunc)cvtScale16f16u - }, - { - (BinaryFunc)GET_OPTIMIZED(cvtScale8u16s), (BinaryFunc)GET_OPTIMIZED(cvtScale8s16s), (BinaryFunc)GET_OPTIMIZED(cvtScale16u16s), - (BinaryFunc)GET_OPTIMIZED(cvtScale16s), (BinaryFunc)GET_OPTIMIZED(cvtScale32s16s), (BinaryFunc)GET_OPTIMIZED(cvtScale32f16s), - (BinaryFunc)cvtScale64f16s, 0 //(BinaryFunc)cvtScale16f16s - }, - { - (BinaryFunc)GET_OPTIMIZED(cvtScale8u32s), (BinaryFunc)GET_OPTIMIZED(cvtScale8s32s), (BinaryFunc)GET_OPTIMIZED(cvtScale16u32s), - (BinaryFunc)GET_OPTIMIZED(cvtScale16s32s), (BinaryFunc)GET_OPTIMIZED(cvtScale32s), (BinaryFunc)GET_OPTIMIZED(cvtScale32f32s), - (BinaryFunc)cvtScale64f32s, 0 //(BinaryFunc)cvtScale16f32s - }, - { - (BinaryFunc)GET_OPTIMIZED(cvtScale8u32f), (BinaryFunc)GET_OPTIMIZED(cvtScale8s32f), (BinaryFunc)GET_OPTIMIZED(cvtScale16u32f), - (BinaryFunc)GET_OPTIMIZED(cvtScale16s32f), (BinaryFunc)GET_OPTIMIZED(cvtScale32s32f), (BinaryFunc)GET_OPTIMIZED(cvtScale32f), - (BinaryFunc)cvtScale64f32f, 0 //(BinaryFunc)cvtScale16f32f - }, - { - (BinaryFunc)cvtScale8u64f, (BinaryFunc)cvtScale8s64f, (BinaryFunc)cvtScale16u64f, - (BinaryFunc)cvtScale16s64f, (BinaryFunc)cvtScale32s64f, (BinaryFunc)cvtScale32f64f, - (BinaryFunc)cvtScale64f, 0 //(BinaryFunc)cvtScale16f64f - }, - { - 0, 0, 0, 0, 0, 0, 0, 0 - /*(BinaryFunc)cvtScale8u16f, (BinaryFunc)cvtScale8s16f, (BinaryFunc)cvtScale16u16f, - (BinaryFunc)cvtScale16s16f, (BinaryFunc)cvtScale32s16f, (BinaryFunc)cvtScale32f16f, - (BinaryFunc)cvtScale64f16f, (BinaryFunc)cvtScale16f*/ - }, - }; - - return cvtScaleTab[CV_MAT_DEPTH(ddepth)][CV_MAT_DEPTH(sdepth)]; + CV_INSTRUMENT_REGION(); + CV_CPU_DISPATCH(getConvertScaleFunc, (sdepth, ddepth), + CV_CPU_DISPATCH_MODES_ALL); } #ifdef HAVE_OPENCL @@ -407,10 +85,7 @@ static bool ocl_convertScaleAbs( InputArray _src, OutputArray _dst, double alpha #endif -} //cv:: - - -void cv::convertScaleAbs( InputArray _src, OutputArray _dst, double alpha, double beta ) +void convertScaleAbs(InputArray _src, OutputArray _dst, double alpha, double beta) { CV_INSTRUMENT_REGION(); @@ -444,8 +119,6 @@ void cv::convertScaleAbs( InputArray _src, OutputArray _dst, double alpha, doubl //================================================================================================== -namespace cv { - #ifdef HAVE_OPENCL static bool ocl_normalize( InputArray _src, InputOutputArray _dst, InputArray _mask, int dtype, @@ -535,10 +208,8 @@ static bool ocl_normalize( InputArray _src, InputOutputArray _dst, InputArray _m #endif -} // cv:: - -void cv::normalize( InputArray _src, InputOutputArray _dst, double a, double b, - int norm_type, int rtype, InputArray _mask ) +void normalize(InputArray _src, InputOutputArray _dst, double a, double b, + int norm_type, int rtype, InputArray _mask) { CV_INSTRUMENT_REGION(); @@ -584,3 +255,5 @@ void cv::normalize( InputArray _src, InputOutputArray _dst, double a, double b, temp.copyTo( _dst, _mask ); } } + +} // namespace diff --git a/modules/core/src/convert_scale.simd.hpp b/modules/core/src/convert_scale.simd.hpp index b17bed8f58..6e1207a299 100644 --- a/modules/core/src/convert_scale.simd.hpp +++ b/modules/core/src/convert_scale.simd.hpp @@ -4,16 +4,20 @@ #include "precomp.hpp" -#include "opencl_kernels_core.hpp" #include "convert.hpp" +namespace cv { +CV_CPU_OPTIMIZATION_NAMESPACE_BEGIN + +BinaryFunc getCvtScaleAbsFunc(int depth); +BinaryFunc getConvertScaleFunc(int sdepth, int ddepth); + +#ifndef CV_CPU_OPTIMIZATION_DECLARATIONS_ONLY + /****************************************************************************************\ * convertScale[Abs] * \****************************************************************************************/ -namespace cv -{ - template inline void cvtabs_32f( const _Ts* src, size_t sstep, _Td* dst, size_t dstep, Size size, float a, float b ) @@ -287,7 +291,7 @@ DEF_CVT_SCALE_FUNC(32f16f, cvt1_32f, float, float16_t, float) DEF_CVT_SCALE_FUNC(64f16f, cvt_64f, double, float16_t, double) DEF_CVT_SCALE_FUNC(16f, cvt1_32f, float16_t, float16_t, float)*/ -static BinaryFunc getCvtScaleAbsFunc(int depth) +BinaryFunc getCvtScaleAbsFunc(int depth) { static BinaryFunc cvtScaleAbsTab[] = { @@ -349,238 +353,7 @@ BinaryFunc getConvertScaleFunc(int sdepth, int ddepth) return cvtScaleTab[CV_MAT_DEPTH(ddepth)][CV_MAT_DEPTH(sdepth)]; } -#ifdef HAVE_OPENCL - -static bool ocl_convertScaleAbs( InputArray _src, OutputArray _dst, double alpha, double beta ) -{ - const ocl::Device & d = ocl::Device::getDefault(); - - int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); - bool doubleSupport = d.doubleFPConfig() > 0; - if (!doubleSupport && depth == CV_64F) - return false; - - _dst.create(_src.size(), CV_8UC(cn)); - int kercn = 1; - if (d.isIntel()) - { - static const int vectorWidths[] = {4, 4, 4, 4, 4, 4, 4, -1}; - kercn = ocl::checkOptimalVectorWidth( vectorWidths, _src, _dst, - noArray(), noArray(), noArray(), - noArray(), noArray(), noArray(), - noArray(), ocl::OCL_VECTOR_MAX); - } - else - kercn = ocl::predictOptimalVectorWidthMax(_src, _dst); - - int rowsPerWI = d.isIntel() ? 4 : 1; - char cvt[2][50]; - int wdepth = std::max(depth, CV_32F); - String build_opt = format("-D OP_CONVERT_SCALE_ABS -D UNARY_OP -D dstT=%s -D DEPTH_dst=%d -D srcT1=%s" - " -D workT=%s -D wdepth=%d -D convertToWT1=%s -D convertToDT=%s" - " -D workT1=%s -D rowsPerWI=%d%s", - ocl::typeToStr(CV_8UC(kercn)), CV_8U, - ocl::typeToStr(CV_MAKE_TYPE(depth, kercn)), - ocl::typeToStr(CV_MAKE_TYPE(wdepth, kercn)), wdepth, - ocl::convertTypeStr(depth, wdepth, kercn, cvt[0]), - ocl::convertTypeStr(wdepth, CV_8U, kercn, cvt[1]), - ocl::typeToStr(wdepth), rowsPerWI, - doubleSupport ? " -D DOUBLE_SUPPORT" : ""); - ocl::Kernel k("KF", ocl::core::arithm_oclsrc, build_opt); - if (k.empty()) - return false; - - UMat src = _src.getUMat(); - UMat dst = _dst.getUMat(); - - ocl::KernelArg srcarg = ocl::KernelArg::ReadOnlyNoSize(src), - dstarg = ocl::KernelArg::WriteOnly(dst, cn, kercn); - - if (wdepth == CV_32F) - k.args(srcarg, dstarg, (float)alpha, (float)beta); - else if (wdepth == CV_64F) - k.args(srcarg, dstarg, alpha, beta); - - size_t globalsize[2] = { (size_t)src.cols * cn / kercn, ((size_t)src.rows + rowsPerWI - 1) / rowsPerWI }; - return k.run(2, globalsize, NULL, false); -} - #endif -} //cv:: - - -void cv::convertScaleAbs( InputArray _src, OutputArray _dst, double alpha, double beta ) -{ - CV_INSTRUMENT_REGION(); - - CV_OCL_RUN(_src.dims() <= 2 && _dst.isUMat(), - ocl_convertScaleAbs(_src, _dst, alpha, beta)) - - Mat src = _src.getMat(); - int cn = src.channels(); - double scale[] = {alpha, beta}; - _dst.create( src.dims, src.size, CV_8UC(cn) ); - Mat dst = _dst.getMat(); - BinaryFunc func = getCvtScaleAbsFunc(src.depth()); - CV_Assert( func != 0 ); - - if( src.dims <= 2 ) - { - Size sz = getContinuousSize2D(src, dst, cn); - func( src.ptr(), src.step, 0, 0, dst.ptr(), dst.step, sz, scale ); - } - else - { - const Mat* arrays[] = {&src, &dst, 0}; - uchar* ptrs[2] = {}; - NAryMatIterator it(arrays, ptrs); - Size sz((int)it.size*cn, 1); - - for( size_t i = 0; i < it.nplanes; i++, ++it ) - func( ptrs[0], 0, 0, 0, ptrs[1], 0, sz, scale ); - } -} - -//================================================================================================== - -namespace cv { - -#ifdef HAVE_OPENCL - -static bool ocl_normalize( InputArray _src, InputOutputArray _dst, InputArray _mask, int dtype, - double scale, double delta ) -{ - UMat src = _src.getUMat(); - - if( _mask.empty() ) - src.convertTo( _dst, dtype, scale, delta ); - else if (src.channels() <= 4) - { - const ocl::Device & dev = ocl::Device::getDefault(); - - int stype = _src.type(), sdepth = CV_MAT_DEPTH(stype), cn = CV_MAT_CN(stype), - ddepth = CV_MAT_DEPTH(dtype), wdepth = std::max(CV_32F, std::max(sdepth, ddepth)), - rowsPerWI = dev.isIntel() ? 4 : 1; - - float fscale = static_cast(scale), fdelta = static_cast(delta); - bool haveScale = std::fabs(scale - 1) > DBL_EPSILON, - haveZeroScale = !(std::fabs(scale) > DBL_EPSILON), - haveDelta = std::fabs(delta) > DBL_EPSILON, - doubleSupport = dev.doubleFPConfig() > 0; - - if (!haveScale && !haveDelta && stype == dtype) - { - _src.copyTo(_dst, _mask); - return true; - } - if (haveZeroScale) - { - _dst.setTo(Scalar(delta), _mask); - return true; - } - - if ((sdepth == CV_64F || ddepth == CV_64F) && !doubleSupport) - return false; - - char cvt[2][40]; - String opts = format("-D srcT=%s -D dstT=%s -D convertToWT=%s -D cn=%d -D rowsPerWI=%d" - " -D convertToDT=%s -D workT=%s%s%s%s -D srcT1=%s -D dstT1=%s", - ocl::typeToStr(stype), ocl::typeToStr(dtype), - ocl::convertTypeStr(sdepth, wdepth, cn, cvt[0]), cn, - rowsPerWI, ocl::convertTypeStr(wdepth, ddepth, cn, cvt[1]), - ocl::typeToStr(CV_MAKE_TYPE(wdepth, cn)), - doubleSupport ? " -D DOUBLE_SUPPORT" : "", - haveScale ? " -D HAVE_SCALE" : "", - haveDelta ? " -D HAVE_DELTA" : "", - ocl::typeToStr(sdepth), ocl::typeToStr(ddepth)); - - ocl::Kernel k("normalizek", ocl::core::normalize_oclsrc, opts); - if (k.empty()) - return false; - - UMat mask = _mask.getUMat(), dst = _dst.getUMat(); - - ocl::KernelArg srcarg = ocl::KernelArg::ReadOnlyNoSize(src), - maskarg = ocl::KernelArg::ReadOnlyNoSize(mask), - dstarg = ocl::KernelArg::ReadWrite(dst); - - if (haveScale) - { - if (haveDelta) - k.args(srcarg, maskarg, dstarg, fscale, fdelta); - else - k.args(srcarg, maskarg, dstarg, fscale); - } - else - { - if (haveDelta) - k.args(srcarg, maskarg, dstarg, fdelta); - else - k.args(srcarg, maskarg, dstarg); - } - - size_t globalsize[2] = { (size_t)src.cols, ((size_t)src.rows + rowsPerWI - 1) / rowsPerWI }; - return k.run(2, globalsize, NULL, false); - } - else - { - UMat temp; - src.convertTo( temp, dtype, scale, delta ); - temp.copyTo( _dst, _mask ); - } - - return true; -} - -#endif - -} // cv:: - -void cv::normalize( InputArray _src, InputOutputArray _dst, double a, double b, - int norm_type, int rtype, InputArray _mask ) -{ - CV_INSTRUMENT_REGION(); - - double scale = 1, shift = 0; - int type = _src.type(), depth = CV_MAT_DEPTH(type); - - if( rtype < 0 ) - rtype = _dst.fixedType() ? _dst.depth() : depth; - - if( norm_type == CV_MINMAX ) - { - double smin = 0, smax = 0; - double dmin = MIN( a, b ), dmax = MAX( a, b ); - minMaxIdx( _src, &smin, &smax, 0, 0, _mask ); - scale = (dmax - dmin)*(smax - smin > DBL_EPSILON ? 1./(smax - smin) : 0); - if( rtype == CV_32F ) - { - scale = (float)scale; - shift = (float)dmin - (float)(smin*scale); - } - else - shift = dmin - smin*scale; - } - else if( norm_type == CV_L2 || norm_type == CV_L1 || norm_type == CV_C ) - { - scale = norm( _src, norm_type, _mask ); - scale = scale > DBL_EPSILON ? a/scale : 0.; - shift = 0; - } - else - CV_Error( CV_StsBadArg, "Unknown/unsupported norm type" ); - - CV_OCL_RUN(_dst.isUMat(), - ocl_normalize(_src, _dst, _mask, rtype, scale, shift)) - - Mat src = _src.getMat(); - if( _mask.empty() ) - src.convertTo( _dst, rtype, scale, shift ); - else - { - Mat temp; - src.convertTo( temp, rtype, scale, shift ); - temp.copyTo( _dst, _mask ); - } -} +CV_CPU_OPTIMIZATION_NAMESPACE_END +} // namespace From f0ef4b2246015a4a4c138c86e99740f94a216714 Mon Sep 17 00:00:00 2001 From: Pavel Rojtberg Date: Fri, 8 Feb 2019 17:24:31 +0100 Subject: [PATCH 07/12] python: sys.path[0] is reserved for the main script --- modules/python/package/cv2/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/python/package/cv2/__init__.py b/modules/python/package/cv2/__init__.py index b176c0d954..9427365dd2 100644 --- a/modules/python/package/cv2/__init__.py +++ b/modules/python/package/cv2/__init__.py @@ -65,7 +65,7 @@ def bootstrap(): if DEBUG: print('OpenCV loader: BINARIES_PATHS={}'.format(str(l_vars['BINARIES_PATHS']))) for p in reversed(l_vars['PYTHON_EXTENSIONS_PATHS']): - sys.path.insert(0, p) + sys.path.insert(1, p) if os.name == 'nt': os.environ['PATH'] = ';'.join(l_vars['BINARIES_PATHS']) + ';' + os.environ.get('PATH', '') From c8777da2d601f7db6f71de9f9965a22d541b8bd4 Mon Sep 17 00:00:00 2001 From: Oleh <43003542+vertextau@users.noreply.github.com> Date: Mon, 11 Feb 2019 10:45:16 +0200 Subject: [PATCH 08/12] Update video_input_psnr_ssim.markdown --- .../video-input-psnr-ssim/video_input_psnr_ssim.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tutorials/videoio/video-input-psnr-ssim/video_input_psnr_ssim.markdown b/doc/tutorials/videoio/video-input-psnr-ssim/video_input_psnr_ssim.markdown index a07736f88d..80205213a2 100644 --- a/doc/tutorials/videoio/video-input-psnr-ssim/video_input_psnr_ssim.markdown +++ b/doc/tutorials/videoio/video-input-psnr-ssim/video_input_psnr_ssim.markdown @@ -77,7 +77,7 @@ by the @ref cv::VideoCapture::read or the overloaded \>\> operator: @code{.cpp} Mat frameReference, frameUnderTest; captRefrnc >> frameReference; -captUndTst.open(frameUnderTest); +captUndTst.read(frameUnderTest); @endcode The upper read operations will leave empty the *Mat* objects if no frame could be acquired (either cause the video stream was closed or you got to the end of the video file). We can check this with a From 5d9c6723eef192da9222e273b3d975e12a71f519 Mon Sep 17 00:00:00 2001 From: klemens Date: Sat, 9 Feb 2019 22:29:54 +0100 Subject: [PATCH 09/12] spelling fixes backport 997b7b18af10cd70519a65bed9892cdf830bd525 --- apps/traincascade/old_ml.hpp | 4 ++-- doc/pattern_tools/gen_pattern.py | 2 +- doc/py_tutorials/py_feature2d/py_brief/py_brief.markdown | 2 +- modules/calib3d/src/rho.cpp | 2 +- modules/core/include/opencv2/core/cvstd.hpp | 2 +- modules/core/include/opencv2/core/hal/intrin_cpp.hpp | 2 +- modules/core/include/opencv2/core/hal/intrin_neon.hpp | 2 +- modules/core/src/bindings_utils.cpp | 8 ++++---- modules/core/src/precomp.hpp | 2 +- modules/core/src/utils/datafile.cpp | 2 +- modules/dnn/src/onnx/opencv-onnx.proto | 2 +- modules/ml/include/opencv2/ml.hpp | 2 +- modules/ml/src/svm.cpp | 2 +- modules/videoio/src/cap_dshow.cpp | 2 +- 14 files changed, 18 insertions(+), 18 deletions(-) diff --git a/apps/traincascade/old_ml.hpp b/apps/traincascade/old_ml.hpp index de3aa407a2..30e806d77d 100644 --- a/apps/traincascade/old_ml.hpp +++ b/apps/traincascade/old_ml.hpp @@ -88,7 +88,7 @@ struct CvVectors #if 0 /* A structure, representing the lattice range of statmodel parameters. It is used for optimizing statmodel parameters by cross-validation method. - The lattice is logarithmic, so must be greater then 1. */ + The lattice is logarithmic, so must be greater than 1. */ typedef struct CvParamLattice { double min_val; @@ -158,7 +158,7 @@ protected: /* The structure, representing the grid range of statmodel parameters. It is used for optimizing statmodel accuracy by varying model parameters, the accuracy estimate being computed by cross-validation. - The grid is logarithmic, so must be greater then 1. */ + The grid is logarithmic, so must be greater than 1. */ class CvMLData; diff --git a/doc/pattern_tools/gen_pattern.py b/doc/pattern_tools/gen_pattern.py index a846a03d80..a40b7887c5 100755 --- a/doc/pattern_tools/gen_pattern.py +++ b/doc/pattern_tools/gen_pattern.py @@ -12,7 +12,7 @@ python gen_pattern.py -o out.svg -r 11 -c 8 -T circles -s 20.0 -R 5.0 -u mm -w 2 -u, --units - mm, inches, px, m (default mm) -w, --page_width - page width in units (default 216) -h, --page_height - page height in units (default 279) --a, --page_size - page size (default A4), supercedes -h -w arguments +-a, --page_size - page size (default A4), supersedes -h -w arguments -H, --help - show help """ diff --git a/doc/py_tutorials/py_feature2d/py_brief/py_brief.markdown b/doc/py_tutorials/py_feature2d/py_brief/py_brief.markdown index 1dcdee54a6..825183747e 100644 --- a/doc/py_tutorials/py_feature2d/py_brief/py_brief.markdown +++ b/doc/py_tutorials/py_feature2d/py_brief/py_brief.markdown @@ -12,7 +12,7 @@ Theory We know SIFT uses 128-dim vector for descriptors. Since it is using floating point numbers, it takes basically 512 bytes. Similarly SURF also takes minimum of 256 bytes (for 64-dim). Creating such a -vector for thousands of features takes a lot of memory which are not feasible for resouce-constraint +vector for thousands of features takes a lot of memory which are not feasible for resource-constraint applications especially for embedded systems. Larger the memory, longer the time it takes for matching. diff --git a/modules/calib3d/src/rho.cpp b/modules/calib3d/src/rho.cpp index 15af05ea81..3cfa6b19e8 100644 --- a/modules/calib3d/src/rho.cpp +++ b/modules/calib3d/src/rho.cpp @@ -2164,7 +2164,7 @@ inline void RHO_HEST_REFC::refine(void){ * order to compute a candidate homography (newH). * * The system above is solved by Cholesky decomposition of a - * sufficently-damped JtJ into a lower-triangular matrix (and its + * sufficiently-damped JtJ into a lower-triangular matrix (and its * transpose), whose inverse is then computed. This inverse (and its * transpose) then multiply Jte in order to find dH. */ diff --git a/modules/core/include/opencv2/core/cvstd.hpp b/modules/core/include/opencv2/core/cvstd.hpp index 0a3f553ab8..8af1162bfa 100644 --- a/modules/core/include/opencv2/core/cvstd.hpp +++ b/modules/core/include/opencv2/core/cvstd.hpp @@ -103,7 +103,7 @@ double memory deallocation. CV_EXPORTS void fastFree(void* ptr); /*! - The STL-compilant memory Allocator based on cv::fastMalloc() and cv::fastFree() + The STL-compliant memory Allocator based on cv::fastMalloc() and cv::fastFree() */ template class Allocator { diff --git a/modules/core/include/opencv2/core/hal/intrin_cpp.hpp b/modules/core/include/opencv2/core/hal/intrin_cpp.hpp index 65a01f31d7..3b419786a9 100644 --- a/modules/core/include/opencv2/core/hal/intrin_cpp.hpp +++ b/modules/core/include/opencv2/core/hal/intrin_cpp.hpp @@ -2266,7 +2266,7 @@ inline v_float32x4 v_matmuladd(const v_float32x4& v, const v_float32x4& m0, v.s[0]*m0.s[3] + v.s[1]*m1.s[3] + v.s[2]*m2.s[3] + m3.s[3]); } -////// FP16 suport /////// +////// FP16 support /////// inline v_reg::nlanes128> v_load_expand(const float16_t* ptr) diff --git a/modules/core/include/opencv2/core/hal/intrin_neon.hpp b/modules/core/include/opencv2/core/hal/intrin_neon.hpp index 608dc97a87..0cbde4d539 100644 --- a/modules/core/include/opencv2/core/hal/intrin_neon.hpp +++ b/modules/core/include/opencv2/core/hal/intrin_neon.hpp @@ -1635,7 +1635,7 @@ inline void v_lut_deinterleave(const double* tab, const v_int32x4& idxvec, v_flo } #endif -////// FP16 suport /////// +////// FP16 support /////// #if CV_FP16 inline v_float32x4 v_load_expand(const float16_t* ptr) { diff --git a/modules/core/src/bindings_utils.cpp b/modules/core/src/bindings_utils.cpp index c1a549c218..432f280e52 100644 --- a/modules/core/src/bindings_utils.cpp +++ b/modules/core/src/bindings_utils.cpp @@ -33,7 +33,7 @@ String dumpInputArray(InputArray argument) } catch (...) { - ss << " ERROR: exception occured, dump is non-complete"; // need to properly support different kinds + ss << " ERROR: exception occurred, dump is non-complete"; // need to properly support different kinds } return ss.str(); } @@ -70,7 +70,7 @@ CV_EXPORTS_W String dumpInputArrayOfArrays(InputArrayOfArrays argument) } catch (...) { - ss << " ERROR: exception occured, dump is non-complete"; // need to properly support different kinds + ss << " ERROR: exception occurred, dump is non-complete"; // need to properly support different kinds } return ss.str(); } @@ -100,7 +100,7 @@ CV_EXPORTS_W String dumpInputOutputArray(InputOutputArray argument) } catch (...) { - ss << " ERROR: exception occured, dump is non-complete"; // need to properly support different kinds + ss << " ERROR: exception occurred, dump is non-complete"; // need to properly support different kinds } return ss.str(); } @@ -137,7 +137,7 @@ CV_EXPORTS_W String dumpInputOutputArrayOfArrays(InputOutputArrayOfArrays argume } catch (...) { - ss << " ERROR: exception occured, dump is non-complete"; // need to properly support different kinds + ss << " ERROR: exception occurred, dump is non-complete"; // need to properly support different kinds } return ss.str(); } diff --git a/modules/core/src/precomp.hpp b/modules/core/src/precomp.hpp index bcdf175732..acaee08eab 100644 --- a/modules/core/src/precomp.hpp +++ b/modules/core/src/precomp.hpp @@ -250,7 +250,7 @@ BinaryFunc getCopyMaskFunc(size_t esz); // There is some mess in code with vectors representation. // Both vector-column / vector-rows are used with dims=2 (as Mat2D always). -// Reshape matrices if neccessary (in case of vectors) and returns size with scaled width. +// Reshape matrices if necessary (in case of vectors) and returns size with scaled width. Size getContinuousSize2D(Mat& m1, int widthScale=1); Size getContinuousSize2D(Mat& m1, Mat& m2, int widthScale=1); Size getContinuousSize2D(Mat& m1, Mat& m2, Mat& m3, int widthScale=1); diff --git a/modules/core/src/utils/datafile.cpp b/modules/core/src/utils/datafile.cpp index 09b412f09d..f1107b097a 100644 --- a/modules/core/src/utils/datafile.cpp +++ b/modules/core/src/utils/datafile.cpp @@ -344,7 +344,7 @@ cv::String findDataFile(const cv::String& relative_path, #if defined OPENCV_INSTALL_PREFIX && defined OPENCV_DATA_INSTALL_PATH cv::String install_dir(OPENCV_INSTALL_PREFIX); // use core/world module path and verify that library is running from installation directory - // It is neccessary to avoid touching of unrelated common /usr/local path + // It is necessary to avoid touching of unrelated common /usr/local path if (module_path.empty()) // can't determine module_path = install_dir; if (isSubDirectory(install_dir, module_path) || isSubDirectory(utils::fs::canonical(install_dir), utils::fs::canonical(module_path))) diff --git a/modules/dnn/src/onnx/opencv-onnx.proto b/modules/dnn/src/onnx/opencv-onnx.proto index 6433d5d6cb..dd846e65d1 100644 --- a/modules/dnn/src/onnx/opencv-onnx.proto +++ b/modules/dnn/src/onnx/opencv-onnx.proto @@ -119,7 +119,7 @@ message AttributeProto { // implementations needed to use has_field hueristics to determine // which value field was in use. For IR_VERSION 0.0.2 or later, this // field MUST be set and match the f|i|s|t|... field in use. This - // change was made to accomodate proto3 implementations. + // change was made to accommodate proto3 implementations. optional AttributeType type = 20; // discriminator that indicates which field below is in use // Exactly ONE of the following fields must be present for this version of the IR diff --git a/modules/ml/include/opencv2/ml.hpp b/modules/ml/include/opencv2/ml.hpp index f2ca78fe91..534820645b 100644 --- a/modules/ml/include/opencv2/ml.hpp +++ b/modules/ml/include/opencv2/ml.hpp @@ -120,7 +120,7 @@ public: \f[(minVal, minVal*step, minVal*{step}^2, \dots, minVal*{logStep}^n),\f] where \f$n\f$ is the maximal index satisfying \f[\texttt{minVal} * \texttt{logStep} ^n < \texttt{maxVal}\f] - The grid is logarithmic, so logStep must always be greater then 1. Default value is 1. + The grid is logarithmic, so logStep must always be greater than 1. Default value is 1. */ CV_PROP_RW double logStep; diff --git a/modules/ml/src/svm.cpp b/modules/ml/src/svm.cpp index 299e5ef3a3..4d987eb678 100644 --- a/modules/ml/src/svm.cpp +++ b/modules/ml/src/svm.cpp @@ -99,7 +99,7 @@ static void checkParamGrid(const ParamGrid& pg) if( pg.minVal < DBL_EPSILON ) CV_Error( CV_StsBadArg, "Lower bound of the grid must be positive" ); if( pg.logStep < 1. + FLT_EPSILON ) - CV_Error( CV_StsBadArg, "Grid step must greater then 1" ); + CV_Error( CV_StsBadArg, "Grid step must greater than 1" ); } // SVM training parameters diff --git a/modules/videoio/src/cap_dshow.cpp b/modules/videoio/src/cap_dshow.cpp index a9d89bb899..7e12cb0b29 100644 --- a/modules/videoio/src/cap_dshow.cpp +++ b/modules/videoio/src/cap_dshow.cpp @@ -2171,7 +2171,7 @@ void videoInput::setPhyCon(int id, int conn){ // ---------------------------------------------------------------------- -// Check that we are not trying to setup a non-existant device +// Check that we are not trying to setup a non-existent device // Then start the graph building! // ---------------------------------------------------------------------- From 0711dab09ddf5a549be6e542150d0b8f90e0e783 Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Mon, 11 Feb 2019 17:13:39 +0300 Subject: [PATCH 10/12] Fix Intel's Inference Engine backend from future. Second try. --- modules/dnn/src/dnn.cpp | 24 ++++++++++++++++++- .../dnn/src/layers/normalize_bbox_layer.cpp | 20 ++++++++++------ modules/dnn/src/layers/resize_layer.cpp | 2 +- modules/dnn/src/op_inf_engine.cpp | 2 +- modules/dnn/src/op_inf_engine.hpp | 3 ++- modules/dnn/test/test_halide_layers.cpp | 5 ++++ 6 files changed, 45 insertions(+), 11 deletions(-) diff --git a/modules/dnn/src/dnn.cpp b/modules/dnn/src/dnn.cpp index f910185b83..fc5548cd8d 100644 --- a/modules/dnn/src/dnn.cpp +++ b/modules/dnn/src/dnn.cpp @@ -1637,6 +1637,27 @@ struct Net::Impl preferableTarget == DNN_TARGET_MYRIAD || preferableTarget == DNN_TARGET_FPGA) && !fused) { +#if INF_ENGINE_VER_MAJOR_GT(INF_ENGINE_RELEASE_2018R5) + bool hasWeights = false; + for (const std::string& name : {"weights", "biases"}) + { + auto it = ieNode->layer.getParameters().find(name); + if (it != ieNode->layer.getParameters().end()) + { + InferenceEngine::Blob::CPtr bp = it->second.as(); + it->second = (InferenceEngine::Blob::CPtr)convertFp16(std::const_pointer_cast(bp)); + hasWeights = true; + } + } + if (!hasWeights) + { + InferenceEngine::Blob::Ptr blob = InferenceEngine::make_shared_blob( + InferenceEngine::Precision::FP16, + InferenceEngine::Layout::C, {1}); + blob->allocate(); + ieNode->layer.getParameters()["weights"] = (InferenceEngine::Blob::CPtr)blob; + } +#else auto& blobs = ieNode->layer.getConstantData(); if (blobs.empty()) { @@ -1653,6 +1674,7 @@ struct Net::Impl for (auto& it : blobs) it.second = convertFp16(std::const_pointer_cast(it.second)); } +#endif } if (!fused) @@ -1724,7 +1746,7 @@ struct Net::Impl if (!ieNode->net->isInitialized()) { -#if INF_ENGINE_VER_MAJOR_GT(INF_ENGINE_RELEASE_2018R3) +#if INF_ENGINE_VER_MAJOR_EQ(INF_ENGINE_RELEASE_2018R4) // For networks which is built in runtime we need to specify a // version of it's hyperparameters. std::string versionTrigger = "" diff --git a/modules/dnn/src/layers/normalize_bbox_layer.cpp b/modules/dnn/src/layers/normalize_bbox_layer.cpp index 4766f1704e..8e21f116e4 100644 --- a/modules/dnn/src/layers/normalize_bbox_layer.cpp +++ b/modules/dnn/src/layers/normalize_bbox_layer.cpp @@ -276,23 +276,29 @@ public: InferenceEngine::Builder::Layer l = ieLayer; const int numChannels = input->dims[2]; // NOTE: input->dims are reversed (whcn) + InferenceEngine::Blob::Ptr weights; if (blobs.empty()) { - auto weights = InferenceEngine::make_shared_blob(InferenceEngine::Precision::FP32, - InferenceEngine::Layout::C, - {(size_t)numChannels}); - weights->allocate(); + auto onesBlob = InferenceEngine::make_shared_blob(InferenceEngine::Precision::FP32, + InferenceEngine::Layout::C, + {(size_t)numChannels}); + onesBlob->allocate(); std::vector ones(numChannels, 1); - weights->set(ones); - l.addConstantData("weights", weights); + onesBlob->set(ones); + weights = onesBlob; l.getParameters()["channel_shared"] = false; } else { CV_Assert(numChannels == blobs[0].total()); - l.addConstantData("weights", wrapToInfEngineBlob(blobs[0], {(size_t)numChannels}, InferenceEngine::Layout::C)); + weights = wrapToInfEngineBlob(blobs[0], {(size_t)numChannels}, InferenceEngine::Layout::C); l.getParameters()["channel_shared"] = blobs[0].total() == 1; } +#if INF_ENGINE_VER_MAJOR_GT(INF_ENGINE_RELEASE_2018R5) + l.getParameters()["weights"] = (InferenceEngine::Blob::CPtr)weights; +#else + l.addConstantData("weights", weights); +#endif l.getParameters()["across_spatial"] = acrossSpatial; return Ptr(new InfEngineBackendNode(l)); } diff --git a/modules/dnn/src/layers/resize_layer.cpp b/modules/dnn/src/layers/resize_layer.cpp index 03d806ad2c..d5ab1acd6e 100644 --- a/modules/dnn/src/layers/resize_layer.cpp +++ b/modules/dnn/src/layers/resize_layer.cpp @@ -173,7 +173,7 @@ public: ieLayer.getParameters()["antialias"] = false; if (scaleWidth != scaleHeight) CV_Error(Error::StsNotImplemented, "resample with sw != sh"); - ieLayer.getParameters()["factor"] = 1.0 / scaleWidth; + ieLayer.getParameters()["factor"] = 1.0f / scaleWidth; } else if (interpolation == "bilinear") { diff --git a/modules/dnn/src/op_inf_engine.cpp b/modules/dnn/src/op_inf_engine.cpp index 0349d44710..a452064337 100644 --- a/modules/dnn/src/op_inf_engine.cpp +++ b/modules/dnn/src/op_inf_engine.cpp @@ -766,7 +766,7 @@ void InfEngineBackendLayer::forward(InputArrayOfArrays inputs, OutputArrayOfArra CV_Error(Error::StsInternal, "Choose Inference Engine as a preferable backend."); } -InferenceEngine::TBlob::Ptr convertFp16(const InferenceEngine::Blob::Ptr& blob) +InferenceEngine::Blob::Ptr convertFp16(const InferenceEngine::Blob::Ptr& blob) { auto halfs = InferenceEngine::make_shared_blob(InferenceEngine::Precision::FP16, blob->layout(), blob->dims()); halfs->allocate(); diff --git a/modules/dnn/src/op_inf_engine.hpp b/modules/dnn/src/op_inf_engine.hpp index 89e3e339cf..e912725296 100644 --- a/modules/dnn/src/op_inf_engine.hpp +++ b/modules/dnn/src/op_inf_engine.hpp @@ -36,6 +36,7 @@ #define INF_ENGINE_VER_MAJOR_GT(ver) (((INF_ENGINE_RELEASE) / 10000) > ((ver) / 10000)) #define INF_ENGINE_VER_MAJOR_GE(ver) (((INF_ENGINE_RELEASE) / 10000) >= ((ver) / 10000)) #define INF_ENGINE_VER_MAJOR_LT(ver) (((INF_ENGINE_RELEASE) / 10000) < ((ver) / 10000)) +#define INF_ENGINE_VER_MAJOR_EQ(ver) (((INF_ENGINE_RELEASE) / 10000) == ((ver) / 10000)) #if INF_ENGINE_VER_MAJOR_GE(INF_ENGINE_RELEASE_2018R5) #include @@ -252,7 +253,7 @@ Mat infEngineBlobToMat(const InferenceEngine::Blob::Ptr& blob); // Convert Inference Engine blob with FP32 precision to FP16 precision. // Allocates memory for a new blob. -InferenceEngine::TBlob::Ptr convertFp16(const InferenceEngine::Blob::Ptr& blob); +InferenceEngine::Blob::Ptr convertFp16(const InferenceEngine::Blob::Ptr& blob); // This is a fake class to run networks from Model Optimizer. Objects of that // class simulate responses of layers are imported by OpenCV and supported by diff --git a/modules/dnn/test/test_halide_layers.cpp b/modules/dnn/test/test_halide_layers.cpp index 9cbfb0c402..879dd7bbf0 100644 --- a/modules/dnn/test/test_halide_layers.cpp +++ b/modules/dnn/test/test_halide_layers.cpp @@ -694,6 +694,11 @@ TEST_P(Eltwise, Accuracy) Backend backendId = get<0>(get<4>(GetParam())); Target targetId = get<1>(get<4>(GetParam())); +#if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE > 2018050000 + if (backendId == DNN_BACKEND_INFERENCE_ENGINE && targetId == DNN_TARGET_OPENCL) + throw SkipTestException(""); +#endif + Net net; std::vector convLayerIds(numConv); From 1606137df26ad2aa5a7bf76d0499d143332823df Mon Sep 17 00:00:00 2001 From: Dmitry Kurtaev Date: Mon, 11 Feb 2019 20:08:17 +0300 Subject: [PATCH 11/12] Read raw floats data from Caffe models --- modules/dnn/src/caffe/caffe_importer.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/modules/dnn/src/caffe/caffe_importer.cpp b/modules/dnn/src/caffe/caffe_importer.cpp index 3c3119bb79..34e1f62546 100644 --- a/modules/dnn/src/caffe/caffe_importer.cpp +++ b/modules/dnn/src/caffe/caffe_importer.cpp @@ -260,14 +260,23 @@ public: } else { - // Half precision floats. - CV_Assert(pbBlob.raw_data_type() == caffe::FLOAT16); - std::string raw_data = pbBlob.raw_data(); + CV_Assert(pbBlob.has_raw_data()); + const std::string& raw_data = pbBlob.raw_data(); + if (pbBlob.raw_data_type() == caffe::FLOAT16) + { + // Half precision floats. + CV_Assert(raw_data.size() / 2 == (int)dstBlob.total()); - CV_Assert(raw_data.size() / 2 == (int)dstBlob.total()); - - Mat halfs((int)shape.size(), &shape[0], CV_16SC1, (void*)raw_data.c_str()); - convertFp16(halfs, dstBlob); + Mat halfs((int)shape.size(), &shape[0], CV_16SC1, (void*)raw_data.c_str()); + convertFp16(halfs, dstBlob); + } + else if (pbBlob.raw_data_type() == caffe::FLOAT) + { + CV_Assert(raw_data.size() / 4 == (int)dstBlob.total()); + Mat((int)shape.size(), &shape[0], CV_32FC1, (void*)raw_data.c_str()).copyTo(dstBlob); + } + else + CV_Error(Error::StsNotImplemented, "Unexpected blob data type"); } } From a1f4e65d034bc17b783497aafd251ab417f1dcea Mon Sep 17 00:00:00 2001 From: Thang Tran Date: Mon, 11 Feb 2019 22:07:30 +0100 Subject: [PATCH 12/12] calib3d: Fix calibrateCamera() misleading error objectPoints and imagePoints are not checked whether they're empty and cause checkVector() to fail, thus result in a wrong error message. Fixes: https://github.com/opencv/opencv/issues/6002 --- modules/calib3d/src/calibration.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/modules/calib3d/src/calibration.cpp b/modules/calib3d/src/calibration.cpp index 112e4c9b89..9fa1af88cc 100644 --- a/modules/calib3d/src/calibration.cpp +++ b/modules/calib3d/src/calibration.cpp @@ -3141,10 +3141,17 @@ static void collectCalibrationData( InputArrayOfArrays objectPoints, for( i = 0; i < nimages; i++ ) { - ni = objectPoints.getMat(i).checkVector(3, CV_32F); + Mat objectPoint = objectPoints.getMat(i); + if (objectPoint.empty()) + CV_Error(CV_StsBadSize, "objectPoints should not contain empty vector of vectors of points"); + ni = objectPoint.checkVector(3, CV_32F); if( ni <= 0 ) CV_Error(CV_StsUnsupportedFormat, "objectPoints should contain vector of vectors of points of type Point3f"); - int ni1 = imagePoints1.getMat(i).checkVector(2, CV_32F); + + Mat imagePoint1 = imagePoints1.getMat(i); + if (imagePoint1.empty()) + CV_Error(CV_StsBadSize, "imagePoints1 should not contain empty vector of vectors of points"); + int ni1 = imagePoint1.checkVector(2, CV_32F); if( ni1 <= 0 ) CV_Error(CV_StsUnsupportedFormat, "imagePoints1 should contain vector of vectors of points of type Point2f"); CV_Assert( ni == ni1 );