fix bug in random posterize

This commit is contained in:
liyong 2021-09-09 19:50:57 +08:00
parent a006016484
commit 0959c54ae9
3 changed files with 34 additions and 3 deletions

View File

@ -56,7 +56,8 @@ Status CVTensor::CreateFromMat(const cv::Mat &mat, const dsize_t rank, CVTensorP
} else if (mat.dims == 2 && rank == 3) {
shape = TensorShape({mat.rows, mat.cols, mat.channels()});
} else {
RETURN_STATUS_UNEXPECTED("Error in creating CVTensor: Invalid input rank or cv::mat dimension.");
// the info of <C, H, W> tensor is: dims = 3, size = (C, H, W), channels = 1
RETURN_STATUS_UNEXPECTED("CreateFromMat: tensor should be in shape of <H,W,C> or <H,W>.");
}
DataType type = DataType::FromCVType(mat_local.type());
RETURN_IF_NOT_OK(CreateFromMemory(shape, type, mat_local.data, &out_tensor));

View File

@ -39,6 +39,7 @@ Status PosterizeOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_pt
lut_vector.push_back(i & mask_value);
}
cv::Mat in_image = input_cv->mat();
cv::Mat output_img;
CHECK_FAIL_RETURN_UNEXPECTED(in_image.depth() == CV_8U || in_image.depth() == CV_8S,
"Posterize: input image data type can not be float, "

View File

@ -16,6 +16,8 @@
#include "common/common.h"
#include "common/cvop_common.h"
#include "minddata/dataset/kernels/image/random_posterize_op.h"
#include "minddata/dataset/include/dataset/execute.h"
#include "minddata/dataset/include/dataset/vision.h"
#include "minddata/dataset/core/cv_tensor.h"
#include "utils/log_adapter.h"
@ -25,8 +27,6 @@ using mindspore::ExceptionType::NoExceptionType;
using mindspore::MsLogLevel::INFO;
class MindDataTestRandomPosterizeOp : public UT::CVOP::CVOpCommon {
public:
MindDataTestRandomPosterizeOp() : CVOpCommon() {}
};
TEST_F(MindDataTestRandomPosterizeOp, TestOp1) {
@ -39,3 +39,32 @@ TEST_F(MindDataTestRandomPosterizeOp, TestOp1) {
EXPECT_TRUE(s.IsOk());
CheckImageShapeAndData(output_tensor, kRandomPosterize);
}
TEST_F(MindDataTestRandomPosterizeOp, TestOp2) {
// Test Eager RandomPosterize image = (h, w, c)
MS_LOG(INFO) << "Doing VisionRandomPosterizeTest.";
std::shared_ptr<Tensor> de_tensor;
std::string dataset_root_path = "data/dataset";
Tensor::CreateFromFile(dataset_root_path + "/testPK/data/class1/0.jpg", &de_tensor);
auto image = mindspore::MSTensor(std::make_shared<DETensor>(de_tensor));
std::shared_ptr<TensorTransform> decode_op = std::make_shared<vision::Decode>();
std::shared_ptr<TensorTransform> randomposterize_op(new vision::RandomPosterize({3, 5}));
auto transform = Execute({decode_op, randomposterize_op});
Status rc = transform(image, &image);
EXPECT_TRUE(rc.IsOk());
EXPECT_EQ(image.Shape().size(), 3);
EXPECT_EQ(image.Shape()[2], 3);
}
TEST_F(MindDataTestRandomPosterizeOp, TestOp3) {
// Test Eager RandomSolarize image.size = {2, 2, 2, 1}
MS_LOG(INFO) << "Doing VisionRandomSolarizeTest.";
std::shared_ptr<Tensor> de_tensor;
Tensor::CreateFromVector(std::vector<uint8_t>({0, 25, 120, 0, 38, 2, 10, 13}), TensorShape({2, 2, 2, 1}), &de_tensor);
auto image = mindspore::MSTensor(std::make_shared<DETensor>(de_tensor));
std::shared_ptr<TensorTransform> randomsolarize_op(new vision::RandomSolarize({12, 25}));
auto transform = Execute({randomsolarize_op});
Status rc = transform(image, &image);
EXPECT_TRUE(rc.IsError());
}