From 12c2e1263b177c908e0054807cd1e94049426602 Mon Sep 17 00:00:00 2001 From: Wiktor Adamski Date: Tue, 19 Feb 2019 13:03:54 +0100 Subject: [PATCH] Refactored tests. --- mace/ops/pad_test.cc | 184 +++++++++++++++++++++---------------------- 1 file changed, 91 insertions(+), 93 deletions(-) diff --git a/mace/ops/pad_test.cc b/mace/ops/pad_test.cc index f0b1308b..63bb449f 100644 --- a/mace/ops/pad_test.cc +++ b/mace/ops/pad_test.cc @@ -27,7 +27,7 @@ class PadTest : public OpsTestBase {}; namespace { template -void Simple() { +void SimpleConstant() { // Construct graph OpsTestNet net; @@ -72,11 +72,99 @@ void Simple() { }); ExpectTensorNear(*expected, *output, 1e-5); } + +template +void Result(const std::vector &input_shape, + const std::vector &input_data, + const std::vector &expected_shape, + const std::vector &expected_data, + const std::vector &paddings, + const PadType pad_type) { + // Construct graph + OpsTestNet net; + std::string input("Input"); + std::string t_input(input); + std::string output("Output"); + std::string t_output(output); + + // Add input data + net.AddInputFromArray(input, input_shape, input_data); + + if (D == DeviceType::CPU) { + t_input = "TInput"; + t_output = "TOutput"; + net.TransformDataFormat(input, NHWC, t_input, NCHW); + } + + OpDefBuilder("Pad", "PadTest") + .Input(t_input) + .Output(t_output) + .AddIntsArg("paddings", paddings) + .AddIntArg("pad_type", static_cast(pad_type)) + .AddIntArg("data_format", DataFormat::NHWC) + .Finalize(net.NewOperatorDef()); + + // Run + net.RunOp(D); + + if (D == DeviceType::CPU) { + net.TransformDataFormat(t_output, NCHW, output, NHWC); + } + + auto actual = net.GetTensor(output.c_str()); + auto expected = net.CreateTensor(expected_shape, expected_data); + + ExpectTensorNear(*expected, *actual, 1e-5); +} + +void SimpleMirror(const std::vector &expected_data, + const PadType pad_type) { + std::vector input_shape{1, 3, 4, 1}; + int size = std::accumulate(input_shape.begin(), input_shape.end(), + 1, std::multiplies()); + std::vector input_data; + std::vector expected_shape{1, 6, 7, 1}; + const std::vector paddings{0, 0, 1, 2, 3, 0, 0, 0}; + + input_data.reserve(size); + for (int i = 1; i <= size; i++) { + input_data.push_back(i); + } + + Result(input_shape, input_data, expected_shape, + expected_data, paddings, pad_type); + Result(input_shape, input_data, expected_shape, + expected_data, paddings, pad_type); + Result(input_shape, input_data, expected_shape, + expected_data, paddings, pad_type); +} } // namespace -TEST_F(PadTest, SimpleCPU) { Simple(); } +TEST_F(PadTest, SimpleConstantCPU) { SimpleConstant(); } + +TEST_F(PadTest, SimpleConstantGPU) { SimpleConstant(); } -TEST_F(PadTest, SimpleGPU) { Simple(); } +TEST_F(PadTest, SimpleReflect) { + SimpleMirror({ + 8, 7, 6, 5, 6, 7, 8, + 4, 3, 2, 1, 2, 3, 4, + 8, 7, 6, 5, 6, 7, 8, + 12, 11, 10, 9, 10, 11, 12, + 8, 7, 6, 5, 6, 7, 8, + 4, 3, 2, 1, 2, 3, 4, + }, PadType::REFLECT); +} + +TEST_F(PadTest, SimpleSymmetric) { + SimpleMirror({ + 3, 2, 1, 1, 2, 3, 4, + 3, 2, 1, 1, 2, 3, 4, + 7, 6, 5, 5, 6, 7, 8, + 11, 10, 9, 9, 10, 11, 12, + 11, 10, 9, 9, 10, 11, 12, + 7, 6, 5, 5, 6, 7, 8, + }, PadType::SYMMETRIC); +} TEST_F(PadTest, ComplexCPU) { // Construct graph @@ -178,52 +266,6 @@ TEST_F(PadTest, ComplexHalf) { } } -namespace { -template -void Result(const std::vector &input_shape, - const std::vector &input_data, - const std::vector &expected_shape, - const std::vector &expected_data, - const std::vector &paddings, - const PadType pad_type) { - // Construct graph - OpsTestNet net; - std::string input("Input"); - std::string t_input(input); - std::string output("Output"); - std::string t_output(output); - - // Add input data - net.AddInputFromArray(input, input_shape, input_data); - - if (D == DeviceType::CPU) { - t_input = "TInput"; - t_output = "TOutput"; - net.TransformDataFormat(input, NHWC, t_input, NCHW); - } - - OpDefBuilder("Pad", "PadTest") - .Input(t_input) - .Output(t_output) - .AddIntsArg("paddings", paddings) - .AddIntArg("pad_type", static_cast(pad_type)) - .AddIntArg("data_format", DataFormat::NHWC) - .Finalize(net.NewOperatorDef()); - - // Run - net.RunOp(D); - - if (D == DeviceType::CPU) { - net.TransformDataFormat(t_output, NCHW, output, NHWC); - } - - auto actual = net.GetTensor(output.c_str()); - auto expected = net.CreateTensor(expected_shape, expected_data); - - ExpectTensorNear(*expected, *actual, 1e-5); -} -} // namespace - TEST_F(PadTest, ReflectCPU) { std::vector input_shape{2, 2, 2, 2}; int size = std::accumulate(input_shape.begin(), input_shape.end(), @@ -426,50 +468,6 @@ TEST_F(PadTest, SymmetricCPU) { expected_data, paddings, PadType::SYMMETRIC); } -TEST_F(PadTest, Result) { - std::vector input_shape{1, 3, 4, 1}; - int size = std::accumulate(input_shape.begin(), input_shape.end(), - 1, std::multiplies()); - std::vector input_data; - std::vector expected_shape{1, 6, 7, 1}; - std::vector expected_reflect{ - 8, 7, 6, 5, 6, 7, 8, - 4, 3, 2, 1, 2, 3, 4, - 8, 7, 6, 5, 6, 7, 8, - 12, 11, 10, 9, 10, 11, 12, - 8, 7, 6, 5, 6, 7, 8, - 4, 3, 2, 1, 2, 3, 4, - }; - std::vector expected_symmetric{ - 3, 2, 1, 1, 2, 3, 4, - 3, 2, 1, 1, 2, 3, 4, - 7, 6, 5, 5, 6, 7, 8, - 11, 10, 9, 9, 10, 11, 12, - 11, 10, 9, 9, 10, 11, 12, - 7, 6, 5, 5, 6, 7, 8, - }; - const std::vector paddings{0, 0, 1, 2, 3, 0, 0, 0}; - - input_data.reserve(size); - for (int i = 1; i <= size; i++) { - input_data.push_back(i); - } - - Result(input_shape, input_data, expected_shape, - expected_reflect, paddings, PadType::REFLECT); - Result(input_shape, input_data, expected_shape, - expected_reflect, paddings, PadType::REFLECT); - Result(input_shape, input_data, expected_shape, - expected_reflect, paddings, PadType::REFLECT); - - Result(input_shape, input_data, expected_shape, - expected_symmetric, paddings, PadType::SYMMETRIC); - Result(input_shape, input_data, expected_shape, - expected_symmetric, paddings, PadType::SYMMETRIC); - Result(input_shape, input_data, expected_shape, - expected_symmetric, paddings, PadType::SYMMETRIC); -} - } // namespace test } // namespace ops } // namespace mace -- GitLab