parent
e73e9a9aee
commit
7766efd58e
@ -0,0 +1,42 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2020 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 "minddata/dataset/kernels/image/random_solarize_op.h"
|
||||||
|
#include "minddata/dataset/kernels/image/solarize_op.h"
|
||||||
|
#include "minddata/dataset/kernels/image/image_utils.h"
|
||||||
|
#include "minddata/dataset/core/cv_tensor.h"
|
||||||
|
#include "minddata/dataset/util/status.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
|
||||||
|
Status RandomSolarizeOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) {
|
||||||
|
IO_CHECK(input, output);
|
||||||
|
CHECK_FAIL_RETURN_UNEXPECTED(threshold_min_ <= threshold_max_,
|
||||||
|
"threshold_min must be smaller or equal to threshold_max.");
|
||||||
|
|
||||||
|
uint8_t threshold_min = std::uniform_int_distribution(threshold_min_, threshold_max_)(rnd_);
|
||||||
|
uint8_t threshold_max = std::uniform_int_distribution(threshold_min_, threshold_max_)(rnd_);
|
||||||
|
|
||||||
|
if (threshold_max < threshold_min) {
|
||||||
|
uint8_t temp = threshold_min;
|
||||||
|
threshold_min = threshold_max;
|
||||||
|
threshold_max = temp;
|
||||||
|
}
|
||||||
|
std::unique_ptr<SolarizeOp> op(new SolarizeOp(threshold_min, threshold_max));
|
||||||
|
return op->Compute(input, output);
|
||||||
|
}
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
@ -0,0 +1,53 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2020 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_RANDOM_SOLARIZE_OP_H
|
||||||
|
#define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_RANDOM_SOLARIZE_OP_H
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "minddata/dataset/core/tensor.h"
|
||||||
|
#include "minddata/dataset/kernels/tensor_op.h"
|
||||||
|
#include "minddata/dataset/util/status.h"
|
||||||
|
#include "minddata/dataset/kernels/image/solarize_op.h"
|
||||||
|
#include "minddata/dataset/util/random.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
class RandomSolarizeOp : public SolarizeOp {
|
||||||
|
public:
|
||||||
|
// Pick a random threshold value to solarize the image with
|
||||||
|
explicit RandomSolarizeOp(uint8_t threshold_min = 0, uint8_t threshold_max = 255)
|
||||||
|
: threshold_min_(threshold_min), threshold_max_(threshold_max) {
|
||||||
|
rnd_.seed(GetSeed());
|
||||||
|
}
|
||||||
|
|
||||||
|
~RandomSolarizeOp() = default;
|
||||||
|
|
||||||
|
Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override;
|
||||||
|
|
||||||
|
std::string Name() const override { return kRandomSolarizeOp; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint8_t threshold_min_;
|
||||||
|
uint8_t threshold_max_;
|
||||||
|
std::mt19937 rnd_;
|
||||||
|
};
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
||||||
|
|
||||||
|
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_RANDOM_SOLARIZE_OP_H
|
@ -0,0 +1,81 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2020 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 "minddata/dataset/kernels/image/solarize_op.h"
|
||||||
|
#include "minddata/dataset/kernels/image/image_utils.h"
|
||||||
|
#include "minddata/dataset/core/cv_tensor.h"
|
||||||
|
#include "minddata/dataset/util/status.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
|
||||||
|
// only supports RGB images
|
||||||
|
const uint8_t kPixelValue = 255;
|
||||||
|
|
||||||
|
Status SolarizeOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) {
|
||||||
|
IO_CHECK(input, output);
|
||||||
|
|
||||||
|
CHECK_FAIL_RETURN_UNEXPECTED(threshold_min_ <= threshold_max_,
|
||||||
|
"threshold_min must be smaller or equal to threshold_max.");
|
||||||
|
|
||||||
|
try {
|
||||||
|
std::shared_ptr<CVTensor> input_cv = CVTensor::AsCVTensor(input);
|
||||||
|
cv::Mat input_img = input_cv->mat();
|
||||||
|
if (!input_cv->mat().data) {
|
||||||
|
RETURN_STATUS_UNEXPECTED("Could not convert to CV Tensor");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input_cv->Rank() != 2 && input_cv->Rank() != 3) {
|
||||||
|
RETURN_STATUS_UNEXPECTED("Shape not of either <H,W,C> or <H,W> format.");
|
||||||
|
}
|
||||||
|
if (input_cv->Rank() == 3) {
|
||||||
|
int num_channels = input_cv->shape()[2];
|
||||||
|
if (num_channels != 3 && num_channels != 1) {
|
||||||
|
RETURN_STATUS_UNEXPECTED("Number of channels is not 1 or 3.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<CVTensor> mask_mat_tensor;
|
||||||
|
std::shared_ptr<CVTensor> output_cv_tensor;
|
||||||
|
RETURN_IF_NOT_OK(CVTensor::CreateFromMat(input_cv->mat(), &mask_mat_tensor));
|
||||||
|
|
||||||
|
RETURN_IF_NOT_OK(CVTensor::CreateEmpty(input_cv->shape(), input_cv->type(), &output_cv_tensor));
|
||||||
|
RETURN_UNEXPECTED_IF_NULL(mask_mat_tensor);
|
||||||
|
RETURN_UNEXPECTED_IF_NULL(output_cv_tensor);
|
||||||
|
|
||||||
|
if (threshold_min_ == threshold_max_) {
|
||||||
|
mask_mat_tensor->mat().setTo(0, ~(input_cv->mat() >= threshold_min_));
|
||||||
|
} else {
|
||||||
|
mask_mat_tensor->mat().setTo(0, ~((input_cv->mat() >= threshold_min_) & (input_cv->mat() <= threshold_max_)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// solarize desired portion
|
||||||
|
output_cv_tensor->mat() = cv::Scalar::all(255) - mask_mat_tensor->mat();
|
||||||
|
input_cv->mat().copyTo(output_cv_tensor->mat(), mask_mat_tensor->mat() == 0);
|
||||||
|
input_cv->mat().copyTo(output_cv_tensor->mat(), input_cv->mat() < threshold_min_);
|
||||||
|
|
||||||
|
*output = std::static_pointer_cast<Tensor>(output_cv_tensor);
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (const cv::Exception &e) {
|
||||||
|
const char *cv_err_msg = e.what();
|
||||||
|
std::string err_message = "Error in SolarizeOp: ";
|
||||||
|
err_message += cv_err_msg;
|
||||||
|
RETURN_STATUS_UNEXPECTED(err_message);
|
||||||
|
}
|
||||||
|
return Status::OK();
|
||||||
|
}
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
@ -0,0 +1,47 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2020 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_SOLARIZE_OP_H
|
||||||
|
#define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_SOLARIZE_OP_H
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "minddata/dataset/core/tensor.h"
|
||||||
|
#include "minddata/dataset/kernels/tensor_op.h"
|
||||||
|
#include "minddata/dataset/util/status.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
class SolarizeOp : public TensorOp {
|
||||||
|
public:
|
||||||
|
explicit SolarizeOp(uint8_t threshold_min = 0, uint8_t threshold_max = 255)
|
||||||
|
: threshold_min_(threshold_min), threshold_max_(threshold_max) {}
|
||||||
|
|
||||||
|
~SolarizeOp() = default;
|
||||||
|
|
||||||
|
Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override;
|
||||||
|
|
||||||
|
std::string Name() const override { return kSolarizeOp; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint8_t threshold_min_;
|
||||||
|
uint8_t threshold_max_;
|
||||||
|
};
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
||||||
|
|
||||||
|
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_SOLARIZE_OP_H
|
@ -0,0 +1,49 @@
|
|||||||
|
/**
|
||||||
|
* 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 "common/common.h"
|
||||||
|
#include "common/cvop_common.h"
|
||||||
|
#include "minddata/dataset/kernels/image/random_solarize_op.h"
|
||||||
|
#include "minddata/dataset/core/cv_tensor.h"
|
||||||
|
#include "minddata/dataset/util/status.h"
|
||||||
|
#include "utils/log_adapter.h"
|
||||||
|
|
||||||
|
using namespace mindspore::dataset;
|
||||||
|
using mindspore::LogStream;
|
||||||
|
using mindspore::ExceptionType::NoExceptionType;
|
||||||
|
using mindspore::MsLogLevel::INFO;
|
||||||
|
|
||||||
|
class MindDataTestRandomSolarizeOp : public UT::CVOP::CVOpCommon {
|
||||||
|
protected:
|
||||||
|
MindDataTestRandomSolarizeOp() : CVOpCommon() {}
|
||||||
|
|
||||||
|
std::shared_ptr<Tensor> output_tensor_;
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(MindDataTestRandomSolarizeOp, TestOp1) {
|
||||||
|
MS_LOG(INFO) << "Doing testRandomSolarizeOp1.";
|
||||||
|
// setting seed here
|
||||||
|
uint32_t curr_seed = GlobalContext::config_manager()->seed();
|
||||||
|
GlobalContext::config_manager()->set_seed(0);
|
||||||
|
|
||||||
|
std::unique_ptr<RandomSolarizeOp> op(new RandomSolarizeOp(100, 100));
|
||||||
|
EXPECT_TRUE(op->OneToOne());
|
||||||
|
Status s = op->Compute(input_tensor_, &output_tensor_);
|
||||||
|
|
||||||
|
EXPECT_TRUE(s.IsOk());
|
||||||
|
CheckImageShapeAndData(output_tensor_, kRandomSolarize);
|
||||||
|
// restoring the seed
|
||||||
|
GlobalContext::config_manager()->set_seed(curr_seed);
|
||||||
|
}
|
@ -0,0 +1,164 @@
|
|||||||
|
/**
|
||||||
|
* 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 "common/common.h"
|
||||||
|
#include "common/cvop_common.h"
|
||||||
|
#include "minddata/dataset/kernels/image/solarize_op.h"
|
||||||
|
#include "minddata/dataset/core/cv_tensor.h"
|
||||||
|
#include "minddata/dataset/util/status.h"
|
||||||
|
#include "utils/log_adapter.h"
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
using namespace mindspore::dataset;
|
||||||
|
using mindspore::LogStream;
|
||||||
|
using mindspore::ExceptionType::NoExceptionType;
|
||||||
|
using mindspore::MsLogLevel::INFO;
|
||||||
|
|
||||||
|
class MindDataTestSolarizeOp : public UT::CVOP::CVOpCommon {
|
||||||
|
protected:
|
||||||
|
MindDataTestSolarizeOp() : CVOpCommon() {}
|
||||||
|
|
||||||
|
std::shared_ptr<Tensor> output_tensor_;
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(MindDataTestSolarizeOp, TestOp1) {
|
||||||
|
MS_LOG(INFO) << "Doing testSolarizeOp1.";
|
||||||
|
|
||||||
|
std::unique_ptr<SolarizeOp> op(new SolarizeOp());
|
||||||
|
EXPECT_TRUE(op->OneToOne());
|
||||||
|
Status s = op->Compute(input_tensor_, &output_tensor_);
|
||||||
|
|
||||||
|
EXPECT_TRUE(s.IsOk());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(MindDataTestSolarizeOp, TestOp2) {
|
||||||
|
MS_LOG(INFO) << "Doing testSolarizeOp2 - test default values";
|
||||||
|
|
||||||
|
// unsigned int threshold = 128;
|
||||||
|
std::unique_ptr<SolarizeOp> op(new SolarizeOp());
|
||||||
|
|
||||||
|
std::vector<uint8_t> test_vector = {3, 4, 59, 210, 255};
|
||||||
|
std::vector<uint8_t> expected_output_vector = {252, 251, 196, 45, 0};
|
||||||
|
std::shared_ptr<Tensor> test_input_tensor;
|
||||||
|
std::shared_ptr<Tensor> expected_output_tensor;
|
||||||
|
Tensor::CreateFromVector(test_vector, TensorShape({1, (long int)test_vector.size(), 1}), &test_input_tensor);
|
||||||
|
Tensor::CreateFromVector(expected_output_vector, TensorShape({1, (long int)test_vector.size(), 1}),
|
||||||
|
&expected_output_tensor);
|
||||||
|
|
||||||
|
std::shared_ptr<Tensor> test_output_tensor;
|
||||||
|
Status s = op->Compute(test_input_tensor, &test_output_tensor);
|
||||||
|
|
||||||
|
EXPECT_TRUE(s.IsOk());
|
||||||
|
|
||||||
|
ASSERT_TRUE(test_output_tensor->shape() == expected_output_tensor->shape());
|
||||||
|
ASSERT_TRUE(test_output_tensor->type() == expected_output_tensor->type());
|
||||||
|
MS_LOG(DEBUG) << *test_output_tensor << std::endl;
|
||||||
|
MS_LOG(DEBUG) << *expected_output_tensor << std::endl;
|
||||||
|
|
||||||
|
ASSERT_TRUE(*test_output_tensor == *expected_output_tensor);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(MindDataTestSolarizeOp, TestOp3) {
|
||||||
|
MS_LOG(INFO) << "Doing testSolarizeOp3 - Pass in only threshold_min parameter";
|
||||||
|
|
||||||
|
// unsigned int threshold = 128;
|
||||||
|
std::unique_ptr<SolarizeOp> op(new SolarizeOp(1));
|
||||||
|
|
||||||
|
std::vector<uint8_t> test_vector = {3, 4, 59, 210, 255};
|
||||||
|
std::vector<uint8_t> expected_output_vector = {252, 251, 196, 45, 0};
|
||||||
|
std::shared_ptr<Tensor> test_input_tensor;
|
||||||
|
std::shared_ptr<Tensor> expected_output_tensor;
|
||||||
|
Tensor::CreateFromVector(test_vector, TensorShape({1, (long int)test_vector.size(), 1}), &test_input_tensor);
|
||||||
|
Tensor::CreateFromVector(expected_output_vector, TensorShape({1, (long int)test_vector.size(), 1}),
|
||||||
|
&expected_output_tensor);
|
||||||
|
|
||||||
|
std::shared_ptr<Tensor> test_output_tensor;
|
||||||
|
Status s = op->Compute(test_input_tensor, &test_output_tensor);
|
||||||
|
|
||||||
|
EXPECT_TRUE(s.IsOk());
|
||||||
|
ASSERT_TRUE(test_output_tensor->shape() == expected_output_tensor->shape());
|
||||||
|
ASSERT_TRUE(test_output_tensor->type() == expected_output_tensor->type());
|
||||||
|
MS_LOG(DEBUG) << *test_output_tensor << std::endl;
|
||||||
|
MS_LOG(DEBUG) << *expected_output_tensor << std::endl;
|
||||||
|
ASSERT_TRUE(*test_output_tensor == *expected_output_tensor);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(MindDataTestSolarizeOp, TestOp4) {
|
||||||
|
MS_LOG(INFO) << "Doing testSolarizeOp4 - Pass in both threshold parameters.";
|
||||||
|
|
||||||
|
// unsigned int threshold = 128;
|
||||||
|
std::unique_ptr<SolarizeOp> op(new SolarizeOp(1, 230));
|
||||||
|
|
||||||
|
std::vector<uint8_t> test_vector = {3, 4, 59, 210, 255};
|
||||||
|
std::vector<uint8_t> expected_output_vector = {252, 251, 196, 45, 255};
|
||||||
|
std::shared_ptr<Tensor> test_input_tensor;
|
||||||
|
std::shared_ptr<Tensor> expected_output_tensor;
|
||||||
|
Tensor::CreateFromVector(test_vector, TensorShape({1, (long int)test_vector.size(), 1}), &test_input_tensor);
|
||||||
|
Tensor::CreateFromVector(expected_output_vector, TensorShape({1, (long int)test_vector.size(), 1}),
|
||||||
|
&expected_output_tensor);
|
||||||
|
|
||||||
|
std::shared_ptr<Tensor> test_output_tensor;
|
||||||
|
Status s = op->Compute(test_input_tensor, &test_output_tensor);
|
||||||
|
|
||||||
|
EXPECT_TRUE(s.IsOk());
|
||||||
|
ASSERT_TRUE(test_output_tensor->shape() == expected_output_tensor->shape());
|
||||||
|
ASSERT_TRUE(test_output_tensor->type() == expected_output_tensor->type());
|
||||||
|
MS_LOG(DEBUG) << *test_output_tensor << std::endl;
|
||||||
|
MS_LOG(DEBUG) << *expected_output_tensor << std::endl;
|
||||||
|
ASSERT_TRUE(*test_output_tensor == *expected_output_tensor);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(MindDataTestSolarizeOp, TestOp5) {
|
||||||
|
MS_LOG(INFO) << "Doing testSolarizeOp5 - Rank 2 input tensor.";
|
||||||
|
|
||||||
|
// unsigned int threshold = 128;
|
||||||
|
std::unique_ptr<SolarizeOp> op(new SolarizeOp(1, 230));
|
||||||
|
|
||||||
|
std::vector<uint8_t> test_vector = {3, 4, 59, 210, 255};
|
||||||
|
std::vector<uint8_t> expected_output_vector = {252, 251, 196, 45, 255};
|
||||||
|
std::shared_ptr<Tensor> test_input_tensor;
|
||||||
|
std::shared_ptr<Tensor> expected_output_tensor;
|
||||||
|
Tensor::CreateFromVector(test_vector, TensorShape({1, (long int)test_vector.size()}), &test_input_tensor);
|
||||||
|
Tensor::CreateFromVector(expected_output_vector, TensorShape({1, (long int)test_vector.size()}),
|
||||||
|
&expected_output_tensor);
|
||||||
|
|
||||||
|
std::shared_ptr<Tensor> test_output_tensor;
|
||||||
|
Status s = op->Compute(test_input_tensor, &test_output_tensor);
|
||||||
|
|
||||||
|
EXPECT_TRUE(s.IsOk());
|
||||||
|
ASSERT_TRUE(test_output_tensor->shape() == expected_output_tensor->shape());
|
||||||
|
ASSERT_TRUE(test_output_tensor->type() == expected_output_tensor->type());
|
||||||
|
MS_LOG(DEBUG) << *test_output_tensor << std::endl;
|
||||||
|
MS_LOG(DEBUG) << *expected_output_tensor << std::endl;
|
||||||
|
|
||||||
|
ASSERT_TRUE(*test_output_tensor == *expected_output_tensor);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(MindDataTestSolarizeOp, TestOp6) {
|
||||||
|
MS_LOG(INFO) << "Doing testSolarizeOp6 - Bad Input.";
|
||||||
|
|
||||||
|
std::unique_ptr<SolarizeOp> op(new SolarizeOp(10, 1));
|
||||||
|
|
||||||
|
std::vector<uint8_t> test_vector = {3, 4, 59, 210, 255};
|
||||||
|
std::shared_ptr<Tensor> test_input_tensor;
|
||||||
|
std::shared_ptr<Tensor> test_output_tensor;
|
||||||
|
Tensor::CreateFromVector(test_vector, TensorShape({1, (long int)test_vector.size(), 1}), &test_input_tensor);
|
||||||
|
|
||||||
|
Status s = op->Compute(test_input_tensor, &test_output_tensor);
|
||||||
|
|
||||||
|
EXPECT_TRUE(s.IsError());
|
||||||
|
EXPECT_NE(s.ToString().find("threshold_min must be smaller or equal to threshold_max."), std::string::npos);
|
||||||
|
ASSERT_TRUE(s.get_code() == StatusCode::kUnexpectedError);
|
||||||
|
}
|
Binary file not shown.
After Width: | Height: | Size: 544 KiB |
@ -0,0 +1,112 @@
|
|||||||
|
# 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.
|
||||||
|
# ==============================================================================
|
||||||
|
"""
|
||||||
|
Testing RandomSolarizeOp op in DE
|
||||||
|
"""
|
||||||
|
import pytest
|
||||||
|
import mindspore.dataset as ds
|
||||||
|
import mindspore.dataset.transforms.vision.c_transforms as vision
|
||||||
|
from mindspore import log as logger
|
||||||
|
from util import visualize_list, save_and_check_md5, config_get_set_seed, config_get_set_num_parallel_workers
|
||||||
|
|
||||||
|
GENERATE_GOLDEN = False
|
||||||
|
|
||||||
|
DATA_DIR = ["../data/dataset/test_tf_file_3_images/train-0000-of-0001.data"]
|
||||||
|
SCHEMA_DIR = "../data/dataset/test_tf_file_3_images/datasetSchema.json"
|
||||||
|
|
||||||
|
|
||||||
|
def test_random_solarize_op(threshold=None, plot=False):
|
||||||
|
"""
|
||||||
|
Test RandomSolarize
|
||||||
|
"""
|
||||||
|
logger.info("Test RandomSolarize")
|
||||||
|
|
||||||
|
# First dataset
|
||||||
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
|
||||||
|
decode_op = vision.Decode()
|
||||||
|
|
||||||
|
if threshold is None:
|
||||||
|
solarize_op = vision.RandomSolarize()
|
||||||
|
else:
|
||||||
|
solarize_op = vision.RandomSolarize(threshold)
|
||||||
|
data1 = data1.map(input_columns=["image"], operations=decode_op)
|
||||||
|
data1 = data1.map(input_columns=["image"], operations=solarize_op)
|
||||||
|
|
||||||
|
# Second dataset
|
||||||
|
data2 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"])
|
||||||
|
data2 = data2.map(input_columns=["image"], operations=decode_op)
|
||||||
|
|
||||||
|
image_solarized = []
|
||||||
|
image = []
|
||||||
|
for item1, item2 in zip(data1.create_dict_iterator(), data2.create_dict_iterator()):
|
||||||
|
image_solarized.append(item1["image"].copy())
|
||||||
|
image.append(item2["image"].copy())
|
||||||
|
if plot:
|
||||||
|
visualize_list(image, image_solarized)
|
||||||
|
|
||||||
|
|
||||||
|
def test_random_solarize_md5():
|
||||||
|
"""
|
||||||
|
Test RandomSolarize
|
||||||
|
"""
|
||||||
|
logger.info("Test RandomSolarize")
|
||||||
|
original_seed = config_get_set_seed(0)
|
||||||
|
original_num_parallel_workers = config_get_set_num_parallel_workers(1)
|
||||||
|
|
||||||
|
data1 = ds.TFRecordDataset(DATA_DIR, SCHEMA_DIR, columns_list=["image"], shuffle=False)
|
||||||
|
decode_op = vision.Decode()
|
||||||
|
random_solarize_op = vision.RandomSolarize((10, 150))
|
||||||
|
data1 = data1.map(input_columns=["image"], operations=decode_op)
|
||||||
|
data1 = data1.map(input_columns=["image"], operations=random_solarize_op)
|
||||||
|
# Compare with expected md5 from images
|
||||||
|
filename = "random_solarize_01_result.npz"
|
||||||
|
save_and_check_md5(data1, filename, generate_golden=GENERATE_GOLDEN)
|
||||||
|
|
||||||
|
# Restore config setting
|
||||||
|
ds.config.set_seed(original_seed)
|
||||||
|
ds.config.set_num_parallel_workers(original_num_parallel_workers)
|
||||||
|
|
||||||
|
|
||||||
|
def test_random_solarize_errors():
|
||||||
|
"""
|
||||||
|
Test that RandomSolarize errors with bad input
|
||||||
|
"""
|
||||||
|
with pytest.raises(ValueError) as error_info:
|
||||||
|
vision.RandomSolarize((12, 1))
|
||||||
|
assert "threshold must be in min max format numbers" in str(error_info.value)
|
||||||
|
|
||||||
|
with pytest.raises(ValueError) as error_info:
|
||||||
|
vision.RandomSolarize((12, 1000))
|
||||||
|
assert "Input is not within the required interval of (0 to 255)." in str(error_info.value)
|
||||||
|
|
||||||
|
with pytest.raises(TypeError) as error_info:
|
||||||
|
vision.RandomSolarize((122.1, 140))
|
||||||
|
assert "Argument threshold[0] with value 122.1 is not of type (<class 'int'>,)." in str(error_info.value)
|
||||||
|
|
||||||
|
with pytest.raises(ValueError) as error_info:
|
||||||
|
vision.RandomSolarize((122, 100, 30))
|
||||||
|
assert "threshold must be a sequence of two numbers" in str(error_info.value)
|
||||||
|
|
||||||
|
with pytest.raises(ValueError) as error_info:
|
||||||
|
vision.RandomSolarize((120,))
|
||||||
|
assert "threshold must be a sequence of two numbers" in str(error_info.value)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_random_solarize_op((100, 100), plot=True)
|
||||||
|
test_random_solarize_op((12, 120), plot=True)
|
||||||
|
test_random_solarize_op(plot=True)
|
||||||
|
test_random_solarize_errors()
|
||||||
|
test_random_solarize_md5()
|
Loading…
Reference in new issue