keras使用load_weights遇到报错

问题

  • 在自定义继承自keras.Model的模型时,使用model.save("mode.h5")会报错,于是,可行的方法是,使用model.save_weights("seg_model_weights.h5")时正确保存。但是在加载模型时报错:

'Unable to load weights saved in HDF5 format into a subclassed ’ ValueError: Unable to load weights saved in HDF5 format into a
subclassed Model which has not created its variables yet. Call the
Model first, then load the weights.

解决方法

  • 在加载之前先buildmodel.build(input_shape =)。下面是样例
model.build(input_shape =(None,256,256,3))
model.load_weights(path)

你可能感兴趣的:(解决问题,机器学习专题,keras,深度学习,人工智能)