From bc91f7b3ac26c348a5c706d81b33c28b366cc7e4 Mon Sep 17 00:00:00 2001 From: jiangzhiwen Date: Wed, 18 Nov 2020 15:35:25 +0800 Subject: [PATCH] fix pad zero --- .../kernels/image/lite_cv/image_process.cc | 2 +- mindspore/lite/minddata/CMakeLists.txt | 1 + tests/ut/cpp/dataset/image_process_test.cc | 53 +++++++++++++++---- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/image_process.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/image_process.cc index 3c9fa57ea5..59296c885a 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/image_process.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/image_process.cc @@ -716,7 +716,7 @@ bool Merge(const std::vector &mv, LiteMat &dst) { bool Pad(const LiteMat &src, LiteMat &dst, int top, int bottom, int left, int right, PaddBorderType pad_type, uint8_t fill_b_or_gray, uint8_t fill_g, uint8_t fill_r) { - if (top < 0 || bottom <= 0 || left < 0 || right < 0) { + if (top < 0 || bottom < 0 || left < 0 || right < 0) { return false; } if (src.IsEmpty()) { diff --git a/mindspore/lite/minddata/CMakeLists.txt b/mindspore/lite/minddata/CMakeLists.txt index 576c829b35a..7e312147b4 100644 --- a/mindspore/lite/minddata/CMakeLists.txt +++ b/mindspore/lite/minddata/CMakeLists.txt @@ -2,6 +2,7 @@ set(MINDDATA_DIR ${CCSRC_DIR}/minddata/dataset) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -g2 -ggdb -fno-inline-functions -fno-omit-frame-pointer -D_LIBCPP_INLINE_VISIBILITY='' -D_LIBCPP_DISABLE_EXTERN_TEMPLATE=1 -DHALF_ENABLE_CPP11_USER_LITERALS=0 -D_FORTIFY_SOURCE=2 -Wno-cpp") +set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -Werror -Wno-return-std-move -Wno-unused-private-field -Wno-unused-lambda-capture -Wno-sign-compare -Wno-overloaded-virtual -Wno-unneeded-internal-declaration -Wno-unused-variable -Wno-pessimizing-move -Wno-inconsistent-missing-override") set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} -I/usr/local/include -std=c++17 -Wall -fPIC") diff --git a/tests/ut/cpp/dataset/image_process_test.cc b/tests/ut/cpp/dataset/image_process_test.cc index 717d494770..447a464fda 100644 --- a/tests/ut/cpp/dataset/image_process_test.cc +++ b/tests/ut/cpp/dataset/image_process_test.cc @@ -387,15 +387,13 @@ TEST_F(MindDataImageProcess, TestPadd) { std::string filename = "data/dataset/apple.jpg"; cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR); - cv::Mat resize_256_image; - cv::resize(image, resize_256_image, cv::Size(256, 256), CV_INTER_LINEAR); int left = 10; - int right = 10; - int top = 10; - int bottom = 10; + int right = 20; + int top = 30; + int bottom = 40; cv::Mat b_image; cv::Scalar color = cv::Scalar(255, 255, 255); - cv::copyMakeBorder(resize_256_image, b_image, top, bottom, left, right, cv::BORDER_CONSTANT, color); + cv::copyMakeBorder(image, b_image, top, bottom, left, right, cv::BORDER_CONSTANT, color); cv::Mat rgba_mat; cv::cvtColor(image, rgba_mat, CV_BGR2RGBA); @@ -403,13 +401,48 @@ TEST_F(MindDataImageProcess, TestPadd) { bool ret = InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr); ASSERT_TRUE(ret == true); - LiteMat lite_mat_resize; - ret = ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256); ASSERT_TRUE(ret == true); LiteMat makeborder; - ret = Pad(lite_mat_resize, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255); + ret = Pad(lite_mat_bgr, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255); ASSERT_TRUE(ret == true); - cv::Mat dst_image(256 + top + bottom, 256 + left + right, CV_8UC3, makeborder.data_ptr_); + size_t total_size = makeborder.height_ * makeborder.width_ * makeborder.channel_; + double distance = 0.0f; + for (size_t i = 0; i < total_size; i++) { + distance += pow((uint8_t)b_image.data[i] - ((uint8_t*)makeborder)[i], 2); + } + distance = sqrt(distance / total_size); + EXPECT_EQ(distance, 0.0f); +} + +TEST_F(MindDataImageProcess, TestPadZero) { + std::string filename = "data/dataset/apple.jpg"; + cv::Mat image = cv::imread(filename, cv::ImreadModes::IMREAD_COLOR); + + int left = 0; + int right = 0; + int top = 0; + int bottom = 0; + cv::Mat b_image; + cv::Scalar color = cv::Scalar(255, 255, 255); + cv::copyMakeBorder(image, b_image, top, bottom, left, right, cv::BORDER_CONSTANT, color); + cv::Mat rgba_mat; + cv::cvtColor(image, rgba_mat, CV_BGR2RGBA); + + LiteMat lite_mat_bgr; + bool ret = + InitFromPixel(rgba_mat.data, LPixelType::RGBA2BGR, LDataType::UINT8, rgba_mat.cols, rgba_mat.rows, lite_mat_bgr); + ASSERT_TRUE(ret == true); + ASSERT_TRUE(ret == true); + LiteMat makeborder; + ret = Pad(lite_mat_bgr, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255); + ASSERT_TRUE(ret == true); + size_t total_size = makeborder.height_ * makeborder.width_ * makeborder.channel_; + double distance = 0.0f; + for (size_t i = 0; i < total_size; i++) { + distance += pow((uint8_t)b_image.data[i] - ((uint8_t*)makeborder)[i], 2); + } + distance = sqrt(distance / total_size); + EXPECT_EQ(distance, 0.0f); } TEST_F(MindDataImageProcess, TestGetDefaultBoxes) {