Browse Source

Fix construtor Execute with pointers

feature/build-system-rewrite
YangLuo 4 years ago
parent
commit
ba79c3c7cb
2 changed files with 102 additions and 6 deletions
  1. +6
    -6
      mindspore/ccsrc/minddata/dataset/api/execute.cc
  2. +96
    -0
      tests/ut/cpp/dataset/execute_test.cc

+ 6
- 6
mindspore/ccsrc/minddata/dataset/api/execute.cc View File

@@ -100,10 +100,10 @@ Execute::Execute(const std::reference_wrapper<TensorTransform> &op, MapTargetDev
// Execute function for the example case: auto decode(new vision::Decode());
Execute::Execute(TensorTransform *op, MapTargetDevice device_type, uint32_t device_id) {
// Initialize the transforms_ and other context
std::shared_ptr<TensorTransform> smart_ptr_op(op);
transforms_.emplace_back(smart_ptr_op);
ops_.emplace_back(op->Parse());

info_ = std::make_shared<ExtraInfo>();
info_->init_with_shared_ptr_ = false;
device_type_ = device_type;
(void)InitResource(device_type, device_id);
}
@@ -147,12 +147,12 @@ Execute::Execute(const std::vector<std::reference_wrapper<TensorTransform>> &ops
// Execute function for the example vector case: auto decode(new vision::Decode());
Execute::Execute(const std::vector<TensorTransform *> &ops, MapTargetDevice device_type, uint32_t device_id) {
// Initialize the transforms_ and other context
for (auto &op : ops) {
std::shared_ptr<TensorTransform> smart_ptr_op(op);
transforms_.emplace_back(smart_ptr_op);
}
(void)std::transform(
ops.begin(), ops.end(), std::back_inserter(ops_),
[](TensorTransform *operation) -> std::shared_ptr<TensorOperation> { return operation->Parse(); });

info_ = std::make_shared<ExtraInfo>();
info_->init_with_shared_ptr_ = false;
device_type_ = device_type;
(void)InitResource(device_type, device_id);
}


+ 96
- 0
tests/ut/cpp/dataset/execute_test.cc View File

@@ -2289,3 +2289,99 @@ TEST_F(MindDataTestExecute, TestSpectralCentroidWithWrongArg) {
Status rc = transform({input_tensor}, &input_tensor);
EXPECT_FALSE(rc.IsOk());
}

/// Feature: Execute Construct Demo1
/// Description: demonstrate how to construct a Execute
/// Expectation: Construct Execute object and run
TEST_F(MindDataTestExecute, TestConstructorDemo1) {
MS_LOG(INFO) << "Doing MindDataTestExecute-TestConstructorDemo1.";
// Read images
mindspore::MSTensor image = ReadFileToTensor("data/dataset/apple.jpg");
mindspore::MSTensor aug_image;

// Pass Transformation object
auto decode = vision::Decode();
auto resize = vision::Resize({66, 77});
auto transform = Execute({decode, resize});

Status rc = transform(image, &aug_image);
EXPECT_EQ(rc, Status::OK());

EXPECT_EQ(aug_image.Shape()[0], 66);
EXPECT_EQ(aug_image.Shape()[1], 77);
}

/// Feature: Execute Construct Demo2
/// Description: demonstrate how to construct a Execute
/// Expectation: Construct Execute object and run
TEST_F(MindDataTestExecute, TestConstructorDemo2) {
MS_LOG(INFO) << "Doing MindDataTestExecute-TestConstructorDemo2.";
// Read images
mindspore::MSTensor image = ReadFileToTensor("data/dataset/apple.jpg");
mindspore::MSTensor aug_image;

// Pass address of Transformation object
auto decode = vision::Decode();
auto resize = vision::Resize({66, 77});
std::vector<TensorTransform *> ops = {&decode};
bool use_resize = true;
if (use_resize) {
ops.push_back(&resize);
}

auto transform = Execute(ops);
Status rc = transform(image, &aug_image);
EXPECT_EQ(rc, Status::OK());

EXPECT_EQ(aug_image.Shape()[0], 66);
EXPECT_EQ(aug_image.Shape()[1], 77);
}

/// Feature: Execute Construct Demo3
/// Description: demonstrate how to construct a Execute
/// Expectation: Construct Execute object and run
TEST_F(MindDataTestExecute, TestConstructorDemo3) {
MS_LOG(INFO) << "Doing MindDataTestExecute-TestConstructorDemo3.";
// Read images
mindspore::MSTensor image = ReadFileToTensor("data/dataset/apple.jpg");
std::vector<mindspore::MSTensor> images = {image};
std::vector<mindspore::MSTensor> aug_images;

// Pass smart pointers of Transformation object
std::shared_ptr<TensorTransform> decode(new vision::Decode());
std::shared_ptr<TensorTransform> resize(new vision::Resize({66, 77}));
std::vector<std::shared_ptr<TensorTransform>> ops = {decode, resize};

auto transform = Execute(ops);
Status rc = transform(images, &aug_images);
EXPECT_EQ(rc, Status::OK());

EXPECT_EQ(aug_images[0].Shape()[0], 66);
EXPECT_EQ(aug_images[0].Shape()[1], 77);
}

/// Feature: Execute Construct Demo4
/// Description: demonstrate how to construct a Execute
/// Expectation: Construct Execute object and run
TEST_F(MindDataTestExecute, TestConstructorDemo4) {
MS_LOG(INFO) << "Doing MindDataTestExecute-TestConstructorDemo4.";
// Read images
mindspore::MSTensor image = ReadFileToTensor("data/dataset/apple.jpg");
std::vector<mindspore::MSTensor> images = {image};
std::vector<mindspore::MSTensor> aug_images;

// Pass raw pointers of Transformation object
auto decode = new vision::Decode();
auto resize = new vision::Resize({66, 77});
std::vector<TensorTransform *> ops = {decode, resize};

auto transform = Execute(ops);
Status rc = transform(images, &aug_images);
EXPECT_EQ(rc, Status::OK());

EXPECT_EQ(aug_images[0].Shape()[0], 66);
EXPECT_EQ(aug_images[0].Shape()[1], 77);

delete decode;
delete resize;
}

Loading…
Cancel
Save