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