Fixed: Shapes of all inputs must match … [Op:Pack] in tf.stack
Stacking two images into a batch stopped with InvalidArgumentError: … Shapes of all inputs must match: values[0].shape = [240,320,3] != values[1].shape = [320,240,3] [Op:Pack]. tf.stack only combines tensors of exactly the same shape, and a landscape and a portrait photo aren't. Resizing both to the same size first fixed it.
What I Was Doing
I was putting images into one batch, the way a model takes them: a tensor with one extra axis, one entry per image. Photos come in different sizes, so the example has one landscape image, 240 pixels high and 320 wide, and one portrait, 320 × 240. To keep it small, both are blank images made with tf.zeros:
import tensorflow as tf
landscape = tf.zeros([240, 320, 3])
portrait = tf.zeros([320, 240, 3])
batch = tf.stack([landscape, portrait])
print(batch.shape)
I ran it in a uv project with TensorFlow 2.22.0:
uv run python make_batch.py
What I Expected
A batch with 2 images: a shape starting with 2.
What I Got
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
W0000 00:00:1796444851.940446 172218 local_rendezvous.cc:461] Local rendezvous is aborting with status: INVALID_ARGUMENT: Shapes of all inputs must match: values[0].shape = [240,320,3] != values[1].shape = [320,240,3]
=== Source Location Trace: ===
tensorflow/core/kernels/pack_op.cc:108
tensorflow/core/common_runtime/single_threaded_executor.cc:449
tensorflow/core/common_runtime/function.cc:1418
Traceback (most recent call last):
File "/Users/you/source/fx-stack-shapes/make_batch.py", line 6, in <module>
batch = tf.stack([landscape, portrait])
File "/Users/you/source/fx-stack-shapes/.venv/lib/python3.14/site-packages/tensorflow/python/util/traceback_utils.py", line 167, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/Users/you/source/fx-stack-shapes/.venv/lib/python3.14/site-packages/tensorflow/python/framework/ops.py", line 6084, in raise_from_not_ok_status
raise core._status_to_exception(e) from None # pylint: disable=protected-access
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tensorflow.python.framework.errors_impl.InvalidArgumentError: {{function_node __wrapped__Pack_N_2_device_/job:localhost/replica:0/task:0/device:CPU:0}} Shapes of all inputs must match: values[0].shape = [240,320,3] != values[1].shape = [320,240,3] [Op:Pack] name: stack
![The terminal after uv run python make_batch.py: TensorFlow's W0000 log line saying Local rendezvous is aborting with status INVALID_ARGUMENT, then a traceback ending in InvalidArgumentError: Shapes of all inputs must match: values[0].shape = [240,320,3] != values[1].shape = [320,240,3], for the Pack operation.](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/ai/tensorflow/fixes/2026/tensorflow-pack-shapes-01-error.webp)
The lines at the top, starting with W0000, are TensorFlow's own log: a warning that the operation stopped, with the same message, and where in TensorFlow's code it happened. The traceback after them is Python's, and its last line is the error.
What I Checked
1. The two shapes in the message
values[0] is the first tensor in the list, landscape, with the shape [240,320,3]. values[1] is portrait, [320,240,3]. They hold the same number of values, but 240 rows of 320 pixels isn't the same shape as 320 rows of 240.
2. What Pack is
[Op:Pack] names the TensorFlow operation that failed, and the log's trace points at pack_op.cc. tf.stack runs it: the tf.raw_ops.Pack page describes it as "Packs a list of N rank-R tensors into one rank-(R+1) tensor", the same job as tf.stack.
3. What the tf.stack documentation says
The tf.stack API page says its input is "A list of Tensor objects with the same shape and type."
Why It Happens
tf.stack lines the tensors up along a new axis, value against value, so every tensor must have the same shape. A batch of photos only works once every photo has the same height, width and number of colour channels.
The Fix
Resize every image to the same size, then stack them:
import tensorflow as tf
landscape = tf.zeros([240, 320, 3])
portrait = tf.zeros([320, 240, 3])
same_size = [tf.image.resize(image, [180, 180]) for image in [landscape, portrait]]
batch = tf.stack(same_size)
print(batch.shape)
The line with for builds a new list: tf.image.resize runs on each image in turn and resizes it to 180 × 180, the size TensorFlow's image loading tutorial uses.
Another way: pad instead of stretching
tf.image.resize stretches an image whose shape differs from the new size. tf.image.resize_with_pad(image, 180, 180) keeps each image's shape and fills the rest with black. Used in the same line instead of tf.image.resize, the batch had the same shape, (2, 180, 180, 3).
After the Fix
uv run python make_batch.py
(2, 180, 180, 3)

The batch has the shape (2, 180, 180, 3): 2 images of 180 × 180 pixels, with 3 colour values each. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Resizing only some of the images | Shapes of all inputs must match: values[0].shape = [180,180,3] != values[1].shape = [320,240,3] [Op:Pack] | Resize every image in the list |
Switching to tf.concat | It joins along an existing axis and stops too: ConcatOp : Dimension 1 in both shapes must be equal: shape[0] = [240,320,3] vs. shape[1] = [320,240,3] [Op:ConcatV2] | Use tf.stack on images of the same size |
| A gray image among colour ones | Shapes of all inputs must match: values[0].shape = [180,180,1] != values[1].shape = [180,180,3] [Op:Pack]: the same size, but 1 channel against 3 | Decode every photo with channels=3 |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
values[0].shape = [240,320,3] != values[1].shape = [320,240,3] | Images of different sizes | Resize them all to one size first |
values[0].shape = [180,180,1] != values[1].shape = [180,180,3] | A gray image among colour ones | tf.io.decode_jpeg(data, channels=3) |
cannot compute Pack as input #1(zero-based) was expected to be a float tensor but is a uint8 tensor [Op:Pack] | One image was resized, which makes it float32, and another wasn't, so it's still uint8 | Resize every image, or cast them all to one type |
FAQ
Does resizing distort the photos?
When the new size has a different shape from the photo, yes. The tf.image.resize page warns: "Resized images will be distorted if their original aspect ratio is not the same as size." tf.image.resize_with_pad avoids it.
Why don't I see this when I load photos with Keras?
Keras's keras.utils.image_dataset_from_directory resizes every photo as it loads it. Its documentation describes image_size as the "Size to resize images to after they are read from disk", because "the pipeline processes batches of images that must all have the same size".
Conclusion
Shapes of all inputs must match … [Op:Pack] meant tf.stack got a 240 × 320 image and a 320 × 240 one. Resizing both to 180 × 180 first gave a batch of shape (2, 180, 180, 3).
Additional Resources
- tf.stack — the shape rule for its inputs.
- tf.image.resize — resizing images.
- tf.image.resize_with_pad — resizing without distortion.
