机器学习新手求教:如何导出并部署自定义TensorFlow模型GoogleTensorflow?
Hey there! Since you've already got your custom image model trained and working in Jupyter, let's walk through exporting it using TensorFlow's inference export tools and deploying it via TensorFlow Serving—building on the MNIST steps you already nailed.
Step 1: Export Your Trained Model to SavedModel Format
TensorFlow Serving works best with the SavedModel format, so we'll use tf.saved_model.save() (the modern replacement for older export_inference_graph tools) to package your model. Below are examples based on how you built your model:
If You Used Keras for Training
Most beginners use Keras, so this is the most likely scenario:
import tensorflow as tf from tensorflow.keras.models import load_model # Load your trained "GoogleTensorflow" model # Replace with the path to your saved model (e.g., .h5 file or saved checkpoint directory) model = load_model("/path/to/your/GoogleTensorflow_model") # Define the export directory—note the version number (1) at the end # TensorFlow Serving automatically uses the highest versioned model in this folder export_dir = "/tmp/GoogleTensorflow_exported/1" # Export the model as a SavedModel tf.saved_model.save(model, export_dir)
If You Used Native TensorFlow (tf.Graph)
If you built your model with raw TensorFlow graphs and checkpoints, you'll need to explicitly define input/output signatures:
import tensorflow as tf # Load your trained graph and variables with tf.Graph().as_default() as graph: sess = tf.Session() # Restore from your checkpoint files saver = tf.train.import_meta_graph("/path/to/your/model.meta") saver.restore(sess, tf.train.latest_checkpoint("/path/to/checkpoint_dir")) # Get your model's input and output tensors (replace with your actual tensor names) # You can find these by printing the graph's tensor names during training input_tensor = graph.get_tensor_by_name("image_input:0") # Example input name output_tensor = graph.get_tensor_by_name("classification_output/Softmax:0") # Example output name # Create a serving signature (defines how the model accepts inputs and returns outputs) serving_signature = tf.saved_model.signature_def_utils.predict_signature_def( inputs={"image": input_tensor}, outputs={"predictions": output_tensor} ) # Build and save the SavedModel builder = tf.saved_model.builder.SavedModelBuilder("/tmp/GoogleTensorflow_exported/1") builder.add_meta_graph_and_variables( sess, [tf.saved_model.tag_constants.SERVING], signature_def_map={ tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: serving_signature } ) builder.save()
Step 2: Deploy the Model with TensorFlow Serving
You already have Bazel set up from the MNIST example, so deploying your custom model is straightforward:
Start the TensorFlow Serving Server
Run this command in your terminal (adjust paths as needed):
bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server \ --model_name=GoogleTensorflow \ --model_base_path=/tmp/GoogleTensorflow_exported \ --port=8501
--model_name: The name you'll use to reference the model in API calls--model_base_path: The parent directory containing your versioned SavedModel folders (not the specific version folder)--port: The HTTP port for the service (8500 is the default GRPC port if you prefer that)
Test the Deployed Service
To make sure everything works, write a quick Python script to send an image to the server:
import requests import numpy as np from PIL import Image # Load and preprocess a test image (match the preprocessing you used during training!) test_image = Image.open("/path/to/your/test_image.jpg") test_image = test_image.resize((224, 224)) # Replace with your model's input size image_array = np.array(test_image) / 255.0 # Normalize if your model expects this image_array = np.expand_dims(image_array, axis=0) # Add batch dimension # Send a POST request to the serving API api_url = "http://localhost:8501/v1/models/GoogleTensorflow:predict" request_data = {"instances": image_array.tolist()} response = requests.post(api_url, json=request_data) # Parse and print the prediction predictions = response.json()["predictions"] print(f"Model Prediction: {predictions}")
Quick Troubleshooting Tips
- Tensor Name Issues: If you can't find your input/output tensor names, print all tensor names during training with
[tensor.name for tensor in tf.get_default_graph().as_graph_def().node]. - Preprocessing Mismatch: Make sure the test image preprocessing (size, normalization, channel order) matches exactly what you used during training—this is a common source of bad predictions.
- Alternative Deployment: If Bazel feels cumbersome, try using the official TensorFlow Serving Docker image instead:
docker run -p 8501:8501 \ --mount type=bind,source=/tmp/GoogleTensorflow_exported,target=/models/GoogleTensorflow \ -e MODEL_NAME=GoogleTensorflow \ tensorflow/serving
内容的提问来源于stack exchange,提问作者Shobhit

