NCNN 源码剖析(四) Net

Net 类在头文件中声明。
Net 类是对神经网络的抽象,负责调度计算单元进行计算。

接口声明

公开接口

Net 类公开的接口如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#if NCNN_STRING
int register_custom_layer(const char* type, layer_creator func);
#endif
int register_custom_layer(int index, layer_creator func);
#if NCNN_STDIO
#if NCNN_STRING
int load_param(FILE* fp);
int load_param(const char* protopath);
#endif
int load_param_bin(FILE* fp);
int load_param_bin(const char* protopath);
int load_model(FILE* fp);
int load_model(const char* modelpath);
#endif
int load_param(const unsigned char* mem);
int load_model(const unsigned char* mem);
void clear();

Extractor create_extractor()const;

这里可以看出公开接口的主要内容还是载入数据和模型.

另外还有一个 create_extractor 接口,初始化一个此网络的提取器

保护接口

Net 类的保护接口如下

1
2
3
4
5
6
7
8
#if NCNN_STRING
int find_blob_by_name(const char* name)const;
int find_layer_by_name(const char* name)const;
int custom_layer_to_index(const char* type)const;
Layer* create_custom_layer(const char* type);
#endif
Layer* create_custom_layer(int index);
int forward_layer(int layer_index, std::vector<Mat>& blob_mats, bool lightmode)const;

保护接口主要是关于以下几方面

  • 根据给定 name 查找 blob
  • 根据给定 name 查找 layer
  • 根据给定 type 定制 layer
  • 根据给定 type 创建 layer
  • 根据给定 index 创建 layer
  • 根据 layer_index 指定的层向前传播计算

私有成员

Net 类的成员属性如下

1
2
3
4
protected:
std::vector<Blob> blobs;
std::vector<Layer*> layers;
std::vector<layer_registry_entry> custom_layer_registry;

Net 实现源码分析

Net 中调度 Layer 执行前向传播的计算的接口为 forward_layer

接口声明如下

1
int Net::forward_layer(int layer_index, std::vector<Mat>& blob_mats, bool lightmode) const;

Net 源码

声明源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Extractor;
class Net
{
public:
// empty init
Net();
// clear and destroy
~Net();

#if NCNN_STRING
// register custom layer by layer type name
// return 0 if success
int register_custom_layer(const char* type, layer_creator_func creator);
#endif // NCNN_STRING
// register custom layer by layer type
// return 0 if success
int register_custom_layer(int index, layer_creator_func creator);

#if NCNN_STDIO
#if NCNN_STRING
// load network structure from plain param file
// return 0 if success
int load_param(FILE* fp);
int load_param(const char* protopath);
#endif // NCNN_STRING
// load network structure from binary param file
// return 0 if success
int load_param_bin(FILE* fp);
int load_param_bin(const char* protopath);

// load network weight data from model file
// return 0 if success
int load_model(FILE* fp);
int load_model(const char* modelpath);
#endif // NCNN_STDIO

// load network structure from external memory
// memory pointer must be 32-bit aligned
// return bytes consumed
int load_param(const unsigned char* mem);

// reference network weight data from external memory
// weight data is not copied but referenced
// so external memory should be retained when used
// memory pointer must be 32-bit aligned
// return bytes consumed
int load_model(const unsigned char* mem);

// unload network structure and weight data
void clear();

// construct an Extractor from network
Extractor create_extractor() const;

protected:
friend class Extractor;
#if NCNN_STRING
int find_blob_index_by_name(const char* name) const;
int find_layer_index_by_name(const char* name) const;
int custom_layer_to_index(const char* type);
Layer* create_custom_layer(const char* type);
#endif // NCNN_STRING
Layer* create_custom_layer(int index);
int forward_layer(int layer_index, std::vector<Mat>& blob_mats, bool lightmode) const;

protected:
std::vector<Blob> blobs;
std::vector<Layer*> layers;

std::vector<layer_registry_entry> custom_layer_registry;
};