You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

上传模型至Google AI Platform时变量未找到的问题排查

问题:AI Platform部署模型后预测报错找不到变量

我将训练好的模型上传至Google AI Platform,使用命令行测试预测时未得到预期结果,反而出现报错。操作步骤如下:

  • 安装Gcloud
  • 执行命令保存模型:
gcloud ai-platform local train \
    --module-name trainer.final_task \
    --package-path trainer/ --
  • 手动创建存储桶
  • 将步骤2生成的saved_model.pb文件上传至该存储桶
  • 参考官方文档在Gcloud创建模型并关联存储桶(已确认本地与存储桶的Python、TensorFlow配置一致)
  • 命令行测试模型:
MODEL_NAME=ML6Mugs
VERSION=FinalModel6

gcloud ai-platform predict \
    --region europe-west1 \
    --model $MODEL_NAME \
    --version $VERSION \
    --json-instances check_deployed_model/test.json

模型架构

def model(input_layer):
    """Returns a compiled model.

    This function is expected to return a model to identity the different mugs.
    The model's outputs are expected to be probabilities for the classes and
    and it should be ready for training. The input layer specifies the shape of the images. The preprocessing
    applied to the images is specified in data.py.

    Add your solution below.

    Parameters:
        input_layer: A tf.keras.layers.InputLayer() specifying the shape of the input.
            RGB colored images, shape: (width, height, 3)
    Returns:
        model: A compiled model
    """
    input_shape=(input_layer.shape[1], input_layer.shape[2], input_layer.shape[3])
    base_model = tf.keras.applications.MobileNetV2(weights='imagenet', input_shape=input_shape, include_top=False)
    for layer in base_model.layers:
        layer.trainable = False
    model = models.Sequential()
    model.add(base_model)
    model.add(layers.GlobalAveragePooling2D())
    model.add(layers.Dense(4, activation='softmax'))
    model.compile(optimizer="rmsprop", loss='sparse_categorical_crossentropy', metrics=["accuracy"])
    return model

报错信息

ERROR: (gcloud.ai-platform.predict) HTTP request failed. Response: {
  "error": {
    "code": 400,
    "message": "{
    \"error\": \"Could not find variable block_15_depthwise_BN/beta. This could mean that the variable has been deleted. In TF1, it can also mean the variable is uninitialized. Debug info: container=localhost, status error message=Container localhost does not exist. (Could not find resource: localhost/block_15_depthwise_BN/beta)\\n\\t [[{{function_node __inference__wrapped_model_15632}}{{node model/sequential/mobilenetv2_1.00_224/block_15_depthwise_BN/ReadVariableOp_1}}]]\"
}",
    "status": "INVALID_ARGUMENT"
  }
}

问题原因与解决步骤

原因

报错显示找不到MobileNetV2预训练模型的权重变量,核心问题是你只上传了saved_model.pb文件,而遗漏了模型权重所在的variables文件夹。Keras模型完整保存后会生成一个包含saved_model.pb和variables子文件夹的目录,其中variables里存储着模型的权重参数,缺少这些文件会导致AI Platform加载模型时无法找到所需变量。

解决步骤

  1. 完整保存模型:在训练代码中,确保使用model.save()方法保存整个模型,而不是仅导出单个pb文件。例如在训练完成后添加:
model.save("./saved_model")

执行后会生成一个saved_model文件夹,里面包含saved_model.pb和variables目录。

  1. 上传完整模型目录:将整个saved_model文件夹上传至GCS存储桶,而不是只上传saved_model.pb。

  2. 重新部署模型版本:在AI Platform创建新版本时,指定存储桶中完整模型文件夹的路径(例如gs://your-bucket-name/saved_model/),而非单个pb文件的路径。

  3. 本地验证:在上传前,先本地加载保存的模型并测试预测功能,确保模型在本地能正常运行,避免因模型本身损坏导致的问题。

内容的提问来源于stack exchange,提问作者yemy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 10:20:33