1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2019 Intel Corporation
#include "test_precomp.hpp"
#include <stdexcept>
#include <ade/util/iota_range.hpp>
#include "logger.hpp"
#include <opencv2/gapi/plaidml/core.hpp>
#include <opencv2/gapi/plaidml/plaidml.hpp>
namespace opencv_test
{
#ifdef HAVE_PLAIDML
inline cv::gapi::plaidml::config getConfig()
{
auto read_var_from_env = [](const char* env)
{
const char* raw = std::getenv(env);
if (!raw)
{
cv::util::throw_error(std::runtime_error(std::string(env) + " is't set"));
}
return std::string(raw);
};
auto dev_id = read_var_from_env("PLAIDML_DEVICE");
auto trg_id = read_var_from_env("PLAIDML_TARGET");
return cv::gapi::plaidml::config{std::move(dev_id),
std::move(trg_id)};
}
TEST(GAPI_PlaidML_Pipelines, SimpleArithmetic)
{
cv::Size size(1920, 1080);
int type = CV_8UC1;
cv::Mat in_mat1(size, type);
cv::Mat in_mat2(size, type);
// NB: What about overflow ? PlaidML doesn't handle it
cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(127));
cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(127));
cv::Mat out_mat(size, type, cv::Scalar::all(0));
cv::Mat ref_mat(size, type, cv::Scalar::all(0));
////////////////////////////// G-API //////////////////////////////////////
cv::GMat in1, in2;
auto out = in1 + in2;
cv::GComputation comp(cv::GIn(in1, in2), cv::GOut(out));
comp.apply(cv::gin(in_mat1, in_mat2), cv::gout(out_mat),
cv::compile_args(getConfig(),
cv::gapi::use_only{cv::gapi::core::plaidml::kernels()}));
////////////////////////////// OpenCV /////////////////////////////////////
cv::add(in_mat1, in_mat2, ref_mat, cv::noArray(), type);
EXPECT_EQ(0, cv::norm(out_mat, ref_mat));
}
// FIXME PlaidML cpu backend does't support bitwise operations
TEST(GAPI_PlaidML_Pipelines, DISABLED_ComplexArithmetic)
{
cv::Size size(1920, 1080);
int type = CV_8UC1;
cv::Mat in_mat1(size, type);
cv::Mat in_mat2(size, type);
cv::randu(in_mat1, cv::Scalar::all(0), cv::Scalar::all(255));
cv::randu(in_mat2, cv::Scalar::all(0), cv::Scalar::all(255));
cv::Mat out_mat(size, type, cv::Scalar::all(0));
cv::Mat ref_mat(size, type, cv::Scalar::all(0));
////////////////////////////// G-API //////////////////////////////////////
cv::GMat in1, in2;
auto out = in1 | (in2 ^ (in1 & (in2 + (in1 - in2))));
cv::GComputation comp(cv::GIn(in1, in2), cv::GOut(out));
comp.apply(cv::gin(in_mat1, in_mat2), cv::gout(out_mat),
cv::compile_args(getConfig(),
cv::gapi::use_only{cv::gapi::core::plaidml::kernels()}));
////////////////////////////// OpenCV /////////////////////////////////////
cv::subtract(in_mat1, in_mat2, ref_mat, cv::noArray(), type);
cv::add(in_mat2, ref_mat, ref_mat, cv::noArray(), type);
cv::bitwise_and(in_mat1, ref_mat, ref_mat);
cv::bitwise_xor(in_mat2, ref_mat, ref_mat);
cv::bitwise_or(in_mat1, ref_mat, ref_mat);
EXPECT_EQ(0, cv::norm(out_mat, ref_mat));
}
TEST(GAPI_PlaidML_Pipelines, TwoInputOperations)
{
cv::Size size(1920, 1080);
int type = CV_8UC1;
constexpr int kNumInputs = 4;
std::vector<cv::Mat> in_mat(kNumInputs, cv::Mat(size, type));
for (int i = 0; i < kNumInputs; ++i)
{
cv::randu(in_mat[i], cv::Scalar::all(0), cv::Scalar::all(60));
}
cv::Mat out_mat(size, type, cv::Scalar::all(0));
cv::Mat ref_mat(size, type, cv::Scalar::all(0));
////////////////////////////// G-API //////////////////////////////////////
cv::GMat in[4];
auto out = (in[3] - in[0]) + (in[2] - in[1]);
cv::GComputation comp(cv::GIn(in[0], in[1], in[2], in[3]), cv::GOut(out));
// FIXME Doesn't work just apply(in_mat, out_mat, ...)
comp.apply(cv::gin(in_mat[0], in_mat[1], in_mat[2], in_mat[3]), cv::gout(out_mat),
cv::compile_args(getConfig(),
cv::gapi::use_only{cv::gapi::core::plaidml::kernels()}));
////////////////////////////// OpenCV /////////////////////////////////////
cv::subtract(in_mat[3], in_mat[0], ref_mat, cv::noArray(), type);
cv::add(ref_mat, in_mat[2], ref_mat, cv::noArray(), type);
cv::subtract(ref_mat, in_mat[1], ref_mat, cv::noArray(), type);
EXPECT_EQ(0, cv::norm(out_mat, ref_mat));
}
TEST(GAPI_PlaidML_Pipelines, TwoOutputOperations)
{
cv::Size size(1920, 1080);
int type = CV_8UC1;
constexpr int kNumInputs = 4;
std::vector<cv::Mat> in_mat(kNumInputs, cv::Mat(size, type));
for (int i = 0; i < kNumInputs; ++i)
{
cv::randu(in_mat[i], cv::Scalar::all(0), cv::Scalar::all(60));
}
std::vector<cv::Mat> out_mat(kNumInputs, cv::Mat(size, type, cv::Scalar::all(0)));
std::vector<cv::Mat> ref_mat(kNumInputs, cv::Mat(size, type, cv::Scalar::all(0)));
////////////////////////////// G-API //////////////////////////////////////
cv::GMat in[4], out[2];
out[0] = in[0] + in[3];
out[1] = in[1] + in[2];
cv::GComputation comp(cv::GIn(in[0], in[1], in[2], in[3]), cv::GOut(out[0], out[1]));
// FIXME Doesn't work just apply(in_mat, out_mat, ...)
comp.apply(cv::gin(in_mat[0], in_mat[1], in_mat[2], in_mat[3]),
cv::gout(out_mat[0], out_mat[1]),
cv::compile_args(getConfig(),
cv::gapi::use_only{cv::gapi::core::plaidml::kernels()}));
////////////////////////////// OpenCV /////////////////////////////////////
cv::add(in_mat[0], in_mat[3], ref_mat[0], cv::noArray(), type);
cv::add(in_mat[1], in_mat[2], ref_mat[1], cv::noArray(), type);
EXPECT_EQ(0, cv::norm(out_mat[0], ref_mat[0]));
EXPECT_EQ(0, cv::norm(out_mat[1], ref_mat[1]));
}
#else // HAVE_PLAIDML
TEST(GAPI_PlaidML_Pipelines, ThrowIfPlaidMLNotFound)
{
ASSERT_ANY_THROW(cv::gapi::core::plaidml::kernels());
}
#endif // HAVE_PLAIDML
} // namespace opencv_test
|