首页 > 编程语言 > 详细

C++计算tensor指定坐标对应的falt数组内存偏移量

时间:2021-05-19 23:58:29      阅读:42      评论:0      收藏:0      [点我收藏+]

需求

使用华为HiAi框架进行模型推理
成功转换模型并构造输入输出tensor
目前需要对output feature map进行解析需要获取tensor指定坐标的值
类似使用array[0,0,0,0]访问返回flat[0]

实现

输出tensor提供了header地址float*和shape数据类型float32
已根据bhwc格式进行flat操作

// get value from bhwc(1,126,28,23) tensor via its header
// WARNING: No memory range check, use with caution!!!
float get_value(const float* feature_ptr,const int& height,const int& width,const int& channel)
{
    // every value takes 4 bytes in ram!!!
    return *(feature_ptr + ( height  + 28 * (width + channel * 23) ) * 4);
}

??注意这里有个地方比较坑:
对于flat数组内存偏移量,还要乘每个元素的字节长度sizeof(float32) = 4

C++计算tensor指定坐标对应的falt数组内存偏移量

原文:https://www.cnblogs.com/azureology/p/14787081.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!