parent
8f10a84753
commit
465a69a70a
@ -0,0 +1,45 @@
|
|||||||
|
/**
|
||||||
|
* 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/crop_op.h"
|
||||||
|
|
||||||
|
#include "minddata/dataset/kernels/image/image_utils.h"
|
||||||
|
#include "minddata/dataset/util/status.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
|
||||||
|
Status CropOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) {
|
||||||
|
IO_CHECK(input, output);
|
||||||
|
CHECK_FAIL_RETURN_UNEXPECTED(input->shape().Size() >= 2, "The shape size " + std::to_string(input->shape().Size()) +
|
||||||
|
" of input tensor is invalid");
|
||||||
|
int32_t input_h = static_cast<int>(input->shape()[0]);
|
||||||
|
int32_t input_w = static_cast<int>(input->shape()[1]);
|
||||||
|
CHECK_FAIL_RETURN_UNEXPECTED(y_ + height_ <= input_h, "Crop height dimensions exceed image dimensions");
|
||||||
|
CHECK_FAIL_RETURN_UNEXPECTED(x_ + width_ <= input_w, "Crop width dimensions exceed image dimensions");
|
||||||
|
return Crop(input, output, x_, y_, height_, width_);
|
||||||
|
}
|
||||||
|
|
||||||
|
Status CropOp::OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) {
|
||||||
|
RETURN_IF_NOT_OK(TensorOp::OutputShape(inputs, outputs));
|
||||||
|
outputs.clear();
|
||||||
|
TensorShape out = TensorShape{height_, width_};
|
||||||
|
if (inputs[0].Rank() == 2) outputs.emplace_back(out);
|
||||||
|
if (inputs[0].Rank() == 3) outputs.emplace_back(out.AppendDim(inputs[0][2]));
|
||||||
|
if (!outputs.empty()) return Status::OK();
|
||||||
|
return Status(StatusCode::kUnexpectedError, "Input has a wrong shape");
|
||||||
|
}
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
@ -0,0 +1,63 @@
|
|||||||
|
/**
|
||||||
|
* 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_CROP_OP_H_
|
||||||
|
#define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_CROP_OP_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "minddata/dataset/core/tensor.h"
|
||||||
|
#include "minddata/dataset/kernels/image/image_utils.h"
|
||||||
|
#include "minddata/dataset/kernels/tensor_op.h"
|
||||||
|
#include "minddata/dataset/util/status.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
class CropOp : public TensorOp {
|
||||||
|
public:
|
||||||
|
/// \brief Constructor to Crop Op
|
||||||
|
/// \param[in] x - the horizontal starting coordinate
|
||||||
|
/// \param[in] y - the vertical starting coordinate
|
||||||
|
/// \param[in] height - the height of the crop box
|
||||||
|
/// \param[in] width - the width of the crop box
|
||||||
|
explicit CropOp(int32_t x, int32_t y, int32_t height, int32_t width) : x_(x), y_(y), height_(height), width_(width) {}
|
||||||
|
|
||||||
|
CropOp(const CropOp &rhs) = default;
|
||||||
|
|
||||||
|
CropOp(CropOp &&rhs) = default;
|
||||||
|
|
||||||
|
~CropOp() override = default;
|
||||||
|
|
||||||
|
void Print(std::ostream &out) const override {
|
||||||
|
out << "CropOp x: " << x_ << " y: " << y_ << " w: " << width_ << " h: " << height_;
|
||||||
|
}
|
||||||
|
|
||||||
|
Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override;
|
||||||
|
Status OutputShape(const std::vector<TensorShape> &inputs, std::vector<TensorShape> &outputs) override;
|
||||||
|
|
||||||
|
std::string Name() const override { return kCropOp; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
int32_t x_;
|
||||||
|
int32_t y_;
|
||||||
|
int32_t height_;
|
||||||
|
int32_t width_;
|
||||||
|
};
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
||||||
|
|
||||||
|
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_IMAGE_CROP_OP_H_
|
@ -0,0 +1,29 @@
|
|||||||
|
/**
|
||||||
|
* 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/swap_red_blue_op.h"
|
||||||
|
|
||||||
|
#include "minddata/dataset/kernels/image/image_utils.h"
|
||||||
|
#include "minddata/dataset/util/status.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
|
||||||
|
Status SwapRedBlueOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) {
|
||||||
|
IO_CHECK(input, output);
|
||||||
|
return SwapRedAndBlue(input, output);
|
||||||
|
}
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
@ -0,0 +1,57 @@
|
|||||||
|
/**
|
||||||
|
* 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_SWAP_RED_BLUE_OP_H_
|
||||||
|
#define MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_SWAP_RED_BLUE_OP_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "minddata/dataset/core/tensor.h"
|
||||||
|
#include "minddata/dataset/kernels/image/image_utils.h"
|
||||||
|
#include "minddata/dataset/kernels/tensor_op.h"
|
||||||
|
#include "minddata/dataset/util/status.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace dataset {
|
||||||
|
class SwapRedBlueOp : public TensorOp {
|
||||||
|
public:
|
||||||
|
// SwapRedBlues the image to the output specified size. If only one value is provided,
|
||||||
|
// the it will crop the smaller size and maintains the aspect ratio.
|
||||||
|
// @param size1: the first size of output. If only this parameter is provided
|
||||||
|
// the smaller dimension will be cropd to this and then the other dimension changes
|
||||||
|
// such that the aspect ratio is maintained.
|
||||||
|
// @param size2: the second size of output. If this is also provided, the output size
|
||||||
|
// will be (size1, size2)
|
||||||
|
// @param InterpolationMode: the interpolation mode being used.
|
||||||
|
SwapRedBlueOp() {}
|
||||||
|
|
||||||
|
SwapRedBlueOp(const SwapRedBlueOp &rhs) = default;
|
||||||
|
|
||||||
|
SwapRedBlueOp(SwapRedBlueOp &&rhs) = default;
|
||||||
|
|
||||||
|
~SwapRedBlueOp() override = default;
|
||||||
|
|
||||||
|
void Print(std::ostream &out) const override { out << "SwapRedBlueOp x"; }
|
||||||
|
|
||||||
|
Status Compute(const std::shared_ptr<Tensor> &input, std::shared_ptr<Tensor> *output) override;
|
||||||
|
|
||||||
|
std::string Name() const override { return kSwapRedBlueOp; }
|
||||||
|
};
|
||||||
|
} // namespace dataset
|
||||||
|
} // namespace mindspore
|
||||||
|
|
||||||
|
#endif // MINDSPORE_CCSRC_MINDDATA_DATASET_KERNELS_SWAP_RED_BLUE_OP_H_
|
@ -0,0 +1,77 @@
|
|||||||
|
/**
|
||||||
|
* 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 "common/common.h"
|
||||||
|
#include "common/cvop_common.h"
|
||||||
|
#include "minddata/dataset/kernels/image/crop_op.h"
|
||||||
|
#include "utils/log_adapter.h"
|
||||||
|
|
||||||
|
using namespace mindspore::dataset;
|
||||||
|
using mindspore::MsLogLevel::INFO;
|
||||||
|
using mindspore::ExceptionType::NoExceptionType;
|
||||||
|
using mindspore::LogStream;
|
||||||
|
|
||||||
|
class MindDataTestCropOp : public UT::CVOP::CVOpCommon {
|
||||||
|
protected:
|
||||||
|
MindDataTestCropOp() : CVOpCommon() {}
|
||||||
|
|
||||||
|
std::shared_ptr<Tensor> output_tensor_;
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(MindDataTestCropOp, TestOp1) {
|
||||||
|
MS_LOG(INFO) << "Doing testCrop.";
|
||||||
|
// Crop params
|
||||||
|
int crop_height = 18;
|
||||||
|
int crop_width = 12;
|
||||||
|
std::unique_ptr<CropOp> op(new CropOp(0, 0, crop_height, crop_width));
|
||||||
|
EXPECT_TRUE(op->OneToOne());
|
||||||
|
Status s = op->Compute(input_tensor_, &output_tensor_);
|
||||||
|
size_t actual = 0;
|
||||||
|
if (s == Status::OK()) {
|
||||||
|
actual = output_tensor_->shape()[0] * output_tensor_->shape()[1] * output_tensor_->shape()[2];
|
||||||
|
}
|
||||||
|
EXPECT_EQ(crop_height, output_tensor_->shape()[1]);
|
||||||
|
EXPECT_EQ(actual, crop_height * crop_width * 3);
|
||||||
|
EXPECT_EQ(s, Status::OK());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(MindDataTestCropOp, TestOp2) {
|
||||||
|
MS_LOG(INFO) << "Doing testCrop negative coordinates.";
|
||||||
|
// Crop params
|
||||||
|
unsigned int crop_height = 10;
|
||||||
|
unsigned int crop_width = 10;
|
||||||
|
|
||||||
|
std::unique_ptr<CropOp> op(
|
||||||
|
new CropOp(-10, -10, crop_height, crop_width));
|
||||||
|
EXPECT_TRUE(op->OneToOne());
|
||||||
|
Status s = op->Compute(input_tensor_, &output_tensor_);
|
||||||
|
EXPECT_EQ(false, s.IsOk());
|
||||||
|
MS_LOG(INFO) << "testCrop coordinate exception end.";
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(MindDataTestCropOp, TestOp3) {
|
||||||
|
MS_LOG(INFO) << "Doing testCrop size too large.";
|
||||||
|
// Crop params
|
||||||
|
unsigned int crop_height = 1200000;
|
||||||
|
unsigned int crop_width = 1200000;
|
||||||
|
|
||||||
|
std::unique_ptr<CropOp> op(
|
||||||
|
new CropOp(0, 0, crop_height, crop_width));
|
||||||
|
EXPECT_TRUE(op->OneToOne());
|
||||||
|
Status s = op->Compute(input_tensor_, &output_tensor_);
|
||||||
|
EXPECT_EQ(false, s.IsOk());
|
||||||
|
MS_LOG(INFO) << "testCrop size exception end.";
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,46 @@
|
|||||||
|
/**
|
||||||
|
* 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 "common/common.h"
|
||||||
|
#include "common/cvop_common.h"
|
||||||
|
#include "minddata/dataset/kernels/image/swap_red_blue_op.h"
|
||||||
|
#include "utils/log_adapter.h"
|
||||||
|
|
||||||
|
using namespace mindspore::dataset;
|
||||||
|
using mindspore::MsLogLevel::INFO;
|
||||||
|
using mindspore::ExceptionType::NoExceptionType;
|
||||||
|
using mindspore::LogStream;
|
||||||
|
|
||||||
|
class MindDataTestSwapRedBlueOp : public UT::CVOP::CVOpCommon {
|
||||||
|
protected:
|
||||||
|
MindDataTestSwapRedBlueOp() : CVOpCommon() {}
|
||||||
|
|
||||||
|
std::shared_ptr<Tensor> output_tensor_;
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(MindDataTestSwapRedBlueOp, TestOp1) {
|
||||||
|
MS_LOG(INFO) << "Doing testSwapRedBlue.";
|
||||||
|
// SwapRedBlue params
|
||||||
|
std::unique_ptr<SwapRedBlueOp> op(new SwapRedBlueOp());
|
||||||
|
EXPECT_TRUE(op->OneToOne());
|
||||||
|
Status s = op->Compute(input_tensor_, &output_tensor_);
|
||||||
|
size_t actual = 0;
|
||||||
|
if (s == Status::OK()) {
|
||||||
|
actual = output_tensor_->shape()[0] * output_tensor_->shape()[1] * output_tensor_->shape()[2];
|
||||||
|
}
|
||||||
|
EXPECT_EQ(actual, input_tensor_->shape()[0] * input_tensor_->shape()[1] * 3);
|
||||||
|
EXPECT_EQ(s, Status::OK());
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in new issue