Error when checking target expected to have shape 256, 256, 1 but got array with shape 256, 256, 3
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This shape error usually means your model expects a single-channel target tensor, but your dataset is supplying three channels instead. In image problems, that often means the labels or masks are being loaded as RGB images even though the model was built for grayscale or single-channel outputs.
Read the Error Carefully
The important word is target. That usually refers to y, not x.
If the error says the target should have shape (256, 256, 1) but got (256, 256, 3), the model's output layer is probably configured for one channel while your labels still have three channels.
Typical cases include:
- segmentation masks stored as color PNG files
- grayscale labels loaded with an image library defaulting to RGB
- model output configured for binary segmentation, but labels encoded for multiclass color masks
A Typical Binary Segmentation Setup
A binary segmentation model often ends with one output channel and a sigmoid.
That output shape is (None, 256, 256, 1). So the target masks must also be single-channel.
Fix 1: Load Masks as Grayscale
If the labels represent binary masks, load them as one channel from the start.
Now the mask shape becomes (256, 256, 1), which matches the model output.
Fix 2: Convert RGB Masks Explicitly
Sometimes you already have masks in memory as RGB arrays. If all three channels carry the same information, keep one channel and drop the rest.
Only do this if the RGB channels are redundant representations of the same label. If the colors encode classes, you need a different conversion.
Fix 3: Align the Model to the Label Format
If your labels genuinely encode three classes or three channels of meaning, the model may need three output channels instead of one.
This is the right fix only when the task itself is multiclass or multi-channel. Do not change the model just to silence the error if the labels are supposed to be binary masks.
Check the Whole Pipeline, Not Just One Batch
Shape bugs often come from preprocessing code, generators, or dataset mapping functions. Print shapes at the exact point fed into model.fit.
That usually tells you immediately whether the mismatch is in the loader, augmentation step, or model definition.
Common Pitfalls
The biggest mistake is changing the model input shape when the error is actually about the target shape.
Another mistake is converting RGB labels to grayscale without checking whether the color channels encode distinct classes.
A third issue is mixing binary-loss configuration with multiclass labels. The output layer, activation, target encoding, and loss function all need to agree.
Finally, be careful with image libraries. Many load images as three-channel RGB by default, even when the file visually looks like a black-and-white mask.
Summary
- This error usually means the labels or masks have three channels while the model expects one.
- For binary segmentation, load masks as grayscale and keep shape
(H, W, 1). - If the labels are truly multiclass, adjust the model output and loss accordingly.
- Inspect shapes right before training, not just earlier in the pipeline.
- Do not confuse model input shape with target shape.
- The correct fix is to make model output, labels, and loss function agree.

