|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739 |
- /**
- * Copyright 2019 Huawei Technologies Co., Ltd
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #include <iostream>
- #include <memory>
- #include <vector>
-
-
- #include "common/common.h"
- #include "minddata/dataset/core/client.h"
- #include "minddata/dataset/core/tensor.h"
- #include "minddata/dataset/engine/datasetops/source/image_folder_op.h"
- #include "minddata/dataset/kernels/image/decode_op.h"
- #include "minddata/dataset/kernels/image/resize_op.h"
- #include "minddata/dataset/kernels/tensor_op.h"
- #include "utils/log_adapter.h"
-
- using namespace mindspore::dataset;
- using mindspore::LogStream;
- using mindspore::MsLogLevel::INFO;
-
- namespace mindspore {
- namespace dataset {
- namespace test {
- class NoOp : public TensorOp {
- public:
- NoOp(){};
-
- ~NoOp(){};
-
- Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override {
- *output = std::move(input);
- return Status::OK();
- };
-
- void Print(std::ostream &out) const override { out << "NoOp"; };
-
- std::string Name() const override { return kNoOp; }
- };
-
- class ThreeToOneOp : public TensorOp {
- public:
- ThreeToOneOp(){};
-
- ~ThreeToOneOp(){};
-
- uint32_t NumInput() override { return 3; }
- // Compute function that holds the actual implementation of the operation.
- Status Compute(const TensorRow &input, TensorRow *output) override {
- output->push_back(input[0]);
- return Status::OK();
- };
-
- void Print(std::ostream &out) const override { out << "ThreeToOneOp"; };
-
- std::string Name() const override { return "ThreeToOneOp"; }
- };
-
- class OneToThreeOp : public TensorOp {
- public:
- OneToThreeOp(){};
-
- ~OneToThreeOp(){};
-
- uint32_t NumOutput() override { return 3; }
-
- // Compute function that holds the actual implementation of the operation.
- // Simply pushing the same shared pointer of the first element of input vector three times.
- Status Compute(const TensorRow &input, TensorRow *output) override {
- output->push_back(input[0]);
- output->push_back(input[0]);
- output->push_back(input[0]);
- return Status::OK();
- };
-
- void Print(std::ostream &out) const override { out << "OneToThreeOp"; };
-
- std::string Name() const override { return "OneToThreeOp"; };
- };
- } // namespace test
- } // namespace dataset
- } // namespace mindspore
-
- class MindDataTestMapOp : public UT::DatasetOpTesting {
- public:
- void SetUp() override {
- DatasetOpTesting::SetUp();
- dataset_path_ = datasets_root_path_ + "" + "/testDataset2/testDataset2.data";
- schema_path_ = datasets_root_path_ + "" + "/testDataset2/datasetSchema.json";
-
- GlobalInit();
-
- // Start with an empty execution tree
- my_tree_ = std::make_shared<ExecutionTree>();
- }
-
- std::shared_ptr<TFReaderOp> CreateTFReaderOp() {
- std::shared_ptr<TFReaderOp> my_tfreader_op;
- TFReaderOp::Builder builder;
- builder.SetDatasetFilesList({dataset_path_})
- .SetColumnsToLoad({"image", "label", "A", "B"})
- .SetRowsPerBuffer(2)
- .SetWorkerConnectorSize(2)
- .SetNumWorkers(2);
-
- std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
- schema->LoadSchemaFile(schema_path_, {});
- builder.SetDataSchema(std::move(schema));
-
- Status rc = builder.Build(&my_tfreader_op);
- EXPECT_TRUE(rc.IsOk());
- return my_tfreader_op;
- }
-
- std::shared_ptr<ExecutionTree> my_tree_;
-
- private:
- std::string dataset_path_;
- std::string schema_path_;
- };
-
- std::shared_ptr<ImageFolderOp> ImageFolder(int64_t num_works, int64_t rows, int64_t conns, std::string path,
- bool shuf = false, std::shared_ptr<SamplerRT> sampler = nullptr,
- std::map<std::string, int32_t> map = {}, bool decode = false);
-
- std::shared_ptr<ExecutionTree> Build(std::vector<std::shared_ptr<DatasetOp>> ops);
-
- // TestAsMap scenario:
- // TFReaderOp reads a dataset that have column ordering |image|label|A|B|.
- // A TensorOp that does nothing picks the "image" column and produces a column named "X".
- // Thus, based on the new MapOp behaviour, the column ordering will be |X|label|A|B|.
- // Verify that the "image" column is removed and "X" column is added.
- TEST_F(MindDataTestMapOp, TestAsMap) {
- Status rc;
- MS_LOG(INFO) << "Doing TestAsMap.";
-
- // Note: The above TFReader config yields 5 buffers, each with 2 rows, for a total of 10 rows.
- auto my_tfreader_op = this->CreateTFReaderOp();
- rc = my_tree_->AssociateNode(my_tfreader_op);
- EXPECT_TRUE(rc.IsOk());
- auto my_no_op = std::make_shared<mindspore::dataset::test::NoOp>();
- std::vector<std::shared_ptr<TensorOp>> my_func_list;
- my_func_list.push_back(my_no_op);
- std::shared_ptr<MapOp> my_map_op;
- MapOp::Builder builder;
- builder.SetInColNames({"image"}).SetOutColNames({"X"}).SetTensorFuncs(std::move(my_func_list)).SetNumWorkers(1);
- rc = builder.Build(&my_map_op);
- rc = my_tree_->AssociateNode(my_map_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_map_op->AddChild(my_tfreader_op);
- EXPECT_TRUE(rc.IsOk());
-
- // Assign the tree root
- rc = my_tree_->AssignRoot(my_map_op);
- EXPECT_TRUE(rc.IsOk());
-
- // Now prepare the tree
- rc = my_tree_->Prepare();
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->Launch();
- EXPECT_TRUE(rc.IsOk());
-
- // Start the loop of reading tensors from our pipeline
- DatasetIterator di(my_tree_);
- TensorMap tensor_map;
- rc = di.GetNextAsMap(&tensor_map);
- EXPECT_TRUE(rc.IsOk());
- EXPECT_EQ(tensor_map.size(), 4);
- EXPECT_EQ(tensor_map.find("image"), tensor_map.end());
- EXPECT_NE(tensor_map.find("label"), tensor_map.end());
- EXPECT_NE(tensor_map.find("X"), tensor_map.end());
- EXPECT_NE(tensor_map.find("A"), tensor_map.end());
- EXPECT_NE(tensor_map.find("B"), tensor_map.end());
- }
-
- // Test3to1 scenario:
- // TFReaderOp reads a dataset that have column ordering |image|label|A|B|.
- // A 3-to-1 TensorOp picks the columns [image, A, B] and produce a column named "X".
- // Thus, based on the new MapOp behaviour, the column ordering will be |X|label|.
- // Verify that the only columns "X" and "label" exist.
- TEST_F(MindDataTestMapOp, Test3to1) {
- Status rc;
- MS_LOG(INFO) << "Doing Test3to1.";
-
- // Note: The above TFReader config yields 5 buffers, each with 2 rows, for a total of 10 rows.
- auto my_tfreader_op = this->CreateTFReaderOp();
- rc = my_tree_->AssociateNode(my_tfreader_op);
- EXPECT_TRUE(rc.IsOk());
- auto my_op = std::make_shared<mindspore::dataset::test::ThreeToOneOp>();
- std::vector<std::shared_ptr<TensorOp>> my_func_list;
- my_func_list.push_back(my_op);
- std::shared_ptr<MapOp> my_map_op;
- MapOp::Builder builder;
- builder.SetInColNames({"image", "A", "B"})
- .SetOutColNames({"X"})
- .SetTensorFuncs(std::move(my_func_list))
- .SetNumWorkers(1);
- rc = builder.Build(&my_map_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->AssociateNode(my_map_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_map_op->AddChild(my_tfreader_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->AssignRoot(my_map_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->Prepare();
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->Launch();
- EXPECT_TRUE(rc.IsOk());
-
- // Start the loop of reading tensors from our pipeline
- DatasetIterator di(my_tree_);
- TensorMap tensor_map;
- rc = di.GetNextAsMap(&tensor_map);
- EXPECT_TRUE(rc.IsOk());
- while (!tensor_map.empty()) {
- EXPECT_EQ(tensor_map.size(), 2);
- EXPECT_EQ(tensor_map.find("image"), tensor_map.end());
- EXPECT_NE(tensor_map.find("label"), tensor_map.end());
- EXPECT_NE(tensor_map.find("X"), tensor_map.end());
- EXPECT_EQ(tensor_map.find("A"), tensor_map.end());
- EXPECT_EQ(tensor_map.find("B"), tensor_map.end());
- rc = di.GetNextAsMap(&tensor_map);
- EXPECT_TRUE(rc.IsOk());
- }
- }
-
- // Test1to3 scenario:
- // TFReaderOp reads a dataset that have column ordering |image|label|A|B|.
- // A 1-to-3 TensorOp picks the columns [image] and produce a column named [X, Y, Z].
- // Thus, based on the new MapOp behaviour, the column ordering will be |X|Y|Z|label|A|B|.
- // Verify that the only columns X, Y, Z are added (to the front) and followed by columns label, A, B..
- TEST_F(MindDataTestMapOp, Test1to3) {
- Status rc;
- MS_LOG(INFO) << "Doing Test1to3.";
-
- // Note: The above TFReader config yields 5 buffers, each with 2 rows, for a total of 10 rows.
- auto my_tfreader_op = this->CreateTFReaderOp();
- rc = my_tree_->AssociateNode(my_tfreader_op);
- EXPECT_TRUE(rc.IsOk());
- auto my_op = std::make_shared<mindspore::dataset::test::OneToThreeOp>();
- std::vector<std::shared_ptr<TensorOp>> my_func_list;
- my_func_list.push_back(my_op);
- std::shared_ptr<MapOp> my_map_op;
- MapOp::Builder builder;
- builder.SetInColNames({"image"})
- .SetOutColNames({"X", "Y", "Z"})
- .SetTensorFuncs(std::move(my_func_list))
- .SetNumWorkers(1);
-
- // ProjectOp
- std::vector<std::string> columns_to_project = {"X", "Y", "Z", "label", "A", "B"};
- std::shared_ptr<ProjectOp> my_project_op = std::make_shared<ProjectOp>(columns_to_project);
- rc = my_tree_->AssociateNode(my_project_op);
- ASSERT_TRUE(rc.IsOk());
-
- rc = my_tree_->AssignRoot(my_project_op);
- ASSERT_TRUE(rc.IsOk());
-
- rc = builder.Build(&my_map_op);
- rc = my_tree_->AssociateNode(my_map_op);
- EXPECT_TRUE(rc.IsOk());
-
- rc = my_project_op->AddChild(my_map_op);
- EXPECT_TRUE(rc.IsOk());
-
- rc = my_map_op->AddChild(my_tfreader_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->Prepare();
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->Launch();
- EXPECT_TRUE(rc.IsOk());
-
- // Start the loop of reading tensors from our pipeline
- DatasetIterator di(my_tree_);
- TensorMap tensor_map;
- rc = di.GetNextAsMap(&tensor_map);
- EXPECT_TRUE(rc.IsOk());
- EXPECT_EQ(tensor_map.size(), 6);
- EXPECT_EQ(tensor_map.find("image"), tensor_map.end());
- EXPECT_NE(tensor_map.find("label"), tensor_map.end());
- EXPECT_NE(tensor_map.find("A"), tensor_map.end());
- EXPECT_NE(tensor_map.find("B"), tensor_map.end());
- EXPECT_NE(tensor_map.find("X"), tensor_map.end());
- EXPECT_NE(tensor_map.find("Y"), tensor_map.end());
- EXPECT_NE(tensor_map.find("Z"), tensor_map.end());
-
- // Getting the next row as vector (by position).
- TensorRow tensor_list;
- rc = di.FetchNextTensorRow(&tensor_list);
- EXPECT_TRUE(rc.IsOk());
-
- // Based on the schema file, create the golden result to compare with.
- std::vector<DataType::Type> golden_types({DataType::Type::DE_UINT8, DataType::Type::DE_UINT8,
- DataType::Type::DE_UINT8, DataType::Type::DE_INT64,
- DataType::Type::DE_FLOAT32, DataType::Type::DE_INT64});
-
- std::vector<uint64_t> golden_ranks({3, 3, 3, 1, 4, 1});
-
- std::vector<TensorShape> golden_shapes({TensorShape({3, 4, 2}), TensorShape({3, 4, 2}), TensorShape({3, 4, 2}),
- TensorShape({7}), TensorShape({1, 13, 14, 12}), TensorShape({9})});
-
- while (!tensor_list.empty()) {
- for (uint32_t i = 0; i < tensor_list.size(); i++) {
- EXPECT_EQ(tensor_list[i]->type(), golden_types[i]);
- EXPECT_EQ(tensor_list[i]->Rank(), golden_ranks[i]);
- EXPECT_EQ(tensor_list[i]->shape(), golden_shapes[i]);
- EXPECT_NE(tensor_list[i]->GetBuffer(), nullptr);
- }
- rc = di.FetchNextTensorRow(&tensor_list);
- EXPECT_TRUE(rc.IsOk());
- }
- }
-
- // TestMultiTensorOp scenario:
- // TFReaderOp reads a dataset that have column ordering |image|label|A|B|.
- // A series of 3-to-1 and 1-to-3 TensorOps are applied to [image, A, B] and
- // produce final output columns [X, Y, Z].
- // Based on the new MapOp behaviour, the column ordering will be |X|Y|Z|label|.
- TEST_F(MindDataTestMapOp, TestMultiTensorOp) {
- Status rc;
- MS_LOG(INFO) << "Doing TestMultiTensorOp.";
-
- // Note: The above TFReader config yields 5 buffers, each with 2 rows, for a total of 10 rows.
- auto my_tfreader_op = this->CreateTFReaderOp();
- rc = my_tree_->AssociateNode(my_tfreader_op);
- EXPECT_TRUE(rc.IsOk());
- auto my_op1 = std::make_shared<mindspore::dataset::test::ThreeToOneOp>();
- auto my_op2 = std::make_shared<mindspore::dataset::test::OneToThreeOp>();
- std::vector<std::shared_ptr<TensorOp>> my_func_list;
- my_func_list.push_back(my_op1);
- my_func_list.push_back(my_op2);
- std::shared_ptr<MapOp> my_map_op;
- MapOp::Builder builder;
- builder.SetInColNames({"image", "A", "B"})
- .SetOutColNames({"X", "Y", "Z"})
- .SetTensorFuncs(std::move(my_func_list))
- .SetNumWorkers(1);
- rc = builder.Build(&my_map_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->AssociateNode(my_map_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_map_op->AddChild(my_tfreader_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->AssignRoot(my_map_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->Prepare();
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->Launch();
- EXPECT_TRUE(rc.IsOk());
-
- // Start the loop of reading tensors from our pipeline
- DatasetIterator di(my_tree_);
- TensorMap tensor_map;
- rc = di.GetNextAsMap(&tensor_map);
- EXPECT_TRUE(rc.IsOk());
- while (!tensor_map.empty()) {
- EXPECT_EQ(tensor_map.size(), 4);
- EXPECT_EQ(tensor_map.find("image"), tensor_map.end());
- EXPECT_EQ(tensor_map.find("A"), tensor_map.end());
- EXPECT_EQ(tensor_map.find("B"), tensor_map.end());
- EXPECT_NE(tensor_map.find("label"), tensor_map.end());
- EXPECT_NE(tensor_map.find("X"), tensor_map.end());
- EXPECT_NE(tensor_map.find("Y"), tensor_map.end());
- EXPECT_NE(tensor_map.find("Z"), tensor_map.end());
-
- // XYZ are Tensor shared_ptr to image, so it should have the same shape as image column.
- EXPECT_EQ(tensor_map["X"]->shape(), TensorShape({3, 4, 2}));
- EXPECT_EQ(tensor_map["Y"]->shape(), TensorShape({3, 4, 2}));
- EXPECT_EQ(tensor_map["Z"]->shape(), TensorShape({3, 4, 2}));
- rc = di.GetNextAsMap(&tensor_map);
- EXPECT_TRUE(rc.IsOk());
- }
- }
-
- TEST_F(MindDataTestMapOp, TestTFReaderRepeatMap) {
- Status rc;
- MS_LOG(INFO) << "Doing TestTFReaderRepeatMap.";
- uint32_t num_repeats = 3;
-
- // Note: The above TFReader config yields 5 buffers, each with 2 rows, for a total
- // of 10 rows.
- auto my_tfreader_op = this->CreateTFReaderOp();
- rc = my_tree_->AssociateNode(my_tfreader_op);
- EXPECT_TRUE(rc.IsOk());
- auto my_no_op = std::make_shared<mindspore::dataset::test::NoOp>();
- std::vector<std::shared_ptr<TensorOp>> my_func_list;
- my_func_list.push_back(my_no_op);
-
- std::shared_ptr<RepeatOp> my_repeat_op;
- rc = RepeatOp::Builder(num_repeats).Build(&my_repeat_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->AssociateNode(my_repeat_op);
- EXPECT_TRUE(rc.IsOk());
-
- std::shared_ptr<MapOp> my_map_op;
- MapOp::Builder builder;
- builder.SetInColNames({"label"}).SetOutColNames({}).SetTensorFuncs(std::move(my_func_list)).SetNumWorkers(5);
- rc = builder.Build(&my_map_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->AssociateNode(my_map_op);
- EXPECT_TRUE(rc.IsOk());
-
- rc = my_map_op->AddChild(my_repeat_op);
- EXPECT_TRUE(rc.IsOk());
-
- rc = my_repeat_op->AddChild(my_tfreader_op);
- EXPECT_TRUE(rc.IsOk());
-
- rc = my_tree_->AssignRoot(my_map_op);
- EXPECT_TRUE(rc.IsOk());
-
- rc = my_tree_->Prepare();
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->Launch();
- EXPECT_TRUE(rc.IsOk());
-
- // Start the loop of reading tensors from our pipeline
- DatasetIterator di(my_tree_);
- TensorRow tensor_list;
- rc = di.FetchNextTensorRow(&tensor_list);
- EXPECT_TRUE(rc.IsOk());
- EXPECT_EQ(tensor_list.size(), 4);
- uint32_t row_count = 0;
- while (!tensor_list.empty()) {
- row_count++;
- MS_LOG(INFO) << "row_count: " << row_count << ".";
- rc = di.FetchNextTensorRow(&tensor_list);
- EXPECT_TRUE(rc.IsOk());
- }
- ASSERT_EQ(row_count, 10 * num_repeats);
- }
-
- TEST_F(MindDataTestMapOp, TestTFReaderMapRepeat) {
- Status rc;
- MS_LOG(INFO) << "Doing TestTFReaderMapRepeat.";
- uint32_t num_repeats = 3;
-
- // Note: The above TFReader config yields 5 buffers, each with 2 rows, for a total
- // of 10 rows.
- auto my_tfreader_op = this->CreateTFReaderOp();
- rc = my_tree_->AssociateNode(my_tfreader_op);
- EXPECT_TRUE(rc.IsOk());
- auto my_no_op = std::make_shared<mindspore::dataset::test::NoOp>();
- std::vector<std::shared_ptr<TensorOp>> my_func_list;
- my_func_list.push_back(my_no_op);
-
- std::shared_ptr<RepeatOp> my_repeat_op;
- rc = RepeatOp::Builder(num_repeats).Build(&my_repeat_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->AssociateNode(my_repeat_op);
- EXPECT_TRUE(rc.IsOk());
-
- std::shared_ptr<MapOp> my_map_op;
- MapOp::Builder builder;
- builder.SetInColNames({"label"}).SetOutColNames({}).SetTensorFuncs(std::move(my_func_list)).SetNumWorkers(50);
- rc = builder.Build(&my_map_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->AssociateNode(my_map_op);
- EXPECT_TRUE(rc.IsOk());
-
- rc = my_repeat_op->AddChild(my_map_op);
- EXPECT_TRUE(rc.IsOk());
-
- rc = my_map_op->AddChild(my_tfreader_op);
- EXPECT_TRUE(rc.IsOk());
-
- rc = my_tree_->AssignRoot(my_repeat_op);
- EXPECT_TRUE(rc.IsOk());
-
- rc = my_tree_->Prepare();
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->Launch();
- EXPECT_TRUE(rc.IsOk());
-
- // Start the loop of reading tensors from our pipeline
- DatasetIterator di(my_tree_);
- TensorRow tensor_list;
- rc = di.FetchNextTensorRow(&tensor_list);
- EXPECT_TRUE(rc.IsOk());
- EXPECT_EQ(tensor_list.size(), 4);
- uint32_t row_count = 0;
- while (!tensor_list.empty()) {
- row_count++;
- MS_LOG(INFO) << "row_count: " << row_count << ".";
- rc = di.FetchNextTensorRow(&tensor_list);
- EXPECT_TRUE(rc.IsOk());
- }
- ASSERT_EQ(row_count, 10 * num_repeats);
- }
-
- TEST_F(MindDataTestMapOp, TFReader_Decode_Repeat_Resize) {
- Status rc;
- MS_LOG(INFO) << "Doing TFReader_Decode_Repeat_Resize.";
- uint32_t num_repeats = 2;
-
- std::string dataset_path_ = datasets_root_path_ + "/" + "test_tf_file_3_images/train-0000-of-0001.data";
- std::shared_ptr<TFReaderOp> my_tfreader_op;
- TFReaderOp::Builder sobuilder;
- sobuilder.SetDatasetFilesList({dataset_path_})
- .SetColumnsToLoad({"image", "label"})
- .SetRowsPerBuffer(2)
- .SetWorkerConnectorSize(2)
- .SetNumWorkers(2);
- rc = sobuilder.Build(&my_tfreader_op);
- EXPECT_TRUE(rc.IsOk());
-
- rc = my_tree_->AssociateNode(my_tfreader_op);
- EXPECT_TRUE(rc.IsOk());
- auto decode_op = std::make_shared<DecodeOp>();
- std::vector<std::shared_ptr<TensorOp>> my_func_list;
- my_func_list.push_back(decode_op);
-
- std::shared_ptr<RepeatOp> my_repeat_op;
- rc = RepeatOp::Builder(num_repeats).Build(&my_repeat_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->AssociateNode(my_repeat_op);
- EXPECT_TRUE(rc.IsOk());
-
- std::shared_ptr<MapOp> my_map_decode_op;
- MapOp::Builder builder;
- builder.SetInColNames({"image"}).SetOutColNames({}).SetTensorFuncs(std::move(my_func_list)).SetNumWorkers(4);
- rc = builder.Build(&my_map_decode_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->AssociateNode(my_map_decode_op);
- EXPECT_TRUE(rc.IsOk());
-
- auto resize_op = std::make_shared<ResizeOp>(300, 300);
- std::vector<std::shared_ptr<TensorOp>> my_func_list2;
- my_func_list2.push_back(resize_op);
- std::shared_ptr<MapOp> my_map_resize_op;
- MapOp::Builder builder2;
- builder2.SetInColNames({"image"}).SetOutColNames({}).SetTensorFuncs(std::move(my_func_list2)).SetNumWorkers(5);
- rc = builder2.Build(&my_map_resize_op);
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->AssociateNode(my_map_resize_op);
- EXPECT_TRUE(rc.IsOk());
-
- rc = my_map_decode_op->AddChild(my_tfreader_op);
- EXPECT_TRUE(rc.IsOk());
-
- rc = my_repeat_op->AddChild(my_map_decode_op);
- EXPECT_TRUE(rc.IsOk());
-
- rc = my_map_resize_op->AddChild(my_repeat_op);
- EXPECT_TRUE(rc.IsOk());
-
- rc = my_tree_->AssignRoot(my_map_resize_op);
- EXPECT_TRUE(rc.IsOk());
-
- rc = my_tree_->Prepare();
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->Launch();
- EXPECT_TRUE(rc.IsOk());
-
- // Start the loop of reading tensors from our pipeline
- DatasetIterator di(my_tree_);
- TensorRow tensor_list;
- rc = di.FetchNextTensorRow(&tensor_list);
- EXPECT_TRUE(rc.IsOk());
- EXPECT_EQ(tensor_list.size(), 2);
- uint32_t row_count = 0;
- while (!tensor_list.empty()) {
- row_count++;
- rc = di.FetchNextTensorRow(&tensor_list);
- EXPECT_TRUE(rc.IsOk());
- }
-
- ASSERT_EQ(row_count, 6);
- }
-
- TEST_F(MindDataTestMapOp, ImageFolder_Decode_Repeat_Resize) {
- Status rc;
- MS_LOG(INFO) << "Doing ImageFolder_Decode_Repeat_Resize.";
-
- std::string folder_path = datasets_root_path_ + "/testPK/data";
-
- uint32_t num_repeats = 2;
- std::shared_ptr<RepeatOp> repeat_op;
- rc = RepeatOp::Builder(num_repeats).Build(&repeat_op);
- EXPECT_TRUE(rc.IsOk());
-
- auto decode_op = std::make_shared<DecodeOp>();
- std::vector<std::shared_ptr<TensorOp>> func_list;
- func_list.push_back(decode_op);
-
- std::shared_ptr<MapOp> map_decode_map;
- MapOp::Builder map_decode_builder;
- map_decode_builder.SetInColNames({"image"}).SetOutColNames({}).SetTensorFuncs(func_list).SetNumWorkers(4);
- rc = map_decode_builder.Build(&map_decode_map);
- EXPECT_TRUE(rc.IsOk());
-
- auto resize_op = std::make_shared<ResizeOp>(300, 300);
- std::vector<std::shared_ptr<TensorOp>> func_list2;
- func_list2.push_back(resize_op);
- std::shared_ptr<MapOp> map_resize_op;
- MapOp::Builder map_resize_builder;
- map_resize_builder.SetInColNames({"image"}).SetOutColNames({}).SetTensorFuncs(func_list2).SetNumWorkers(5);
- rc = map_resize_builder.Build(&map_resize_op);
- EXPECT_TRUE(rc.IsOk());
-
- my_tree_ = Build({ImageFolder(16, 2, 32, folder_path, false), map_decode_map, repeat_op, map_resize_op});
- rc = my_tree_->Prepare();
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->Launch();
- EXPECT_TRUE(rc.IsOk());
-
- // Start the loop of reading tensors from our pipeline
- DatasetIterator di(my_tree_);
- TensorMap tensor_map;
- di.GetNextAsMap(&tensor_map);
- EXPECT_TRUE(rc.IsOk());
- uint64_t i = 0;
- int32_t label = 0;
- int32_t img_class[] = {0, 1, 2, 3};
- std::string result;
- while (tensor_map.size() != 0) {
- tensor_map["label"]->GetItemAt<int32_t>(&label, {});
- MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
- EXPECT_TRUE(img_class[(i % 44) / 11] == label);
- // Dump all the image into string, to be used as a comparison later.
- result.append((char *)tensor_map["image"]->GetBuffer(), (int64_t)tensor_map["image"]->Size());
- di.GetNextAsMap(&tensor_map);
- i++;
- }
- EXPECT_TRUE(i == 88);
-
- // Part-2 : creating mapop with performance mode = false, to check if the result is the same
- // as when performance mode = true.
- rc = RepeatOp::Builder(num_repeats).Build(&repeat_op);
- EXPECT_TRUE(rc.IsOk());
-
- map_decode_builder.SetInColNames({"image"})
- .SetOutColNames({})
- .SetTensorFuncs(func_list)
- .SetNumWorkers(14);
- rc = map_decode_builder.Build(&map_decode_map);
- EXPECT_TRUE(rc.IsOk());
-
- map_resize_builder.SetInColNames({"image"})
- .SetOutColNames({})
- .SetTensorFuncs(func_list2)
- .SetNumWorkers(15);
- rc = map_resize_builder.Build(&map_resize_op);
- EXPECT_TRUE(rc.IsOk());
-
- auto my_tree_2 = Build({ImageFolder(16, 2, 32, folder_path, false), map_decode_map, repeat_op, map_resize_op});
-
- rc = my_tree_2->Prepare();
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_2->Launch();
- EXPECT_TRUE(rc.IsOk());
-
- // Start the loop of reading tensors from our pipeline
- DatasetIterator di2(my_tree_2);
- di2.GetNextAsMap(&tensor_map);
- EXPECT_TRUE(rc.IsOk());
- i = 0;
- label = 0;
- std::string result2;
- while (tensor_map.size() != 0) {
- tensor_map["label"]->GetItemAt<int32_t>(&label, {});
- MS_LOG(DEBUG) << "row:" << i << "\tlabel:" << label << "\n";
- EXPECT_TRUE(img_class[(i % 44) / 11] == label);
- result2.append((char *)tensor_map["image"]->GetBuffer(), (int64_t)tensor_map["image"]->Size());
- di2.GetNextAsMap(&tensor_map);
- i++;
- }
- EXPECT_TRUE(i == 88);
-
- EXPECT_EQ(result.size(), result2.size());
- EXPECT_EQ(result, result2);
- }
-
- TEST_F(MindDataTestMapOp, ImageFolder_Decode_Repeat_Resize_NoInputColumns) {
- Status rc;
- MS_LOG(INFO) << "Doing ImageFolder_Decode_Repeat_Resize_NoInputColumns.";
-
- std::string folder_path = datasets_root_path_ + "/testPK/data";
-
- uint32_t num_repeats = 2;
- std::shared_ptr<RepeatOp> repeat_op;
- rc = RepeatOp::Builder(num_repeats).Build(&repeat_op);
- EXPECT_TRUE(rc.IsOk());
-
- auto decode_op = std::make_shared<DecodeOp>();
- std::vector<std::shared_ptr<TensorOp>> func_list;
- func_list.push_back(decode_op);
-
- std::shared_ptr<MapOp> map_decode_map;
- MapOp::Builder map_decode_builder;
- map_decode_builder.SetInColNames({}).SetOutColNames({}).SetTensorFuncs(func_list).SetNumWorkers(4);
- rc = map_decode_builder.Build(&map_decode_map);
- EXPECT_TRUE(rc.IsOk());
-
- auto resize_op = std::make_shared<ResizeOp>(300, 300);
- std::vector<std::shared_ptr<TensorOp>> func_list2;
- func_list2.push_back(resize_op);
- std::shared_ptr<MapOp> map_resize_op;
- MapOp::Builder map_resize_builder;
- map_resize_builder.SetTensorFuncs(func_list2).SetNumWorkers(5);
- rc = map_resize_builder.Build(&map_resize_op);
- EXPECT_TRUE(rc.IsOk());
-
- my_tree_ = Build({ImageFolder(16, 2, 32, folder_path, false), map_decode_map, repeat_op, map_resize_op});
- rc = my_tree_->Prepare();
- EXPECT_TRUE(rc.IsOk());
- rc = my_tree_->Launch();
- EXPECT_TRUE(rc.IsOk());
-
- // Start the loop of reading tensors from our pipeline
- DatasetIterator di(my_tree_);
- TensorMap tensor_map;
- di.GetNextAsMap(&tensor_map);
- EXPECT_TRUE(rc.IsOk());
- uint64_t i = 0;
- int32_t label = 0;
- int32_t img_class[] = {0, 1, 2, 3};
- std::string result;
- while (tensor_map.size() != 0) {
- tensor_map["label"]->GetItemAt<int32_t>(&label, {});
- EXPECT_TRUE(img_class[(i % 44) / 11] == label);
- di.GetNextAsMap(&tensor_map);
- i++;
- }
- EXPECT_TRUE(i == 88);
- }
|