gpt4 book ai didi

python - 无法在 matlab 中导入 keras(python 生成)模型 - 不存在的字段 "class_name"

转载 作者:行者123 更新时间:2023-12-01 08:36:48 25 4
gpt4 key购买 nike

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

% 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上运行吗?

如果有任何帮助,我将不胜感激。

最佳答案

您的“keras_version”:“2.2.4”。改成2.1.2可以解决这个问题。

关于python - 无法在 matlab 中导入 keras(python 生成)模型 - 不存在的字段 "class_name",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53677655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com