为什么tf.keras.models.load_model再次构建模型?

问题描述 投票:0回答:1
ValueError: Sequential model 'sequential_2' has already been configured to use 
input shape (None, 224, 224, 3). You cannot build it with input_shape [None, 224, 224, 3]
def load_model(model_path):
  """
  Loads a saved model from a specified path.
  """

  tf.keras.config.enable_unsafe_deserialization()
  print(f"Loading saved model from: {model_path}")
  model = tf.keras.models.load_model(model_path)
  return model

loaded_1000_image_model = load_model('/content/drive/MyDrive/Dog Vision/models/20241002-16491727887796-1000-images-mobilenetv2-Adam.h5')

我期望它会加载模型而不会出现任何错误,但由于某种原因,即使我没有尝试重建或再次给出任何输入形状,它也会给出值错误。

tensorflow machine-learning keras
1个回答
0
投票

模型在保存之前是否已编译?如果没有,您应该在保存之前尝试编译。您还可以尝试将编译设置为 false,并查看编译步骤是否出现问题。您还可以通过设置 safe_mode=False 来跳过“tf.keras.config.enable_unsafe_deserialization()”。

tf.keras.models.load_model(
    filepath, custom_objects=None, compile=True, safe_mode=True
)
© www.soinside.com 2019 - 2024. All rights reserved.