提交 373adaa5 编写于 作者: L luoyang

!4402 [Dataset] Fix c_api bugs

上级 d3ef9f77
...@@ -397,6 +397,10 @@ bool ValidateCommonDatasetParams(std::string dataset_dir) { ...@@ -397,6 +397,10 @@ bool ValidateCommonDatasetParams(std::string dataset_dir) {
MS_LOG(ERROR) << "No dataset path is specified"; MS_LOG(ERROR) << "No dataset path is specified";
return false; return false;
} }
if (access(dataset_dir.c_str(), R_OK) == -1) {
MS_LOG(ERROR) << "No access to specified dataset path: " << dataset_dir;
return false;
}
return true; return true;
} }
...@@ -1176,8 +1180,8 @@ std::vector<std::shared_ptr<DatasetOp>> RepeatDataset::Build() { ...@@ -1176,8 +1180,8 @@ std::vector<std::shared_ptr<DatasetOp>> RepeatDataset::Build() {
} }
bool RepeatDataset::ValidateParams() { bool RepeatDataset::ValidateParams() {
if (repeat_count_ != -1 && repeat_count_ <= 0) { if (repeat_count_ <= 0 && repeat_count_ != -1) {
MS_LOG(ERROR) << "Repeat: Repeat count cannot be" << repeat_count_; MS_LOG(ERROR) << "Repeat: repeat_count should be either -1 or positive integer, repeat_count_: " << repeat_count_;
return false; return false;
} }
...@@ -1223,7 +1227,7 @@ std::vector<std::shared_ptr<DatasetOp>> SkipDataset::Build() { ...@@ -1223,7 +1227,7 @@ std::vector<std::shared_ptr<DatasetOp>> SkipDataset::Build() {
// Function to validate the parameters for SkipDataset // Function to validate the parameters for SkipDataset
bool SkipDataset::ValidateParams() { bool SkipDataset::ValidateParams() {
if (skip_count_ <= -1) { if (skip_count_ <= -1) {
MS_LOG(ERROR) << "Skip: Invalid input, skip_count: " << skip_count_; MS_LOG(ERROR) << "Skip: skip_count should not be negative, skip_count: " << skip_count_;
return false; return false;
} }
...@@ -1244,8 +1248,8 @@ std::vector<std::shared_ptr<DatasetOp>> TakeDataset::Build() { ...@@ -1244,8 +1248,8 @@ std::vector<std::shared_ptr<DatasetOp>> TakeDataset::Build() {
// Function to validate the parameters for TakeDataset // Function to validate the parameters for TakeDataset
bool TakeDataset::ValidateParams() { bool TakeDataset::ValidateParams() {
if (take_count_ < -1) { if (take_count_ < 0 && take_count_ != -1) {
MS_LOG(ERROR) << "Take: Invalid input, take_count: " << take_count_; MS_LOG(ERROR) << "Take: take_count should be either -1 or positive integer, take_count: " << take_count_;
return false; return false;
} }
......
...@@ -25,7 +25,7 @@ namespace api { ...@@ -25,7 +25,7 @@ namespace api {
void Iterator::GetNextRow(TensorMap *row) { void Iterator::GetNextRow(TensorMap *row) {
Status rc = iterator_->GetNextAsMap(row); Status rc = iterator_->GetNextAsMap(row);
if (rc.IsError()) { if (rc.IsError()) {
MS_LOG(ERROR) << "GetNextRow: Failed to get next row."; MS_LOG(ERROR) << "GetNextRow: Failed to get next row. Error status: " << rc;
row->clear(); row->clear();
} }
} }
...@@ -35,7 +35,7 @@ void Iterator::GetNextRow(TensorVec *row) { ...@@ -35,7 +35,7 @@ void Iterator::GetNextRow(TensorVec *row) {
TensorRow tensor_row; TensorRow tensor_row;
Status rc = iterator_->FetchNextTensorRow(&tensor_row); Status rc = iterator_->FetchNextTensorRow(&tensor_row);
if (rc.IsError()) { if (rc.IsError()) {
MS_LOG(ERROR) << "GetNextRow: Failed to get next row."; MS_LOG(ERROR) << "GetNextRow: Failed to get next row. Error status: " << rc;
row->clear(); row->clear();
} }
// Generate a vector as return // Generate a vector as return
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_DATASETS_H_ #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_DATASETS_H_
#define MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_DATASETS_H_ #define MINDSPORE_CCSRC_MINDDATA_DATASET_INCLUDE_DATASETS_H_
#include <unistd.h>
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <set> #include <set>
......
...@@ -437,22 +437,22 @@ TEST_F(MindDataTestPipeline, TestRepeatDefault) { ...@@ -437,22 +437,22 @@ TEST_F(MindDataTestPipeline, TestRepeatDefault) {
// Create an ImageFolder Dataset // Create an ImageFolder Dataset
std::string folder_path = datasets_root_path_ + "/testPK/data/"; std::string folder_path = datasets_root_path_ + "/testPK/data/";
std::shared_ptr <Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10)); std::shared_ptr <Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
EXPECT_NE(ds,nullptr); EXPECT_NE(ds, nullptr);
// Create a Repeat operation on ds // Create a Repeat operation on ds
// Default value of repeat count is -1, expected to repeat infinitely // Default value of repeat count is -1, expected to repeat infinitely
ds = ds->Repeat(); ds = ds->Repeat();
EXPECT_NE(ds,nullptr); EXPECT_NE(ds, nullptr);
// Create a Batch operation on ds // Create a Batch operation on ds
int32_t batch_size = 1; int32_t batch_size = 1;
ds = ds->Batch(batch_size); ds = ds->Batch(batch_size);
EXPECT_NE(ds,nullptr); EXPECT_NE(ds, nullptr);
// Create an iterator over the result of the above dataset // Create an iterator over the result of the above dataset
// This will trigger the creation of the Execution Tree and launch it. // This will trigger the creation of the Execution Tree and launch it.
std::shared_ptr <Iterator> iter = ds->CreateIterator(); std::shared_ptr <Iterator> iter = ds->CreateIterator();
EXPECT_NE(iter,nullptr); EXPECT_NE(iter, nullptr);
// Iterate the dataset and get each row // Iterate the dataset and get each row
std::unordered_map <std::string, std::shared_ptr<Tensor>> row; std::unordered_map <std::string, std::shared_ptr<Tensor>> row;
...@@ -460,14 +460,16 @@ TEST_F(MindDataTestPipeline, TestRepeatDefault) { ...@@ -460,14 +460,16 @@ TEST_F(MindDataTestPipeline, TestRepeatDefault) {
uint64_t i = 0; uint64_t i = 0;
while (row.size()!= 0) { while (row.size()!= 0) {
// manually stop // manually stop
if(i==100){break;} if (i == 100) {
break;
}
i++; i++;
auto image = row["image"]; auto image = row["image"];
MS_LOG(INFO)<< "Tensor image shape: " << image->shape(); MS_LOG(INFO)<< "Tensor image shape: " << image->shape();
iter->GetNextRow(&row); iter->GetNextRow(&row);
} }
EXPECT_EQ(i,100); EXPECT_EQ(i, 100);
// Manually terminate the pipeline // Manually terminate the pipeline
iter->Stop(); iter->Stop();
} }
...@@ -478,22 +480,22 @@ TEST_F(MindDataTestPipeline, TestRepeatOne) { ...@@ -478,22 +480,22 @@ TEST_F(MindDataTestPipeline, TestRepeatOne) {
// Create an ImageFolder Dataset // Create an ImageFolder Dataset
std::string folder_path = datasets_root_path_ + "/testPK/data/"; std::string folder_path = datasets_root_path_ + "/testPK/data/";
std::shared_ptr <Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10)); std::shared_ptr <Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
EXPECT_NE(ds,nullptr); EXPECT_NE(ds, nullptr);
// Create a Repeat operation on ds // Create a Repeat operation on ds
int32_t repeat_num = 1; int32_t repeat_num = 1;
ds = ds->Repeat(repeat_num); ds = ds->Repeat(repeat_num);
EXPECT_NE(ds,nullptr); EXPECT_NE(ds, nullptr);
// Create a Batch operation on ds // Create a Batch operation on ds
int32_t batch_size = 1; int32_t batch_size = 1;
ds = ds->Batch(batch_size); ds = ds->Batch(batch_size);
EXPECT_NE(ds,nullptr); EXPECT_NE(ds, nullptr);
// Create an iterator over the result of the above dataset // Create an iterator over the result of the above dataset
// This will trigger the creation of the Execution Tree and launch it. // This will trigger the creation of the Execution Tree and launch it.
std::shared_ptr <Iterator> iter = ds->CreateIterator(); std::shared_ptr <Iterator> iter = ds->CreateIterator();
EXPECT_NE(iter,nullptr); EXPECT_NE(iter, nullptr);
// Iterate the dataset and get each row // Iterate the dataset and get each row
std::unordered_map <std::string, std::shared_ptr<Tensor>> row; std::unordered_map <std::string, std::shared_ptr<Tensor>> row;
...@@ -506,13 +508,27 @@ TEST_F(MindDataTestPipeline, TestRepeatOne) { ...@@ -506,13 +508,27 @@ TEST_F(MindDataTestPipeline, TestRepeatOne) {
iter->GetNextRow(&row); iter->GetNextRow(&row);
} }
EXPECT_EQ(i,10); EXPECT_EQ(i, 10);
// Manually terminate the pipeline // Manually terminate the pipeline
iter->Stop(); iter->Stop();
} }
TEST_F(MindDataTestPipeline, TestRepeatFail) { TEST_F(MindDataTestPipeline, TestRepeatFail1) {
MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRepeatFail."; MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRepeatFail1.";
// Create an ImageFolder Dataset
std::string folder_path = datasets_root_path_ + "/testPK/data/";
std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 10));
EXPECT_NE(ds, nullptr);
// Create a Repeat operation on ds
int32_t repeat_num = 0;
ds = ds->Repeat(repeat_num);
EXPECT_EQ(ds, nullptr);
}
TEST_F(MindDataTestPipeline, TestRepeatFail2) {
MS_LOG(INFO) << "Doing MindDataTestPipeline-TestRepeatFail2.";
// This case is expected to fail because the repeat count is invalid (<-1 && !=0). // This case is expected to fail because the repeat count is invalid (<-1 && !=0).
// Create an ImageFolder Dataset // Create an ImageFolder Dataset
......
...@@ -133,3 +133,25 @@ TEST_F(MindDataTestPipeline, TestMnistFail1) { ...@@ -133,3 +133,25 @@ TEST_F(MindDataTestPipeline, TestMnistFail1) {
std::shared_ptr<Dataset> ds = Mnist("", RandomSampler(false, 10)); std::shared_ptr<Dataset> ds = Mnist("", RandomSampler(false, 10));
EXPECT_EQ(ds, nullptr); EXPECT_EQ(ds, nullptr);
} }
TEST_F(MindDataTestPipeline, TestImageFolderFail2) {
MS_LOG(INFO) << "Doing MindDataTestPipeline-TestImageFolderFail2.";
// Create an ImageFolder Dataset
std::string folder_path = datasets_root_path_ + "/testPK/data/";
std::shared_ptr<Dataset> ds = ImageFolder(folder_path, true, RandomSampler(false, 2), {".JGP"});
EXPECT_NE(ds, nullptr);
// Create an iterator over the result of the above dataset
// This will trigger the creation of the Execution Tree and launch it.
std::shared_ptr<Iterator> iter = ds->CreateIterator();
EXPECT_NE(iter, nullptr);
// Iterate the dataset and get each row
std::unordered_map<std::string, std::shared_ptr<Tensor>> row;
iter->GetNextRow(&row);
EXPECT_EQ(row.size(), 0);
// Manually terminate the pipeline
iter->Stop();
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册