keras 权重保存和权重载入方式

来自:互联网
时间:2020-05-21
阅读:

如果需要全部权重载入,直接使用权重载入方式

model.save_weights('./weigths.h5')
model2.load_weights('./weigths.h5')

但是有时候你只需要载入部分权重

所以你可以这样操作

首先,为所有层命名,在层中直接加入方法 name='layer1'

第二,使用,将你不需要载入权重的值更改名字。

最后,载入权重。

x=BatchNormalization(axis=channel_axis,name='layer2')(x)
 
model2.layers[-1].name='pred'
model2.load_weights('./weigths.h5',by_name=True)

上面的代码是对应的操作,这里我除了最后一层,其他层我都加载了权重,记住,by_name 必须赋值为True 这样才能够按照名称对应赋值权重。

注意:两个模型结构必须一样,不然可能出问题

补充知识:Keras中保存和加载权重及模型结构

1. 保存和加载模型结构

(1)保存为JSON字串

json_string = model.to_json()

(2)从JSON字串重构模型

from keras.models import model_from_json
model = model_from_json(json_string)

(3)保存为YAML字串

yaml_string = model.to_yaml()

(4)从YAML字串重构模型

model = model_from_yaml(yaml_string)

2. 保存和加载模型权重(参数)

from keras.models import load_model
 
# 创建HDF5文件'my_model.h5',保存模型参数
model.save('my_model.h5')
 
# 加载模型参数
load_model('my_model.h5')

2.1 处理已保存模型中的自定义层(或其他自定义对象)

如果要加载的模型包含自定义层或其他自定义类或函数,则可以通过 custom_objects 参数将它们传递给加载机制:

from keras.models import load_model
 
# 假设你的模型包含一个 AttentionLayer 类的实例
model = load_model('my_model.h5', custom_objects={'AttentionLayer': AttentionLayer})

或者,你可以使用 自定义对象作用域:

from keras.utils import CustomObjectScope
 
with CustomObjectScope({'AttentionLayer': AttentionLayer}):
 model = load_model('my_model.h5')

自定义对象的处理与 load_model, model_from_json, model_from_yaml 的工作方式相同:

from keras.models import model_from_json

model = model_from_json(json_string, custom_objects={'AttentionLayer': AttentionLayer})

2019年6月1号更新:

更详细的使用方法:

如何保存Keras模型?

(1)一个HDF5文件即保存模型的结构又保存模型的权重

我们不推荐使用pickle或cPickle来保存Keras模型。

你可以使用model.save(filepath)将Keras模型和权重保存在一个HDF5文件中,该文件将包含:

模型的结构,以便重构该模型

模型的权重

训练配置(损失函数,优化器等)

优化器的状态,以便于从上次训练中断的地方开始

使用keras.models.load_model(filepath)来重新实例化你的模型,如果文件中存储了训练配置的话,该函数还会同时完成模型的编译。

例子:

from keras.models import load_model
 
model.save('my_model.h5') # creates a HDF5 file 'my_model.h5'
del model # deletes the existing model
 
# returns a compiled model
# identical to the previous one
model = load_model('my_model.h5')

(2)只保存模型的结构

如果你只是希望保存模型的结构,而不包含其权重或配置信息,可以使用:

# save as JSON
json_string = model.to_json()
 
# save as YAML
yaml_string = model.to_yaml()

这项操作将把模型序列化为json或yaml文件,这些文件对人而言也是友好的,如果需要的话你甚至可以手动打开这些文件并进行编辑。

当然,你也可以从保存好的json文件或yaml文件中载入模型:

# model reconstruction from JSON:
from keras.models import model_from_json
model = model_from_json(json_string)
 
# model reconstruction from YAML
model = model_from_yaml(yaml_string)

(3)只保存模型的权重

如果需要保存模型的权重,可通过下面的代码利用HDF5进行保存。注意,在使用前需要确保你已安装了HDF5和其Python库h5py。

model.save_weights('my_model_weights.h5')

如果你需要在代码中初始化一个完全相同的模型,请使用:

model.load_weights('my_model_weights.h5')

如果你需要加载权重到不同的网络结构(有些层一样)中,例如fine-tune或transfer-learning,你可以通过层名字来加载模型:

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

例如:

"""
假如原模型为:
 model = Sequential()
 model.add(Dense(2, input_dim=3, name="dense_1"))
 model.add(Dense(3, name="dense_2"))
 ...
 model.save_weights(fname)
"""
# new model
model = Sequential()
model.add(Dense(2, input_dim=3, name="dense_1")) # will be loaded
model.add(Dense(10, name="new_dense")) # will not be loaded
 
# load weights from first model; will only affect the first layer, dense_1.
model.load_weights(fname, by_name=True)
返回顶部
顶部