Keras模型加載之通過json文件

導入圖

  • model_from_json

導入權重

  • load_weights

代碼示例

import numpy as np
from keras.models import Model, load_model
from keras.models import model_from_json
import json

json_file = '/path/to/model.json'
with open(json_file, 'r') as f:
    json_str = f.read()

## NOTE: 因爲模型中有自定義的模塊,所以導入json的時候需要添加一個參數custom_objects,一般情況下是不用加的
model = model_from_json(json_str, custom_objects={'BilinearUpSampling2D': BilinearUpSampling2D})
print(type(model))

## NOTE:注意可以只加載weights中有的一些權重,沒有的就跳過,可以通過by_name參數實現
model.load_weights(
    '/path/to/AtrousFCN_Resnet50_16sweights.01-0.74.hdf5',
    by_name=True)
print("Load model sucess *****")
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章