From 72861a68f3c72766b2b6a744f28c40bf316d1a77 Mon Sep 17 00:00:00 2001 From: xulei2020 <“xulei83@huawei.com”> Date: Fri, 11 Sep 2020 17:28:11 +0800 Subject: [PATCH] add code --- .../kernels/image/lite_cv/image_process.cc | 28 +++++++++++++------ .../kernels/image/lite_cv/image_process.h | 4 +-- mindspore/lite/CMakeLists.txt | 11 ++++++-- tests/ut/cpp/dataset/image_process_test.cc | 26 ++++++++--------- 4 files changed, 44 insertions(+), 25 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 f1aac99f90..c6c31bc428 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 @@ -208,6 +208,9 @@ static void ResizeBilinear1C(const unsigned char *src, int src_width, int src_he } bool ResizeBilinear(const LiteMat &src, LiteMat &dst, int dst_w, int dst_h) { + if (dst_h <= 0 || dst_w <= 0) { + return false; + } if (src.data_type_ != LDataType::UINT8) { return false; } @@ -269,6 +272,9 @@ static bool ConvertRGBAToGRAY(const unsigned char *data, LDataType data_type, in } bool InitFromPixel(const unsigned char *data, LPixelType pixel_type, LDataType data_type, int w, int h, LiteMat &m) { + if (w <= 0 || h <= 0) { + return false; + } if (pixel_type == LPixelType::RGBA2BGR) { return ConvertRGBAToBGR(data, data_type, w, h, m); } else if (pixel_type == LPixelType::RGBA2GRAY) { @@ -314,7 +320,10 @@ static void CropInternal(const LiteMat &src, LiteMat &dst, int x, int y, int w, } bool Crop(const LiteMat &src, LiteMat &dst, int x, int y, int w, int h) { - if (y < 0 || y + h > src.height_ || x < 0 || x + w > src.width_) { + if (x <= 0 || y <= 0 || w <= 0 || h <= 0) { + return false; + } + if (y + h > src.height_ || x + w > src.width_) { return false; } @@ -371,9 +380,9 @@ bool SubStractMeanNormalize(const LiteMat &src, LiteMat &dst, float *mean, float } template -static void PaddWithConstant(const LiteMat &src, LiteMat &dst, const int top, const int bottom, const int left, - const int right, const PaddBorderType pad_type, uint8_t fill_b_or_gray, uint8_t fill_g, - uint8_t fill_r) { +static void PadWithConstant(const LiteMat &src, LiteMat &dst, const int top, const int bottom, const int left, + const int right, const PaddBorderType pad_type, uint8_t fill_b_or_gray, uint8_t fill_g, + uint8_t fill_r) { dst.Init(src.width_ + left + right, src.height_ + top + bottom, src.channel_, src.data_type_); const T *src_start_p = src; T *dst_start_p = dst; @@ -444,12 +453,15 @@ static void PaddWithConstant(const LiteMat &src, LiteMat &dst, const int top, co } } -bool Padd(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) { +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) { + return false; + } if (pad_type == PADD_BORDER_CONSTANT && src.data_type_ == LDataType::FLOAT32) { - PaddWithConstant(src, dst, top, bottom, left, right, pad_type, fill_b_or_gray, fill_g, fill_r); + PadWithConstant(src, dst, top, bottom, left, right, pad_type, fill_b_or_gray, fill_g, fill_r); } else if (pad_type == PADD_BORDER_CONSTANT && src.data_type_ == LDataType::UINT8) { - PaddWithConstant(src, dst, top, bottom, left, right, pad_type, fill_b_or_gray, fill_g, fill_r); + PadWithConstant(src, dst, top, bottom, left, right, pad_type, fill_b_or_gray, fill_g, fill_r); } else { return false; } diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/image_process.h b/mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/image_process.h index 69243999ba..f4a0ee17c4 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/image_process.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/lite_cv/image_process.h @@ -66,8 +66,8 @@ bool Crop(const LiteMat &src, LiteMat &dst, int x, int y, int w, int h); bool SubStractMeanNormalize(const LiteMat &src, LiteMat &dst, float *mean, float *norm); /// \brief padd image, the channel supports is 3 and 1 -bool Padd(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); +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); /// \brief Apply affine transformation for 1 channel image void Affine(LiteMat &src, LiteMat &out_img, double M[6], std::vector dsize, UINT8_C1 borderValue); diff --git a/mindspore/lite/CMakeLists.txt b/mindspore/lite/CMakeLists.txt index 00c0741633..b6bf6d6dd6 100644 --- a/mindspore/lite/CMakeLists.txt +++ b/mindspore/lite/CMakeLists.txt @@ -210,8 +210,15 @@ if (BUILD_MINDDATA STREQUAL "lite" OR BUILD_MINDDATA STREQUAL "full") endif () if (BUILD_MINDDATA STREQUAL "lite_cv") - # TODO: add sentencepiece dependency - #include(${TOP_DIR}/cmake/external_libs/sentencepiece.cmake) + if (PLATFORM_ARM64) + set(COMPONENT_NAME minddata-arm64-${PROCESS_UNIT}) + elseif (PLATFORM_ARM32) + set(COMPONENT_NAME minddata-arm32-${PROCESS_UNIT}) + elseif (WIN32) + set(COMPONENT_NAME minddata-win-${PROCESS_UNIT}) + else () + set(COMPONENT_NAME minddata-ubuntu-${PROCESS_UNIT}) + endif() add_compile_definitions(ENABLE_ANDROID) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/minddata) endif () diff --git a/tests/ut/cpp/dataset/image_process_test.cc b/tests/ut/cpp/dataset/image_process_test.cc index c0402b7622..d80751509c 100644 --- a/tests/ut/cpp/dataset/image_process_test.cc +++ b/tests/ut/cpp/dataset/image_process_test.cc @@ -51,7 +51,7 @@ LiteMat Lite3CImageProcess(LiteMat &lite_mat_bgr) { MS_LOG(ERROR) << "ResizeBilinear error"; } LiteMat lite_mat_convert_float; - ret = ConvertTo(lite_mat_resize, lite_mat_convert_float, 1.0 ); + ret = ConvertTo(lite_mat_resize, lite_mat_convert_float, 1.0); if (!ret) { MS_LOG(ERROR) << "ConvertTo error"; } @@ -222,7 +222,7 @@ TEST_F(MindDataImageProcess, TestPadd) { ResizeBilinear(lite_mat_bgr, lite_mat_resize, 256, 256); LiteMat makeborder; - Padd(lite_mat_resize, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255); + Pad(lite_mat_resize, makeborder, top, bottom, left, right, PaddBorderType::PADD_BORDER_CONSTANT, 255, 255, 255); cv::Mat dst_image(256 + top + bottom, 256 + left + right, CV_8UC3, makeborder.data_ptr_); @@ -245,7 +245,7 @@ TEST_F(MindDataImageProcess, TestGetDefaultBoxes) { int cols = 4; std::vector benchmark_boxes(rows * cols); std::ifstream in(benchmark, std::ios::in | std::ios::binary); - in.read(reinterpret_cast(benchmark_boxes.data()), benchmark_boxes.size() * sizeof(double)); + in.read(reinterpret_cast(benchmark_boxes.data()), benchmark_boxes.size() * sizeof(double)); in.close(); std::vector> default_boxes = GetDefaultBoxes(config); @@ -284,13 +284,13 @@ TEST_F(MindDataImageProcess, TestAffine) { for (size_t i = 0; i < rows; i++) { for (size_t j = 0; j < cols; j++) { if (i == 2 && j == 2) { - static_cast(src.data_ptr_)[i * cols + j] = 3; + static_cast(src.data_ptr_)[i * cols + j] = 3; } else if (i == 2) { - static_cast(src.data_ptr_)[i * cols + j] = 2; + static_cast(src.data_ptr_)[i * cols + j] = 2; } else if (j == 2) { - static_cast(src.data_ptr_)[i * cols + j] = 1; + static_cast(src.data_ptr_)[i * cols + j] = 1; } else { - static_cast(src.data_ptr_)[i * cols + j] = 0; + static_cast(src.data_ptr_)[i * cols + j] = 0; } } } @@ -305,13 +305,13 @@ TEST_F(MindDataImageProcess, TestAffine) { for (size_t i = 0; i < rows; i++) { for (size_t j = 0; j < cols; j++) { if (i == 2 && j == 2) { - static_cast(expect.data_ptr_)[i * cols + j] = 3; + static_cast(expect.data_ptr_)[i * cols + j] = 3; } else if (i == 2) { - static_cast(expect.data_ptr_)[i * cols + j] = 1; + static_cast(expect.data_ptr_)[i * cols + j] = 1; } else if (j == 2) { - static_cast(expect.data_ptr_)[i * cols + j] = 2; + static_cast(expect.data_ptr_)[i * cols + j] = 2; } else { - static_cast(expect.data_ptr_)[i * cols + j] = 0; + static_cast(expect.data_ptr_)[i * cols + j] = 0; } } } @@ -329,8 +329,8 @@ TEST_F(MindDataImageProcess, TestAffine) { for (size_t i = 0; i < rows; i++) { for (size_t j = 0; j < cols; j++) { - EXPECT_EQ(static_cast(expect.data_ptr_)[i * cols + j].c1, - static_cast(dst.data_ptr_)[i * cols + j].c1); + EXPECT_EQ(static_cast(expect.data_ptr_)[i * cols + j].c1, + static_cast(dst.data_ptr_)[i * cols + j].c1); } } }