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

11 views (last 30 days)
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!

Accepted Answer

Don Mathis
Don Mathis on 20 Dec 2018
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 Comments
MohamedElHefnawy
MohamedElHefnawy on 13 Mar 2020
Edited: MohamedElHefnawy on 13 Mar 2020
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'.

Sign in to comment.

More Answers (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 Comments
MohamedElHefnawy
MohamedElHefnawy on 13 Mar 2020
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'.

Sign in to comment.


Ben Wetherill
Ben Wetherill on 27 Aug 2019
Edited: Ben Wetherill on 27 Aug 2019
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 Comments
Ben Wetherill
Ben Wetherill on 28 Aug 2019
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. ;)

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!