parent
e14fff871d
commit
477528de7f
@ -0,0 +1,51 @@
|
|||||||
|
/**
|
||||||
|
* 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_sharpness_op.h"
|
||||||
|
#include <random>
|
||||||
|
#include "minddata/dataset/kernels/image/sharpness_op.h"
|
||||||
|
#include "minddata/dataset/core/cv_tensor.h"
|
||||||
|
#include "minddata/dataset/util/random.h"
|
||||||
|
#include "minddata/dataset/util/status.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
|
||||||
|
const float RandomSharpnessOp::kDefStartDegree = 0.1;
|
||||||
|
const float RandomSharpnessOp::kDefEndDegree = 1.9;
|
||||||
|
|
||||||
|
/// constructor
|
||||||
|
RandomSharpnessOp::RandomSharpnessOp(float start_degree, float end_degree)
|
||||||
|
: start_degree_(start_degree), end_degree_(end_degree) {
|
||||||
|
rnd_.seed(GetSeed());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// main function call for random sharpness : Generate the random degrees
|
||||||
|
Status RandomSharpnessOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) {
|
||||||
|
IO_CHECK(input, output);
|
||||||
|
float random_double = distribution_(rnd_);
|
||||||
|
/// get the degree sharpness range
|
||||||
|
/// the way this op works (uniform distribution)
|
||||||
|
/// assumption here is that mDegreesEnd > mDegreeStart so we always get positive number
|
||||||
|
float degree_range = (end_degree_ - start_degree_) / 2;
|
||||||
|
float mid = (end_degree_ + start_degree_) / 2;
|
||||||
|
alpha_ = mid + random_double * degree_range;
|
||||||
|
|
||||||
|
SharpnessOp::Compute(input, output);
|
||||||
|
return Status::OK();
|
||||||
|
}
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
@ -0,0 +1,56 @@
|
|||||||
|
/**
|
||||||
|
* 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_SHARPNESS_OP_H_
|
||||||
|
#define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_RANDOM_SHARPNESS_OP_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "minddata/dataset/kernels/image/sharpness_op.h"
|
||||||
|
#include "minddata/dataset/util/status.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
|
||||||
|
class RandomSharpnessOp : public SharpnessOp {
|
||||||
|
public:
|
||||||
|
static const float kDefStartDegree;
|
||||||
|
static const float kDefEndDegree;
|
||||||
|
|
||||||
|
/// Adjust the sharpness of the input image by a random degree within the given range.
|
||||||
|
/// \@param[in] start_degree A float indicating the beginning of the range.
|
||||||
|
/// \@param[in] end_degree A float indicating the end of the range.
|
||||||
|
|
||||||
|
explicit RandomSharpnessOp(float start_degree = kDefStartDegree, const float end_degree = kDefEndDegree);
|
||||||
|
~RandomSharpnessOp() override = default;
|
||||||
|
void Print(std::ostream &out) const override { out << Name(); }
|
||||||
|
|
||||||
|
Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override;
|
||||||
|
|
||||||
|
std::string Name() const override { return kRandomSharpnessOp; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
float start_degree_;
|
||||||
|
float end_degree_;
|
||||||
|
std::uniform_real_distribution<float> distribution_{-1.0, 1.0};
|
||||||
|
std::mt19937 rnd_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
||||||
|
|
||||||
|
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_RANDOM_SHARPNESS_OP_H_
|
@ -0,0 +1,84 @@
|
|||||||
|
/**
|
||||||
|
* 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/sharpness_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 {
|
||||||
|
|
||||||
|
const float SharpnessOp::kDefAlpha = 1.0;
|
||||||
|
|
||||||
|
Status SharpnessOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) {
|
||||||
|
IO_CHECK(input, output);
|
||||||
|
|
||||||
|
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() != 3 && input_cv->Rank() != 2) {
|
||||||
|
RETURN_STATUS_UNEXPECTED("Shape not <H,W,C> or <H,W>");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get number of channels and image matrix
|
||||||
|
std::size_t num_of_channels = input_cv->shape()[2];
|
||||||
|
if (num_of_channels != 1 && num_of_channels != 3) {
|
||||||
|
RETURN_STATUS_UNEXPECTED("Number of channels is not 1 or 3.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// creating a smoothing filter. 1, 1, 1,
|
||||||
|
/// 1, 5, 1,
|
||||||
|
/// 1, 1, 1
|
||||||
|
|
||||||
|
float filterSum = 13.0;
|
||||||
|
cv::Mat filter = cv::Mat(3, 3, CV_32F, cv::Scalar::all(1.0 / filterSum));
|
||||||
|
filter.at<float>(1, 1) = 5.0 / filterSum;
|
||||||
|
|
||||||
|
/// applying filter on channels
|
||||||
|
cv::Mat result = cv::Mat();
|
||||||
|
cv::filter2D(input_img, result, -1, filter);
|
||||||
|
|
||||||
|
int height = input_cv->shape()[0];
|
||||||
|
int width = input_cv->shape()[1];
|
||||||
|
|
||||||
|
/// restoring the edges
|
||||||
|
input_img.row(0).copyTo(result.row(0));
|
||||||
|
input_img.row(height - 1).copyTo(result.row(height - 1));
|
||||||
|
input_img.col(0).copyTo(result.col(0));
|
||||||
|
input_img.col(width - 1).copyTo(result.col(width - 1));
|
||||||
|
|
||||||
|
/// blend based on alpha : (alpha_ *input_img) + ((1.0-alpha_) * result);
|
||||||
|
cv::addWeighted(input_img, alpha_, result, 1.0 - alpha_, 0.0, result);
|
||||||
|
|
||||||
|
std::shared_ptr<CVTensor> output_cv;
|
||||||
|
RETURN_IF_NOT_OK(CVTensor::CreateFromMat(result, &output_cv));
|
||||||
|
RETURN_UNEXPECTED_IF_NULL(output_cv);
|
||||||
|
|
||||||
|
*output = std::static_pointer_cast<Tensor>(output_cv);
|
||||||
|
}
|
||||||
|
|
||||||
|
catch (const cv::Exception &e) {
|
||||||
|
RETURN_STATUS_UNEXPECTED("OpenCV error in random sharpness");
|
||||||
|
}
|
||||||
|
return Status::OK();
|
||||||
|
}
|
||||||
|
} // 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_SHARPNESS_OP_H_
|
||||||
|
#define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_SHARPNESS_OP_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
#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 SharpnessOp : public TensorOp {
|
||||||
|
public:
|
||||||
|
/// Default values, also used by bindings.cc
|
||||||
|
static const float kDefAlpha;
|
||||||
|
|
||||||
|
/// This class can be used to adjust the sharpness of an image.
|
||||||
|
/// \@param[in] alpha A float indicating the enhancement factor.
|
||||||
|
/// a factor of 0.0 gives a blurred image, a factor of 1.0 gives the
|
||||||
|
/// original image, and a factor of 2.0 gives a sharpened image.
|
||||||
|
|
||||||
|
explicit SharpnessOp(const float alpha = kDefAlpha) : alpha_(alpha) {}
|
||||||
|
|
||||||
|
~SharpnessOp() override = default;
|
||||||
|
|
||||||
|
Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override;
|
||||||
|
|
||||||
|
std::string Name() const override { return kSharpnessOp; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
float alpha_;
|
||||||
|
};
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
||||||
|
|
||||||
|
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_SHARPNESS_OP_H_
|
@ -0,0 +1,40 @@
|
|||||||
|
/**
|
||||||
|
* 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/invert_op.h"
|
||||||
|
#include "common/common.h"
|
||||||
|
#include "common/cvop_common.h"
|
||||||
|
#include "utils/log_adapter.h"
|
||||||
|
|
||||||
|
using namespace mindspore::dataset;
|
||||||
|
using mindspore::MsLogLevel::INFO;
|
||||||
|
using mindspore::ExceptionType::NoExceptionType;
|
||||||
|
using mindspore::LogStream;
|
||||||
|
|
||||||
|
class MindDataTestInvert : public UT::CVOP::CVOpCommon {
|
||||||
|
public:
|
||||||
|
MindDataTestInvert() : CVOpCommon() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(MindDataTestInvert, TestOp) {
|
||||||
|
MS_LOG(INFO) << "Doing test Invert.";
|
||||||
|
std::shared_ptr<Tensor> output_tensor;
|
||||||
|
std::unique_ptr<InvertOp> op(new InvertOp());
|
||||||
|
EXPECT_TRUE(op->OneToOne());
|
||||||
|
Status st = op->Compute(input_tensor_, &output_tensor);
|
||||||
|
EXPECT_TRUE(st.IsOk());
|
||||||
|
CheckImageShapeAndData(output_tensor, kInvert);
|
||||||
|
MS_LOG(INFO) << "testInvert end.";
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
/**
|
||||||
|
* 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_sharpness_op.h"
|
||||||
|
#include "common/common.h"
|
||||||
|
#include "common/cvop_common.h"
|
||||||
|
#include "utils/log_adapter.h"
|
||||||
|
#include "minddata/dataset/core/config_manager.h"
|
||||||
|
#include "minddata/dataset/core/global_context.h"
|
||||||
|
|
||||||
|
using namespace mindspore::dataset;
|
||||||
|
using mindspore::MsLogLevel::INFO;
|
||||||
|
using mindspore::ExceptionType::NoExceptionType;
|
||||||
|
using mindspore::LogStream;
|
||||||
|
|
||||||
|
class MindDataTestRandomSharpness : public UT::CVOP::CVOpCommon {
|
||||||
|
public:
|
||||||
|
MindDataTestRandomSharpness() : CVOpCommon() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(MindDataTestRandomSharpness, TestOp) {
|
||||||
|
MS_LOG(INFO) << "Doing test RandomSharpness.";
|
||||||
|
// setting seed here
|
||||||
|
u_int32_t curr_seed = GlobalContext::config_manager()->seed();
|
||||||
|
GlobalContext::config_manager()->set_seed(120);
|
||||||
|
// Sharpness with a factor in range [0.2,1.8]
|
||||||
|
float start_degree = 0.2;
|
||||||
|
float end_degree = 1.8;
|
||||||
|
std::shared_ptr<Tensor> output_tensor;
|
||||||
|
// sharpening
|
||||||
|
std::unique_ptr<RandomSharpnessOp> op(new RandomSharpnessOp(start_degree, end_degree));
|
||||||
|
EXPECT_TRUE(op->OneToOne());
|
||||||
|
Status st = op->Compute(input_tensor_, &output_tensor);
|
||||||
|
EXPECT_TRUE(st.IsOk());
|
||||||
|
CheckImageShapeAndData(output_tensor, kRandomSharpness);
|
||||||
|
// restoring the seed
|
||||||
|
GlobalContext::config_manager()->set_seed(curr_seed);
|
||||||
|
MS_LOG(INFO) << "testRandomSharpness end.";
|
||||||
|
}
|
Binary file not shown.
After Width: | Height: | Size: 430 KiB |
After Width: | Height: | Size: 435 KiB |
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue