Keras load_model reported an error?

problem description

I used Keras to build a neural network that can be trained or saved with model.save ("model.h5"), but when I load the model with model = load_model ("model.h5"), I get the following error:

Traceback (most recent call last):
  File "C:/programming/pycharm/cnn_attention_lstm/cnn_attention_lstm/train7.py", line 31, in
    main()
  File "C:/programming/pycharm/cnn_attention_lstm/cnn_attention_lstm/train7.py", line 23, in main
    history = classifier.fit(data_dir_path=input_dir_path, model_dir_path=output_dir_path, data_set_name=data_set_name)
  File "C:\programming\pycharm\cnn_attention_lstm\cnn_attention_lstm\model7again.py", line 113, in fit
    model = self.create_model()
  File "C:\programming\pycharm\cnn_attention_lstm\cnn_attention_lstm\model7again.py", line 66, in create_model
    model = load_model("model.h5")
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 263, in load_model
    load_weights_from_hdf5_group(f["model_weights"], model.layers)
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 915, in load_weights_from_hdf5_group
    reshape=reshape)
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 554, in preprocess_weights_for_loading
    weights = convert_nested_time_distributed(weights)
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 513, in convert_nested_time_distributed
    layer.layer, weights, original_keras_version, original_backend)
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 556, in preprocess_weights_for_loading
    weights = convert_nested_model(weights)
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 532, in convert_nested_model
    original_backend=original_backend))
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 556, in preprocess_weights_for_loading
    weights = convert_nested_model(weights)
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 544, in convert_nested_model
    original_backend=original_backend))
  File "C:\Users\tong\AppData\Local\Programs\Python\Python36\Lib\site-packages\keras\engine\saving.py", line 673, in preprocess_weights_for_loading
    elif layer_weights_shape != weights[0].shape:
IndexError: list index out of range
Process finished with exit code 1
Oct.02,2021

I have encountered this problem recently, and I have solved it. I don't know if you have the same problem.
you can take a look at the last sentence of keras's multi-GPU model
keras document-_ multi-gpu_model
On model saving
To save the multi-gpu model, use .save (fname) or. Save _ weights (fname) with the template model (the argument you passed to multi_gpu_model), rather than the model returned by multi_gpu_model.

I estimate that you are saving the gpu model after parallel processing, so there will be problems with Load this model. You should save the model
before feeding the function, for example:
parralel_model = multi_gpu_model (model, multi_gpu)
you should save model instead of parralel_model,. In addition, both models need to be compiled before saving, otherwise Load will make errors

.
Menu