|
13 | 13 | namespace caffe {
|
14 | 14 |
|
15 | 15 | bool NetNeedsUpgrade(const NetParameter& net_param) {
|
16 |
| - return NetNeedsV0ToV1Upgrade(net_param) || NetNeedsV1ToV2Upgrade(net_param); |
| 16 | + return NetNeedsV0ToV1Upgrade(net_param) || NetNeedsV1ToV2Upgrade(net_param) |
| 17 | + || NetNeedsDataUpgrade(net_param) || NetNeedsInputUpgrade(net_param); |
17 | 18 | }
|
18 | 19 |
|
19 | 20 | bool UpgradeNetAsNeeded(const string& param_file, NetParameter* param) {
|
@@ -655,12 +656,14 @@ void UpgradeNetDataTransformation(NetParameter* net_param) {
|
655 | 656 | }
|
656 | 657 |
|
657 | 658 | bool UpgradeV1Net(const NetParameter& v1_net_param, NetParameter* net_param) {
|
658 |
| - bool is_fully_compatible = true; |
659 | 659 | if (v1_net_param.layer_size() > 0) {
|
660 |
| - LOG(ERROR) << "Input NetParameter to be upgraded already specifies 'layer' " |
661 |
| - << "fields; these will be ignored for the upgrade."; |
662 |
| - is_fully_compatible = false; |
| 660 | + LOG(FATAL) << "Refusing to upgrade inconsistent NetParameter input; " |
| 661 | + << "the definition includes both 'layer' and 'layers' fields. " |
| 662 | + << "The current format defines 'layer' fields with string type like " |
| 663 | + << "layer { type: 'Layer' ... } and not layers { type: LAYER ... }. " |
| 664 | + << "Manually switch the definition to 'layer' format to continue."; |
663 | 665 | }
|
| 666 | + bool is_fully_compatible = true; |
664 | 667 | net_param->CopyFrom(v1_net_param);
|
665 | 668 | net_param->clear_layers();
|
666 | 669 | net_param->clear_layer();
|
@@ -952,29 +955,35 @@ bool NetNeedsInputUpgrade(const NetParameter& net_param) {
|
952 | 955 | }
|
953 | 956 |
|
954 | 957 | void UpgradeNetInput(NetParameter* net_param) {
|
955 |
| - LayerParameter* layer_param = net_param->add_layer(); |
956 |
| - layer_param->set_name("input"); |
957 |
| - layer_param->set_type("Input"); |
958 |
| - InputParameter* input_param = layer_param->mutable_input_param(); |
| 958 | + // Collect inputs and convert to Input layer definitions. |
| 959 | + // If the NetParameter holds an input alone, without shape/dim, then |
| 960 | + // it's a legacy caffemodel and simply stripping the input field is enough. |
959 | 961 | bool has_shape = net_param->input_shape_size() > 0;
|
960 |
| - // Convert input fields into a layer. |
961 |
| - for (int i = 0; i < net_param->input_size(); ++i) { |
962 |
| - layer_param->add_top(net_param->input(i)); |
963 |
| - if (has_shape) { |
964 |
| - input_param->add_shape()->CopyFrom(net_param->input_shape(i)); |
965 |
| - } else { |
966 |
| - // Turn legacy input dimensions into shape. |
967 |
| - BlobShape* shape = input_param->add_shape(); |
968 |
| - int first_dim = i*4; |
969 |
| - int last_dim = first_dim + 4; |
970 |
| - for (int j = first_dim; j < last_dim; j++) { |
971 |
| - shape->add_dim(net_param->input_dim(j)); |
| 962 | + bool has_dim = net_param->input_dim_size() > 0; |
| 963 | + if (has_shape || has_dim) { |
| 964 | + LayerParameter* layer_param = net_param->add_layer(); |
| 965 | + layer_param->set_name("input"); |
| 966 | + layer_param->set_type("Input"); |
| 967 | + InputParameter* input_param = layer_param->mutable_input_param(); |
| 968 | + // Convert input fields into a layer. |
| 969 | + for (int i = 0; i < net_param->input_size(); ++i) { |
| 970 | + layer_param->add_top(net_param->input(i)); |
| 971 | + if (has_shape) { |
| 972 | + input_param->add_shape()->CopyFrom(net_param->input_shape(i)); |
| 973 | + } else { |
| 974 | + // Turn legacy input dimensions into shape. |
| 975 | + BlobShape* shape = input_param->add_shape(); |
| 976 | + int first_dim = i*4; |
| 977 | + int last_dim = first_dim + 4; |
| 978 | + for (int j = first_dim; j < last_dim; j++) { |
| 979 | + shape->add_dim(net_param->input_dim(j)); |
| 980 | + } |
972 | 981 | }
|
973 | 982 | }
|
974 |
| - } |
975 |
| - // Swap input layer to beginning of net to satisfy layer dependencies. |
976 |
| - for (int i = net_param->layer_size() - 1; i > 0; --i) { |
977 |
| - net_param->mutable_layer(i-1)->Swap(net_param->mutable_layer(i)); |
| 983 | + // Swap input layer to beginning of net to satisfy layer dependencies. |
| 984 | + for (int i = net_param->layer_size() - 1; i > 0; --i) { |
| 985 | + net_param->mutable_layer(i-1)->Swap(net_param->mutable_layer(i)); |
| 986 | + } |
978 | 987 | }
|
979 | 988 | // Clear inputs.
|
980 | 989 | net_param->clear_input();
|
|
0 commit comments