我已經成功地訓練了一個 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'
我究竟做錯了什么 ?是在我保存模型時還是在加載模型時?
謝謝 !
uj5u.com熱心網友回復:
predict_segmentation不是普通 Keras 模型中可用的功能。看起來它是在keras_segmentation庫中創建模型后添加的,這可能是Keras無法再次加載它的原因。
我認為你有兩個選擇。
- 您可以使用我鏈接的代碼中的行手動將函式添加回模型。
model.predict_segmentation = MethodType(keras_segmentation.predict.predict, model)
- 您可以
vgg_unet在重新加載模型時創建一個具有相同引數的新模型,并hdf5按照Keras 檔案中的建議將權重從您的檔案傳輸到該模型。
model = vgg_unet(n_classes=50, input_height=512, input_width=608)
model.load_weights('my_model.hdf5')
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/375435.html
