Flask+Docker环境下Werkzeug文件对象转Tensor失败问题求助
Hey there, let's work through this error you're hitting. The problem is straightforward: TensorFlow has no idea how to convert Flask's werkzeug.datastructures.FileStorage object directly into a tensor—you need to first extract the actual content from the uploaded file, then process it into a format TF understands.
Here's a step-by-step breakdown of how to fix this:
1. Understand the Root Cause
When you upload a file via Flask, it comes wrapped in a FileStorage object that handles things like file metadata and streaming. TensorFlow can't parse this wrapper; it needs raw file content (bytes, string data, etc.) to create a tensor.
2. Extract File Content & Convert to Tensor
Depending on your file type (image, text, CSV, etc.), the processing steps will vary. Below are common examples:
Example 1: Image Files
If you're uploading images, read the file bytes, decode them into a tensor, and adjust it to match your model's input requirements:
from flask import Flask, request import tensorflow as tf from tensorflow_serving.apis import predict_pb2 from tensorflow_serving.apis import prediction_service_pb2_grpc import grpc app = Flask(__name__) FLAGS = tf.compat.v1.app.flags.FLAGS class mainSessRunning(): def __init__(self): host, port = FLAGS.server.split(':') channel = grpc.insecure_channel(f"{host}:{port}") self.stub = prediction_service_pb2_grpc.PredictionServiceStub(channel) # Initialize the service client sess_runner = mainSessRunning() @app.route('/predict', methods=['POST']) def predict(): if 'input-file' not in request.files: return "No file uploaded", 400 uploaded_file = request.files['input-file'] if uploaded_file.filename == '': return "No selected file", 400 # Extract raw bytes from the FileStorage object file_bytes = uploaded_file.read() try: # Decode bytes into an image tensor img_tensor = tf.io.decode_image( file_bytes, channels=3, # Adjust based on your model (1 for grayscale) expand_animations=False # Ignore GIF frames if needed ) # Resize tensor to match your model's input shape img_tensor = tf.image.resize(img_tensor, (224, 224)) # Add batch dimension (most models expect [batch_size, height, width, channels]) img_tensor = tf.expand_dims(img_tensor, axis=0) # Optional: Normalize pixel values (e.g., 0-1 range) img_tensor = img_tensor / 255.0 except Exception as e: return f"Error converting image to tensor: {str(e)}", 500 # Prepare request for TensorFlow Serving request = predict_pb2.PredictRequest() request.model_spec.name = 'your_model_name' request.model_spec.signature_name = 'serving_default' request.inputs['input_tensor'].CopyFrom(tf.make_tensor_proto(img_tensor)) # Send request to the server response = sess_runner.stub.Predict(request, 10.0) # 10s timeout # Process the response and generate output # ... (your output logic here) ... return "Prediction completed successfully", 200 if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
Example 2: Text Files
For text files, decode the bytes to a string, then apply your text preprocessing (tokenization, embedding, etc.):
@app.route('/process-text', methods=['POST']) def process_text(): uploaded_file = request.files.get('input-file') if not uploaded_file: return "No file uploaded", 400 # Read bytes and decode to string text_content = uploaded_file.read().decode('utf-8') # Apply text preprocessing (adjust based on your model) tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=10000) # Assume you've fit the tokenizer on your training data beforehand sequence = tokenizer.texts_to_sequences([text_content]) padded_sequence = tf.keras.preprocessing.sequence.pad_sequences(sequence, maxlen=128) text_tensor = tf.convert_to_tensor(padded_sequence) # Prepare and send request to TensorFlow Serving # ... (similar to image example) ... return "Text processed successfully", 200
3. Additional Checks
- Validate File Types: Add checks to ensure users upload the correct file type (e.g.,
.jpg,.txt) to avoid decoding errors. - Docker Dependencies: Make sure your Dockerfile installs all required packages (
flask,tensorflow,grpcio,werkzeug) with compatible versions. - Error Handling: Wrap tensor conversion and prediction calls in try-except blocks to catch and return meaningful errors to the client.
That should resolve the conversion error—remember, always extract the raw content from the FileStorage object before feeding it into TensorFlow!
内容的提问来源于stack exchange,提问作者jane1912

