当前位置:网站首页>Solve valueerror: no model found in config file

Solve valueerror: no model found in config file

2022-06-11 08:10:00 sinysama

A friend showed me one bug, Before you say that .h5 All files can be loaded normally , Why does the above problem suddenly appear . I looked at , The first reaction is that there is no given model structure , That is, the following ↓

 Insert picture description here

( Record only , Prevent more people from stepping on the pit . If there is anything wrong , Please give me more advice .)

1 Wrong presentation

No model found in config file.

2 The process of the problem

Use load_model() Method to save the model

3 Problem cause analysis

Look at the code , Just two lines . Plus the bag (from tensorflow.keras.models import load_model), Only three lines , Yes , It's just a process of loading the model .

filepath="./CNN.weights.h5"
model=load_model(filepath)

There is no problem with this code , After all, I often use , Quite familiar . that , The problem can only occur in .h5 How to save and how to load the model , Let's rule it out one by one .

4 Solution

When saving the model , There are some main ways to save :
①save()→ Save model parameters and network structure
②save_weights()→ Save only model parameters .
③tf.keras.callbacks.ModelCheckpoint()→ After configuring the parameters , It can be saved at any time during training .

Have a look at the code of the friend generation model , He was in model.fit Have adopted the ③ The way :

tf.keras.callbacks.ModelCheckpoint(filepath=filepath,monitor='val_accuracy',verbose=1,save_best_only=True,save_weights_only=True)

Sure enough ,save_weights_only=True, This is the reason for the error report , Missing network diagram structure , Because the code only saves parameter weights (only!!!). therefore , That's easy .

4.1 Solution one : Add network diagram structure

To match .h5 file , Write the network diagram structure again , then load_model Just fine .

4.2 Solution two : Save a new .h5 file

Get rid of save_weights_only=True Or the True Change it to False, Retrain the model and save .h5 file .( get to the root of sth. , When saving the model in the future, it is recommended to save the diagram structure and parameters together , Just take up more memory , The usual toy model is not very big , Why bother yourself by being lazy ?┗|`O′|┛ Ow ~~)

tf.keras.callbacks.ModelCheckpoint(filepath=filepath,monitor='val_accuracy',verbose=1,save_best_only=True)

The error report disappeared
 Insert picture description here

summary

Save only network parameters , Use load_weights().
Save graph structure and network parameters , Use load_model()( The saved .h5 The file is bigger );
Generally, no error will be reported if it is used without confusion
The comparison is as follows :
 Insert picture description here

原网站

版权声明
本文为[sinysama]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020515459461.html