上传SavedModel至ML Engine遇版本错误:是TensorFlow还是脚本问题?
I trained a model locally using TensorFlow 1.5, following the TensorFlow for Poets guide. I converted the trained model to a SavedModel using this save_model.py script:
import tensorflow as tf from tensorflow.python.saved_model import signature_constants from tensorflow.python.saved_model import tag_constants from tensorflow.python.saved_model import builder as saved_model_builder input_graph = 'retrained_graph.pb' saved_model_dir = 'my_model' with tf.Graph().as_default() as graph: # Read in the export graph with tf.gfile.FastGFile(input_graph, 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) tf.import_graph_def(graph_def, name='') # Define SavedModel Signature (inputs and outputs) in_image = graph.get_tensor_by_name('DecodeJpeg/contents:0') inputs = {'image_bytes': tf.saved_model.utils.build_tensor_info(in_image)} out_classes = graph.get_tensor_by_name('final_result:0') outputs = {'prediction': tf.saved_model.utils.build_tensor_info(out_classes)} signature = tf.saved_model.signature_def_utils.build_signature_def( inputs=inputs, outputs=outputs, method_name='tensorflow/serving/predict' ) with tf.Session(graph=graph) as sess: # Save out the SavedModel. b = saved_model_builder.SavedModelBuilder(saved_model_dir) b.add_meta_graph_and_variables(sess, [tf.saved_model.tag_constants.SERVING], signature_def_map={'serving_default': signature}) b.save()
When I try to upload this SavedModel to ML Engine for online inference, I get an error saying I need to use a runtime version of 1.2 or higher. Is this a problem with my local TensorFlow version, or is there an issue with my save_model.py script?
Great question—let’s break this down clearly:
First: The error is almost certainly about ML Engine's runtime version configuration, not your local TensorFlow 1.5 version (1.5 is way higher than the 1.2 minimum required).
Here’s what’s happening: When you deploy a model to ML Engine, you need to explicitly tell it which TensorFlow runtime version to use. If you don’t specify this, ML Engine might fall back to an older default version (one that’s below 1.2), which triggers the error you’re seeing.
Second: Let’s validate your save_model.py script (it looks mostly correct, but a few checks):
- Your use of the SavedModel APIs is compatible with TensorFlow 1.5: The
tag_constants.SERVINGtag, thetensorflow/serving/predictmethod name, and the way you build the signature def all follow the TF 1.x SavedModel spec. - Quick way to verify your exported model is valid: Run this command locally (with your TF 1.5 environment activated):
This will display the model’s input/output tensors, signature definitions, and tags. Confirm that thesaved_model_cli show --dir my_model --allserving_defaultsignature exists, and the input (image_bytes) and output (prediction) tensors match what you defined.
Fix steps to resolve the error:
When deploying your model to ML Engine, explicitly set the runtime version to match your local TensorFlow version (1.5) to avoid compatibility gaps. Here’s how to do it with the gcloud CLI:
- First create your model resource (if you haven’t already):
gcloud ai-platform models create YOUR_MODEL_NAME --regions us-central1 - Then create a version of the model with the correct runtime version:
Replace placeholders likegcloud ai-platform versions create YOUR_VERSION_NAME \ --model YOUR_MODEL_NAME \ --origin gs://YOUR_GCS_BUCKET_PATH/my_model \ --runtime-version 1.5 \ --framework TENSORFLOWYOUR_MODEL_NAME,YOUR_VERSION_NAME, andYOUR_GCS_BUCKET_PATHwith your actual values.
Extra note:
TensorFlow 1.5 is a supported runtime version on ML Engine (for legacy models), so specifying this should resolve the "needs 1.2+" error entirely. If you still run into issues, double-check that your SavedModel was uploaded correctly to Google Cloud Storage (GCS)—ML Engine needs access to the full my_model directory, not just individual files.
内容的提问来源于stack exchange,提问作者Starchand

