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/cutmix_batch_op.h"
19 #include "utils/log_adapter.h"
20
21 using namespace mindspore::dataset;
22 using mindspore::LogStream;
23 using mindspore::ExceptionType::NoExceptionType;
24 using mindspore::MsLogLevel::INFO;
25
26 class MindDataTestCutMixBatchOp : public UT::CVOP::CVOpCommon {
27 protected:
MindDataTestCutMixBatchOp()28 MindDataTestCutMixBatchOp() : CVOpCommon() {}
29 };
30
TEST_F(MindDataTestCutMixBatchOp,TestSuccess1)31 TEST_F(MindDataTestCutMixBatchOp, TestSuccess1) {
32 MS_LOG(INFO) << "Doing MindDataTestCutMixBatchOp success1 case";
33 std::shared_ptr<Tensor> input_tensor_resized;
34 std::shared_ptr<Tensor> batched_tensor;
35 std::shared_ptr<Tensor> batched_labels;
36 Resize(input_tensor_, &input_tensor_resized, 227, 403);
37
38 Tensor::CreateEmpty(TensorShape({2, input_tensor_resized->shape()[0], input_tensor_resized->shape()[1],
39 input_tensor_resized->shape()[2]}), input_tensor_resized->type(), &batched_tensor);
40 for (int i = 0; i < 2; i++) {
41 batched_tensor->InsertTensor({i}, input_tensor_resized);
42 }
43 Tensor::CreateFromVector(std::vector<uint32_t>({0, 1, 1, 0}), TensorShape({2, 2}), &batched_labels);
44 std::shared_ptr<CutMixBatchOp> op = std::make_shared<CutMixBatchOp>(ImageBatchFormat::kNHWC, 1.0, 1.0);
45 TensorRow in;
46 in.push_back(batched_tensor);
47 in.push_back(batched_labels);
48 TensorRow out;
49 ASSERT_TRUE(op->Compute(in, &out).IsOk());
50
51 EXPECT_EQ(in.at(0)->shape()[0], out.at(0)->shape()[0]);
52 EXPECT_EQ(in.at(0)->shape()[1], out.at(0)->shape()[1]);
53 EXPECT_EQ(in.at(0)->shape()[2], out.at(0)->shape()[2]);
54 EXPECT_EQ(in.at(0)->shape()[3], out.at(0)->shape()[3]);
55
56 EXPECT_EQ(in.at(1)->shape()[0], out.at(1)->shape()[0]);
57 EXPECT_EQ(in.at(1)->shape()[1], out.at(1)->shape()[1]);
58 }
59
TEST_F(MindDataTestCutMixBatchOp,TestSuccess2)60 TEST_F(MindDataTestCutMixBatchOp, TestSuccess2) {
61 MS_LOG(INFO) << "Doing MindDataTestCutMixBatchOp success2 case";
62 std::shared_ptr<Tensor> input_tensor_resized;
63 std::shared_ptr<Tensor> batched_tensor;
64 std::shared_ptr<Tensor> batched_labels;
65 std::shared_ptr<Tensor> chw_tensor;
66 Resize(input_tensor_, &input_tensor_resized, 227, 403);
67
68 ASSERT_TRUE(HwcToChw(input_tensor_resized, &chw_tensor).IsOk());
69 Tensor::CreateEmpty(TensorShape({2, chw_tensor->shape()[0], chw_tensor->shape()[1], chw_tensor->shape()[2]}),
70 chw_tensor->type(), &batched_tensor);
71 for (int i = 0; i < 2; i++) {
72 batched_tensor->InsertTensor({i}, chw_tensor);
73 }
74 Tensor::CreateFromVector(std::vector<uint32_t>({0, 1, 1, 0}), TensorShape({2, 2}), &batched_labels);
75 std::shared_ptr<CutMixBatchOp> op = std::make_shared<CutMixBatchOp>(ImageBatchFormat::kNCHW, 1.0, 0.5);
76 TensorRow in;
77 in.push_back(batched_tensor);
78 in.push_back(batched_labels);
79 TensorRow out;
80 ASSERT_TRUE(op->Compute(in, &out).IsOk());
81
82 EXPECT_EQ(in.at(0)->shape()[0], out.at(0)->shape()[0]);
83 EXPECT_EQ(in.at(0)->shape()[1], out.at(0)->shape()[1]);
84 EXPECT_EQ(in.at(0)->shape()[2], out.at(0)->shape()[2]);
85 EXPECT_EQ(in.at(0)->shape()[3], out.at(0)->shape()[3]);
86
87 EXPECT_EQ(in.at(1)->shape()[0], out.at(1)->shape()[0]);
88 EXPECT_EQ(in.at(1)->shape()[1], out.at(1)->shape()[1]);
89 }
90
TEST_F(MindDataTestCutMixBatchOp,TestFail1)91 TEST_F(MindDataTestCutMixBatchOp, TestFail1) {
92 // This is a fail case because our labels are not batched and are 1-dimensional
93 MS_LOG(INFO) << "Doing MindDataTestCutMixBatchOp fail1 case";
94 std::shared_ptr<Tensor> labels;
95 Tensor::CreateFromVector(std::vector<uint32_t>({0, 1, 1, 0}), TensorShape({4}), &labels);
96 std::shared_ptr<CutMixBatchOp> op = std::make_shared<CutMixBatchOp>(ImageBatchFormat::kNHWC, 1.0, 1.0);
97 TensorRow in;
98 in.push_back(input_tensor_);
99 in.push_back(labels);
100 TensorRow out;
101 ASSERT_FALSE(op->Compute(in, &out).IsOk());
102 }
103
TEST_F(MindDataTestCutMixBatchOp,TestFail2)104 TEST_F(MindDataTestCutMixBatchOp, TestFail2) {
105 // This should fail because the image_batch_format provided is not the same as the actual format of the images
106 MS_LOG(INFO) << "Doing MindDataTestCutMixBatchOp fail2 case";
107 std::shared_ptr<Tensor> batched_tensor;
108 std::shared_ptr<Tensor> batched_labels;
109 Tensor::CreateEmpty(TensorShape({2, input_tensor_->shape()[0], input_tensor_->shape()[1], input_tensor_->shape()[2]}),
110 input_tensor_->type(), &batched_tensor);
111 for (int i = 0; i < 2; i++) {
112 batched_tensor->InsertTensor({i}, input_tensor_);
113 }
114 Tensor::CreateFromVector(std::vector<uint32_t>({0, 1, 1, 0}), TensorShape({2, 2}), &batched_labels);
115 std::shared_ptr<CutMixBatchOp> op = std::make_shared<CutMixBatchOp>(ImageBatchFormat::kNCHW, 1.0, 1.0);
116 TensorRow in;
117 in.push_back(batched_tensor);
118 in.push_back(batched_labels);
119 TensorRow out;
120 ASSERT_FALSE(op->Compute(in, &out).IsOk());
121 }
122