• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 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 <fstream>
17 #include "common/common.h"
18 #include "common/cvop_common.h"
19 #include "minddata/dataset/kernels/image/decode_op.h"
20 #include "minddata/dataset/kernels/image/random_crop_and_resize_op.h"
21 #include "minddata/dataset/kernels/image/random_crop_decode_resize_op.h"
22 #include "minddata/dataset/core/config_manager.h"
23 #include "utils/log_adapter.h"
24 
25 using namespace mindspore::dataset;
26 using mindspore::LogStream;
27 using mindspore::ExceptionType::NoExceptionType;
28 using mindspore::MsLogLevel::INFO;
29 constexpr double kMseThreshold = 2.5;
30 
31 class MindDataTestRandomCropDecodeResizeOp : public UT::CVOP::CVOpCommon {
32  public:
MindDataTestRandomCropDecodeResizeOp()33   MindDataTestRandomCropDecodeResizeOp() : CVOpCommon() {}
34 };
35 
TEST_F(MindDataTestRandomCropDecodeResizeOp,TestOp2)36 TEST_F(MindDataTestRandomCropDecodeResizeOp, TestOp2) {
37   MS_LOG(INFO) << "starting RandomCropDecodeResizeOp test 1";
38 
39   std::shared_ptr<Tensor> crop_and_decode_output;
40 
41   constexpr int target_height = 884;
42   constexpr int target_width = 718;
43   constexpr float scale_lb = 0.08;
44   constexpr float scale_ub = 1.0;
45   constexpr float aspect_lb = 0.75;
46   constexpr float aspect_ub = 1.333333;
47   const InterpolationMode interpolation = InterpolationMode::kLinear;
48   constexpr uint32_t max_iter = 10;
49 
50   auto crop_and_decode = RandomCropDecodeResizeOp(target_height, target_width, scale_lb, scale_ub, aspect_lb, aspect_ub,
51                                                   interpolation, max_iter);
52   auto crop_and_decode_copy = crop_and_decode;
53   auto decode_and_crop = static_cast<RandomCropAndResizeOp>(crop_and_decode_copy);
54   GlobalContext::config_manager()->set_seed(42);
55   TensorRow input_tensor_row_decode;
56   TensorRow output_tensor_row_decode;
57   input_tensor_row_decode.push_back(raw_input_tensor_);
58   TensorRow input_tensor_row;
59   TensorRow output_tensor_row;
60   input_tensor_row.push_back(input_tensor_);
61   for (int k = 0; k < 10; k++) {
62     (void)crop_and_decode.Compute(input_tensor_row_decode, &output_tensor_row_decode);
63     (void)decode_and_crop.Compute(input_tensor_row, &output_tensor_row);
64     cv::Mat output1 = CVTensor::AsCVTensor(output_tensor_row_decode[0])->mat().clone();
65     cv::Mat output2 = CVTensor::AsCVTensor(output_tensor_row[0])->mat().clone();
66 
67     long int mse_sum = 0;
68     long int count = 0;
69     int a, b;
70     for (int i = 0; i < target_height; i++) {
71       for (int j = 0; j < target_width; j++) {
72         a = static_cast<int>(output1.at<cv::Vec3b>(i, j)[1]);
73         b = static_cast<int>(output2.at<cv::Vec3b>(i, j)[1]);
74         mse_sum += sqrt((a - b) * (a - b));
75         if (a != b) {
76           count++;
77         };
78       }
79     }
80     double mse;
81     mse = count > 0 ? static_cast<double>(mse_sum) / count : mse_sum;
82     MS_LOG(INFO) << "mse: " << mse << std::endl;
83     EXPECT_LT(mse, kMseThreshold);
84   }
85 
86   MS_LOG(INFO) << "RandomCropDecodeResizeOp test 1 finished";
87 }
88 
TEST_F(MindDataTestRandomCropDecodeResizeOp,TestOp1)89 TEST_F(MindDataTestRandomCropDecodeResizeOp, TestOp1) {
90   MS_LOG(INFO) << "starting RandomCropDecodeResizeOp test 2";
91   constexpr int h = 884;
92   constexpr int w = 718;
93   constexpr float scale_lb = 0.1;
94   constexpr float scale_ub = 1;
95   constexpr float aspect_lb = 0.1;
96   constexpr float aspect_ub = 10;
97 
98   std::shared_ptr<Tensor> decoded, decoded_and_cropped, cropped_and_decoded;
99   std::mt19937 rd;
100   std::uniform_real_distribution<float> rd_scale(scale_lb, scale_ub);
101   std::uniform_real_distribution<float> rd_aspect(aspect_lb, aspect_ub);
102   DecodeOp op(true);
103   op.Compute(raw_input_tensor_, &decoded);
104   Status crop_and_decode_status, decode_and_crop_status;
105   float scale, aspect;
106   int crop_width, crop_height;
107   bool crop_success = false;
108   int mse_sum, m1, m2, count;
109   double mse;
110 
111   for (int k = 0; k < 10; ++k) {
112     mse_sum = 0;
113     count = 0;
114     for (auto i = 0; i < 10; i++) {
115       scale = rd_scale(rd);
116       aspect = rd_aspect(rd);
117       crop_width = std::round(std::sqrt(h * w * scale / aspect));
118       crop_height = std::round(crop_width * aspect);
119       if (crop_width <= w && crop_height <= h) {
120         crop_success = true;
121         break;
122       }
123     }
124     if (crop_success == false) {
125       aspect = static_cast<float>(h) / w;
126       scale = rd_scale(rd);
127       crop_width = std::round(std::sqrt(h * w * scale / aspect));
128       crop_height = std::round(crop_width * aspect);
129       crop_height = (crop_height > h) ? h : crop_height;
130       crop_width = (crop_width > w) ? w : crop_width;
131     }
132     std::uniform_int_distribution<> rd_x(0, w - crop_width);
133     std::uniform_int_distribution<> rd_y(0, h - crop_height);
134     int x = rd_x(rd);
135     int y = rd_y(rd);
136 
137     op.Compute(raw_input_tensor_, &decoded);
138     crop_and_decode_status = Crop(decoded, &decoded_and_cropped, x, y, crop_width, crop_height);
139     decode_and_crop_status = JpegCropAndDecode(raw_input_tensor_, &cropped_and_decoded, x, y, crop_width, crop_height);
140     {
141       cv::Mat M1 = CVTensor::AsCVTensor(decoded_and_cropped)->mat().clone();
142       cv::Mat M2 = CVTensor::AsCVTensor(cropped_and_decoded)->mat().clone();
143       for (int i = 0; i < crop_height; ++i) {
144         for (int j = 0; j < crop_width; ++j) {
145           m1 = M1.at<cv::Vec3b>(i, j)[1];
146           m2 = M2.at<cv::Vec3b>(i, j)[1];
147           mse_sum += sqrt((m1 - m2) * (m1 - m2));
148           if (m1 != m2) {
149             count++;
150           }
151         }
152       }
153     }
154 
155     mse = count > 0 ? static_cast<double>(mse_sum) / count : mse_sum;
156     MS_LOG(INFO) << "mse: " << mse << std::endl;
157     EXPECT_LT(mse, kMseThreshold);
158   }
159   MS_LOG(INFO) << "RandomCropDecodeResizeOp test 2 finished";
160 }
161