预测:无法在系统权重中加载模型“ VGG16 model.h5”

时间:2019-12-27 06:34:02

标签: python tensorflow keras pycharm h5py

负载模型

  

model.load_weights(VGG16(include_top = False,weights ='imagenet'))


    VGG16=load_model('E:/TBirds/TBirdDOCS/PythonCode/DOCMODEL/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5')

    # build the VGG16 network
    #model = application.VGG16(include_top=False, weights='imagenet')
    model.load_weights(VGG16(include_top=False, weights='imagenet'))
    # get the bottleneck prediction from the pre-trained VGG16 model
    bottleneck_prediction = model.predict(image)
    #print(bottleneck_prediction)

ValueError:无法以只读模式创建组。

Traceback (most recent call last):
  File "C:/Users/Administrator.SQL6/AppData/Roaming/Python/Python36/site-packages/keras/engine/Auto-AI.py", line 254, in <module>
    main()
  File "C:/Users/Administrator.SQL6/AppData/Roaming/Python/Python36/site-packages/keras/engine/Auto-AI.py", line 244, in main
    prediction_And_Bookmark(writer, Total_Number_of_Pages, pdf_source,File_Name)
  File "C:/Users/Administrator.SQL6/AppData/Roaming/Python/Python36/site-packages/keras/engine/Auto-AI.py", line 189, in prediction_And_Bookmark
    layb, count, pos = predict(test_imag)
  File "C:/Users/Administrator.SQL6/AppData/Roaming/Python/Python36/site-packages/keras/engine/Auto-AI.py", line 97, in predict
    VGG16=load_model('E:/TBirds/TBirdDOCS/PythonCode/DOCMODEL/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5')
  File "C:\Users\Administrator.SQL6\AppData\Roaming\Python\Python36\site-packages\keras\engine\saving.py", line 492, in load_wrapper
    return load_function(*args, **kwargs)
  File "C:\Users\Administrator.SQL6\AppData\Roaming\Python\Python36\site-packages\keras\engine\saving.py", line 584, in load_model
    model = _deserialize_model(h5dict, custom_objects, compile)
  File "C:\Users\Administrator.SQL6\AppData\Roaming\Python\Python36\site-packages\keras\engine\saving.py", line 270, in _deserialize_model
    model_config = h5dict['model_config']
  File "C:\Users\Administrator.SQL6\AppData\Roaming\Python\Python36\site-packages\keras\utils\io_utils.py", line 318, in __getitem__
    raise ValueError('Cannot create group in read-only mode.')
ValueError: Cannot create group in read-only mode.

Process finished with exit code 1
  

ValueError:无法以只读模式创建组。

2 个答案:

答案 0 :(得分:0)

如果您分别保存权重和体系结构,并尝试使用model.load_weights('model_weights.h5')加载权重,那么您将获得ValueError: Cannot create group in read-only mode

这意味着您正在尝试在没有模型架构的情况下加载模型的权重。

要解决此问题,您可以将.h5文件一次性保存权重和体系结构,并按如下所示轻松还原模型

model.save('model_vgg16.h5')
model = load_model('model_vgg16.h5')

另一种使用JSON进行保存和还原的方法,如下所示:

# Save the model

model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)

model.save_weights("model.h5")

#Load the model

json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")

答案 1 :(得分:0)

  • 下载VGG16模型

    from tensorflow.keras.applications.vgg16 import VGG16
    model=VGG16(include_top=False , weights="imagenet")
    
  • 以加载保存的重量文件“ VGG16.h5”

    • 复制包含file.h5的目录路径

      from tensorflow.keras.applications.vgg16 import VGG16
      
      model_path='C:\\Users\\nourh\\.keras\\models\\vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'
      
      model = VGG16(include_top=False ,weights=model_path)
      

windows 10,tensorflow 1.14.0