What to set 'ClassNames' to when using importKerasNetwork() to import network and weights for a regression model?

4 次查看(过去 30 天)
Hi,
I have trained a regression model in Keras/Tensorflow and exported the network achitecure and weights to .json and .h5 files. I would like to import my regression model to Matlab (R2018a) and looks like importKerasNetwork() does just that. However, it keeps throwing the following error: "Reference to non-existent field 'class_name'." I don't pass any 'Classes' (R2018b) or 'ClassNames' (R2018a) becasue I'm doing regression not classification. This is the how I call the function:
net = importKerasNetwork('model_architecture.json','WeightFile','my_model_weights.h5', 'OutputLayerType','regression');
The documentation (R2018b as well as R2018a) doesn't state what to pass in 'Classes' when you are dealing with a regression problem. I tried passing an empty array but got the same error. Any ideas how to solve this problem?
Thanks!

采纳的回答

Don Mathis
Don Mathis 2018-12-20
There were a few updates to that support package after the initial release. Try downloading it and installing it again through the Add-Ons icon in MATLAB. Also if you can upload your model files here I can take a look.
  13 个评论
Yousaf Rahman
Yousaf Rahman 2019-4-3
I am having the same issue, I am using the latest version of the importer (downloaded 4/2/2019), but I am using MATLAB 2017b. I had trouble installing the importer in 2018b, and have a Mathworks Support case ongoing for that.
MohamedElHefnawy
MohamedElHefnawy 2020-3-13
编辑:MohamedElHefnawy 2020-3-13
I am having the same issue. I am using R2018a and installed the latest version of the package (March 13 2020).
cnn_model_demo = importKerasNetwork('model.json','WeightFile','cnn_model.h5', 'OutputLayerType','classification')
Error using importKerasNetwork (line 86)
Reference to non-existent field 'class_name'.

请先登录,再进行评论。

更多回答(2 个)

Sriramkrishnan Muralikrishnan
Hi,
I have the same problem with importing
net = importKerasNetwork(modelfile,'OutputLayerType','regression')
Error using importKerasNetwork (line 77)
Reference to non-existent field 'class_name'
I am using R2017b and just installed the add on for Keras. Could you please let me know where I can find the update which might resolve this problem.
Thanks
  2 个评论
MM
MM 2019-3-2
Click on the little drop-down arrow on the Add-Ons icon --> Check for updates --> Add-on fetaures. This usually worked for me.
MohamedElHefnawy
MohamedElHefnawy 2020-3-13
I am having the same issue. I am using R2018a and installed the latest version of the package (March 13 2020).
cnn_model_demo = importKerasNetwork('model.json','WeightFile','cnn_model.h5', 'OutputLayerType','classification')
Error using importKerasNetwork (line 86)
Reference to non-existent field 'class_name'.

请先登录,再进行评论。


Ben Wetherill
Ben Wetherill 2019-8-27
编辑:Ben Wetherill 2019-8-27
Hello. I'm also having this issue. I recently downloaded the tool for 2017b via the offline installation method. After some digging and use of HDFViewer I can see the issue.
My NN is built as follows:
...
model = Sequential()
model.add(Dense(200, input_shape=(input_size,), activation='relu'))
model.add(Dense(160, activation='relu'))
model.add(Dense(output_size, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
history = model.fit(input_data_train, output_data_train, validation_data = (input_data_test,output_data_test),
epochs=100, batch_size=64)
output_data_pred = model.predict(input_data_test)
model.save('unvalidated_input_data.h5')
Calling importKerasLayers() from the Matlab prompt leads to the following line in KerasLayerInsideSequentialModel.m throwing the error.
this.ClassName = Struct.class_name;
When I look at the contents of the struct at this point, I'm dealing with an object called 'name' with a value of 'sequential_9'. This was easy to find in HDFViewer. See below for the object dump.
{"class_name": "Sequential", "config": {"name": "sequential_9", "layers": [{"class_name": "Dense", "config": {"name": "dense_24", "trainable": true, "batch_input_shape": [null, 248], "dtype": "float32", "units": 62, "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_25", "trainable": true, "units": 108, "activation": "sigmoid", "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}}]}}
So the cause is clear, but I don't know if its my version of the importer or the way I've setup the model in Keras. From this thread others have had a similar issue but have fixed it with a tool update. My version should include this update. I'm using Keras version 2.2.4.
Any thoughts?
  2 个评论
Don Mathis
Don Mathis 2019-8-27
I'm pretty sure this is due to a breaking change made by Keras in version 2.2.3 that changed their representation of Sequential models. That change was made in late 2018. The earliest Keras importer version that supports the breaking change was R2018b. You could try to save the Keras model in a version earlier than 2.2.3.
Ben Wetherill
Ben Wetherill 2019-8-28
Yup, that fixed it. Well, I say fixed it - now I have an issue in that the binary_crossentropy loss function isn't implemented in the 2017b version of the importer and my IT department hasn't released 2018b yet. :/ Yay for Mordac, The Preventer of Information Services. ;)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

产品


版本

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by