From 85962adffd7cb072af3f37391f5bf8f2a1924cf4 Mon Sep 17 00:00:00 2001 From: xulei2020 <“xulei83@huawei.com”> Date: Wed, 16 Sep 2020 15:55:11 +0800 Subject: [PATCH] add code --- .../kernels/image/lite_cv/image_process.cc | 51 ++++++++++++++++--- .../kernels/image/lite_cv/image_process.h | 3 +- tests/ut/cpp/dataset/image_process_test.cc | 12 ++--- 3 files changed, 52 insertions(+), 14 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 fd15e13059b..003796a707d 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 @@ -17,13 +17,15 @@ #include "lite_cv/image_process.h" #include -#include +#include #include #include namespace mindspore { namespace dataset { +#define Equ(a, b) ((std::fabs((a) - (b)) < 1e-6)) + static inline void InitBilinearWeight(int *data_ptr, int16_t *weight_ptr, double scale, int dst_length, int src_length, int a) { const int RESIZE_SCALE = 1 << 11; @@ -359,16 +361,51 @@ bool Crop(const LiteMat &src, LiteMat &dst, int x, int y, int w, int h) { return true; } -bool SubStractMeanNormalize(const LiteMat &src, LiteMat &dst, float *mean, float *norm) { +static bool CheckZero(const std::vector &vs) { + for (int i = 0; i < vs.size(); i++) { + if (Equ(vs[i], 0.0f)) { + return true; + } + } + return false; +} + +static bool CheckMeanAndStd(int channel, const std::vector &mean, const std::vector &std) { + if (mean.size() == 0 && std.size() == 0) { + return false; + } + if (mean.size() > 0) { + if (CheckZero(mean)) { + return false; + } + if (mean.size() != channel) { + return false; + } + } + if (std.size() > 0) { + if (CheckZero(std)) { + return false; + } + if (std.size() != channel) { + return false; + } + } + return true; +} +bool SubStractMeanNormalize(const LiteMat &src, LiteMat &dst, const std::vector &mean, + const std::vector &std) { if (src.data_type_ != LDataType::FLOAT32) { return false; } + if (!CheckMeanAndStd(src.channel_, mean, std)) { + return false; + } dst.Init(src.width_, src.height_, src.channel_, LDataType::FLOAT32); const float *src_start_p = src; float *dst_start_p = dst; - if (mean && !norm) { + if ((!mean.empty()) && std.empty()) { for (int h = 0; h < src.height_; h++) { for (int w = 0; w < src.width_; w++) { for (int c = 0; c < src.channel_; c++) { @@ -377,21 +414,21 @@ bool SubStractMeanNormalize(const LiteMat &src, LiteMat &dst, float *mean, float } } } - } else if (!mean && norm) { + } else if (mean.empty() && (!std.empty())) { for (int h = 0; h < src.height_; h++) { for (int w = 0; w < src.width_; w++) { for (int c = 0; c < src.channel_; c++) { int index = (h * src.width_ + w) * src.channel_ + c; - dst_start_p[index] = src_start_p[index] * norm[c]; + dst_start_p[index] = src_start_p[index] / std[c]; } } } - } else if (mean && norm) { + } else if ((!mean.empty()) && (!std.empty())) { for (int h = 0; h < src.height_; h++) { for (int w = 0; w < src.width_; w++) { for (int c = 0; c < src.channel_; c++) { int index = (h * src.width_ + w) * src.channel_ + c; - dst_start_p[index] = (src_start_p[index] - mean[c]) * norm[c]; + dst_start_p[index] = (src_start_p[index] - mean[c]) / std[c]; } } } 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 f4a0ee17c41..d03b1b28e26 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 @@ -63,7 +63,8 @@ bool ConvertTo(const LiteMat &src, LiteMat &dst, double scale = 1.0); bool Crop(const LiteMat &src, LiteMat &dst, int x, int y, int w, int h); /// \brief normalize image, currently the supports data type is float -bool SubStractMeanNormalize(const LiteMat &src, LiteMat &dst, float *mean, float *norm); +bool SubStractMeanNormalize(const LiteMat &src, LiteMat &dst, const std::vector &mean, + const std::vector &std); /// \brief padd image, the channel supports is 3 and 1 bool Pad(const LiteMat &src, LiteMat &dst, int top, int bottom, int left, int right, PaddBorderType pad_type, diff --git a/tests/ut/cpp/dataset/image_process_test.cc b/tests/ut/cpp/dataset/image_process_test.cc index d80751509c4..0f21f5d7e95 100644 --- a/tests/ut/cpp/dataset/image_process_test.cc +++ b/tests/ut/cpp/dataset/image_process_test.cc @@ -62,11 +62,11 @@ LiteMat Lite3CImageProcess(LiteMat &lite_mat_bgr) { MS_LOG(ERROR) << "Crop error"; } - float means[3] = {0.485, 0.456, 0.406}; - float vars[3] = {1.0 / 0.229, 1.0 / 0.224, 1.0 / 0.225}; + std::vector means = {0.485, 0.456, 0.406}; + std::vector stds = {0.229, 0.224, 0.225}; LiteMat lite_norm_mat_cut; - SubStractMeanNormalize(lite_mat_crop, lite_norm_mat_cut, means, vars); + SubStractMeanNormalize(lite_mat_crop, lite_norm_mat_cut, means, stds); return lite_norm_mat_cut; } @@ -138,12 +138,12 @@ LiteMat Lite1CImageProcess(LiteMat &lite_mat_bgr) { Crop(lite_mat_convert_float, lite_mat_cut, 16, 16, 224, 224); - float means[1] = {0.485}; - float vars[1] = {1.0 / 0.229}; + std::vector means = {0.485}; + std::vector stds = {0.229}; LiteMat lite_norm_mat_cut; - SubStractMeanNormalize(lite_mat_cut, lite_norm_mat_cut, means, vars); + SubStractMeanNormalize(lite_mat_cut, lite_norm_mat_cut, means, stds); return lite_norm_mat_cut; }