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 "common/common.h"
17 #include "common/cvop_common.h"
18 #include <random>
19 #include "minddata/dataset/kernels/image/random_crop_and_resize_op.h"
20 #include "utils/log_adapter.h"
21
22 using namespace mindspore::dataset;
23 using mindspore::LogStream;
24 using mindspore::ExceptionType::NoExceptionType;
25 using mindspore::MsLogLevel::INFO;
26
27 class MindDataTestRandomCropAndResizeOp : public UT::CVOP::CVOpCommon {
28 public:
MindDataTestRandomCropAndResizeOp()29 MindDataTestRandomCropAndResizeOp() : CVOpCommon() {}
30 };
TEST_F(MindDataTestRandomCropAndResizeOp,TestOpSimpleTest1)31 TEST_F(MindDataTestRandomCropAndResizeOp, TestOpSimpleTest1) {
32 MS_LOG(INFO) << " starting RandomCropAndResizeOp simple test";
33 TensorShape s_in = input_tensor_->shape();
34 TensorRow input_tensor_row;
35 input_tensor_row.push_back(input_tensor_);
36 input_tensor_row.push_back(input_tensor_);
37 TensorRow output_tensor_row;
38 std::shared_ptr<Tensor> output_tensor;
39 int h_out = 1024;
40 int w_out = 2048;
41 float aspect_lb = 2;
42 float aspect_ub = 2.5;
43 float scale_lb = 0.2;
44 float scale_ub = 2.0;
45
46 TensorShape s_out({h_out, w_out, s_in[2]});
47
48 auto op = std::make_unique<RandomCropAndResizeOp>(h_out, w_out, scale_lb, scale_ub, aspect_lb, aspect_ub);
49 Status s;
50 for (auto i = 0; i < 100; i++) {
51 s = op->Compute(input_tensor_row, &output_tensor_row);
52 EXPECT_TRUE(s.IsOk());
53 }
54
55 MS_LOG(INFO) << "RandomCropAndResizeOp simple test finished";
56 }
TEST_F(MindDataTestRandomCropAndResizeOp,TestOpSimpleTest2)57 TEST_F(MindDataTestRandomCropAndResizeOp, TestOpSimpleTest2) {
58 MS_LOG(INFO) << " starting RandomCropAndResizeOp simple test";
59 TensorShape s_in = input_tensor_->shape();
60 TensorRow input_tensor_row;
61 input_tensor_row.push_back(input_tensor_);
62 input_tensor_row.push_back(input_tensor_);
63 TensorRow output_tensor_row;
64 std::shared_ptr<Tensor> output_tensor;
65 int h_out = 1024;
66 int w_out = 2048;
67 float aspect_lb = 1;
68 float aspect_ub = 1.5;
69 float scale_lb = 0.2;
70 float scale_ub = 2.0;
71
72 TensorShape s_out({h_out, w_out, s_in[2]});
73
74 auto op = std::make_unique<RandomCropAndResizeOp>(h_out, w_out, scale_lb, scale_ub, aspect_lb, aspect_ub);
75 Status s;
76 for (auto i = 0; i < 100; i++) {
77 s = op->Compute(input_tensor_row, &output_tensor_row);
78 EXPECT_TRUE(s.IsOk());
79 }
80
81 MS_LOG(INFO) << "RandomCropAndResizeOp simple test finished";
82 }
TEST_F(MindDataTestRandomCropAndResizeOp,TestOpSimpleTest3)83 TEST_F(MindDataTestRandomCropAndResizeOp, TestOpSimpleTest3) {
84 MS_LOG(INFO) << " starting RandomCropAndResizeOp simple test";
85 TensorShape s_in = input_tensor_->shape();
86 TensorRow input_tensor_row;
87 input_tensor_row.push_back(input_tensor_);
88 input_tensor_row.push_back(input_tensor_);
89 TensorRow output_tensor_row;
90 std::shared_ptr<Tensor> output_tensor;
91 int h_out = 1024;
92 int w_out = 2048;
93 float aspect_lb = 0.2;
94 float aspect_ub = 3;
95 float scale_lb = 0.2;
96 float scale_ub = 2.0;
97
98 TensorShape s_out({h_out, w_out, s_in[2]});
99
100 auto op = std::make_unique<RandomCropAndResizeOp>(h_out, w_out, scale_lb, scale_ub, aspect_lb, aspect_ub);
101 Status s;
102 for (auto i = 0; i < 100; i++) {
103 s = op->Compute(input_tensor_row, &output_tensor_row);
104 EXPECT_TRUE(s.IsOk());
105 }
106
107 MS_LOG(INFO) << "RandomCropAndResizeOp simple test finished";
108 }