[PP GAPI] - Generic precision conversion kernel; support for U8 (#2076)
authorAnton Potapov <anton.potapov@intel.com>
Wed, 9 Sep 2020 12:30:08 +0000 (15:30 +0300)
committerGitHub <noreply@github.com>
Wed, 9 Sep 2020 12:30:08 +0000 (15:30 +0300)
- added U8 support
- tests are extended

inference-engine/src/preprocessing/ie_preprocess_gapi_kernels.cpp
inference-engine/src/preprocessing/ie_preprocess_gapi_kernels.hpp
inference-engine/src/preprocessing/ie_preprocess_gapi_kernels_impl.hpp
inference-engine/tests_deprecated/fluid_preproc/cpu/fluid_tests_cpu.cpp

index 654b625..e104dcf 100644 (file)
@@ -2247,25 +2247,27 @@ GAPI_FLUID_KERNEL(FConvertDepth, ConvertDepth, false) {
     static const int Window = 1;
 
     static void run(const cv::gapi::fluid::View& src, int depth, cv::gapi::fluid::Buffer& dst) {
-        GAPI_Assert(src.meta().depth == CV_16U || src.meta().depth == CV_32F);
-        GAPI_Assert(dst.meta().depth == CV_32F || dst.meta().depth == CV_16U);
+        GAPI_Assert(src.meta().depth == CV_8U || src.meta().depth == CV_32F || src.meta().depth == CV_16U);
+        GAPI_Assert(dst.meta().depth == CV_8U || dst.meta().depth == CV_32F || dst.meta().depth == CV_16U);
         GAPI_Assert(src.meta().chan == 1);
         GAPI_Assert(dst.meta().chan == 1);
         GAPI_Assert(src.length() == dst.length());
 
-        constexpr unsigned supported_types_n = 2;
+        constexpr unsigned supported_types_n = 3;
         using p_f = void (*)( const uint8_t* src,  uint8_t* dst, const int width);
         using table_string_t = std::array<p_f, supported_types_n>;
 
         constexpr std::array<table_string_t, supported_types_n> func_table = {
-                table_string_t{convert_precision<uint16_t, uint16_t>, convert_precision<uint16_t, float>},
-                table_string_t{convert_precision<float,    uint16_t>, convert_precision<float,    float>}
+                table_string_t{convert_precision<uint16_t, uint16_t>, convert_precision<uint16_t, float>, convert_precision<uint16_t, uint8_t>},
+                table_string_t{convert_precision<float,    uint16_t>, convert_precision<float,    float>, convert_precision<float,    uint8_t>},
+                table_string_t{convert_precision<uint8_t,  uint16_t>, convert_precision<uint8_t,  float>, convert_precision<uint8_t,  uint8_t>}
         };
 
         auto depth_to_index = [](int depth){
             switch (depth) {
                 case  CV_16U: return 0;
                 case  CV_32F: return 1;
+                case  CV_8U:  return 2;
                 default: GAPI_Assert(!"not supported depth"); return -1;
             }
         };
index 7b6acdb..685eae4 100644 (file)
@@ -145,8 +145,8 @@ namespace gapi {
 
     G_TYPED_KERNEL(ConvertDepth, <cv::GMat(cv::GMat, int depth)>, "com.intel.ie.ConvertDepth") {
         static cv::GMatDesc outMeta(const cv::GMatDesc& in, int depth) {
-            GAPI_Assert(in.depth == CV_16U || in.depth == CV_32F);
-            GAPI_Assert(depth == CV_32F || depth == CV_16U);
+            GAPI_Assert(in.depth == CV_8U || in.depth == CV_16U || in.depth == CV_32F);
+            GAPI_Assert(depth == CV_8U || depth == CV_32F || depth == CV_16U);
 
             return in.withDepth(depth);
         }
index 6d5161f..bce88e1 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <climits>
 #include <cstdint>
+#include <limits>
 
 #if defined(__GNUC__) && (__GNUC__ <= 5)
 #include <cmath>
@@ -38,12 +39,20 @@ template<> inline short saturate_cast(int x) { return (std::min)(SHRT_MAX, (std:
 template<> inline short saturate_cast(float x) { return saturate_cast<short>(static_cast<int>(std::rint(x))); }
 template<> inline float saturate_cast(float x) { return x; }
 template<> inline short saturate_cast(short x) { return x; }
+
 template<> inline uint16_t saturate_cast(uint16_t x) { return x; }
 template<> inline float    saturate_cast(uint16_t x) { return x; }
+
 template<> inline uint16_t saturate_cast(int x) { return (std::min)(USHRT_MAX, (std::max)(0, x)); }
 template<> inline uint16_t saturate_cast(float x)    { return saturate_cast<uint16_t>(static_cast<int>(std::rint(x))); }
-template<> inline uchar saturate_cast<uchar>(int v) { return (uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); }
+template<> inline uchar saturate_cast<uchar>(int v)  { return (uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); }
+
+template<> inline uint16_t saturate_cast(uint8_t x) { return x; }
+template<> inline float    saturate_cast(uint8_t x) { return x; }
+template<> inline uint8_t  saturate_cast(uint8_t x) { return x; }
 
+template<> inline uint8_t saturate_cast(uint16_t x) { using lim = std::numeric_limits<uint8_t>; return std::min(static_cast<uint16_t>(lim::max()), std::max(static_cast<uint16_t>(lim::min()), x));}
+template<> inline uint8_t saturate_cast(float x)    { return saturate_cast<uint8_t>(static_cast<int>(std::rint(x))); }
 //------------------------------------------------------------------------------
 
 constexpr static const int ONE = 1 << 15;
index 954bff9..1e21638 100644 (file)
@@ -171,8 +171,8 @@ INSTANTIATE_TEST_CASE_P(I420toRGBTestFluid, I420toRGBTestGAPI,
                                 Values(0)));
 
 INSTANTIATE_TEST_CASE_P(ConvertDepthFluid, ConvertDepthTestGAPI,
-                        Combine(Values(CV_16U, CV_32F),
-                                Values(CV_32F, CV_16U),
+                        Combine(Values(CV_16U, CV_32F, CV_8U),
+                                Values(CV_32F, CV_16U, CV_8U),
                                 Values(cv::Size(3840, 2160),
                                        cv::Size(1920, 1080),
                                        cv::Size(1280,  720),