paddle-lite口罩分类模型android端部署,识别错误率高
Created by: 841384426
模型来源:hub下载 import paddlehub as hub pyramidbox_lite_mobile_mask = hub.Module(name="pyramidbox_lite_mobile_mask") pyramidbox_lite_mobile_mask.processor.save_inference_model(dirname="test_program") 并用opt转化为android端适用的模型
输入: 图片是根据人脸检测检测到的人脸框截取的人脸部分 图片已经缩放到128*128 inputShape ={1,3,128,128}; 图片预处理代码
inputColorFormat=“RGB” inputMean={0.5f,0.5f,0.5f}; inputStd={1.0f,1.0f,1.0f};
private float[] process(Bitmap inputImage,int channels,String inputColorFormat,float[] inputMean,float[] inputStd){ int height = inputImage.getHeight(); int width = inputImage.getWidth(); float[] inputData=new float[channels * width * height]; if (channels == 3) { int[] channelIdx = null; if (inputColorFormat.equalsIgnoreCase("RGB")) { channelIdx = new int[]{0, 1, 2}; } else if (inputColorFormat.equalsIgnoreCase("BGR")) { channelIdx = new int[]{2, 1, 0}; } else { Log.i(TAG, "unknown color format " + inputColorFormat + ", only RGB and BGR color format is " + "supported!"); return null; } int[] channelStride = new int[]{width * height, width * height * 2};
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int color = inputImage.getPixel(x, y);
float ap=256.0f;
float[] rgb = new float[]{(float) red(color)/ap, (float) green(color)/ap,
(float) blue(color)/ap};
inputData[y * width + x] = (rgb[channelIdx[0]] - inputMean[0]) * inputStd[0];
inputData[y * width + x + channelStride[0]] = (rgb[channelIdx[1]] - inputMean[1]) * inputStd[1];
inputData[y * width + x + channelStride[1]] = (rgb[channelIdx[2]] - inputMean[2]) * inputStd[2];
}
}
} else if (channels == 1) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int color = inputImage.getPixel(x, y);
float gray = (float) (red(color) + green(color) + blue(color)) / 3.0f;
inputData[y * width + x] = (gray - inputMean[0]) / inputStd[0];
}
}
} else {
Log.i(TAG, "unsupported channel size " + Integer.toString(channels) + ", only channel 1 and 3 is " +
"supported!");
return null;
}
return inputData;
}