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 "minddata/dataset/include/dataset/datasets.h"
18 #include "minddata/dataset/include/dataset/execute.h"
19 #include "minddata/dataset/include/dataset/vision.h"
20 #include "utils/log_adapter.h"
21
22 using namespace mindspore::dataset;
23
24 class MindDataTestGaussianBlur : public UT::DatasetOpTesting {
25 protected:
26 };
27
TEST_F(MindDataTestGaussianBlur,TestGaussianBlurParamCheck)28 TEST_F(MindDataTestGaussianBlur, TestGaussianBlurParamCheck) {
29 MS_LOG(INFO) << "Doing MindDataTestGaussianBlur-TestGaussianBlurParamCheck with invalid parameters.";
30 // Create an ImageFolder Dataset
31 std::string folder_path = datasets_root_path_ + "/testPK/data/";
32 std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(false, 10));
33 EXPECT_NE(ds, nullptr);
34
35 // Case 1: Kernel size is not positive
36 // Create objects for the tensor ops
37 std::shared_ptr<TensorTransform> gaussian_blur1(new vision::GaussianBlur({-1}));
38 auto ds1 = ds->Map({gaussian_blur1});
39 EXPECT_NE(ds1, nullptr);
40 // Create an iterator over the result of the above dataset
41 std::shared_ptr<Iterator> iter1 = ds1->CreateIterator();
42 // Expect failure: invalid kernel_size for GaussianBlur
43 EXPECT_EQ(iter1, nullptr);
44
45 // Case 2: Kernel size is not odd
46 // Create objects for the tensor ops
47 std::shared_ptr<TensorTransform> gaussian_blur2(new vision::GaussianBlur({2, 2}, {3, 3}));
48 auto ds2 = ds->Map({gaussian_blur2});
49 EXPECT_NE(ds2, nullptr);
50 // Create an iterator over the result of the above dataset
51 std::shared_ptr<Iterator> iter2 = ds2->CreateIterator();
52 // Expect failure: invalid kernel_size for GaussianBlur
53 EXPECT_EQ(iter2, nullptr);
54
55 // Case 3: Sigma is not positive
56 // Create objects for the tensor ops
57 std::shared_ptr<TensorTransform> gaussian_blur3(new vision::GaussianBlur({3}, {-3}));
58 auto ds3 = ds->Map({gaussian_blur3});
59 EXPECT_NE(ds3, nullptr);
60 // Create an iterator over the result of the above dataset
61 std::shared_ptr<Iterator> iter3 = ds3->CreateIterator();
62 // Expect failure: invalid sigma for GaussianBlur
63 EXPECT_EQ(iter3, nullptr);
64 }
65
TEST_F(MindDataTestGaussianBlur,TestGaussianBlurPipeline)66 TEST_F(MindDataTestGaussianBlur, TestGaussianBlurPipeline) {
67 MS_LOG(INFO) << "Doing MindDataTestGaussianBlur-TestGaussianBlurPipeline.";
68
69 // Create an ImageFolder Dataset
70 std::string folder_path = datasets_root_path_ + "/testPK/data/";
71 std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, std::make_shared<RandomSampler>(false, 10));
72 EXPECT_NE(ds, nullptr);
73
74 // Create objects for the tensor ops
75 std::shared_ptr<TensorTransform> gaussian_blur(new vision::GaussianBlur({3, 3}, {5, 5}));
76
77 // Create a Map operation on ds
78 ds = ds->Map({gaussian_blur});
79 EXPECT_NE(ds, nullptr);
80
81 // Create a Batch operation on ds
82 int32_t batch_size = 1;
83 ds = ds->Batch(batch_size);
84 EXPECT_NE(ds, nullptr);
85
86 // Create an iterator over the result of the above dataset
87 // This will trigger the creation of the Execution Tree and launch it.
88 std::shared_ptr<Iterator> iter = ds->CreateIterator();
89 EXPECT_NE(iter, nullptr);
90
91 // Iterate the dataset and get each row
92 std::unordered_map<std::string, mindspore::MSTensor> row;
93 ASSERT_OK(iter->GetNextRow(&row));
94
95 uint64_t i = 0;
96 while (row.size() != 0) {
97 i++;
98 auto image = row["image"];
99 MS_LOG(INFO) << "Tensor image shape: " << image.Shape();
100 ASSERT_OK(iter->GetNextRow(&row));
101 }
102
103 EXPECT_EQ(i, 10);
104
105 // Manually terminate the pipeline
106 iter->Stop();
107 }
108
TEST_F(MindDataTestGaussianBlur,TestGaussianBlurEager)109 TEST_F(MindDataTestGaussianBlur, TestGaussianBlurEager) {
110 MS_LOG(INFO) << "Doing MindDataTestGaussianBlur-TestGaussianBlurEager.";
111
112 // Read images
113 auto image = ReadFileToTensor("data/dataset/apple.jpg");
114
115 // Transform params
116 auto decode = vision::Decode();
117 auto gaussian_blur = vision::GaussianBlur({7}, {3.5});
118
119 auto transform = Execute({decode, gaussian_blur});
120 Status rc = transform(image, &image);
121
122 EXPECT_EQ(rc, Status::OK());
123 }
124