diff --git a/mindspore/ccsrc/dataset/kernels/image/image_utils.cc b/mindspore/ccsrc/dataset/kernels/image/image_utils.cc index 656e44c331..a852a45014 100644 --- a/mindspore/ccsrc/dataset/kernels/image/image_utils.cc +++ b/mindspore/ccsrc/dataset/kernels/image/image_utils.cc @@ -760,10 +760,18 @@ Status UpdateBBoxesForCrop(std::shared_ptr *bboxList, size_t *bboxCount, correct_ind.push_back(i); // adjust BBox corners by bringing into new CropBox if beyond // Also reseting/adjusting for boxes to lie within CropBox instead of Image - subtract CropBox Xmin/YMin - bb_Xmin = bb_Xmin - (std::min(static_cast(0.0), (bb_Xmin - CB_Xmin)) + CB_Xmin); - bb_Xmax = bb_Xmax - (std::max(static_cast(0.0), (bb_Xmax - CB_Xmax)) + CB_Xmin); - bb_Ymin = bb_Ymin - (std::min(static_cast(0.0), (bb_Ymin - CB_Ymin)) + CB_Ymin); - bb_Ymax = bb_Ymax - (std::max(static_cast(0.0), (bb_Ymax - CB_Ymax)) + CB_Ymin); + + bb_Xmin = bb_Xmin - std::min(static_cast(0.0), (bb_Xmin - CB_Xmin)) - CB_Xmin; + bb_Xmax = bb_Xmax - std::max(static_cast(0.0), (bb_Xmax - CB_Xmax)) - CB_Xmin; + bb_Ymin = bb_Ymin - std::min(static_cast(0.0), (bb_Ymin - CB_Ymin)) - CB_Ymin; + bb_Ymax = bb_Ymax - std::max(static_cast(0.0), (bb_Ymax - CB_Ymax)) - CB_Ymin; + + // bound check for float values + bb_Xmin = std::max(bb_Xmin, static_cast(0)); + bb_Ymin = std::max(bb_Ymin, static_cast(0)); + bb_Xmax = std::min(bb_Xmax, static_cast(CB_Xmax - CB_Xmin)); // find max value relative to new image + bb_Ymax = std::min(bb_Ymax, static_cast(CB_Ymax - CB_Ymin)); + // reset min values and calculate width/height from Box corners RETURN_IF_NOT_OK((*bboxList)->SetItemAt({i, 0}, bb_Xmin)); RETURN_IF_NOT_OK((*bboxList)->SetItemAt({i, 1}, bb_Ymin)); diff --git a/tests/ut/cpp/dataset/CMakeLists.txt b/tests/ut/cpp/dataset/CMakeLists.txt index 496afe1ae9..30fd39146f 100644 --- a/tests/ut/cpp/dataset/CMakeLists.txt +++ b/tests/ut/cpp/dataset/CMakeLists.txt @@ -36,14 +36,17 @@ SET(DE_UT_SRCS project_op_test.cc queue_test.cc random_crop_op_test.cc + random_crop_with_bbox_op_test.cc random_crop_decode_resize_op_test.cc random_crop_and_resize_op_test.cc + random_crop_and_resize_with_bbox_op_test.cc random_color_adjust_op_test.cc random_horizontal_flip_op_test.cc random_horizontal_flip_with_bbox_test.cc random_resize_op_test.cc random_rotation_op_test.cc random_vertical_flip_op_test.cc + random_vertical_flip_with_bbox_op_test.cc rename_op_test.cc repeat_op_test.cc skip_op_test.cc diff --git a/tests/ut/cpp/dataset/random_crop_and_resize_with_bbox_op_test.cc b/tests/ut/cpp/dataset/random_crop_and_resize_with_bbox_op_test.cc new file mode 100644 index 0000000000..a1d4481f55 --- /dev/null +++ b/tests/ut/cpp/dataset/random_crop_and_resize_with_bbox_op_test.cc @@ -0,0 +1,99 @@ +/** + * 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/bboxop_common.h" +#include "dataset/kernels/image/random_crop_and_resize_with_bbox_op.h" +#include "utils/log_adapter.h" + +#include "dataset/core/config_manager.h" +#include "dataset/core/global_context.h" + +using namespace mindspore::dataset; +using mindspore::LogStream; +using mindspore::ExceptionType::NoExceptionType; +using mindspore::MsLogLevel::INFO; + +const bool kSaveExpected = false; +const char kOpName[] = "RandomResizedCropWithBBox_C"; + +class MindDataTestRandomCropAndResizeWithBBoxOp : public UT::CVOP::BBOXOP::BBoxOpCommon { + protected: + MindDataTestRandomCropAndResizeWithBBoxOp() : BBoxOpCommon() {} +}; + +TEST_F(MindDataTestRandomCropAndResizeWithBBoxOp, TestOp1) { + MS_LOG(INFO) << "Doing testRandomCropAndResizeWithBBoxOp1."; + // setting seed here + uint32_t current_seed = GlobalContext::config_manager()->seed(); + GlobalContext::config_manager()->set_seed(327362); + TensorRow output_tensor_row_; + TensorTable results; + int h_out = 1024; + int w_out = 2048; + float aspect_lb = 2; + float aspect_ub = 2.5; + float scale_lb = 0.2; + float scale_ub = 2.0; + auto op = std::make_unique(h_out, w_out, scale_lb, scale_ub, aspect_lb, aspect_ub); + Status s; + for (auto tensor_row_ : images_and_annotations_) { + s = op->Compute(tensor_row_, &output_tensor_row_); + EXPECT_TRUE(s.IsOk()); + results.push_back(output_tensor_row_); + } + if (kSaveExpected) { + SaveImagesWithAnnotations(FileType::kExpected, std::string(kOpName), results); + } + SaveImagesWithAnnotations(FileType::kActual, std::string(kOpName), results); + if (!kSaveExpected) { + CompareActualAndExpected(std::string(kOpName)); + } + GlobalContext::config_manager()->set_seed(current_seed); +} + +TEST_F(MindDataTestRandomCropAndResizeWithBBoxOp, TestOp2) { + MS_LOG(INFO) << "Doing testRandomCropAndResizeWithBBoxOp2."; + TensorRow output_tensor_row_; + int h_out = 1024; + int w_out = 2048; + float aspect_lb = 1; + float aspect_ub = 1.5; + float scale_lb = 0.2; + float scale_ub = 2.0; + auto op = std::make_unique(h_out, w_out, scale_lb, scale_ub, aspect_lb, aspect_ub); + Status s; + for (auto tensor_row_ : images_and_annotations_) { + s = op->Compute(tensor_row_, &output_tensor_row_); + EXPECT_TRUE(s.IsOk()); + } +} + +TEST_F(MindDataTestRandomCropAndResizeWithBBoxOp, TestOp3) { + MS_LOG(INFO) << "Doing testRandomCropAndResizeWithBBoxOp3."; + TensorRow output_tensor_row_; + int h_out = 1024; + int w_out = 2048; + float aspect_lb = 0.2; + float aspect_ub = 3; + float scale_lb = 0.2; + float scale_ub = 2.0; + auto op = std::make_unique(h_out, w_out, scale_lb, scale_ub, aspect_lb, aspect_ub); + Status s; + for (auto tensor_row_ : images_and_annotations_) { + s = op->Compute(tensor_row_, &output_tensor_row_); + EXPECT_TRUE(s.IsOk()); + } + MS_LOG(INFO) << "testRandomCropAndResizeWithBBoxOp end."; +} \ No newline at end of file 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 new file mode 100644 index 0000000000..3790574e02 --- /dev/null +++ b/tests/ut/cpp/dataset/random_crop_with_bbox_op_test.cc @@ -0,0 +1,91 @@ +/** + * 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/bboxop_common.h" +#include "dataset/kernels/image/random_crop_with_bbox_op.h" +#include "utils/log_adapter.h" + +#include "dataset/core/config_manager.h" +#include "dataset/core/global_context.h" + +using namespace mindspore::dataset; +using mindspore::LogStream; +using mindspore::ExceptionType::NoExceptionType; +using mindspore::MsLogLevel::INFO; + +const bool kSaveExpected = false; +const char kOpName[] = "RandomCropWithBBox_C"; + +class MindDataTestRandomCropWithBBoxOp : public UT::CVOP::BBOXOP::BBoxOpCommon { + protected: + MindDataTestRandomCropWithBBoxOp() : BBoxOpCommon() {} + TensorRow output_tensor_row_; +}; + +TEST_F(MindDataTestRandomCropWithBBoxOp, TestOp1) { + MS_LOG(INFO) << "Doing testRandomCropWithBBoxOp1."; + TensorTable results; + unsigned int crop_height = 128; + unsigned int crop_width = 128; + // setting seed here + uint32_t current_seed = GlobalContext::config_manager()->seed(); + GlobalContext::config_manager()->set_seed(327362); + std::unique_ptr op( + new RandomCropWithBBoxOp(crop_height, crop_width, 0, 0, 0, 0, BorderType::kConstant, false)); + for (auto tensor_row_ : images_and_annotations_) { + Status s = op->Compute(tensor_row_, &output_tensor_row_); + size_t actual = 0; + if (s == Status::OK()) { + TensorShape get_shape = output_tensor_row_[0]->shape(); + actual = get_shape[0] * get_shape[1] * get_shape[2]; + results.push_back(output_tensor_row_); + } + EXPECT_EQ(actual, crop_height * crop_width * 3); + EXPECT_EQ(s, Status::OK()); + EXPECT_EQ(4, output_tensor_row_[1]->shape()[1]); // check for existence of 4 columns + // Compare Code + if (kSaveExpected) { + SaveImagesWithAnnotations(FileType::kExpected, std::string(kOpName), results); + } + SaveImagesWithAnnotations(FileType::kActual, std::string(kOpName), results); + if (!kSaveExpected) { + CompareActualAndExpected(std::string(kOpName)); + } + GlobalContext::config_manager()->set_seed(current_seed); + } +} + +TEST_F(MindDataTestRandomCropWithBBoxOp, TestOp2) { + MS_LOG(INFO) << "Doing testRandomCropWithBBoxOp2."; + // Crop params + unsigned int crop_height = 1280; + unsigned int crop_width = 1280; + std::unique_ptr op( + new RandomCropWithBBoxOp(crop_height, crop_width, 513, 513, 513, 513, BorderType::kConstant, false)); + + for (auto tensor_row_ : images_and_annotations_) { + Status s = op->Compute(tensor_row_, &output_tensor_row_); + size_t actual = 0; + if (s == Status::OK()) { + TensorShape get_shape = output_tensor_row_[0]->shape(); + actual = get_shape[0] * get_shape[1] * get_shape[2]; + } + EXPECT_EQ(actual, crop_height * crop_width * 3); + 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."; +} diff --git a/tests/ut/cpp/dataset/random_vertical_flip_with_bbox_op_test.cc b/tests/ut/cpp/dataset/random_vertical_flip_with_bbox_op_test.cc new file mode 100644 index 0000000000..2fea8c6c34 --- /dev/null +++ b/tests/ut/cpp/dataset/random_vertical_flip_with_bbox_op_test.cc @@ -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 "common/bboxop_common.h" +#include "dataset/kernels/image/random_vertical_flip_with_bbox_op.h" +#include "utils/log_adapter.h" + +using namespace mindspore::dataset; +using mindspore::LogStream; +using mindspore::ExceptionType::NoExceptionType; +using mindspore::MsLogLevel::INFO; + +const bool kSaveExpected = false; +const char kOpName[] = "RandomVerticalFlipWithBBox_C"; + +class MindDataTestRandomVerticalFlipWithBBoxOp : public UT::CVOP::BBOXOP::BBoxOpCommon { + protected: + MindDataTestRandomVerticalFlipWithBBoxOp() : BBoxOpCommon() {} +}; +TEST_F(MindDataTestRandomVerticalFlipWithBBoxOp, TestOp) { + MS_LOG(INFO) << "Doing testRandomVerticalFlipWithBBoxOp."; + TensorTable results; + std::unique_ptr op(new RandomVerticalFlipWithBBoxOp(1)); + for (const auto &tensor_row_ : images_and_annotations_) { + TensorRow output_row; + Status s = op->Compute(tensor_row_, &output_row); + EXPECT_TRUE(s.IsOk()); + results.push_back(output_row); + } + if (kSaveExpected) { + SaveImagesWithAnnotations(FileType::kExpected, std::string(kOpName), results); + } + SaveImagesWithAnnotations(FileType::kActual, std::string(kOpName), results); + if (!kSaveExpected) { + CompareActualAndExpected(std::string(kOpName)); + } + MS_LOG(INFO) << "testRandomVerticalFlipWithBBoxOp end."; +} diff --git a/tests/ut/data/dataset/imagefolder/ExpectedRandomCropWithBBox_C0.jpg b/tests/ut/data/dataset/imagefolder/ExpectedRandomCropWithBBox_C0.jpg new file mode 100644 index 0000000000..362d841170 Binary files /dev/null and b/tests/ut/data/dataset/imagefolder/ExpectedRandomCropWithBBox_C0.jpg differ diff --git a/tests/ut/data/dataset/imagefolder/ExpectedRandomResizedCropWithBBox_C0.jpg b/tests/ut/data/dataset/imagefolder/ExpectedRandomResizedCropWithBBox_C0.jpg new file mode 100644 index 0000000000..d7666adb9b Binary files /dev/null and b/tests/ut/data/dataset/imagefolder/ExpectedRandomResizedCropWithBBox_C0.jpg differ diff --git a/tests/ut/data/dataset/imagefolder/ExpectedRandomVerticalFlipWithBBox_C0.jpg b/tests/ut/data/dataset/imagefolder/ExpectedRandomVerticalFlipWithBBox_C0.jpg new file mode 100644 index 0000000000..c5fe8ff540 Binary files /dev/null and b/tests/ut/data/dataset/imagefolder/ExpectedRandomVerticalFlipWithBBox_C0.jpg differ