赞
踩
本次来学习NCNN中最重要的组件之一:Mat,在前几次的学习中,每次遇到Mat时,都要去查一下Mat的各种用法,这一次就直接把Mat学透。
参考链接:https://zhuanlan.zhihu.com/p/578501922、https://zhuanlan.zhihu.com/p/336359747
NCNN中的Mat是计算的最基本的单元之一,任何的操作都是基于Mat进行的。如caffe中以blob、tensorflow/pytorch中tensor。不仅能够储存数据,还能提供了大量的辅助函数,用于计算。先学习Mat的成员。
class NCNN_EXPORT Mat
{
public:
void* data; // 数据存放地址,和opencv中的data 和 std::vector.data() 的作用一致
int* refcount; // 引用计数,参考shared_ptr,有引用时计数加1,取消引用时计数减一
size_t elemsize; // elemsize 是一个代表元素大小的数值,通常在描述计算机程序中的数据类型时使用。它表示每个元素所占用的存储空间大小,单位是字节。
int elempack; // 每个输入数据中的元素在计算时被分成多少个组,例如一个 float32×4_t 机会被认为是4个元素
Allocator* allocator; // 内存分配器,根据不同的平台分配内存,使得内存对齐。
int dims; // 数据维度,最多4维度
int w; // 宽
int h; // 高
int c; // channel
size_t cstep; // Mat中的数据排布是CHW顺序,这个值表示了每两个channel之间的stride
};
ncnn::Mat的构造函数有很多,可以根据不同的方式进行构造:
Mat(int w, size_t elemsize = 4u, Allocator* allocator = 0);
Mat(int w, int h, size_t elemsize = 4u, Allocator* allocator = 0);
Mat(int w, int h, int c, size_t elemsize = 4u, Allocator* allocator = 0);
Mat(int w, int h, int d, int c, size_t elemsize = 4u, Allocator* allocator = 0);
Mat(int w, size_t elemsize, int elempack, Allocator* allocator = 0);
Mat(int w, int h, size_t elemsize, int elempack, Allocator* allocator = 0);
Mat(int w, int h, int c, size_t elemsize, int elempack, Allocator* allocator = 0);
Mat(int w, int h, int d, int c, size_t elemsize, int elempack, Allocator* allocator = 0);
Mat(const Mat& m);
Mat(int w, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
Mat(int w, int h, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
Mat(int w, int h, int c, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
Mat(int w, int h, int d, int c, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
Mat(int w, void* data, size_t elemsize, int elempack, Allocator* allocator = 0);
Mat(int w, int h, void* data, size_t elemsize, int elempack, Allocator* allocator = 0);
Mat(int w, int h, int c, void* data, size_t elemsize, int elempack, Allocator* allocator = 0);
Mat(int w, int h, int d, int c, void* data, size_t elemsize, int elempack, Allocator* allocator = 0);
实际上调用的是create函数,如:
void Mat::create(int _w, int _h, int _c, size_t _elemsize, Allocator* _allocator)
{
if (dims == 3 && w == _w && h == _h && c == _c && elemsize == _elemsize && elempack == 1 && allocator == _allocator)
return;
release();
elemsize = _elemsize;
elempack = 1;
allocator = _allocator;
dims = 3;
w = _w;
h = _h;
d = 1;
c = _c;
cstep = alignSize((size_t)w * h * elemsize, 16) / elemsize;
size_t totalsize = alignSize(total() * elemsize, 4);
if (totalsize > 0)
{
if (allocator)
data = allocator->fastMalloc(totalsize + (int)sizeof(*refcount));
else
data = fastMalloc(totalsize + (int)sizeof(*refcount));
}
if (data)
{
refcount = (int*)(((unsigned char*)data) + totalsize);
*refcount = 1;
}
}
ncnn::Mat 还提供了许多成员函数,可以很方便的对数据进行处理。如:
void Mat::substract_mean_normalize(const float* mean_vals, const float* norm_vals)
{
Layer* op;
// substract mean and normalize
op = create_layer(LayerType::Scale);
...
Mat weights[2];
weights[0] = Mat(c);
weights[1] = Mat(c);
for (int q = 0; q < c; q++)
{
weights[0][q] = norm_vals[q];
weights[1][q] = -mean_vals[q] * norm_vals[q];
}
op->load_model(ModelBinFromMatArray(weights));
op->create_pipeline(opt);
op->forward_inplace(*this, opt);
op->destroy_pipeline(opt);
delete op;
}
Mat Mat::from_pixels(const unsigned char* pixels, int type, int w, int h, Allocator* allocator)
{
int type_from = type & PIXEL_FORMAT_MASK;
if (type_from == PIXEL_RGB || type_from == PIXEL_BGR)
{
return Mat::from_pixels(pixels, type, w, h, w * 3, allocator);
}
else if (type_from == PIXEL_GRAY)
{
return Mat::from_pixels(pixels, type, w, h, w * 1, allocator);
}
else if (type_from == PIXEL_RGBA || type_from == PIXEL_BGRA)
{
return Mat::from_pixels(pixels, type, w, h, w * 4, allocator);
}
// unknown convert type
NCNN_LOGE("unknown convert type %d", type);
return Mat();
}
本次主要是对NCNN中的Mat进行学习,主要参考前言中两位大佬详细的解释!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。