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 #include "common/common.h" 17 #include "common/cvop_common.h" 18 #include "minddata/dataset/kernels/image/mixup_batch_op.h" 19 #include "utils/log_adapter.h" 20 21 using namespace mindspore::dataset; 22 using mindspore::MsLogLevel::INFO; 23 using mindspore::ExceptionType::NoExceptionType; 24 using mindspore::LogStream; 25 26 class MindDataTestMixUpBatchOp : public UT::CVOP::CVOpCommon { 27 protected: 28 MindDataTestMixUpBatchOp() : CVOpCommon() {} 29 30 std::shared_ptr<Tensor> output_tensor_; 31 }; 32 33 TEST_F(MindDataTestMixUpBatchOp, TestSuccess) { 34 MS_LOG(INFO) << "Doing MindDataTestMixUpBatchOp success case"; 35 std::shared_ptr<Tensor> batched_tensor; 36 std::shared_ptr<Tensor> batched_labels; 37 Tensor::CreateEmpty(TensorShape({2, input_tensor_->shape()[0], input_tensor_->shape()[1], input_tensor_->shape()[2]}), input_tensor_->type(), &batched_tensor); 38 for (int i = 0; i < 2; i++) { 39 batched_tensor->InsertTensor({i}, input_tensor_); 40 } 41 Tensor::CreateFromVector(std::vector<uint32_t>({0, 1, 1, 0}), TensorShape({2, 2}), &batched_labels); 42 std::shared_ptr<MixUpBatchOp> op = std::make_shared<MixUpBatchOp>(1); 43 TensorRow in; 44 in.push_back(batched_tensor); 45 in.push_back(batched_labels); 46 TensorRow out; 47 ASSERT_TRUE(op->Compute(in, &out).IsOk()); 48 49 EXPECT_EQ(in.at(0)->shape()[0], out.at(0)->shape()[0]); 50 EXPECT_EQ(in.at(0)->shape()[1], out.at(0)->shape()[1]); 51 EXPECT_EQ(in.at(0)->shape()[2], out.at(0)->shape()[2]); 52 EXPECT_EQ(in.at(0)->shape()[3], out.at(0)->shape()[3]); 53 54 EXPECT_EQ(in.at(1)->shape()[0], out.at(1)->shape()[0]); 55 EXPECT_EQ(in.at(1)->shape()[1], out.at(1)->shape()[1]); 56 } 57 58 TEST_F(MindDataTestMixUpBatchOp, TestFail) { 59 // This is a fail case because our labels are not batched and are 1-dimensional 60 MS_LOG(INFO) << "Doing MindDataTestMixUpBatchOp fail case"; 61 std::shared_ptr<Tensor> labels; 62 Tensor::CreateFromVector(std::vector<uint32_t>({0, 1, 1, 0}), TensorShape({4}), &labels); 63 std::shared_ptr<MixUpBatchOp> op = std::make_shared<MixUpBatchOp>(1); 64 TensorRow in; 65 in.push_back(input_tensor_); 66 in.push_back(labels); 67 TensorRow out; 68 ASSERT_FALSE(op->Compute(in, &out).IsOk()); 69 } 70