Caffe源码分析—phase/level/stage理解

2020-04-22  本文已影响0人  陈泽_06aa

在caffe中,train.prototxt 、test.prototxt和 deploy.prototxt中间为了增加网络结构(.prototxt)文件的灵活性,可以通过设置phase、stage、level参数实现train、test、deploy文件统一合并在一个.prototxt文件中(通常见到的train_test合并为一个文件或分开为两个文件就是使用了这些参数)。

  1. 对于一个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。

  1. 对于每一个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;
}
  1. NetParameter除了作为Net内部配置以外,与直接作为SolverParameter的字段来影响slover过程。SolverParameter定义还是比较复杂,但是整体目标是从SolverParameter中创建出唯一训练网络以及多个测试网络。
  1. 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]);
  }
上一篇 下一篇

猜你喜欢

热点阅读