You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

pad_end_op_test.cc 5.0 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "common/common.h"
  17. #include "dataset/kernels/data/pad_end_op.h"
  18. #include "utils/log_adapter.h"
  19. using namespace mindspore::dataset;
  20. using mindspore::LogStream;
  21. using mindspore::ExceptionType::NoExceptionType;
  22. using mindspore::MsLogLevel::INFO;
  23. class MindDataTestPadEndOp : public UT::Common {
  24. protected:
  25. MindDataTestPadEndOp() {}
  26. };
  27. TEST_F(MindDataTestPadEndOp, TestOp) {
  28. MS_LOG(INFO) << "Doing MindDataTestPadEndOp.";
  29. // first set of testunits for numeric values
  30. TensorShape pad_data_shape({1});
  31. // prepare input tensor
  32. float_t orig1[4] = {1, 1, 1, 1};
  33. TensorShape input_shape1({2, 2});
  34. std::vector<TensorShape> input_shape1_vector = {input_shape1};
  35. std::shared_ptr<Tensor> input1 =
  36. std::make_shared<Tensor>(input_shape1, DataType(DataType::DE_FLOAT32), reinterpret_cast<unsigned char *>(orig1));
  37. // pad_shape
  38. TensorShape pad_shape1[3] = {TensorShape({3, 3}), TensorShape({2, 4}), TensorShape({4, 2})};
  39. // value to pad
  40. float_t pad_data1[3][1] = {0, 3.5, 3.5};
  41. std::shared_ptr<Tensor> expected1[3];
  42. // expected tensor output for testunit 1
  43. float_t out1[9] = {1, 1, 0, 1, 1, 0, 0, 0, 0};
  44. expected1[0] =
  45. std::make_shared<Tensor>(pad_shape1[0], DataType(DataType::DE_FLOAT32), reinterpret_cast<unsigned char *>(out1));
  46. // expected tensor output for testunit 2
  47. float_t out2[8] = {1, 1, 3.5, 3.5, 1, 1, 3.5, 3.5};
  48. expected1[1] =
  49. std::make_shared<Tensor>(pad_shape1[1], DataType(DataType::DE_FLOAT32), reinterpret_cast<unsigned char *>(out2));
  50. // expected tensor output for testunit 3
  51. float_t out3[8] = {1, 1, 1, 1, 3.5, 3.5, 3.5, 3.5};
  52. expected1[2] =
  53. std::make_shared<Tensor>(pad_shape1[2], DataType(DataType::DE_FLOAT32), reinterpret_cast<unsigned char *>(out3));
  54. // run the PadEndOp
  55. for (auto i = 0; i < 3; i++) {
  56. std::shared_ptr<Tensor> output;
  57. std::vector<TensorShape> output_shape = {TensorShape({})};
  58. std::shared_ptr<Tensor> pad_value1 = std::make_shared<Tensor>(pad_data_shape, DataType(DataType::DE_FLOAT32),
  59. reinterpret_cast<unsigned char *>(pad_data1[i]));
  60. std::unique_ptr<PadEndOp> op(new PadEndOp(pad_shape1[i], pad_value1));
  61. Status s = op->Compute(input1, &output);
  62. EXPECT_TRUE(s.IsOk());
  63. ASSERT_TRUE(output->shape() == expected1[i]->shape());
  64. ASSERT_TRUE(output->type() == expected1[i]->type());
  65. MS_LOG(DEBUG) << *output << std::endl;
  66. MS_LOG(DEBUG) << *expected1[i] << std::endl;
  67. ASSERT_TRUE(*output == *expected1[i]);
  68. s = op->OutputShape(input_shape1_vector, output_shape);
  69. EXPECT_TRUE(s.IsOk());
  70. ASSERT_TRUE(output_shape.size() == 1);
  71. ASSERT_TRUE(output->shape() == output_shape[0]);
  72. }
  73. // second set of testunits for string
  74. // input tensor
  75. std::vector<std::string> orig2 = {"this", "is"};
  76. TensorShape input_shape2({2});
  77. std::vector<TensorShape> input_shape2_vector = {input_shape2};
  78. std::shared_ptr<Tensor> input2;
  79. Tensor::CreateTensor(&input2, orig2, input_shape2);
  80. // pad_shape
  81. TensorShape pad_shape2[3] = {TensorShape({5}), TensorShape({2}), TensorShape({10})};
  82. // pad value
  83. std::vector<std::string> pad_data2[3] = {{""}, {"P"}, {" "}};
  84. std::shared_ptr<Tensor> pad_value2[3];
  85. // expected output for 3 testunits
  86. std::shared_ptr<Tensor> expected2[3];
  87. std::vector<std::string> outstring[3] = {
  88. {"this", "is", "", "", ""}, {"this", "is"}, {"this", "is", " ", " ", " ", " ", " ", " ", " ", " "}};
  89. for (auto i = 0; i < 3; i++) {
  90. // pad value
  91. Tensor::CreateTensor(&pad_value2[i], pad_data2[i], pad_data_shape);
  92. std::shared_ptr<Tensor> output;
  93. std::vector<TensorShape> output_shape = {TensorShape({})};
  94. std::unique_ptr<PadEndOp> op(new PadEndOp(pad_shape2[i], pad_value2[i]));
  95. Status s = op->Compute(input2, &output);
  96. Tensor::CreateTensor(&expected2[i], outstring[i], pad_shape2[i]);
  97. EXPECT_TRUE(s.IsOk());
  98. ASSERT_TRUE(output->shape() == expected2[i]->shape());
  99. ASSERT_TRUE(output->type() == expected2[i]->type());
  100. MS_LOG(DEBUG) << *output << std::endl;
  101. MS_LOG(DEBUG) << *expected2[i] << std::endl;
  102. ASSERT_TRUE(*output == *expected2[i]);
  103. s = op->OutputShape(input_shape2_vector, output_shape);
  104. EXPECT_TRUE(s.IsOk());
  105. ASSERT_TRUE(output_shape.size() == 1);
  106. ASSERT_TRUE(output->shape() == output_shape[0]);
  107. }
  108. MS_LOG(INFO) << "MindDataTestPadEndOp end.";
  109. }