AttributeError: 'Functional' object has no attribute 'predict_segmentation' When importing TensorFlow model Keras
AttributeError: 'Functional' object has no attribute 'predict_segmentation' When importing TensorFlow model Keras
我已经成功地训练了一个 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'
我做错了什么?
是在保存模型时还是在加载模型时?
谢谢!
predict_segmentation
不是普通 Keras 模型中可用的函数。看起来是在 keras_segmentation
库中创建模型后添加的,这可能是 Keras 无法再次加载它的原因。
我想你有 2 个选择。
- 您可以使用我链接的代码中的行手动将函数添加回模型。
model.predict_segmentation = MethodType(keras_segmentation.predict.predict, model)
- 您可以在重新加载模型时使用相同的参数创建一个新的
vgg_unet
,然后按照 Keras documentation 中的建议将权重从 hdf5
文件传输到该模型。
model = vgg_unet(n_classes=50, input_height=512, input_width=608)
model.load_weights('my_model.hdf5')
我已经成功地训练了一个 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'
我做错了什么? 是在保存模型时还是在加载模型时?
谢谢!
predict_segmentation
不是普通 Keras 模型中可用的函数。看起来是在 keras_segmentation
库中创建模型后添加的,这可能是 Keras 无法再次加载它的原因。
我想你有 2 个选择。
- 您可以使用我链接的代码中的行手动将函数添加回模型。
model.predict_segmentation = MethodType(keras_segmentation.predict.predict, model)
- 您可以在重新加载模型时使用相同的参数创建一个新的
vgg_unet
,然后按照 Keras documentation 中的建议将权重从hdf5
文件传输到该模型。
model = vgg_unet(n_classes=50, input_height=512, input_width=608)
model.load_weights('my_model.hdf5')