未验证 提交 04f4775b 编写于 作者: Y Yanzhan Yang 提交者: GitHub

fix ios CPU preprocess test=develop (#1972)

* fix ios CPU preprocess test=develop

* fix style test=develop
上级 c49958a2
......@@ -36,7 +36,7 @@ repos:
entry: bash ./tools/codestyle/cpplint_pre_commit.hook
language: system
files: \.(c|cc|cxx|cpp|cu|h|hpp|hxx)$
exclude: ^(mobile/|metal/|web/)
exclude: ^(mobile/) | ^(metal/) | ^(web/)
#- repo: local
#hooks:
#- id: pylint-doc-string
......
......@@ -102,9 +102,9 @@ Executor<Device, T>::Executor(const Program<Device> &program,
}
int count = 0;
#ifdef PADDLE_MOBILE_PROFILE
std::vector<ProfInfo> profile(ops_of_block0_.size());
struct timespec ts;
int op_index = 0;
std::vector<ProfInfo> profile(ops_of_block0_.size());
struct timespec ts;
int op_index = 0;
#endif
for (auto &op_handler : ops_of_block0_) {
#ifdef PADDLE_MOBILE_PROFILE
......@@ -115,8 +115,8 @@ Executor<Device, T>::Executor(const Program<Device> &program,
op_handler->Init();
#ifdef PADDLE_MOBILE_PROFILE
clock_gettime(CLOCK_MONOTONIC, &ts);
profile[op_index].runEnd = (uint64_t)ts.tv_sec * 1e9 + ts.tv_nsec;
++op_index;
profile[op_index].runEnd = (uint64_t)ts.tv_sec * 1e9 + ts.tv_nsec;
++op_index;
#endif
}
#ifdef PADDLE_MOBILE_PROFILE
......
......@@ -139,6 +139,18 @@
*/
- (PaddleMobileCPUResult *)predict:(CGImageRef)image dim:(NSArray<NSNumber *> *)dim means:(NSArray<NSNumber *> *)means scale:(float)scale;
/**
@b 进行预测, means stds和 scale 为训练模型时的预处理参数, 如训练时没有做这些预处理则直接使用 predict, 每一个像素经过这样的预处理 (x + means) * scale, 其中 x 为像素值
@param image 输入图像
@param dim 输入维度
@param means 预处理中 means
@param stds 预处理中 stds
@param scale 预处理中 scale
@return 预测结果
*/
- (PaddleMobileCPUResult *)predict:(CGImageRef)image dim:(NSArray<NSNumber *> *)dim means:(NSArray<NSNumber *> *)means stds:(NSArray<NSNumber *> *)stds scale:(float)scale;
/**
@b 进行预测, 预处理 means 值为 0, scale 值为 1
......
......@@ -181,25 +181,22 @@ static std::mutex shared_mutex;
for (int x = 0; x < wanted_input_width; ++x) {
int in_row = (y * imageHeight) / wanted_input_height;
int in_col = (x * imageWidth) / wanted_input_width;
const UInt8 *in_pixel = input + (in_row * imageWidth * imageChannels) + (in_col * imageChannels);
const UInt8 *in_pixel = input + (in_row * sourceRowBytes) + (in_col * imageChannels);
float *out_pos = out_row + x;
if (c == 0) {
*out_pos = (in_pixel[c] - means[c].floatValue) * scale;
}else if (c == 1){
*out_pos = (in_pixel[c] - means[c].floatValue) * scale;
}else if (c == 2){
*out_pos = (in_pixel[c] - means[c].floatValue) * scale;
}
*out_pos = (in_pixel[2 - c] - means[c].floatValue) * scale;
}
}
}
}
-(void)preprocess:(const UInt8 *)input output:(float *)output imageWidth:(int)imageWidth imageHeight:(int)imageHeight imageChannels:(int)imageChannels means:(NSArray<NSNumber *> *)means scale:(float)scale dim:(std::vector<int64_t>)dim{
-(void)preprocess:(const UInt8 *)input output:(float *)output bytesPerRow:(int)bytesPerRow imageWidth:(int)imageWidth imageHeight:(int)imageHeight imageChannels:(int)imageChannels means:(NSArray<NSNumber *> *)means stds:(NSArray<NSNumber *> *)stds scale:(float)scale dim:(std::vector<int64_t>)dim {
if (means == nil) {
means = @[@0, @0, @0];
}
if (stds == nil) {
stds = @[@1, @1, @1];
}
int wanted_input_width = dim[3];
int wanted_input_height = dim[2];
......@@ -212,15 +209,9 @@ static std::mutex shared_mutex;
for (int x = 0; x < wanted_input_width; ++x) {
int in_row = (y * imageHeight) / wanted_input_height;
int in_col = (x * imageWidth) / wanted_input_width;
const UInt8 *in_pixel = input + (in_row * imageWidth * imageChannels) + (in_col * imageChannels);
const UInt8 *in_pixel = input + (in_row * bytesPerRow) + (in_col * imageChannels);
float *out_pos = out_row + x;
if (c == 0) {
*out_pos = (in_pixel[c] - means[c].floatValue) * scale;
}else if (c == 1){
*out_pos = (in_pixel[c] - means[c].floatValue) * scale;
}else if (c == 2){
*out_pos = (in_pixel[c] - means[c].floatValue) * scale;
}
*out_pos = (in_pixel[2 - c] - means[c].floatValue) / stds[c].floatValue * scale;
}
}
}
......@@ -278,8 +269,7 @@ static std::mutex shared_mutex;
return cpuResult;
}
- (PaddleMobileCPUResult *)predict:(CGImageRef)image dim:(NSArray<NSNumber *> *)dim means:(NSArray<NSNumber *> *)means scale:(float)scale{
// printf(" predict one ");
- (PaddleMobileCPUResult *)predict:(CGImageRef)image dim:(NSArray<NSNumber *> *)dim means:(NSArray<NSNumber *> *)means stds:(NSArray<NSNumber *> *)stds scale:(float)scale {
std::lock_guard<std::mutex> lock(shared_mutex);
if (!loaded_) {
printf("PaddleMobile doesn't be loaded yet");
......@@ -310,7 +300,7 @@ static std::mutex shared_mutex;
// sample image
float *output = (float *)malloc(numel*sizeof(float));
[self preprocess:input output:output imageWidth:image_width imageHeight:image_height imageChannels:image_channels means:means scale:scale dim:dim_vec];
[self preprocess:input output:output bytesPerRow:sourceRowBytes imageWidth:image_width imageHeight:image_height imageChannels:image_channels means:means stds:stds scale:scale dim:dim_vec];
float *dataPointer = nullptr;
if (nullptr != output) {
dataPointer = output;
......@@ -351,7 +341,11 @@ static std::mutex shared_mutex;
}
- (PaddleMobileCPUResult *)predict:(CGImageRef)image dim:(NSArray<NSNumber *> *)dim {
return [self predict:image dim:dim means:nil scale:1];
return [self predict:image dim:dim means:nil stds:nil scale:1];
}
- (PaddleMobileCPUResult *)predict:(CGImageRef)image dim:(NSArray<NSNumber *> *)dim means:(NSArray<NSNumber *> *)means scale:(float)scale {
return [self predict:image dim:dim means:means stds:nil scale:scale];
}
- (PaddleMobileCPUResult *)fetchOutput{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册