You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

c_api_dataset_save.cc 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Copyright 2020-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 <stdio.h>
  17. #include "common/common.h"
  18. #include "minddata/dataset/include/dataset/datasets.h"
  19. #include "minddata/dataset/include/dataset/transforms.h"
  20. using namespace mindspore::dataset;
  21. class MindDataTestPipeline : public UT::DatasetOpTesting {
  22. protected:
  23. };
  24. TEST_F(MindDataTestPipeline, TestSaveCifar10AndLoad) {
  25. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSaveCifar10AndLoad(single mindrecord file).";
  26. // Stage 1: load original dataset
  27. // Create a Cifar10 Dataset
  28. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  29. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<SequentialSampler>(0, 10));
  30. EXPECT_NE(ds, nullptr);
  31. // Create an iterator over the result of the above dataset
  32. // This will trigger the creation of the Execution Tree and launch it.
  33. std::shared_ptr<Iterator> iter = ds->CreateIterator();
  34. EXPECT_NE(iter, nullptr);
  35. // Iterate the dataset and get each row
  36. std::unordered_map<std::string, mindspore::MSTensor> row;
  37. std::vector<mindspore::MSTensor> original_data;
  38. ASSERT_OK(iter->GetNextRow(&row));
  39. // Save original data for comparison
  40. uint64_t i = 0;
  41. while (row.size() != 0) {
  42. auto label = row["label"];
  43. original_data.push_back(label);
  44. TEST_MS_LOG_MSTENSOR(INFO, "Tensor label: ", label);
  45. ASSERT_OK(iter->GetNextRow(&row));
  46. i++;
  47. }
  48. // Expect 10 samples
  49. EXPECT_EQ(i, 10);
  50. // Manually terminate the pipeline
  51. iter->Stop();
  52. // Stage 2: Save data processed by the dataset pipeline
  53. // Create an iterator over the result of the above dataset
  54. // This will trigger the creation of the Execution Tree and launch it.
  55. std::string temp_file = datasets_root_path_ + "/testCifar10Data/mind.mind";
  56. std::string temp_file_db = datasets_root_path_ + "/testCifar10Data/mind.mind.db";
  57. bool rc = ds->Save(temp_file);
  58. // if save fails, no need to continue the execution
  59. // save could fail if temp_file already exists
  60. ASSERT_EQ(rc, true);
  61. // Stage 3: Load dataset from file output by stage 2
  62. // Create a MindData Dataset
  63. std::shared_ptr<Dataset> ds_minddata = MindData(temp_file, {}, std::make_shared<SequentialSampler>(0, 10));
  64. // Create objects for the tensor ops
  65. // uint32 will be casted to int64 implicitly in mindrecord file, so we have to cast it back to uint32
  66. std::shared_ptr<TensorTransform> type_cast =
  67. std::make_shared<transforms::TypeCast>(mindspore::DataType::kNumberTypeUInt32);
  68. EXPECT_NE(type_cast, nullptr);
  69. // Create a Map operation on ds
  70. ds_minddata = ds_minddata->Map({type_cast}, {"label"});
  71. EXPECT_NE(ds_minddata, nullptr);
  72. // Create an iterator over the result of the above dataset
  73. // This will trigger the creation of the Execution Tree and launch it.
  74. std::shared_ptr<Iterator> iter_minddata = ds_minddata->CreateIterator();
  75. EXPECT_NE(iter_minddata, nullptr);
  76. // Iterate the dataset and get each row
  77. std::unordered_map<std::string, mindspore::MSTensor> row_minddata;
  78. ASSERT_OK(iter_minddata->GetNextRow(&row_minddata));
  79. // Check column name for each row
  80. EXPECT_NE(row_minddata.find("image"), row_minddata.end());
  81. EXPECT_NE(row_minddata.find("label"), row_minddata.end());
  82. // Expect the output data is same with original_data
  83. uint64_t j = 0;
  84. while (row_minddata.size() != 0) {
  85. auto label = row_minddata["label"];
  86. EXPECT_MSTENSOR_EQ(original_data[j], label);
  87. TEST_MS_LOG_MSTENSOR(INFO, "Tensor label: ", label);
  88. ASSERT_OK(iter_minddata->GetNextRow(&row_minddata));
  89. j++;
  90. }
  91. // Expect 10 samples
  92. EXPECT_EQ(j, 10);
  93. // Manually terminate the pipeline
  94. iter_minddata->Stop();
  95. // Delete temp file
  96. EXPECT_EQ(remove(temp_file.c_str()), 0);
  97. EXPECT_EQ(remove(temp_file_db.c_str()), 0);
  98. }
  99. TEST_F(MindDataTestPipeline, TestSaveFail) {
  100. MS_LOG(INFO) << "Doing MindDataTestPipeline-TestSaveFail with incorrect param.";
  101. // Create a Cifar10 Dataset
  102. std::string folder_path = datasets_root_path_ + "/testCifar10Data/";
  103. std::shared_ptr<Dataset> ds = Cifar10(folder_path, "all", std::make_shared<SequentialSampler>(0, 10));
  104. EXPECT_NE(ds, nullptr);
  105. // fail with invalid dataset_path
  106. std::string temp_file1 = "";
  107. bool rc1 = ds->Save(temp_file1);
  108. EXPECT_EQ(rc1, false);
  109. // fail with invalid dataset_path
  110. std::string temp_file2 = datasets_root_path_ + "/testCifar10Data/";
  111. bool rc2 = ds->Save(temp_file2);
  112. EXPECT_EQ(rc2, false);
  113. // fail with invalid num_files
  114. std::string temp_file3 = datasets_root_path_ + "/testCifar10Data/mind.mind";
  115. bool rc3 = ds->Save(temp_file3, 0);
  116. EXPECT_EQ(rc3, false);
  117. // fail with invalid num_files
  118. std::string temp_file4 = datasets_root_path_ + "/testCifar10Data/mind.mind";
  119. bool rc4 = ds->Save(temp_file4, 1001);
  120. EXPECT_EQ(rc4, false);
  121. // fail with invalid dataset_type
  122. std::string temp_file5 = datasets_root_path_ + "/testCifar10Data/mind.mind";
  123. bool rc5 = ds->Save(temp_file5, 5, "tfrecord");
  124. EXPECT_EQ(rc5, false);
  125. }