- 定义一个名字(字符串)与函数一一映射关系,通过REGISTER_LAYER_CREATOR将名字以及对应的函数注册到系统中
//define a function
shared_ptr<LayerBase> GetConvolutionLayer(const LayerParameter& param,
Type ftype, Type btype, size_t) {
//do something
}
shared_ptr<LayerBase> GetSigmoidLayer(const LayerParameter& param,
Type ftype, Type btype, size_t){}
shared_ptr<LayerBase> GetSoftmaxLayer(const LayerParameter& param,
Type ftype, Type btype, size_t) {}
//regist "Convolution" to GetConvolutionLayer function
REGISTER_LAYER_CREATOR(Convolution, GetConvolutionLayer);
//实际上会扩展为:static LayerRegisterer g_creator_Convolution("Convolution", GetConvolutionLayer);
//其中REGISTER_LAYER_CREATOR宏定义如下:
#define REGISTER_LAYER_CREATOR(type, creator) \
static LayerRegisterer g_creator_##type(#type, creator);
2. 通过g_registry_来保存键值对
class LayerRegistry {
public:
typedef shared_ptr<LayerBase> (*Creator)(const LayerParameter&, Type, Type, size_t);
typedef std::map<string, Creator> CreatorRegistry;
static CreatorRegistry& Registry() {
static CreatorRegistry g_registry_;
return g_registry_;
}
- 最后就可以通过create_layer接口来创建新的layer
static shared_ptr<LayerBase> CreateLayer(const LayerParameter& param, size_t solver_rank) {
//...
return registry[layer_type](param, ftype, btype, solver_rank);
}
更详细内容需要参考layer_factory.cpp文件.