用keras如何读取现有的模型
model.load_weights(cfg.MODEL.pretrained_model)
的时候,出现报错:
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py\h5f.pyx", line 88, in h5py.h5f.open
OSError: Unable to open file (file signature not found)
这么一看,好像是要什么h5?
于是查看tensorflow官方文档,居然发现有好几个相似的用法???
首先是tf.keras.Model中的
load_weights(
filepath, by_name=False, skip_mismatch=False
)
然后是tf.keras.models中的
tf.keras.models.load_model(
filepath, custom_objects=None, compile=True
)
还有tf.saved_model里的:
tf.saved_model.load(
export_dir, tags=None
)
对比一下,keras里的两个方法都要用h5文件,saved_model里的可以加载我现在保存的模型,但是官方有这么一段文字:
Loading can be done with tf.saved_model.load(). However, since it is an API that is on the lower level (and hence has a wider range of use cases), it does not return a Keras model. Instead, it returns an object that contain functions that can be used to do inference. For example:
saved_model不能拿来加载权重继续训练了········于是乎···又要改代码,训练时要用keras···save_weights保存权重到h5文件,才能用load_weights继续训练前面的又白训练了
补充:其实官方也给了将save_model转换成kerasmodel的方法···用tensorflow_hub
但是累了,不想搞了,我还是选择只改一行代码······
回复
回复