| @@ -36,6 +36,10 @@ Status CenterCropOp::Compute(const std::shared_ptr<Tensor> &input, std::shared_p | |||||
| int32_t top = crop_het_ - input->shape()[0]; // number of pixels to pad (top and bottom) | int32_t top = crop_het_ - input->shape()[0]; // number of pixels to pad (top and bottom) | ||||
| int32_t left = crop_wid_ - input->shape()[1]; | int32_t left = crop_wid_ - input->shape()[1]; | ||||
| std::shared_ptr<Tensor> pad_image; | std::shared_ptr<Tensor> 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 | 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); | return Pad(input, output, top / 2 + top % 2, top / 2, left / 2 + left % 2, left / 2, BorderType::kConstant); | ||||
| } else if (top > 0) { | } else if (top > 0) { | ||||
| @@ -56,6 +56,10 @@ Status RandomCropOp::ImagePadding(const std::shared_ptr<Tensor> &input, std::sha | |||||
| *t_pad_left = pad_left_; | *t_pad_left = pad_left_; | ||||
| *t_pad_right = pad_right_; | *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( | RETURN_IF_NOT_OK( | ||||
| Pad(input, pad_image, pad_top_, pad_bottom_, pad_left_, pad_right_, border_type_, fill_r_, fill_g_, fill_b_)); | 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"); | CHECK_FAIL_RETURN_UNEXPECTED((*pad_image)->shape().Size() >= 2, "Abnormal shape"); | ||||
| @@ -20,17 +20,17 @@ | |||||
| #include "utils/log_adapter.h" | #include "utils/log_adapter.h" | ||||
| using namespace mindspore::dataset; | using namespace mindspore::dataset; | ||||
| using mindspore::MsLogLevel::INFO; | |||||
| using mindspore::ExceptionType::NoExceptionType; | |||||
| using mindspore::LogStream; | using mindspore::LogStream; | ||||
| using mindspore::ExceptionType::NoExceptionType; | |||||
| using mindspore::MsLogLevel::INFO; | |||||
| class MindDataTestCenterCropOp : public UT::CVOP::CVOpCommon { | class MindDataTestCenterCropOp : public UT::CVOP::CVOpCommon { | ||||
| public: | public: | ||||
| MindDataTestCenterCropOp() : CVOpCommon() {} | 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<Tensor> output_tensor; | std::shared_ptr<Tensor> output_tensor; | ||||
| int het = 256; | int het = 256; | ||||
| int wid = 128; | int wid = 128; | ||||
| @@ -42,3 +42,16 @@ TEST_F(MindDataTestCenterCropOp, TestOp) { | |||||
| EXPECT_EQ(wid, output_tensor->shape()[1]); | EXPECT_EQ(wid, output_tensor->shape()[1]); | ||||
| std::shared_ptr<CVTensor> p = CVTensor::AsCVTensor(output_tensor); | std::shared_ptr<CVTensor> 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<Tensor> output_tensor; | |||||
| int64_t wid = input_tensor_->shape()[0] * 10 + 1; | |||||
| int64_t het = input_tensor_->shape()[1] * 10 + 1; | |||||
| std::unique_ptr<CenterCropOp> op(new CenterCropOp(het, wid)); | |||||
| Status s = op->Compute(input_tensor_, &output_tensor); | |||||
| EXPECT_TRUE(s.IsError()); | |||||
| ASSERT_TRUE(s.get_code() == StatusCode::kUnexpectedError); | |||||
| } | |||||
| @@ -66,6 +66,7 @@ TEST_F(MindDataTestRandomCropWithBBoxOp, TestOp1) { | |||||
| } | } | ||||
| GlobalContext::config_manager()->set_seed(current_seed); | GlobalContext::config_manager()->set_seed(current_seed); | ||||
| } | } | ||||
| MS_LOG(INFO) << "testRandomCropWithBBoxOp1 end."; | |||||
| } | } | ||||
| TEST_F(MindDataTestRandomCropWithBBoxOp, TestOp2) { | TEST_F(MindDataTestRandomCropWithBBoxOp, TestOp2) { | ||||
| @@ -87,5 +88,22 @@ TEST_F(MindDataTestRandomCropWithBBoxOp, TestOp2) { | |||||
| EXPECT_EQ(s, Status::OK()); | EXPECT_EQ(s, Status::OK()); | ||||
| EXPECT_EQ(4, output_tensor_row_[1]->shape()[1]); // check for existence of 4 columns | 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<RandomCropWithBBoxOp> 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."; | |||||
| } | |||||
| @@ -138,6 +138,17 @@ def test_crop_grayscale(height=375, width=375): | |||||
| assert (c_image.ndim == 3 and c_image.shape[2] == 1) | 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__": | if __name__ == "__main__": | ||||
| test_center_crop_op(600, 600, plot=True) | test_center_crop_op(600, 600, plot=True) | ||||
| test_center_crop_op(300, 600) | test_center_crop_op(300, 600) | ||||
| @@ -241,7 +241,7 @@ def test_random_crop_with_bbox_op_bad_c(): | |||||
| check_bad_bbox(data_voc2, test_op, InvalidBBoxType.WrongShape, "4 features") | 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 | 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))) | 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) | 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__": | if __name__ == "__main__": | ||||
| test_random_crop_with_bbox_op_c(plot_vis=True) | 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_edge_c(plot_vis=True) | ||||
| test_random_crop_with_bbox_op_invalid_c() | test_random_crop_with_bbox_op_invalid_c() | ||||
| test_random_crop_with_bbox_op_bad_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() | |||||