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 4.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 "minddata/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. std::vector<float> orig1 = {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. Tensor::CreateFromVector(orig1, input_shape1, &input1);
  37. // pad_shape
  38. TensorShape pad_shape1[3] = {TensorShape({3, 3}), TensorShape({2, 4}), TensorShape({4, 2})};
  39. // value to pad
  40. std::vector<std::vector<float>> pad_data1 = {{0}, {3.5}, {3.5}};
  41. std::shared_ptr<Tensor> expected1[3];
  42. // expected tensor output for testunit 1
  43. std::vector<float> out1 = {1, 1, 0, 1, 1, 0, 0, 0, 0};
  44. Tensor::CreateFromVector(out1, pad_shape1[0], &(expected1[0]));
  45. // expected tensor output for testunit 2
  46. std::vector<float> out2 = {1, 1, 3.5, 3.5, 1, 1, 3.5, 3.5};
  47. Tensor::CreateFromVector(out2, pad_shape1[1], &(expected1[1]));
  48. // expected tensor output for testunit 3
  49. std::vector<float> out3 = {1, 1, 1, 1, 3.5, 3.5, 3.5, 3.5};
  50. Tensor::CreateFromVector(out3, pad_shape1[2], &(expected1[2]));
  51. // run the PadEndOp
  52. for (auto i = 0; i < 3; i++) {
  53. std::shared_ptr<Tensor> output;
  54. std::vector<TensorShape> output_shape = {TensorShape({})};
  55. std::shared_ptr<Tensor> pad_value1;
  56. Tensor::CreateFromVector(pad_data1[i], pad_data_shape, &pad_value1);
  57. std::unique_ptr<PadEndOp> op(new PadEndOp(pad_shape1[i], pad_value1));
  58. Status s = op->Compute(input1, &output);
  59. EXPECT_TRUE(s.IsOk());
  60. ASSERT_TRUE(output->shape() == expected1[i]->shape());
  61. ASSERT_TRUE(output->type() == expected1[i]->type());
  62. MS_LOG(DEBUG) << *output << std::endl;
  63. MS_LOG(DEBUG) << *expected1[i] << std::endl;
  64. ASSERT_TRUE(*output == *expected1[i]);
  65. s = op->OutputShape(input_shape1_vector, output_shape);
  66. EXPECT_TRUE(s.IsOk());
  67. ASSERT_TRUE(output_shape.size() == 1);
  68. ASSERT_TRUE(output->shape() == output_shape[0]);
  69. }
  70. // second set of testunits for string
  71. // input tensor
  72. std::vector<std::string> orig2 = {"this", "is"};
  73. TensorShape input_shape2({2});
  74. std::vector<TensorShape> input_shape2_vector = {input_shape2};
  75. std::shared_ptr<Tensor> input2;
  76. Tensor::CreateFromVector(orig2, input_shape2, &input2);
  77. // pad_shape
  78. TensorShape pad_shape2[3] = {TensorShape({5}), TensorShape({2}), TensorShape({10})};
  79. // pad value
  80. std::vector<std::string> pad_data2[3] = {{""}, {"P"}, {" "}};
  81. std::shared_ptr<Tensor> pad_value2[3];
  82. // expected output for 3 testunits
  83. std::shared_ptr<Tensor> expected2[3];
  84. std::vector<std::string> outstring[3] = {
  85. {"this", "is", "", "", ""}, {"this", "is"}, {"this", "is", " ", " ", " ", " ", " ", " ", " ", " "}};
  86. for (auto i = 0; i < 3; i++) {
  87. // pad value
  88. Tensor::CreateFromVector(pad_data2[i], pad_data_shape, &pad_value2[i]);
  89. std::shared_ptr<Tensor> output;
  90. std::vector<TensorShape> output_shape = {TensorShape({})};
  91. std::unique_ptr<PadEndOp> op(new PadEndOp(pad_shape2[i], pad_value2[i]));
  92. Status s = op->Compute(input2, &output);
  93. Tensor::CreateFromVector(outstring[i], pad_shape2[i], &expected2[i]);
  94. EXPECT_TRUE(s.IsOk());
  95. ASSERT_TRUE(output->shape() == expected2[i]->shape());
  96. ASSERT_TRUE(output->type() == expected2[i]->type());
  97. MS_LOG(DEBUG) << *output << std::endl;
  98. MS_LOG(DEBUG) << *expected2[i] << std::endl;
  99. ASSERT_TRUE(*output == *expected2[i]);
  100. s = op->OutputShape(input_shape2_vector, output_shape);
  101. EXPECT_TRUE(s.IsOk());
  102. ASSERT_TRUE(output_shape.size() == 1);
  103. ASSERT_TRUE(output->shape() == output_shape[0]);
  104. }
  105. MS_LOG(INFO) << "MindDataTestPadEndOp end.";
  106. }