基于Keras后端提取模型中间层输出时遇input_3占位符错误求助
input_3 Placeholder Error in K.function Hey there, let's break down how to figure out why that mysterious input_3 placeholder is popping up even though your model should only have two inputs. Here are actionable troubleshooting steps to track down the issue:
1. Verify Your Model's Actual Inputs
First, double-check that your model really only has two inputs—sometimes we assume things but the graph tells a different story. Run this code to list all input tensors of your model:
print([input_tensor.name for input_tensor in yolo_model.inputs])
If input_3:0 shows up in the output, that means your model does have a third input you didn't account for. This might happen if you accidentally added a placeholder during model construction (e.g., in a custom layer or Lambda layer) that got tied to the model's graph.
2. Check Nested Submodel Input Dependencies
Your code references yolo_model.layers[1] (a nested submodel), which might be the source of the extra placeholder. Submodels can sometimes carry their own input tensors that aren't explicitly linked to the main model's inputs. Try listing the inputs of the submodel to see if input_3 lives there:
submodel = yolo_model.layers[1] print([input_tensor.name for input_tensor in submodel.inputs])
If the submodel has an extra input, you'll need to include it in your K.function input list—even if you thought the main model's inputs covered it.
3. Use yolo_model.inputs Instead of Manual Input Selection
Instead of manually picking yolo_model.layers[0].input and yolo_model.layers[4].input, let Keras handle getting all required inputs automatically. Modify your K.function definition to use yolo_model.inputs:
get_intermediate_outputs = K.function( yolo_model.inputs + [K.learning_phase()], [ yolo_model.layers[1].layers[17].output, yolo_model.layers[1].layers[27].output, yolo_model.layers[1].layers[43].output, yolo_model.layers[1].layers[69].output ] )
This ensures you're feeding every input tensor the model's graph expects, not just the two you think are needed. When calling this function, pass your two input arrays plus the learning phase (0 for inference, 1 for training):
outputs = get_intermediate_outputs([np.zeros((1,416,416,3)), your_second_input, 0])
4. Trace the input_3 Tensor's Usage
If the above steps don't clarify things, directly inspect the input_3 tensor to see which layers are using it. Run this code to find its consumers:
import tensorflow as tf input_3 = tf.get_default_graph().get_tensor_by_name('input_3:0') print("Layers using input_3:", [op.name for op in input_3.consumers()])
This will show you exactly which operations in the graph depend on input_3, helping you trace back to where this placeholder was created (e.g., a forgotten custom layer or a leftover from model cloning/modification).
5. Review Model Construction Code
Go back through how you built yolo_model and its submodels. Look for any instances where you might have created a placeholder with tf.placeholder() or K.placeholder() that wasn't added as an official input to the model. Common culprits include:
- Custom layers that create internal placeholders instead of using the layer's input tensor
- Lambda layers that reference external placeholders
- Model cloning or modification steps that left orphaned tensors in the graph
内容的提问来源于stack exchange,提问作者徐瑞年

