• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include "common/common.h"
18 #include "common/cvop_common.h"
19 #include "minddata/dataset/kernels/image/random_color_op.h"
20 #include "minddata/dataset/core/cv_tensor.h"
21 #include "utils/log_adapter.h"
22 
23 using namespace mindspore::dataset;
24 using mindspore::LogStream;
25 using mindspore::ExceptionType::NoExceptionType;
26 using mindspore::MsLogLevel::INFO;
27 
28 class MindDataTestRandomColorOp : public UT::CVOP::CVOpCommon {
29  public:
MindDataTestRandomColorOp()30   MindDataTestRandomColorOp() : CVOpCommon(), shape({3, 3, 3}) {
31     std::shared_ptr<Tensor> in;
32     std::shared_ptr<Tensor> gray;
33 
34     (void)Tensor::CreateEmpty(shape, DataType(DataType::DE_UINT8), &in);
35     (void)Tensor::CreateEmpty(shape, DataType(DataType::DE_UINT8), &input_tensor);
36     Status s = in->Fill<uint8_t>(42);
37     s = input_tensor->Fill<uint8_t>(42);
38     cvt_in = CVTensor::AsCVTensor(in);
39     cv::Mat m2;
40     auto m1 = cvt_in->mat();
41     cv::cvtColor(m1, m2, cv::COLOR_RGB2GRAY);
42     cv::Mat temp[3] = {m2 , m2 , m2 };
43     cv::Mat cv_out;
44     cv::merge(temp, 3, cv_out);
45     std::shared_ptr<CVTensor> cvt_out;
46     CVTensor::CreateFromMat(cv_out, 3, &cvt_out);
47     gray_tensor = std::static_pointer_cast<Tensor>(cvt_out);
48   }
49   TensorShape shape;
50   std::shared_ptr<Tensor> input_tensor;
51   std::shared_ptr<CVTensor> cvt_in;
52   std::shared_ptr<Tensor> gray_tensor;
53 };
54 
Compare(std::shared_ptr<Tensor> t1,std::shared_ptr<Tensor> t2)55 int64_t Compare(std::shared_ptr<Tensor> t1, std::shared_ptr<Tensor> t2) {
56   auto shape = t1->shape();
57   int64_t sum = 0;
58   for (auto i = 0; i < shape[0]; i++) {
59     for (auto j = 0; j < shape[1]; j++) {
60       for (auto k = 0; k < shape[2]; k++) {
61         uint8_t value1;
62         uint8_t value2;
63         (void)t1->GetItemAt<uint8_t>(&value1, {i, j, k});
64         (void)t2->GetItemAt<uint8_t>(&value2, {i, j, k});
65         sum += abs(static_cast<int>(value1) - static_cast<int>(value2));
66       }
67     }
68   }
69   return sum;
70 }
71 
72 // these tests are tautological, write better tests when the requirements for the output are determined
73 // e. g. how do we want to convert to gray and what does it mean to blend with a gray image (pre- post- gamma corrected,
74 // what weights).
TEST_F(MindDataTestRandomColorOp,TestOp1)75 TEST_F(MindDataTestRandomColorOp, TestOp1) {
76   std::shared_ptr<Tensor> output_tensor;
77   auto op = RandomColorOp(1, 1);
78   auto s = op.Compute(input_tensor, &output_tensor);
79   auto res = Compare(input_tensor, output_tensor);
80   EXPECT_EQ(0, res);
81 }
82 
TEST_F(MindDataTestRandomColorOp,TestOp2)83 TEST_F(MindDataTestRandomColorOp, TestOp2) {
84   std::shared_ptr<Tensor> output_tensor;
85   auto op = RandomColorOp(0, 0);
86   auto s = op.Compute(input_tensor, &output_tensor);
87   EXPECT_TRUE(s.IsOk());
88   auto res = Compare(output_tensor, gray_tensor);
89   EXPECT_EQ(res, 0);
90 }
91 
TEST_F(MindDataTestRandomColorOp,TestOp3)92 TEST_F(MindDataTestRandomColorOp, TestOp3) {
93   std::shared_ptr<Tensor> output_tensor;
94   auto op = RandomColorOp(0.0, 1.0);
95   for (auto i = 0; i < 1; i++) {
96     auto s = op.Compute(input_tensor, &output_tensor);
97     EXPECT_TRUE(s.IsOk());
98   }
99 }
100