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.

solarize_op_test.cc 6.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * Copyright 2019 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 "common/cvop_common.h"
  18. #include "minddata/dataset/kernels/image/solarize_op.h"
  19. #include "minddata/dataset/core/cv_tensor.h"
  20. #include "minddata/dataset/util/status.h"
  21. #include "utils/log_adapter.h"
  22. #include "gtest/gtest.h"
  23. using namespace mindspore::dataset;
  24. using mindspore::LogStream;
  25. using mindspore::ExceptionType::NoExceptionType;
  26. using mindspore::MsLogLevel::INFO;
  27. class MindDataTestSolarizeOp : public UT::CVOP::CVOpCommon {
  28. protected:
  29. MindDataTestSolarizeOp() : CVOpCommon() {}
  30. std::shared_ptr<Tensor> output_tensor_;
  31. };
  32. TEST_F(MindDataTestSolarizeOp, TestOp1) {
  33. MS_LOG(INFO) << "Doing testSolarizeOp1.";
  34. std::unique_ptr<SolarizeOp> op(new SolarizeOp());
  35. EXPECT_TRUE(op->OneToOne());
  36. Status s = op->Compute(input_tensor_, &output_tensor_);
  37. EXPECT_TRUE(s.IsOk());
  38. }
  39. TEST_F(MindDataTestSolarizeOp, TestOp2) {
  40. MS_LOG(INFO) << "Doing testSolarizeOp2 - test default values";
  41. // unsigned int threshold = 128;
  42. std::unique_ptr<SolarizeOp> op(new SolarizeOp());
  43. std::vector<uint8_t> test_vector = {3, 4, 59, 210, 255};
  44. std::vector<uint8_t> expected_output_vector = {252, 251, 196, 45, 0};
  45. std::shared_ptr<Tensor> test_input_tensor;
  46. std::shared_ptr<Tensor> expected_output_tensor;
  47. Tensor::CreateFromVector(test_vector, TensorShape({1, (long int)test_vector.size(), 1}), &test_input_tensor);
  48. Tensor::CreateFromVector(expected_output_vector, TensorShape({1, (long int)test_vector.size(), 1}),
  49. &expected_output_tensor);
  50. std::shared_ptr<Tensor> test_output_tensor;
  51. Status s = op->Compute(test_input_tensor, &test_output_tensor);
  52. EXPECT_TRUE(s.IsOk());
  53. ASSERT_TRUE(test_output_tensor->shape() == expected_output_tensor->shape());
  54. ASSERT_TRUE(test_output_tensor->type() == expected_output_tensor->type());
  55. MS_LOG(DEBUG) << *test_output_tensor << std::endl;
  56. MS_LOG(DEBUG) << *expected_output_tensor << std::endl;
  57. ASSERT_TRUE(*test_output_tensor == *expected_output_tensor);
  58. }
  59. TEST_F(MindDataTestSolarizeOp, TestOp3) {
  60. MS_LOG(INFO) << "Doing testSolarizeOp3 - Pass in only threshold_min parameter";
  61. // unsigned int threshold = 128;
  62. std::vector<uint8_t> threshold ={1, 255};
  63. std::unique_ptr<SolarizeOp> op(new SolarizeOp(threshold));
  64. std::vector<uint8_t> test_vector = {3, 4, 59, 210, 255};
  65. std::vector<uint8_t> expected_output_vector = {252, 251, 196, 45, 0};
  66. std::shared_ptr<Tensor> test_input_tensor;
  67. std::shared_ptr<Tensor> expected_output_tensor;
  68. Tensor::CreateFromVector(test_vector, TensorShape({1, (long int)test_vector.size(), 1}), &test_input_tensor);
  69. Tensor::CreateFromVector(expected_output_vector, TensorShape({1, (long int)test_vector.size(), 1}),
  70. &expected_output_tensor);
  71. std::shared_ptr<Tensor> test_output_tensor;
  72. Status s = op->Compute(test_input_tensor, &test_output_tensor);
  73. EXPECT_TRUE(s.IsOk());
  74. ASSERT_TRUE(test_output_tensor->shape() == expected_output_tensor->shape());
  75. ASSERT_TRUE(test_output_tensor->type() == expected_output_tensor->type());
  76. MS_LOG(DEBUG) << *test_output_tensor << std::endl;
  77. MS_LOG(DEBUG) << *expected_output_tensor << std::endl;
  78. ASSERT_TRUE(*test_output_tensor == *expected_output_tensor);
  79. }
  80. TEST_F(MindDataTestSolarizeOp, TestOp4) {
  81. MS_LOG(INFO) << "Doing testSolarizeOp4 - Pass in both threshold parameters.";
  82. std::vector<uint8_t> threshold ={1, 230};
  83. std::unique_ptr<SolarizeOp> op(new SolarizeOp(threshold));
  84. std::vector<uint8_t> test_vector = {3, 4, 59, 210, 255};
  85. std::vector<uint8_t> expected_output_vector = {252, 251, 196, 45, 255};
  86. std::shared_ptr<Tensor> test_input_tensor;
  87. std::shared_ptr<Tensor> expected_output_tensor;
  88. Tensor::CreateFromVector(test_vector, TensorShape({1, (long int)test_vector.size(), 1}), &test_input_tensor);
  89. Tensor::CreateFromVector(expected_output_vector, TensorShape({1, (long int)test_vector.size(), 1}),
  90. &expected_output_tensor);
  91. std::shared_ptr<Tensor> test_output_tensor;
  92. Status s = op->Compute(test_input_tensor, &test_output_tensor);
  93. EXPECT_TRUE(s.IsOk());
  94. ASSERT_TRUE(test_output_tensor->shape() == expected_output_tensor->shape());
  95. ASSERT_TRUE(test_output_tensor->type() == expected_output_tensor->type());
  96. MS_LOG(DEBUG) << *test_output_tensor << std::endl;
  97. MS_LOG(DEBUG) << *expected_output_tensor << std::endl;
  98. ASSERT_TRUE(*test_output_tensor == *expected_output_tensor);
  99. }
  100. TEST_F(MindDataTestSolarizeOp, TestOp5) {
  101. MS_LOG(INFO) << "Doing testSolarizeOp5 - Rank 2 input tensor.";
  102. std::vector<uint8_t> threshold ={1, 230};
  103. std::unique_ptr<SolarizeOp> op(new SolarizeOp(threshold));
  104. std::vector<uint8_t> test_vector = {3, 4, 59, 210, 255};
  105. std::vector<uint8_t> expected_output_vector = {252, 251, 196, 45, 255};
  106. std::shared_ptr<Tensor> test_input_tensor;
  107. std::shared_ptr<Tensor> expected_output_tensor;
  108. Tensor::CreateFromVector(test_vector, TensorShape({1, (long int)test_vector.size()}), &test_input_tensor);
  109. Tensor::CreateFromVector(expected_output_vector, TensorShape({1, (long int)test_vector.size()}),
  110. &expected_output_tensor);
  111. std::shared_ptr<Tensor> test_output_tensor;
  112. Status s = op->Compute(test_input_tensor, &test_output_tensor);
  113. EXPECT_TRUE(s.IsOk());
  114. ASSERT_TRUE(test_output_tensor->shape() == expected_output_tensor->shape());
  115. ASSERT_TRUE(test_output_tensor->type() == expected_output_tensor->type());
  116. MS_LOG(DEBUG) << *test_output_tensor << std::endl;
  117. MS_LOG(DEBUG) << *expected_output_tensor << std::endl;
  118. ASSERT_TRUE(*test_output_tensor == *expected_output_tensor);
  119. }
  120. TEST_F(MindDataTestSolarizeOp, TestOp6) {
  121. MS_LOG(INFO) << "Doing testSolarizeOp6 - Bad Input.";
  122. std::vector<uint8_t> threshold ={10, 1};
  123. std::unique_ptr<SolarizeOp> op(new SolarizeOp(threshold));
  124. std::vector<uint8_t> test_vector = {3, 4, 59, 210, 255};
  125. std::shared_ptr<Tensor> test_input_tensor;
  126. std::shared_ptr<Tensor> test_output_tensor;
  127. Tensor::CreateFromVector(test_vector, TensorShape({1, (long int)test_vector.size(), 1}), &test_input_tensor);
  128. Status s = op->Compute(test_input_tensor, &test_output_tensor);
  129. EXPECT_TRUE(s.IsError());
  130. EXPECT_NE(s.ToString().find("threshold_min must be smaller or equal to threshold_max."), std::string::npos);
  131. ASSERT_TRUE(s.get_code() == StatusCode::kUnexpectedError);
  132. }