TensorFlow测试训练模型报错:DecodeJpeg/contents:0张量不存在
DecodeJpeg/contents:0不存在错误 Hey there! Let's break down why you're seeing this error and how to fix it.
问题原因
The error TypeError: Cannot interpret feed_dict key as Tensor: The name 'DecodeJpeg/contents:0' refers to a Tensor which does not exist means exactly what it says: the tensor you're trying to feed data into (DecodeJpeg/contents:0) isn't present in the trained model graph you imported.
This usually happens because:
- The retrain script you used might have used a different input node name (depending on TensorFlow version or retrain parameters)
- The
DecodeJpegoperation was optimized out when the model was exported/frozen - You're referencing the wrong node name entirely
解决方案
Step 1: Find the correct input node name
First, let's identify all the nodes in your imported graph to find the right input. Add this code right after importing the graph_def in your session:
with tf.Session() as sess: softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') # Print all operation names in the graph to find the input node print("All nodes in the graph:") for op in sess.graph.get_operations(): print(op.name)
Run this, and look for nodes that sound like they handle image input. Common names for retrained models are:
DecodeJpeg/contents(the one you tried, but maybe it's named differently)inputorinput:0PlaceholderorPlaceholder:0Cast(if the model expects normalized image data)
Step 2: Update your feed_dict with the correct node name
Once you have the right node name, adjust your sess.run line accordingly. Here are two common scenarios:
Scenario 1: The model accepts raw JPEG bytes
If you find a node like DecodeJpeg/contents:0 (or similar, like DecodeJpeg/contents), keep your image_data reading code and just update the key:
predictions = sess.run(softmax_tensor, {'YOUR_CORRECT_NODE_NAME:0': image_data})
Scenario 2: The model expects preprocessed image arrays
If you don't find a DecodeJpeg node, the model probably expects already decoded and normalized image data. Replace your image loading and prediction code with something like this (adjust the image size to match your model's input requirements—e.g., 224x224 for MobileNet, 299x299 for Inception):
# Load and preprocess the image image_path = os.path.join(root, file) img = Image.open(image_path).resize((224, 224)) # Resize to model's input size image_array = np.array(img) / 255.0 # Normalize pixel values to [0,1] (adjust if your model uses a different scale) # Feed the preprocessed array to the correct input node predictions = sess.run(softmax_tensor, {'input:0': [image_array]}) # Replace 'input:0' with your found node name
Step 3: Verify the fix
After updating the node name, run your script again. The error should be gone, and you should see the prediction results for your images.
Example modified code snippet
Here's how your prediction loop might look after fixing the input node (assuming we found input:0 as the correct node):
with tf.Session() as sess: softmax_tensor = sess.graph.get_tensor_by_name('final_result:0') # Optional: Print all nodes to confirm input name # for op in sess.graph.get_operations(): # print(op.name) for root, dirs, files in os.walk('retrain/images/'): for file in files: image_path = os.path.join(root, file) print(image_path) # Load and preprocess image img = Image.open(image_path).resize((224, 224)) image_array = np.array(img) / 255.0 # Run prediction predictions = sess.run(softmax_tensor, {'input:0': [image_array]}) predictions = np.squeeze(predictions) # Show image and results plt.imshow(img) plt.axis('off') plt.show() top_k = predictions.argsort()[::-1] for node_id in top_k: human_string = id_to_string(node_id) score = predictions[node_id] print('%s (score=%.5f)' % (human_string, score)) print()
内容的提问来源于stack exchange,提问作者Zhihau Shiu

