logo
Loading...

載入.h5檔出現ValueError: You are trying to load a weight file containing 16 layers into a model with 0 layers錯誤 - Cupoy

Hi我根據講義的連結https://ithelp.ithome.com.tw/articles/10...

ml100-3,ml100-3-d100

載入.h5檔出現ValueError: You are trying to load a weight file containing 16 layers into a model with 0 layers錯誤

2019/12/24 上午 10:06
機器學習共學討論版
David Teng
觀看數:31
回答數:3
收藏數:1
ml100-3
ml100-3-d100

Hi

我根據講義的連結

https://ithelp.ithome.com.tw/articles/10190971

按照裡面的步驟進行transfer learning, 使用 VGG16 這個 model

然後搭配期末考的花朵辨識資料進行訓練 

最後存出 .h5檔 flower-model-base-VGG16.h5


然後開新.py檔案載入這個 .h5檔

model = load_model('flower-model-base-VGG16.h5')


卻會回傳

ValueError: You are trying to load a weight file containing 16 layers into a model with 0 layers


請問這個該怎麼解決呢?

回答列表

  • 2019/12/24 上午 10:50
    楊哲寧
    贊同數:1
    不贊同數:0
    留言數:1

    可以看一下當初存weight的時候是用save還是save_weigth,前者會存模型加權重,後者只會存權重。如果是用後者的話,要先搭一個跟原本相同的網路,再把weight load進來。

  • 2019/12/26 下午 05:36
    Jeffrey
    贊同數:0
    不贊同數:0
    留言數:0

  • 2020/01/20 下午 04:03
    張維元 (WeiYuan)
    贊同數:0
    不贊同數:0
    留言數:0

    ```

    path_model = 'Sequential_model.keras'

    model.save(path_model)  

    model = load_model(path_model) 

    ```


    改成這樣:


    ```

    model.save_weights('Sequential_model_weights.h5')

    model.load_weights('Sequential_model_weights.h5',by_name=True)

    json_string = model.to_json()

    ```