[NCNN学习笔记]-2

1、前言

本次来学习NCNN中最重要的组件之一:Mat,在前几次的学习中,每次遇到Mat时,都要去查一下Mat的各种用法,这一次就直接把Mat学透。

参考链接:https://zhuanlan.zhihu.com/p/578501922、https://zhuanlan.zhihu.com/p/336359747

2、学习内容

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();
}
3、总结

本次主要是对NCNN中的Mat进行学习,主要参考前言中两位大佬详细的解释!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rex久居

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值