对于NCNN而言,核心在于网络的前向推理过程(Inference),其主要数据类型为mat
,该数据类型以类的形式定义在src/mat.h
中,其中包含了mat
的构造函数、析构函数、常见的运算过程。
#if __ARM_NEON
#include <arm_neon.h>
#endif
通过宏变量__ARM_NEON
控制Neon
的使用。
// empty
Mat();
// vec
Mat(int w, size_t elemsize = 4u, Allocator* allocator = 0);
// image
Mat(int w, int h, size_t elemsize = 4u, Allocator* allocator = 0);
// dim
Mat(int w, int h, int c, size_t elemsize = 4u, Allocator* allocator = 0);
// copy
Mat(const Mat& m);
// external vec
Mat(int w, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
// external image
Mat(int w, int h, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
// external dim
Mat(int w, int h, int c, void* data, size_t elemsize = 4u, Allocator* allocator = 0);
// release
~Mat();
类的一开始声明了多种构造函数和析构函数。其中有空构造函数、向量的构造函数、二维矩阵的构造函数、三维矩阵的构造函数等。Allocator
用于创建内存池构造器,手动为mat
分配内存空间。在32位架构中size_t
表示四字节,定义为typedef unsigned int size_t
;在64位架构中,size_t
表示八字节,定义为typedef unsigned long size_t
。
void fill(float v);
void fill(int v);
template <typename T> void fill(T v);
定义数据填充函数,最后一行采用泛型进行统一,但是出于一定的考虑,又单独实现了对于int
类型和float
类型的接口。
// deep copy
Mat clone(Allocator* allocator = 0) const;
重新分配内存,对一个mat
变量进行深拷贝,不同于直接赋值,直接赋值的方式仅仅是地址的改变。const成员函数不能数据成员进行修改,但是如果成员是一个指针,该函数不能修改指针,但是可以修改指针指向的对象(这是不好的)。
#if NCNN_PIXEL
enum
{
PIXEL_CONVERT_SHIFT = 16,
PIXEL_FORMAT_MASK = 0x0000ffff,
PIXEL_CONVERT_MASK = 0xffff0000,
PIXEL_RGB = 1,
PIXEL_BGR = (1 << 1),
PIXEL_GRAY = (1 << 2),
PIXEL_RGBA = (1 << 3),
PIXEL_RGB2BGR = PIXEL_RGB | (PIXEL_BGR << PIXEL_CONVERT_SHIFT),
PIXEL_RGB2GRAY = PIXEL_RGB | (PIXEL_GRAY << PIXEL_CONVERT_SHIFT),
PIXEL_BGR2RGB = PIXEL_BGR | (PIXEL_RGB << PIXEL_CONVERT_SHIFT),
PIXEL_BGR2GRAY = PIXEL_BGR | (PIXEL_GRAY << PIXEL_CONVERT_SHIFT),
PIXEL_GRAY2RGB = PIXEL_GRAY | (PIXEL_RGB << PIXEL_CONVERT_SHIFT),
PIXEL_GRAY2BGR = PIXEL_GRAY | (PIXEL_BGR << PIXEL_CONVERT_SHIFT),
PIXEL_RGBA2RGB = PIXEL_RGBA | (PIXEL_RGB << PIXEL_CONVERT_SHIFT),
PIXEL_RGBA2BGR = PIXEL_RGBA | (PIXEL_BGR << PIXEL_CONVERT_SHIFT),
PIXEL_RGBA2GRAY = PIXEL_RGBA | (PIXEL_GRAY << PIXEL_CONVERT_SHIFT),
};
// convenient construct from pixel data
static Mat from_pixels(const unsigned char* pixels, int type, int w, int h, Allocator* allocator = 0);
// convenient construct from pixel data and resize to specific size
static Mat from_pixels_resize(const unsigned char* pixels, int type, int w, int h, int target_width, int target_height, Allocator* allocator = 0);
// convenient export to pixel data
void to_pixels(unsigned char* pixels, int type) const;
// convenient export to pixel data and resize to specific size
void to_pixels_resize(unsigned char* pixels, int type, int target_width, int target_height) const;
#endif // NCNN_PIXEL
通过像素数据初始化