【问题标题】:Prediction : Can`t load model "VGG16 model.h5" in system Weights预测:无法在系统权重中加载模型“VGG16 model.h5”
【发布时间】:2020-04-17 04:45:56
【问题描述】:

加载模型

"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: 无法在只读模式下创建组。

【问题讨论】:

    标签: python tensorflow keras pycharm h5py


    【解决方案1】:

    如果您分别保存权重和架构并尝试使用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")
    

    【讨论】:

      【解决方案2】:
      • 下载 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,张量流 1.14.0

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-24
        • 1970-01-01
        • 2018-03-08
        • 1970-01-01
        • 1970-01-01
        • 2020-08-25
        • 1970-01-01
        • 2018-10-08
        相关资源
        最近更新 更多