diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/center_crop_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/center_crop_op.cc index 35079b05cd..5bb9820c08 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/center_crop_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/center_crop_op.cc @@ -36,6 +36,10 @@ Status CenterCropOp::Compute(const std::shared_ptr &input, std::shared_p int32_t top = crop_het_ - input->shape()[0]; // number of pixels to pad (top and bottom) int32_t left = crop_wid_ - input->shape()[1]; std::shared_ptr pad_image; + + CHECK_FAIL_RETURN_UNEXPECTED((top < input->shape()[0] * 10 && left < input->shape()[1] * 10), + "CenterCropOp padding size is too big, it's more than 10 times the original size."); + if (top > 0 && left > 0) { // padding only return Pad(input, output, top / 2 + top % 2, top / 2, left / 2 + left % 2, left / 2, BorderType::kConstant); } else if (top > 0) { diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_op.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_op.cc index 51772e9ec3..c53e0c06d2 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_op.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/random_crop_op.cc @@ -56,6 +56,10 @@ Status RandomCropOp::ImagePadding(const std::shared_ptr &input, std::sha *t_pad_left = pad_left_; *t_pad_right = pad_right_; + CHECK_FAIL_RETURN_UNEXPECTED(pad_top_ < input->shape()[0] * 3 && pad_bottom_ < input->shape()[0] * 3 && + pad_left_ < input->shape()[1] * 3 && pad_right_ < input->shape()[1] * 3, + "RandomCropBBoxOp padding size is too big, it's more than 3 times the original size."); + RETURN_IF_NOT_OK( Pad(input, pad_image, pad_top_, pad_bottom_, pad_left_, pad_right_, border_type_, fill_r_, fill_g_, fill_b_)); CHECK_FAIL_RETURN_UNEXPECTED((*pad_image)->shape().Size() >= 2, "Abnormal shape"); diff --git a/tests/ut/cpp/dataset/center_crop_op_test.cc b/tests/ut/cpp/dataset/center_crop_op_test.cc index cd0f362f64..20e2be3991 100644 --- a/tests/ut/cpp/dataset/center_crop_op_test.cc +++ b/tests/ut/cpp/dataset/center_crop_op_test.cc @@ -20,17 +20,17 @@ #include "utils/log_adapter.h" using namespace mindspore::dataset; -using mindspore::MsLogLevel::INFO; -using mindspore::ExceptionType::NoExceptionType; using mindspore::LogStream; +using mindspore::ExceptionType::NoExceptionType; +using mindspore::MsLogLevel::INFO; class MindDataTestCenterCropOp : public UT::CVOP::CVOpCommon { public: MindDataTestCenterCropOp() : CVOpCommon() {} }; -TEST_F(MindDataTestCenterCropOp, TestOp) { - MS_LOG(INFO) << "Doing MindDataTestCenterCropOp::TestOp."; +TEST_F(MindDataTestCenterCropOp, TestOp1) { + MS_LOG(INFO) << "Doing MindDataTestCenterCropOp::TestOp1."; std::shared_ptr output_tensor; int het = 256; int wid = 128; @@ -42,3 +42,16 @@ TEST_F(MindDataTestCenterCropOp, TestOp) { EXPECT_EQ(wid, output_tensor->shape()[1]); std::shared_ptr p = CVTensor::AsCVTensor(output_tensor); } + +TEST_F(MindDataTestCenterCropOp, TestOp2) { + MS_LOG(INFO) << "MindDataTestCenterCropOp::TestOp2. Cap valid crop size at 10 times the input size"; + std::shared_ptr output_tensor; + + int64_t wid = input_tensor_->shape()[0] * 10 + 1; + int64_t het = input_tensor_->shape()[1] * 10 + 1; + + std::unique_ptr op(new CenterCropOp(het, wid)); + Status s = op->Compute(input_tensor_, &output_tensor); + EXPECT_TRUE(s.IsError()); + ASSERT_TRUE(s.get_code() == StatusCode::kUnexpectedError); +} diff --git a/tests/ut/cpp/dataset/random_crop_with_bbox_op_test.cc b/tests/ut/cpp/dataset/random_crop_with_bbox_op_test.cc index fcf8ba2605..b3b46f09ec 100644 --- a/tests/ut/cpp/dataset/random_crop_with_bbox_op_test.cc +++ b/tests/ut/cpp/dataset/random_crop_with_bbox_op_test.cc @@ -66,6 +66,7 @@ TEST_F(MindDataTestRandomCropWithBBoxOp, TestOp1) { } GlobalContext::config_manager()->set_seed(current_seed); } + MS_LOG(INFO) << "testRandomCropWithBBoxOp1 end."; } TEST_F(MindDataTestRandomCropWithBBoxOp, TestOp2) { @@ -87,5 +88,22 @@ TEST_F(MindDataTestRandomCropWithBBoxOp, TestOp2) { EXPECT_EQ(s, Status::OK()); EXPECT_EQ(4, output_tensor_row_[1]->shape()[1]); // check for existence of 4 columns } - MS_LOG(INFO) << "testRandomCropWithBBoxOp end."; + MS_LOG(INFO) << "testRandomCropWithBBoxOp2 end."; } + +TEST_F(MindDataTestRandomCropWithBBoxOp, TestOp3) { + MS_LOG(INFO) << "Doing testRandomCropWithBBoxOp3."; + // Crop params + unsigned int crop_height = 1280; + unsigned int crop_width = 1280; + std::unique_ptr op(new RandomCropWithBBoxOp(crop_height, crop_width, crop_height * 3 + 1, + crop_height * 3 + 1, crop_width * 3 + 1, + crop_width * 3 + 1, BorderType::kConstant, false)); + + for (auto tensor_row_ : images_and_annotations_) { + Status s = op->Compute(tensor_row_, &output_tensor_row_); + EXPECT_TRUE(s.IsError()); + ASSERT_TRUE(s.get_code() == StatusCode::kUnexpectedError); + } + MS_LOG(INFO) << "testRandomCropWithBBoxOp3 end."; +} \ No newline at end of file diff --git a/tests/ut/python/dataset/test_center_crop.py b/tests/ut/python/dataset/test_center_crop.py index 6dfa9fc7c3..746fbd4751 100644 --- a/tests/ut/python/dataset/test_center_crop.py +++ b/tests/ut/python/dataset/test_center_crop.py @@ -138,6 +138,17 @@ def test_crop_grayscale(height=375, width=375): assert (c_image.ndim == 3 and c_image.shape[2] == 1) +def test_center_crop_errors(): + """ + Test that CenterCropOp errors with bad input + """ + try: + test_center_crop_op(16777216, 16777216) + except RuntimeError as e: + assert "Unexpected error. CenterCropOp padding size is too big, it's more than 10 times the original size." in \ + str(e) + + if __name__ == "__main__": test_center_crop_op(600, 600, plot=True) test_center_crop_op(300, 600) diff --git a/tests/ut/python/dataset/test_random_crop_with_bbox.py b/tests/ut/python/dataset/test_random_crop_with_bbox.py index 00e9e9d077..97a8d2ee86 100644 --- a/tests/ut/python/dataset/test_random_crop_with_bbox.py +++ b/tests/ut/python/dataset/test_random_crop_with_bbox.py @@ -241,7 +241,7 @@ def test_random_crop_with_bbox_op_bad_c(): check_bad_bbox(data_voc2, test_op, InvalidBBoxType.WrongShape, "4 features") -def test_random_crop_with_bbox_op_negative_padding(): +def test_random_crop_with_bbox_op_bad_padding(): """ Test RandomCropWithBBox Op on invalid constructor parameters, expected to raise ValueError """ @@ -263,6 +263,20 @@ def test_random_crop_with_bbox_op_negative_padding(): logger.info("Got an exception in DE: {}".format(str(err))) assert "Input padding is not within the required interval of (0 to 2147483647)." in str(err) + try: + test_op = c_vision.RandomCropWithBBox([512, 512], padding=[16777216, 16777216, 16777216, 16777216]) + + dataVoc2 = dataVoc2.map(input_columns=["image", "annotation"], + output_columns=["image", "annotation"], + columns_order=["image", "annotation"], + operations=[test_op]) + + for _ in dataVoc2.create_dict_iterator(): + break + except RuntimeError as err: + logger.info("Got an exception in DE: {}".format(str(err))) + assert "RandomCropBBoxOp padding size is too big, it\'s more than 3 times the original size." in str(err) + if __name__ == "__main__": test_random_crop_with_bbox_op_c(plot_vis=True) @@ -272,4 +286,4 @@ if __name__ == "__main__": test_random_crop_with_bbox_op_edge_c(plot_vis=True) test_random_crop_with_bbox_op_invalid_c() test_random_crop_with_bbox_op_bad_c() - test_random_crop_with_bbox_op_negative_padding() + test_random_crop_with_bbox_op_bad_padding()