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
Sriramkrishnan Muralikrishnan 2019년 3월 2일
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