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 24 using namespace mindspore::dataset; 25 using mindspore::LogStream; 26 using mindspore::ExceptionType::NoExceptionType; 27 using mindspore::MsLogLevel::INFO; 28 29 class MindDataTestSolarizeOp : public UT::CVOP::CVOpCommon { 30 protected: 31 MindDataTestSolarizeOp() : CVOpCommon() {} 32 33 std::shared_ptr<Tensor> output_tensor_; 34 }; 35 36 TEST_F(MindDataTestSolarizeOp, TestOp1) { 37 MS_LOG(INFO) << "Doing testSolarizeOp1."; 38 39 std::unique_ptr<SolarizeOp> op(new SolarizeOp()); 40 EXPECT_TRUE(op->OneToOne()); 41 Status s = op->Compute(input_tensor_, &output_tensor_); 42 43 EXPECT_TRUE(s.IsOk()); 44 } 45 46 TEST_F(MindDataTestSolarizeOp, TestOp2) { 47 MS_LOG(INFO) << "Doing testSolarizeOp2 - test default values"; 48 49 // unsigned int threshold = 128; 50 std::unique_ptr<SolarizeOp> op(new SolarizeOp()); 51 52 std::vector<uint8_t> test_vector = {3, 4, 59, 210, 255}; 53 std::vector<uint8_t> expected_output_vector = {252, 251, 196, 45, 0}; 54 std::shared_ptr<Tensor> test_input_tensor; 55 std::shared_ptr<Tensor> expected_output_tensor; 56 Tensor::CreateFromVector(test_vector, TensorShape({1, (long int)test_vector.size(), 1}), &test_input_tensor); 57 Tensor::CreateFromVector(expected_output_vector, TensorShape({1, (long int)test_vector.size(), 1}), 58 &expected_output_tensor); 59 60 std::shared_ptr<Tensor> test_output_tensor; 61 Status s = op->Compute(test_input_tensor, &test_output_tensor); 62 63 EXPECT_TRUE(s.IsOk()); 64 65 ASSERT_TRUE(test_output_tensor->shape() == expected_output_tensor->shape()); 66 ASSERT_TRUE(test_output_tensor->type() == expected_output_tensor->type()); 67 MS_LOG(DEBUG) << *test_output_tensor << std::endl; 68 MS_LOG(DEBUG) << *expected_output_tensor << std::endl; 69 70 ASSERT_TRUE(*test_output_tensor == *expected_output_tensor); 71 } 72 73 TEST_F(MindDataTestSolarizeOp, TestOp3) { 74 MS_LOG(INFO) << "Doing testSolarizeOp3 - Pass in only threshold_min parameter"; 75 76 // unsigned int threshold = 128; 77 std::vector<uint8_t> threshold ={1, 255}; 78 std::unique_ptr<SolarizeOp> op(new SolarizeOp(threshold)); 79 80 std::vector<uint8_t> test_vector = {3, 4, 59, 210, 255}; 81 std::vector<uint8_t> expected_output_vector = {252, 251, 196, 45, 0}; 82 std::shared_ptr<Tensor> test_input_tensor; 83 std::shared_ptr<Tensor> expected_output_tensor; 84 Tensor::CreateFromVector(test_vector, TensorShape({1, (long int)test_vector.size(), 1}), &test_input_tensor); 85 Tensor::CreateFromVector(expected_output_vector, TensorShape({1, (long int)test_vector.size(), 1}), 86 &expected_output_tensor); 87 88 std::shared_ptr<Tensor> test_output_tensor; 89 Status s = op->Compute(test_input_tensor, &test_output_tensor); 90 91 EXPECT_TRUE(s.IsOk()); 92 ASSERT_TRUE(test_output_tensor->shape() == expected_output_tensor->shape()); 93 ASSERT_TRUE(test_output_tensor->type() == expected_output_tensor->type()); 94 MS_LOG(DEBUG) << *test_output_tensor << std::endl; 95 MS_LOG(DEBUG) << *expected_output_tensor << std::endl; 96 ASSERT_TRUE(*test_output_tensor == *expected_output_tensor); 97 } 98 99 TEST_F(MindDataTestSolarizeOp, TestOp4) { 100 MS_LOG(INFO) << "Doing testSolarizeOp4 - Pass in both threshold parameters."; 101 102 std::vector<uint8_t> threshold ={1, 230}; 103 std::unique_ptr<SolarizeOp> op(new SolarizeOp(threshold)); 104 105 std::vector<uint8_t> test_vector = {3, 4, 59, 210, 255}; 106 std::vector<uint8_t> expected_output_vector = {252, 251, 196, 45, 255}; 107 std::shared_ptr<Tensor> test_input_tensor; 108 std::shared_ptr<Tensor> expected_output_tensor; 109 Tensor::CreateFromVector(test_vector, TensorShape({1, (long int)test_vector.size(), 1}), &test_input_tensor); 110 Tensor::CreateFromVector(expected_output_vector, TensorShape({1, (long int)test_vector.size(), 1}), 111 &expected_output_tensor); 112 113 std::shared_ptr<Tensor> test_output_tensor; 114 Status s = op->Compute(test_input_tensor, &test_output_tensor); 115 116 EXPECT_TRUE(s.IsOk()); 117 ASSERT_TRUE(test_output_tensor->shape() == expected_output_tensor->shape()); 118 ASSERT_TRUE(test_output_tensor->type() == expected_output_tensor->type()); 119 MS_LOG(DEBUG) << *test_output_tensor << std::endl; 120 MS_LOG(DEBUG) << *expected_output_tensor << std::endl; 121 ASSERT_TRUE(*test_output_tensor == *expected_output_tensor); 122 } 123 124 TEST_F(MindDataTestSolarizeOp, TestOp5) { 125 MS_LOG(INFO) << "Doing testSolarizeOp5 - Rank 2 input tensor."; 126 127 std::vector<uint8_t> threshold ={1, 230}; 128 std::unique_ptr<SolarizeOp> op(new SolarizeOp(threshold)); 129 130 std::vector<uint8_t> test_vector = {3, 4, 59, 210, 255}; 131 std::vector<uint8_t> expected_output_vector = {252, 251, 196, 45, 255}; 132 std::shared_ptr<Tensor> test_input_tensor; 133 std::shared_ptr<Tensor> expected_output_tensor; 134 Tensor::CreateFromVector(test_vector, TensorShape({1, (long int)test_vector.size()}), &test_input_tensor); 135 Tensor::CreateFromVector(expected_output_vector, TensorShape({1, (long int)test_vector.size()}), 136 &expected_output_tensor); 137 138 std::shared_ptr<Tensor> test_output_tensor; 139 Status s = op->Compute(test_input_tensor, &test_output_tensor); 140 141 EXPECT_TRUE(s.IsOk()); 142 ASSERT_TRUE(test_output_tensor->shape() == expected_output_tensor->shape()); 143 ASSERT_TRUE(test_output_tensor->type() == expected_output_tensor->type()); 144 MS_LOG(DEBUG) << *test_output_tensor << std::endl; 145 MS_LOG(DEBUG) << *expected_output_tensor << std::endl; 146 147 ASSERT_TRUE(*test_output_tensor == *expected_output_tensor); 148 } 149 150 TEST_F(MindDataTestSolarizeOp, TestOp6) { 151 MS_LOG(INFO) << "Doing testSolarizeOp6 - Bad Input."; 152 153 std::vector<uint8_t> threshold ={10, 1}; 154 std::unique_ptr<SolarizeOp> op(new SolarizeOp(threshold)); 155 156 std::vector<uint8_t> test_vector = {3, 4, 59, 210, 255}; 157 std::shared_ptr<Tensor> test_input_tensor; 158 std::shared_ptr<Tensor> test_output_tensor; 159 Tensor::CreateFromVector(test_vector, TensorShape({1, (long int)test_vector.size(), 1}), &test_input_tensor); 160 161 Status s = op->Compute(test_input_tensor, &test_output_tensor); 162 163 EXPECT_TRUE(s.IsError()); 164 EXPECT_NE(s.ToString().find("Solarize: threshold_min must be smaller or equal to threshold_max."), 165 std::string::npos); 166 ASSERT_TRUE(s.StatusCode() == StatusCode::kMDUnexpectedError); 167 }