TNN MatConvertParam参数scale和bias设置
设置 参数 scale
2023-09-11 14:19:30 时间
Pytorch的Normalize的计算过程是:TNN MatConvertParam参数设置
使用TNN进行模型推理前,需要进行必要的预处理,如下需要设置TNN_NS::MatConvertParam inputCvtParam的scale和bias两个参数值
/*** Read image ***/
cv::Mat orig_image = cv::imread(IMAGE_NAME);
int imageWidth = orig_image.size[1];
int imageHeight = orig_image.size[0];
printf("image size: width = %d, height = %d\n", imageWidth, imageHeight);
/*** Pre process ***/
/* resize, colorconversion */
cv::Mat inputImage;
cv::resize(orig_image, inputImage, cv::Size(MODEL_WIDTH, MODEL_HEIGHT));
cv::cvtColor(inputImage, inputImage, cv::COLOR_BGR2RGB);
/*** normalize ***/
auto inputMat = std::make_shared<TNN_NS::Mat>(DEVICE_TYPE, TNN_NS::N8UC3, nchw, (uint8_t*)inputImage.data);
CHECK(inputMat != NULL);
TNN_NS::MatConvertParam inputCvtParam;
inputCvtParam.scale = {1 / 0.5 / 255.f, 1 / 0.5 / 255.f, 1 / 0.5 / 255.f, 1 / 0.5 / 255.;
inputCvtParam.bias = {-0.5 / 0.5, -0.5 / 0.5, -0.5 / 0.5, -0.5 / 0.5};
status = instance->SetInputMat(inputMat, inputCvtParam);
TNN_NS::MatConvertParam inputCvtParam的scale和bias的计算规则是:
y = scale*x+bias
如果你的模型是Pytorch训练的模型,一般会使用:
rgb_mean=(0.5, 0.5, 0.5)
rgb_std=(0.5, 0.5, 0.5)
normalize = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(rgb_mean, rgb_std)
])
Pytorch的Normalize的计算过程是减均值除方差,即:
input[channel] = (input[channel] - mean[channel]) / std[channel]
即:y = (x-m)/std (其中输入x是将0~255归一化到0~1的图像数据)
将Pytorch的Normalize和TNN的MatConvertParam进行变换,即可对齐了:
y = (x-m)/std ---> y = scale*x+bias
恒等变换如下:
y = (x-m)/std
# 由于Pytorch的Normalize对输入x是将0~255归一化到0~1的图像数据,因此需要逆归一化为0~255
y = (x-m*255)/(std*255)
# 拆分每项
y=x/(std*255)-m/std
# 比较y = scale*x+bias得:
scale=1/(std*255)=1/std/255
bias=-m/std
因此Pytorch的
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
对应TNN_NS::MatConvertParam的参数为:
TNN_NS::MatConvertParam inputCvtParam;
inputCvtParam.scale = {1 / 0.5 / 255.f, 1 / 0.5 / 255.f, 1 / 0.5 / 255.f, 1 / 0.5 / 255.;
inputCvtParam.bias = {-0.5 / 0.5, -0.5 / 0.5, -0.5 / 0.5, -0.5 / 0.5};
在NCNN的Normalize:
【NCNN源码解读】Mat(二)Normalize - 知乎
ncnn::Mat input = ncnn::Mat::from_pixels(resized.data, ncnn::Mat::PIXEL_BGR2RGB, WIDTH, HEIGHT);
const float mean[3] = { 0.5f * 255.f, 0.5f * 255.f, 0.5f * 255.f };
const float norm[3] = { 1 / 0.5f / 255.f, 1 / 0.5f / 255.f, 1 /0.5f / 255.f };
input.substract_mean_normalize(mean, norm);
相关文章
- 一份你可以在 <head> 里设置的列表
- C# 设置鼠标指针
- 网格导入设置 Import settings for Meshes
- win8共享文件设置 详细教程
- ubuntu(linux): 设置firefox的默认语言(firefox 101.0.1)
- phpcms 用户权限设置
- 设置tomcat内存设定
- 给RelativeLayout设置背景,无效果bug解决
- paip.手机时间设置不能修改灰色禁用 解决大法
- SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置试读版
- Android修行手册-ImageView的adjustViewBounds和设置透明度
- GOPROXY设置
- CentOS 6.8 下 iptables开机脚本设置
- 一文详解jvm之-Xms -Xmx -Xmn -Xss -XX:PermSize -XX:MaxPermSize等参数的设置和优化以及如何选择垃圾回收器
- 【CSS】使用绝对定位 / 浮动解决外边距塌陷问题 ( 为父容器 / 子元素设置内边距 / 边框 | 为子元素设置浮动 | 为子元素设置绝对定位 )
- 【CSS】使用 固定定位 实现顶部导航栏 ( 核心要点 | 固定定位元素居中设置 | 代码示例 )
- 【Groovy】Groovy 脚本调用 ( Groovy 类中调用 Groovy 脚本 | 参考 Script#evaluate 方法 | 创建 Binding 对象并设置 args 参数 )
- Eclipse jvm启动参数在哪设置
- Jupyter Notebook设置主题
- e837. 设置JTabbedPane中卡片的颜色
- 如何将HTML页面的标题设置为“数字天堂”。