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/center_crop_op.h" 19 #include "minddata/dataset/core/cv_tensor.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 MindDataTestCenterCropOp : public UT::CVOP::CVOpCommon { 28 public: 29 MindDataTestCenterCropOp() : CVOpCommon() {} 30 }; 31 32 TEST_F(MindDataTestCenterCropOp, TestOp1) { 33 MS_LOG(INFO) << "Doing MindDataTestCenterCropOp::TestOp1."; 34 std::shared_ptr<Tensor> output_tensor; 35 int het = 256; 36 int wid = 128; 37 std::unique_ptr<CenterCropOp> op(new CenterCropOp(het, wid)); 38 EXPECT_TRUE(op->OneToOne()); 39 Status s = op->Compute(input_tensor_, &output_tensor); 40 EXPECT_TRUE(s.IsOk()); 41 EXPECT_EQ(het, output_tensor->shape()[0]); 42 EXPECT_EQ(wid, output_tensor->shape()[1]); 43 std::shared_ptr<CVTensor> p = CVTensor::AsCVTensor(output_tensor); 44 } 45 46 TEST_F(MindDataTestCenterCropOp, TestOp2) { 47 MS_LOG(INFO) << "MindDataTestCenterCropOp::TestOp2. Cap valid crop size at 10 times the input size"; 48 std::shared_ptr<Tensor> output_tensor; 49 50 int64_t wid = input_tensor_->shape()[0] * 10 + 1; 51 int64_t het = input_tensor_->shape()[1] * 10 + 1; 52 53 std::unique_ptr<CenterCropOp> op(new CenterCropOp(het, wid)); 54 Status s = op->Compute(input_tensor_, &output_tensor); 55 EXPECT_TRUE(s.IsError()); 56 ASSERT_TRUE(s.StatusCode() == StatusCode::kMDUnexpectedError); 57 } 58 59 TEST_F(MindDataTestCenterCropOp, TestOp3) { 60 MS_LOG(INFO) << "Doing MindDataTestCenterCropOp::TestOp3. Test single integer input for square crop."; 61 std::shared_ptr<Tensor> output_tensor; 62 int side = 128; 63 std::unique_ptr<CenterCropOp> op(new CenterCropOp(side)); 64 EXPECT_TRUE(op->OneToOne()); 65 Status s = op->Compute(input_tensor_, &output_tensor); 66 EXPECT_TRUE(s.IsOk()); 67 // Confirm both height and width are of size <side>. 68 EXPECT_EQ(side, output_tensor->shape()[0]); 69 EXPECT_EQ(side, output_tensor->shape()[1]); 70 std::shared_ptr<CVTensor> p = CVTensor::AsCVTensor(output_tensor); 71 } 72