无法在Matlab中导入keras(由python生成)模型-不存在的字段“ class_name”

时间:2018-12-07 22:38:54

标签: python matlab keras neural-network

我正在尝试使用以下代码导入模型:

% Number of classes
classnames={'0','1','2','3','4','5','6','7','8'}; 

% Load model into Matlab
% net = importKerasNetwork(netfile);
netxx = importKerasNetwork('model.json','WeightFile','model.h5', 'classnames', classnames,'OutputLayerType','classification');

我收到以下错误:

>> load_keras_network_from_py
Error using importKerasNetwork (line 86)
Reference to non-existent field 'class_name'.

Error in load_keras_network_from_py (line 20)
netxx = importKerasNetwork('model.json','WeightFile','model.h5', 'classnames',
classnames,'OutputLayerType','classification');

这是我要在MATLAB中导入的JSON模型的结构:

{  
   "class_name":"Sequential",
   "config":{  
      "name":"sequential_1",
      "layers":[  
         {  
            "class_name":"Conv2D",
            "config":{  
               "name":"conv2d_1",
               "trainable":true,
               "batch_input_shape":[  
                  null,
                  128,
                  128,
                  3
               ],
               "dtype":"float32",
               "filters":32,
               "kernel_size":[  
                  3,
                  3
               ],
               "strides":[  
                  1,
                  1
               ],
               "padding":"valid",
               "data_format":"channels_last",
               "dilation_rate":[  
                  1,
                  1
               ],
               "activation":"relu",
               "use_bias":true,
               "kernel_initializer":{  
                  "class_name":"VarianceScaling",
                  "config":{  
                     "scale":1.0,
                     "mode":"fan_avg",
                     "distribution":"uniform",
                     "seed":null
                  }
               },
               "bias_initializer":{  
                  "class_name":"Zeros",
                  "config":{  

                  }
               },
               "kernel_regularizer":null,
               "bias_regularizer":null,
               "activity_regularizer":null,
               "kernel_constraint":null,
               "bias_constraint":null
            }
         },
         {  
            "class_name":"MaxPooling2D",
            "config":{  
               "name":"max_pooling2d_1",
               "trainable":true,
               "pool_size":[  
                  2,
                  2
               ],
               "padding":"valid",
               "strides":[  
                  2,
                  2
               ],
               "data_format":"channels_last"
            }
         },
         {  
            "class_name":"Conv2D",
            "config":{  
               "name":"conv2d_2",
               "trainable":true,
               "filters":32,
               "kernel_size":[  
                  3,
                  3
               ],
               "strides":[  
                  1,
                  1
               ],
               "padding":"valid",
               "data_format":"channels_last",
               "dilation_rate":[  
                  1,
                  1
               ],
               "activation":"relu",
               "use_bias":true,
               "kernel_initializer":{  
                  "class_name":"VarianceScaling",
                  "config":{  
                     "scale":1.0,
                     "mode":"fan_avg",
                     "distribution":"uniform",
                     "seed":null
                  }
               },
               "bias_initializer":{  
                  "class_name":"Zeros",
                  "config":{  

                  }
               },
               "kernel_regularizer":null,
               "bias_regularizer":null,
               "activity_regularizer":null,
               "kernel_constraint":null,
               "bias_constraint":null
            }
         },
         {  
            "class_name":"MaxPooling2D",
            "config":{  
               "name":"max_pooling2d_2",
               "trainable":true,
               "pool_size":[  
                  2,
                  2
               ],
               "padding":"valid",
               "strides":[  
                  2,
                  2
               ],
               "data_format":"channels_last"
            }
         },
         {  
            "class_name":"Conv2D",
            "config":{  
               "name":"conv2d_3",
               "trainable":true,
               "filters":64,
               "kernel_size":[  
                  3,
                  3
               ],
               "strides":[  
                  1,
                  1
               ],
               "padding":"valid",
               "data_format":"channels_last",
               "dilation_rate":[  
                  1,
                  1
               ],
               "activation":"relu",
               "use_bias":true,
               "kernel_initializer":{  
                  "class_name":"VarianceScaling",
                  "config":{  
                     "scale":1.0,
                     "mode":"fan_avg",
                     "distribution":"uniform",
                     "seed":null
                  }
               },
               "bias_initializer":{  
                  "class_name":"Zeros",
                  "config":{  

                  }
               },
               "kernel_regularizer":null,
               "bias_regularizer":null,
               "activity_regularizer":null,
               "kernel_constraint":null,
               "bias_constraint":null
            }
         },
         {  
            "class_name":"MaxPooling2D",
            "config":{  
               "name":"max_pooling2d_3",
               "trainable":true,
               "pool_size":[  
                  2,
                  2
               ],
               "padding":"valid",
               "strides":[  
                  2,
                  2
               ],
               "data_format":"channels_last"
            }
         },
         {  
            "class_name":"Flatten",
            "config":{  
               "name":"flatten_1",
               "trainable":true,
               "data_format":"channels_last"
            }
         },
         {  
            "class_name":"Dense",
            "config":{  
               "name":"dense_1",
               "trainable":true,
               "units":128,
               "activation":"relu",
               "use_bias":true,
               "kernel_initializer":{  
                  "class_name":"VarianceScaling",
                  "config":{  
                     "scale":1.0,
                     "mode":"fan_avg",
                     "distribution":"uniform",
                     "seed":null
                  }
               },
               "bias_initializer":{  
                  "class_name":"Zeros",
                  "config":{  

                  }
               },
               "kernel_regularizer":null,
               "bias_regularizer":null,
               "activity_regularizer":null,
               "kernel_constraint":null,
               "bias_constraint":null
            }
         },
         {  
            "class_name":"Dense",
            "config":{  
               "name":"dense_2",
               "trainable":true,
               "units":1,
               "activation":"softmax",
               "use_bias":true,
               "kernel_initializer":{  
                  "class_name":"VarianceScaling",
                  "config":{  
                     "scale":1.0,
                     "mode":"fan_avg",
                     "distribution":"uniform",
                     "seed":null
                  }
               },
               "bias_initializer":{  
                  "class_name":"Zeros",
                  "config":{  

                  }
               },
               "kernel_regularizer":null,
               "bias_regularizer":null,
               "activity_regularizer":null,
               "kernel_constraint":null,
               "bias_constraint":null
            }
         }
      ]
   },
   "keras_version":"2.2.4",
   "backend":"tensorflow"
}

我已经尝试了几种方法来解决此问题(包括导入h5文件而不是JSON),但我真的不知道为什么会这样...在使用Python保存keras模型时,是否还有其他限制?它在matlab上运行?

我将不胜感激。

1 个答案:

答案 0 :(得分:1)

您的“ keras_version”:“ 2.2.4”。 将其更改为2.1.2可以解决此问题。