在caffe中,train.prototxt 、test.prototxt和 deploy.prototxt中间为了增加网络结构(.prototxt)文件的灵活性,可以通过设置phase、stage、level参数实现train、test、deploy文件统一合并在一个.prototxt文件中(通常见到的train_test合并为一个文件或分开为两个文件就是使用了这些参数)。
- 对于一个prototxt文件,可以在NetParameter层面,通过
state
字段来表示当前proto文件的phase/level/stage信息。
message NetParameter {
...
// The current "state" of the network, including the phase, level, and stage.
// Some layers may be included/excluded depending on this state and the states
// specified in the layers' include and exclude fields.
optional NetState state = 6;
...
// The layers that make up the net.
repeated LayerParameter layer = 100;
}
message NetState {
optional Phase phase = 1 [default = TEST];
optional int32 level = 2 [default = 0];
repeated string stage = 3;
}
其中phase指定当前Proto文件运行所在阶段,level要求Layer级别以及stage过滤。通过这三个字段可以选择或过滤掉相关的Layer。
- 对于每一个layer,定义了两个NetStateRule类型的字段,分别是include和exclude,来控制该Layer在Netstat处于何种状态下存在。
message LayerParameter {
// Rules controlling whether and when a layer is included in the network,
// based on the current NetState. You may specify a non-zero number of rules
// to include OR exclude, but not both. If no include or exclude rules are
// specified, the layer is always included. If the current NetState meets
// ANY (i.e., one or more) of the specified rules, the layer is
// included/excluded.
repeated NetStateRule include = 8;
repeated NetStateRule exclude = 9;
}
message NetStateRule {
// Set phase to require the NetState have a particular phase (TRAIN or TEST)
// to meet this rule.
optional Phase phase = 1;
// Set the minimum and/or maximum levels in which the layer should be used.
// Leave undefined to meet the rule regardless of level.
optional int32 min_level = 2;
optional int32 max_level = 3;
// Customizable sets of stages to include or exclude.
// The net must have ALL of the specified stages and NONE of the specified
// "not_stage"s to meet the rule.
// (Use multiple NetStateRules to specify conjunctions of stages.)
repeated string stage = 4;
repeated string not_stage = 5;
}
- NetParameter除了作为Net内部配置以外,与直接作为SolverParameter的字段来影响slover过程。SolverParameter定义还是比较复杂,但是整体目标是从SolverParameter中创建出唯一训练网络以及多个测试网络。
- SolverParameter中通过net, net_param, train_net, train_net_param四个字段来指定训练网络,但是只允许同时出现一个。其中
net,train_net
为Net文件,而net_param, train_net_param
为内联的网络配置,其实本质上没有太大区别,读取到内存以后,都是NetParameter。 - SolverParameter中通过net, net_param, test_net, test_net_param四个字段来指定测试网络,其中net, net_param由于也会被读取为训练网络,所以只允许有一个存在。test_net, test_net_param运行同时存在并可以多于1个。但是最终有多少个测试网络实际运行是通过
test_iter
参数个数来确定的,如果只指定了2个test_iter,哪怕我们定义了3个测试网络,实际也只会运行2个,另外一个迭代次数为0。优先级上test_net_param>test_net>net_param>net。 - 对于训练网络,可以在SolverParameter中配置train_state,train_state内容会和网络NetParameter内部的Netstate进行合并,其中train_state数据的优先级要高于NetParameter内部的Netstate。
- 对于测试网络,要么给每一个测试网络都在SolverParameter中配置一个test_state,要么都不配置。每一个测试网络的test_state会和测试网络NetParameter内部的Netstate进行合并,其中test_state数据的优先级高于NetParameter内部的Netstate。
- 对于训练网络,Netstat.phase恒等于train;对于测试网络,Netstate.phase恒等于test。
- Caffe.cpp启动参数也支持设置state
DEFINE_string(phase, "",
"Optional; network phase (TRAIN or TEST). Only used for 'time'.");
DEFINE_int32(level, 0,
"Optional; network level.");
DEFINE_string(stage, "",
"Optional; network stages (not to be confused with phase), "
"separated by ','.");
这两个参数实际是与上面SolverParameter的train_state进行合并
solver_param.mutable_train_state()->set_level(FLAGS_level);
for (int i = 0; i < stages.size(); i++) {
solver_param.mutable_train_state()->add_stage(stages[i]);
}
网友评论