我成功地训练了一个Keras模型,比如:
import tensorflow as tf
from keras_segmentation.models.unet import vgg_unet
# initaite the model
model = vgg_unet(n_classes=50, input_height=512, input_width=608)
# Train
model.train(
train_images=train_images,
train_annotations=train_annotations,
checkpoints_path="/tmp/vgg_unet_1", epochs=5
)并以hdf5格式保存:
tf.keras.models.save_model(model,'my_model.hdf5')然后我装载我的模型
model=tf.keras.models.load_model('my_model.hdf5')最后,我想对一幅新的图像进行分割预测。
out = model.predict_segmentation(
inp=image_to_test,
out_fname="/tmp/out.png"
)我得到了以下错误:
AttributeError: 'Functional' object has no attribute 'predict_segmentation'我做错什么了?是当我保存我的模型还是当我装载它的时候?
谢谢!
发布于 2021-12-06 23:09:17
predict_segmentation不是普通Keras模型中可用的函数。看起来它是在keras_segmentation库中创建模型之后添加的,这可能就是Keras不能再次加载它的原因。
我想你有两个选择。
model.predict_segmentation = MethodType(keras_segmentation.predict.predict, model)vgg_unet,并按照Keras文档中的建议,将权重从您的hdf5文件传递到该模型。model = vgg_unet(n_classes=50, input_height=512, input_width=608)
model.load_weights('my_model.hdf5')https://stackoverflow.com/questions/70252159
复制相似问题