!6354 [Data][BUG]add SubStractMeanNormalize input param check

Merge pull request !6354 from xulei/lite_test0905
This commit is contained in:
mindspore-ci-bot 2020-09-17 09:06:19 +08:00 committed by Gitee
commit af1c5aba5b
3 changed files with 52 additions and 14 deletions

View File

@ -17,13 +17,15 @@
#include "lite_cv/image_process.h"
#include <string.h>
#include <math.h>
#include <cmath>
#include <vector>
#include <algorithm>
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<float> &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<float> &mean, const std::vector<float> &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<float> &mean,
const std::vector<float> &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];
}
}
}

View File

@ -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<float> &mean,
const std::vector<float> &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,

View File

@ -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<float> means = {0.485, 0.456, 0.406};
std::vector<float> 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<float> means = {0.485};
std::vector<float> 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;
}