diff --git a/inference-engine/src/preprocessing/ie_preprocess_gapi.cpp b/inference-engine/src/preprocessing/ie_preprocess_gapi.cpp index 57d4f2699ca63b..a53b14f3e418d0 100644 --- a/inference-engine/src/preprocessing/ie_preprocess_gapi.cpp +++ b/inference-engine/src/preprocessing/ie_preprocess_gapi.cpp @@ -81,7 +81,8 @@ inline int get_cv_depth(const TensorDesc &ie_desc) { case Precision::U8: return CV_8U; case Precision::FP32: return CV_32F; case Precision::U16: return CV_16U; - case Precision::FP16: return CV_16U; + case Precision::I16: return CV_16S; + case Precision::FP16: return CV_16F; default: THROW_IE_EXCEPTION << "Unsupported data type"; } diff --git a/inference-engine/src/preprocessing/ie_preprocess_gapi_kernels.cpp b/inference-engine/src/preprocessing/ie_preprocess_gapi_kernels.cpp index f40d579ffe52d4..99800aa3bcc4af 100644 --- a/inference-engine/src/preprocessing/ie_preprocess_gapi_kernels.cpp +++ b/inference-engine/src/preprocessing/ie_preprocess_gapi_kernels.cpp @@ -434,6 +434,11 @@ void splitRow(const uint8_t* in, std::array& outs, int length) { namespace { +struct fp_16_t { + int16_t v; +}; + + template struct cv_type_to_depth; @@ -443,6 +448,7 @@ template<> struct cv_type_to_depth { enum { depth = CV_16U }; } template<> struct cv_type_to_depth { enum { depth = CV_16S }; }; template<> struct cv_type_to_depth { enum { depth = CV_32S }; }; template<> struct cv_type_to_depth { enum { depth = CV_32F }; }; +template<> struct cv_type_to_depth { enum { depth = CV_16F }; }; template struct typelist {}; @@ -500,7 +506,7 @@ bool is_cv_type_in_list(const int type_id) { namespace { -using merge_supported_types = typelist; +using merge_supported_types = typelist; template struct typed_merge_row { @@ -508,6 +514,12 @@ struct typed_merge_row { template p_f operator()(type_to_type ) { return mergeRow; } + + p_f operator()(type_to_type ) { + static_assert(sizeof(fp_16_t) == sizeof(fp_16_t::v), + "fp_16_t should be a plain wrap over FP16 implementation type"); + return mergeRow; + } }; } // namespace @@ -562,8 +574,7 @@ GAPI_FLUID_KERNEL(FMerge4, Merge4, false) { namespace { - -using split_supported_types = typelist; +using split_supported_types = typelist; template struct typed_split_row { @@ -571,6 +582,12 @@ struct typed_split_row { template p_f operator()(type_to_type ) { return splitRow; } + + p_f operator()(type_to_type ) { + static_assert(sizeof(fp_16_t) == sizeof(fp_16_t::v), + "fp_16_t should be a plain wrap over FP16 implementation type"); + return splitRow; + } }; } // namespace diff --git a/inference-engine/tests_deprecated/fluid_preproc/common/fluid_tests.cpp b/inference-engine/tests_deprecated/fluid_preproc/common/fluid_tests.cpp index f0c60539bbb60e..52e2f303f6abd0 100644 --- a/inference-engine/tests_deprecated/fluid_preproc/common/fluid_tests.cpp +++ b/inference-engine/tests_deprecated/fluid_preproc/common/fluid_tests.cpp @@ -193,7 +193,7 @@ InferenceEngine::Blob::Ptr img2Blob(cv::Mat &img, InferenceEngine::Layout layout const size_t height = img.size().height; const size_t width = img.size().width; - CV_Assert(cv::DataType::depth == img.depth()); + CV_Assert(cv::DataType::depth == img.depth() || (PRC == Precision::FP16 && img.depth() == CV_16F)); SizeVector dims = {1, channels, height, width}; Blob::Ptr resultBlob = make_shared_blob(TensorDesc(PRC, dims, layout));; @@ -237,7 +237,8 @@ void Blob2Img(const InferenceEngine::Blob::Ptr& blobP, cv::Mat& img, InferenceEn const size_t height = img.size().height; const size_t width = img.size().width; - CV_Assert(cv::DataType::depth == img.depth()); + //IE and OpenCV use different data types for FP16 representation, so need to check for it explicitly + CV_Assert(cv::DataType::depth == img.depth() || ((img.depth() == CV_16F) && (PRC == Precision::FP16))); data_t* blobData = blobP->buffer().as(); @@ -438,11 +439,20 @@ TEST_P(SplitTestGAPI, AccuracyTest) cv::Size sz = std::get<2>(params); double tolerance = std::get<3>(params); - int srcType = CV_MAKE_TYPE(depth, planes); + auto make_src_type = [planes](int d){ + return CV_MAKE_TYPE(d, planes); + }; + int srcType = make_src_type(depth); int dstType = CV_MAKE_TYPE(depth, 1); cv::Mat in_mat(sz, srcType); - cv::randn(in_mat, cv::Scalar::all(127), cv::Scalar::all(40.f)); + bool const is_fp16 = (depth == CV_16F); + cv::Mat rnd_mat = is_fp16 ? cv::Mat(sz, make_src_type(CV_32F)) : in_mat; + cv::randn(rnd_mat, cv::Scalar::all(127), cv::Scalar::all(40.f)); + + if (is_fp16) { + rnd_mat.convertTo(in_mat, depth); + } std::vector out_mats_gapi(planes, cv::Mat::zeros(sz, dstType)); std::vector out_mats_ocv (planes, cv::Mat::zeros(sz, dstType)); @@ -520,12 +530,21 @@ TEST_P(MergeTestGAPI, AccuracyTest) cv::Size sz = std::get<2>(params); double tolerance = std::get<3>(params); - int srcType = CV_MAKE_TYPE(depth, 1); + auto make_src_type = [](int d){ + return CV_MAKE_TYPE(d, 1); + }; + int srcType = make_src_type(depth); int dstType = CV_MAKE_TYPE(depth, planes); std::vector in_mats(planes, cv::Mat(sz, srcType)); for (int p = 0; p < planes; p++) { - cv::randn(in_mats[p], cv::Scalar::all(127), cv::Scalar::all(40.f)); + bool const is_fp16 = (depth == CV_16F); + cv::Mat rnd_mat = is_fp16 ? cv::Mat(sz, make_src_type(CV_32F)) : in_mats[p]; + cv::randn(rnd_mat, cv::Scalar::all(127), cv::Scalar::all(40.f)); + + if (is_fp16) { + rnd_mat.convertTo(in_mats[p], depth); + } } cv::Mat out_mat_ocv = cv::Mat::zeros(sz, dstType); @@ -754,7 +773,8 @@ TEST_P(ColorConvertTestIE, AccuracyTest) cv::Scalar mean = cv::Scalar::all(127); cv::Scalar stddev = cv::Scalar::all(40.f); - cv::randn(in_mat1, mean, stddev); + if (depth != CV_16F) + cv::randn(in_mat1, mean, stddev); cv::Mat out_mat(size, out_type); cv::Mat out_mat_ocv(size, out_type); @@ -771,7 +791,7 @@ TEST_P(ColorConvertTestIE, AccuracyTest) size_t out_channels = out_mat.channels(); CV_Assert(3 == out_channels || 4 == out_channels); - CV_Assert(CV_8U == depth || CV_32F == depth); + CV_Assert(CV_8U == depth || CV_32F == depth || depth == CV_16S || depth == CV_16F); ASSERT_TRUE(in_mat1.isContinuous() && out_mat.isContinuous()); @@ -780,8 +800,21 @@ TEST_P(ColorConvertTestIE, AccuracyTest) InferenceEngine::SizeVector in_sv = { 1, in_channels, in_height, in_width }; InferenceEngine::SizeVector out_sv = { 1, out_channels, out_height, out_width }; + auto depth_to_precision = [](int depth) -> Precision::ePrecision { + switch (depth) + { + case CV_8U: return Precision::U8; + case CV_16S: return Precision::I16; + case CV_16F: return Precision::FP16; + case CV_32F: return Precision::FP32; + default: + throw std::logic_error("Unsupported configuration"); + } + return Precision::UNSPECIFIED; + }; + // HWC blob: channels are interleaved - Precision precision = CV_8U == depth ? Precision::U8 : Precision::FP32; + Precision precision = depth_to_precision(depth); Blob::Ptr in_blob, out_blob; switch (precision) @@ -796,6 +829,18 @@ TEST_P(ColorConvertTestIE, AccuracyTest) out_blob = img2Blob(out_mat, out_layout); break; + case Precision::I16: + in_blob = img2Blob(in_mat1, in_layout); + out_blob = img2Blob(out_mat, out_layout); + break; + + case Precision::FP16: + in_blob = img2Blob(in_mat1, in_layout); + out_blob = img2Blob(out_mat, out_layout); + + break; + + default: FAIL() << "Unsupported configuration"; } @@ -813,6 +858,8 @@ TEST_P(ColorConvertTestIE, AccuracyTest) { case Precision::U8: Blob2Img (out_blob, out_mat, out_layout); break; case Precision::FP32: Blob2Img(out_blob, out_mat, out_layout); break; + case Precision::I16: Blob2Img (out_blob, out_mat, out_layout); break; + case Precision::FP16: Blob2Img (out_blob, out_mat, out_layout); break; default: FAIL() << "Unsupported configuration"; } diff --git a/inference-engine/tests_deprecated/fluid_preproc/cpu/fluid_tests_cpu.cpp b/inference-engine/tests_deprecated/fluid_preproc/cpu/fluid_tests_cpu.cpp index ec03ade000ec02..4517d2f4c8b82b 100644 --- a/inference-engine/tests_deprecated/fluid_preproc/cpu/fluid_tests_cpu.cpp +++ b/inference-engine/tests_deprecated/fluid_preproc/cpu/fluid_tests_cpu.cpp @@ -132,7 +132,7 @@ INSTANTIATE_TEST_CASE_P(ResizeTestFluid_F32, ResizeTestGAPI, INSTANTIATE_TEST_CASE_P(SplitTestFluid, SplitTestGAPI, Combine(Values(2, 3, 4), - Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32F, CV_32S), + Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_16F, CV_32F, CV_32S), Values(TEST_SIZES), Values(0))); @@ -144,7 +144,7 @@ INSTANTIATE_TEST_CASE_P(ChanToPlaneTestFluid, ChanToPlaneTestGAPI, INSTANTIATE_TEST_CASE_P(MergeTestFluid, MergeTestGAPI, Combine(Values(2, 3, 4), - Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_32F, CV_32S), + Values(CV_8U, CV_8S, CV_16U, CV_16S, CV_16F, CV_32F, CV_32S), Values(TEST_SIZES), Values(0))); @@ -269,7 +269,7 @@ INSTANTIATE_TEST_CASE_P(ColorConvertYUV420Fluid, ColorConvertYUV420TestIE, Values(0))); INSTANTIATE_TEST_CASE_P(Reorder_HWC2CHW, ColorConvertTestIE, - Combine(Values(CV_8U, CV_32F), + Combine(Values(CV_8U, CV_32F, CV_16S, CV_16F), Values(InferenceEngine::ColorFormat::BGR), Values(InferenceEngine::NHWC), Values(InferenceEngine::NCHW), @@ -277,7 +277,7 @@ INSTANTIATE_TEST_CASE_P(Reorder_HWC2CHW, ColorConvertTestIE, Values(0))); INSTANTIATE_TEST_CASE_P(Reorder_CHW2HWC, ColorConvertTestIE, - Combine(Values(CV_8U, CV_32F), + Combine(Values(CV_8U, CV_32F, CV_16S, CV_16F), Values(InferenceEngine::ColorFormat::BGR), Values(InferenceEngine::NCHW), Values(InferenceEngine::NHWC), diff --git a/inference-engine/thirdparty/fluid/modules/gapi/include/opencv2/gapi/own/cvdefs.hpp b/inference-engine/thirdparty/fluid/modules/gapi/include/opencv2/gapi/own/cvdefs.hpp index 73656ed61cdce4..b9ab89cf767a02 100644 --- a/inference-engine/thirdparty/fluid/modules/gapi/include/opencv2/gapi/own/cvdefs.hpp +++ b/inference-engine/thirdparty/fluid/modules/gapi/include/opencv2/gapi/own/cvdefs.hpp @@ -32,7 +32,8 @@ typedef unsigned short ushort; #define CV_32S 4 #define CV_32F 5 #define CV_64F 6 -#define CV_USRTYPE1 7 +#define CV_16F 7 +#define CV_USRTYPE1 8 #define CV_MAT_DEPTH_MASK (CV_DEPTH_MAX - 1) #define CV_MAT_DEPTH(flags) ((flags) & CV_MAT_DEPTH_MASK) @@ -70,6 +71,13 @@ typedef unsigned short ushort; #define CV_32SC4 CV_MAKETYPE(CV_32S,4) #define CV_32SC(n) CV_MAKETYPE(CV_32S,(n)) + +#define CV_16FC1 CV_MAKETYPE(CV_16F,1) +#define CV_16FC2 CV_MAKETYPE(CV_16F,2) +#define CV_16FC3 CV_MAKETYPE(CV_16F,3) +#define CV_16FC4 CV_MAKETYPE(CV_16F,4) +#define CV_16FC(n) CV_MAKETYPE(CV_16F,(n)) + #define CV_32FC1 CV_MAKETYPE(CV_32F,1) #define CV_32FC2 CV_MAKETYPE(CV_32F,2) #define CV_32FC3 CV_MAKETYPE(CV_32F,3)